Class: RbsShrine::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/rbs_shrine/rake_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :'rbs:shrine', &block) ⇒ RakeTask

Returns a new instance of RakeTask.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rbs_shrine/rake_task.rb', line 9

def initialize(name = :'rbs:shrine', &block)
  super()

  @name = name
  @signature_root_dir = Rails.root / "sig/rbs_shrine"

  block&.call(self)

  define_generate_task
  define_clean_task
  define_setup_task
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/rbs_shrine/rake_task.rb', line 7

def name
  @name
end

#signature_root_dirObject

Returns the value of attribute signature_root_dir.



7
8
9
# File 'lib/rbs_shrine/rake_task.rb', line 7

def signature_root_dir
  @signature_root_dir
end

Instance Method Details

#define_clean_taskObject



29
30
31
32
33
34
# File 'lib/rbs_shrine/rake_task.rb', line 29

def define_clean_task
  desc "Clean RBS files for shrine models"
  task "#{name}:clean" do
    signature_root_dir.rmtree if signature_root_dir.exist?
  end
end

#define_generate_taskObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbs_shrine/rake_task.rb', line 36

def define_generate_task
  desc "Generate RBS files for shrine models"
  task("#{name}:generate": :environment) do
    require "rbs_shrine/shrine" # load RbsShrine lazily

    Rails.application.eager_load!

    RbsShrine::Shrine.all.each do |klass|
      rbs = RbsShrine::Shrine.class_to_rbs(klass)
      path = signature_root_dir / "#{klass.name.to_s.underscore}.rbs"
      path.dirname.mkpath
      path.write(rbs)
    end
  end
end

#define_setup_taskObject



22
23
24
25
26
27
# File 'lib/rbs_shrine/rake_task.rb', line 22

def define_setup_task
  desc "Run all tasks of rbs_shrine"

  deps = [:"#{name}:clean", :"#{name}:generate"]
  task("#{name}:setup" => deps)
end