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

Returns a new instance of Generator.



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

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.



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

def config
  @config
end

Instance Method Details

#build_dsl(requested_constants) ⇒ Object



120
121
122
123
124
125
126
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
# File 'lib/tapioca/generator.rb', line 120

def build_dsl(requested_constants)
  load_application(eager_load: requested_constants.empty?)
  load_dsl_generators

  rbi_files_to_purge = existing_rbi_filenames(requested_constants)

  say("Compiling DSL RBI files...")
  say("")

  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|
    filename = compile_dsl_rbi(constant, contents)
    rbi_files_to_purge.delete(filename) if filename
  end

  unless rbi_files_to_purge.empty?
    say("")
    say("Removing stale RBI files...")

    rbi_files_to_purge.sort.each do |filename|
      remove(filename)
    end
  end

  say("")
  say("Done", :green)

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

#build_gem_rbis(gem_names) ⇒ Object



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

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



48
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
# File 'lib/tapioca/generator.rb', line 48

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.generate_command,
    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("tapioca sync", :yellow, :bold)
  say("Please review changes and commit them, then run #{cmd}.", [:green, :bold])
end

#build_todosObject



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

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.generate_command,
    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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/tapioca/generator.rb', line 159

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