Class: Tapioca::Generators::Todo

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

Instance Method Summary collapse

Constructor Details

#initialize(todos_path:, file_header:, default_command:, file_writer: FileWriter.new) ⇒ Todo

Returns a new instance of Todo.


15
16
17
18
19
20
# File 'lib/tapioca/generators/todo.rb', line 15

def initialize(todos_path:, file_header:, default_command:, file_writer: FileWriter.new)
  @todos_path = todos_path
  @file_header = file_header

  super(default_command: default_command, file_writer: file_writer)
end

Instance Method Details

#generateObject


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

def generate
  compiler = Compilers::TodosCompiler.new
  say("Finding all unresolved constants, 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(
    "#{@default_command} todo",
    reason: "unresolved constants",
    strictness: "false"
  )
  content << rbi_string
  content << "\n"

  say("Done", :green)
  create_file(@todos_path, content, verbose: false)

  name = set_color(@todos_path, :yellow, :bold)
  say("\nAll unresolved constants have been written to #{name}.", [:green, :bold])
  say("Please review changes and commit them.", [:green, :bold])
end

#rbi_header(command, reason: nil, strictness: nil) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tapioca/generators/todo.rb', line 55

def rbi_header(command, reason: nil, strictness: nil)
  statement = <<~HEAD
    # DO NOT EDIT MANUALLY
    # This is an autogenerated file for #{reason}.
    # Please instead update this file by running `#{command}`.
  HEAD

  sigil = <<~SIGIL if strictness
    # typed: #{strictness}
  SIGIL

  if @file_header
    [statement, sigil].compact.join("\n").strip.concat("\n\n")
  elsif sigil
    sigil.strip.concat("\n\n")
  else
    ""
  end
end