Class: Tapioca::Generators::Dsl

Inherits:
Base
  • Object
show all
Defined in:
lib/tapioca/generators/dsl.rb

Instance Method Summary collapse

Methods included from CliHelper

#say_error

Constructor Details

#initialize(requested_constants:, outpath:, only:, exclude:, file_header:, compiler_path:, tapioca_path:, default_command:, file_writer: FileWriter.new, should_verify: false, quiet: false, verbose: false, number_of_workers: nil) ⇒ Dsl

Returns a new instance of Dsl.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tapioca/generators/dsl.rb', line 24

def initialize(
  requested_constants:,
  outpath:,
  only:,
  exclude:,
  file_header:,
  compiler_path:,
  tapioca_path:,
  default_command:,
  file_writer: FileWriter.new,
  should_verify: false,
  quiet: false,
  verbose: false,
  number_of_workers: nil
)
  @requested_constants = requested_constants
  @outpath = outpath
  @only = only
  @exclude = exclude
  @file_header = file_header
  @compiler_path = compiler_path
  @tapioca_path = tapioca_path
  @should_verify = should_verify
  @quiet = quiet
  @verbose = verbose
  @number_of_workers = number_of_workers

  super(default_command: default_command, file_writer: file_writer)

  @loader = T.let(nil, T.nilable(Loader))
end

Instance Method Details

#generateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tapioca/generators/dsl.rb', line 57

def generate
  load_dsl_extensions
  load_application(eager_load: @requested_constants.empty?)
  abort_if_pending_migrations!
  load_dsl_generators

  if @should_verify
    say("Checking for out-of-date RBIs...")
  else
    say("Compiling DSL RBI files...")
  end
  say("")

  outpath = @should_verify ? Pathname.new(Dir.mktmpdir) : @outpath
  rbi_files_to_purge = existing_rbi_filenames(@requested_constants)

  compiler = Compilers::DslCompiler.new(
    requested_constants: constantize(@requested_constants),
    requested_generators: constantize_generators(@only),
    excluded_generators: constantize_generators(@exclude),
    error_handler: ->(error) {
      say_error(error, :bold, :red)
    },
    number_of_workers: @number_of_workers
  )

  processed_files = compiler.run do |constant, contents|
    constant_name = T.must(Reflection.name_of(constant))

    if @verbose && !@quiet
      say_status(:processing, constant_name, :yellow)
    end

    compile_dsl_rbi(
      constant_name,
      contents,
      outpath: outpath,
      quiet: @should_verify || @quiet && !@verbose
    )
  end

  processed_files.each { |filename| rbi_files_to_purge.delete(T.must(filename)) }

  say("")

  if @should_verify
    perform_dsl_verification(outpath)
  else
    purge_stale_dsl_rbi_files(rbi_files_to_purge)

    say("Done", :green)

    say("All operations performed in working directory.", [:green, :bold])
    say("Please review changes and commit them.", [:green, :bold])
  end
end