Class: Tapioca::Generators::Dsl

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

Instance Method Summary collapse

Constructor Details

#initialize(requested_constants:, outpath:, generators:, exclude_generators:, file_header:, compiler_path:, tapioca_path:, default_command:, file_writer: FileWriter.new, should_verify: false, quiet: false, verbose: false) ⇒ Dsl

Returns a new instance of Dsl.


23
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
# File 'lib/tapioca/generators/dsl.rb', line 23

def initialize(
  requested_constants:,
  outpath:,
  generators:,
  exclude_generators:,
  file_header:,
  compiler_path:,
  tapioca_path:,
  default_command:,
  file_writer: FileWriter.new,
  should_verify: false,
  quiet: false,
  verbose: false
)
  @requested_constants = requested_constants
  @outpath = outpath
  @generators = generators
  @exclude_generators = exclude_generators
  @file_header = file_header
  @compiler_path = compiler_path
  @tapioca_path = tapioca_path
  @should_verify = should_verify
  @quiet = quiet
  @verbose = verbose

  super(default_command: default_command, file_writer: file_writer)

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

Instance Method Details

#generateObject


54
55
56
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
# File 'lib/tapioca/generators/dsl.rb', line 54

def generate
  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(@generators),
    excluded_generators: constantize_generators(@exclude_generators),
    error_handler: ->(error) {
      say_error(error, :bold, :red)
    }
  )

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

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

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

    if filename
      rbi_files_to_purge.delete(filename)
    end
  end
  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