Class: Tapioca::Cli

Inherits:
Thor
  • Object
show all
Includes:
CliHelper, ConfigHelper
Defined in:
lib/tapioca/cli.rb

Constant Summary collapse

FILE_HEADER_OPTION_DESC =
"Add a \"This file is generated\" header on top of each generated RBI file"

Instance Attribute Summary

Attributes included from ConfigHelper

#command_name, #defaults

Instance Method Summary collapse

Methods included from ConfigHelper

#initialize, #options

Methods included from CliHelper

#say_error

Instance Method Details

#__print_versionObject



291
292
293
# File 'lib/tapioca/cli.rb', line 291

def __print_version
  puts "Tapioca v#{Tapioca::VERSION}"
end

#clean_shims(*files_to_clean) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tapioca/cli.rb', line 226

def clean_shims(*files_to_clean)
  index = RBI::Index.new

  # Index gem RBIs
  gem_rbi_dir = options[:gem_rbi_dir]
  say("Loading gem RBIs from #{gem_rbi_dir}... ")
  gem_rbis_files = Dir.glob("#{gem_rbi_dir}/**/*.rbi").sort
  gem_rbis_trees = RBI::Parser.parse_files(gem_rbis_files)
  index.visit_all(gem_rbis_trees)
  say(" Done", :green)

  # Index dsl RBIs
  dsl_rbi_dir = options[:dsl_rbi_dir]
  say("Loading dsl RBIs from #{dsl_rbi_dir}... ")
  dsl_rbis_files = Dir.glob("#{dsl_rbi_dir}/**/*.rbi").sort
  dsl_rbis_trees = RBI::Parser.parse_files(dsl_rbis_files)
  index.visit_all(dsl_rbis_trees)
  say(" Done", :green)

  # Clean shim RBIs
  if files_to_clean.empty?
    shim_rbi_dir = options[:shim_rbi_dir]
    print("Cleaning shim RBIs from #{shim_rbi_dir}...")
    files_to_clean = Dir.glob("#{shim_rbi_dir}/*.rbi")
  else
    print("Cleaning shim RBIs...")
  end

  done_something = T.let(false, T::Boolean)
  files_to_clean.sort.each do |path|
    original = RBI::Parser.parse_file(path)
    cleaned, operations = RBI::Rewriters::RemoveKnownDefinitions.remove(original, index)

    next if operations.empty?
    done_something = true

    operations.each do |operation|
      print("\n  #{operation}")
    end

    if cleaned.empty?
      print("\n  Deleted empty file #{path}")
      FileUtils.rm(path)
    else
      File.write(path, cleaned.string)
    end
  end

  if done_something
    say("\nDone", :green)
  else
    say(" Done ", :green)
    say("(nothing to do)", :yellow)
  end
rescue Errno::ENOENT => e
  say_error("\nCan't read RBI: #{e}")
  exit(1)
rescue RBI::ParseError => e
  say_error("\nCan't parse RBI: #{e} (#{e.location})")
  exit(1)
end

#dsl(*constants) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tapioca/cli.rb', line 100

def dsl(*constants)
  generator = Generators::Dsl.new(
    requested_constants: constants,
    outpath: Pathname.new(options[:outdir]),
    only: options[:only],
    exclude: options[:exclude],
    file_header: options[:file_header],
    compiler_path: Tapioca::Compilers::Dsl::DSL_COMPILERS_DIR,
    tapioca_path: TAPIOCA_DIR,
    default_command: DEFAULT_COMMAND,
    should_verify: options[:verify],
    quiet: options[:quiet],
    verbose: options[:verbose],
    number_of_workers: options[:workers]
  )

  if options[:workers] != 1
    say(
      "Using more than one worker is experimental and might produce results that are not deterministic",
      :red
    )
  end

  Tapioca.silence_warnings do
    generator.generate
  end
end

#gem(*gems) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/tapioca/cli.rb', line 181

def gem(*gems)
  Tapioca.silence_warnings do
    all = options[:all]
    verify = options[:verify]

    generator = Generators::Gem.new(
      gem_names: all ? [] : gems,
      exclude: options[:exclude],
      prerequire: options[:prerequire],
      postrequire: options[:postrequire],
      typed_overrides: options[:typed_overrides],
      default_command: DEFAULT_COMMAND,
      outpath: Pathname.new(options[:outdir]),
      file_header: options[:file_header],
      doc: options[:doc],
      include_exported_rbis: options[:exported_gem_rbis],
      number_of_workers: options[:workers]
    )

    raise MalformattedArgumentError, "Options '--all' and '--verify' are mutually exclusive" if all && verify

    unless gems.empty?
      raise MalformattedArgumentError, "Option '--all' must be provided without any other arguments" if all
      raise MalformattedArgumentError, "Option '--verify' must be provided without any other arguments" if verify
    end

    if options[:workers] != 1
      say(
        "Using more than one worker is experimental and might produce results that are not deterministic",
        :red
      )
    end

    if gems.empty? && !all
      generator.sync(should_verify: verify)
    else
      generator.generate
    end
  end
end

#initObject



24
25
26
27
28
29
30
31
32
# File 'lib/tapioca/cli.rb', line 24

def init
  generator = Generators::Init.new(
    sorbet_config: SORBET_CONFIG_FILE,
    tapioca_config: TAPIOCA_CONFIG_FILE,
    default_postrequire: DEFAULT_POSTREQUIRE_FILE,
    default_command: DEFAULT_COMMAND
  )
  generator.generate
end

#requireObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/tapioca/cli.rb', line 36

def require
  generator = Generators::Require.new(
    requires_path: options[:postrequire],
    sorbet_config_path: SORBET_CONFIG_FILE,
    default_command: DEFAULT_COMMAND
  )
  Tapioca.silence_warnings do
    generator.generate
  end
end

#todoObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/tapioca/cli.rb', line 55

def todo
  generator = Generators::Todo.new(
    todo_file: options[:todo_file],
    file_header: options[:file_header],
    default_command: DEFAULT_COMMAND
  )
  Tapioca.silence_warnings do
    generator.generate
  end
end