Class: Tapioca::Generator

Inherits:
Thor::Shell::Color
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator



20
21
22
23
24
25
26
27
28
# File 'lib/tapioca/generator.rb', line 20

def initialize(config)
  @config = config
  @bundle = T.let(nil, T.nilable(Gemfile))
  @loader = T.let(nil, T.nilable(Loader))
  @compiler = T.let(nil, T.nilable(Compilers::SymbolTableCompiler))
  @existing_rbis = T.let(nil, T.nilable(T::Hash[String, String]))
  @expected_rbis = T.let(nil, T.nilable(T::Hash[String, String]))
  super()
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/tapioca/generator.rb', line 13

def config
  @config
end

Instance Method Details

#build_dsl(requested_constants, should_verify: false, quiet: false) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tapioca/generator.rb', line 127

def build_dsl(requested_constants, should_verify: false, quiet: false)
  load_application(eager_load: requested_constants.empty?)
  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) : config.outpath
  rbi_files_to_purge = existing_rbi_filenames(requested_constants)

  compiler = Compilers::DslCompiler.new(
    requested_constants: constantize(requested_constants),
    requested_generators: config.generators,
    error_handler: ->(error) {
      say_error(error, :bold, :red)
    }
  )

  compiler.run do |constant, contents|
    constant_name = Module.instance_method(:name).bind(constant).call

    filename = compile_dsl_rbi(
      constant_name,
      contents,
      outpath: outpath,
      quiet: should_verify || quiet
    )

    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

#build_gem_rbis(gem_names) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tapioca/generator.rb', line 31

def build_gem_rbis(gem_names)
  require_gem_file

  gems_to_generate(gem_names)
    .reject { |gem| config.exclude.include?(gem.name) }
    .each do |gem|
      say("Processing '#{gem.name}' gem:", :green)
      indent do
        compile_gem_rbi(gem)
        puts
      end
    end

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

#build_requiresObject



49
50
51
52
53
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
# File 'lib/tapioca/generator.rb', line 49

def build_requires
  requires_path = Config::DEFAULT_POSTREQUIRE
  compiler = Compilers::RequiresCompiler.new(Config::SORBET_CONFIG)
  name = set_color(requires_path, :yellow, :bold)
  say("Compiling #{name}, this may take a few seconds... ")

  rb_string = compiler.compile
  if rb_string.empty?
    say("Nothing to do", :green)
    return
  end

  # Clean all existing requires before regenerating the list so we update
  # it with the new one found in the client code and remove the old ones.
  File.delete(requires_path) if File.exist?(requires_path)

  content = String.new
  content << rbi_header(
    "#{Config::DEFAULT_COMMAND} require",
    reason: "explicit gem requires",
    strictness: "false"
  )
  content << rb_string

  outdir = File.dirname(requires_path)
  FileUtils.mkdir_p(outdir)
  File.write(requires_path, content)

  say("Done", :green)

  say("All requires from this application have been written to #{name}.", [:green, :bold])
  cmd = set_color("#{Config::DEFAULT_COMMAND} sync", :yellow, :bold)
  say("Please review changes and commit them, then run `#{cmd}`.", [:green, :bold])
end

#build_todosObject



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
113
114
115
116
117
118
# File 'lib/tapioca/generator.rb', line 85

def build_todos
  todos_path = config.todos_path
  compiler = Compilers::TodosCompiler.new
  name = set_color(todos_path, :yellow, :bold)
  say("Compiling #{name}, this may take a few seconds... ")

  # Clean all existing unresolved constants before regenerating the list
  # so Sorbet won't grab them as already resolved.
  File.delete(todos_path) if File.exist?(todos_path)

  rbi_string = compiler.compile
  if rbi_string.empty?
    say("Nothing to do", :green)
    return
  end

  content = String.new
  content << rbi_header(
    "#{Config::DEFAULT_COMMAND} todo",
    reason: "unresolved constants",
    strictness: "false"
  )
  content << rbi_string
  content << "\n"

  outdir = File.dirname(todos_path)
  FileUtils.mkdir_p(outdir)
  File.write(todos_path, content)

  say("Done", :green)

  say("All unresolved constants have been written to #{name}.", [:green, :bold])
  say("Please review changes and commit them.", [:green, :bold])
end

#sync_rbis_with_gemfileObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tapioca/generator.rb', line 178

def sync_rbis_with_gemfile
  anything_done = [
    perform_removals,
    perform_additions,
  ].any?

  if anything_done
    say("All operations performed in working directory.", [:green, :bold])
    say("Please review changes and commit them.", [:green, :bold])
  else
    say("No operations performed, all RBIs are up-to-date.", [:green, :bold])
  end

  puts
end