Class: RbsActionmailer::RakeTask

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RakeTask.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rbs_actionmailer/rake_task.rb', line 13

def initialize(name = :'rbs:actionmailer', &block) #: void
  super()

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

  block&.call(self)

  define_clean_task
  define_generate_task
  define_setup_task
end

Instance Attribute Details

#nameObject

: Symbol



8
9
10
# File 'lib/rbs_actionmailer/rake_task.rb', line 8

def name
  @name
end

#signature_root_dirObject

: Pathname



9
10
11
# File 'lib/rbs_actionmailer/rake_task.rb', line 9

def signature_root_dir
  @signature_root_dir
end

Instance Method Details

#define_clean_taskObject

: void



33
34
35
36
37
38
# File 'lib/rbs_actionmailer/rake_task.rb', line 33

def define_clean_task #: void
  desc "Clean up generated RBS files"
  task("#{name}:clean": :environment) do
    sh "rm", "-rf", signature_root_dir.to_s
  end
end

#define_generate_taskObject

: void



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rbs_actionmailer/rake_task.rb', line 40

def define_generate_task #: void
  desc "Generate RBS files for ActionMailer classes"
  task("#{name}:generate": :environment) do
    require "rbs_actionmailer" # load RbsActionMailer lazily

    Rails.application.eager_load!

    ActionMailer::Base.descendants.each do |klass|
      path = signature_root_dir / "app/mailers/#{klass.name.underscore}.rbs"
      path.dirname.mkpath
      rbs = RbsActionmailer::Generator.new(klass).generate
      path.write rbs if rbs
    end
  end
end

#define_setup_taskObject

: void



26
27
28
29
30
31
# File 'lib/rbs_actionmailer/rake_task.rb', line 26

def define_setup_task #: void
  deps = [:"#{name}:clean", :"#{name}:generate"]

  desc "Run all tasks of rbs_actionmailer"
  task("#{name}:setup" => deps)
end