Class: Spoom::Cli::Srb::Sigs

Inherits:
Thor
  • Object
show all
Includes:
Helper
Defined in:
lib/spoom/cli/srb/sigs.rb

Constant Summary

Constants included from Helper

Helper::HIGHLIGHT_COLOR

Instance Method Summary collapse

Methods included from Helper

#blue, #collect_files, #color?, #colorize, #context, #context_requiring_sorbet!, #cyan, #exec_path, #gray, #green, #highlight, #red, #say, #say_error, #say_warning, #yellow

Methods included from Spoom::Colorize

#set_color

Instance Method Details

#export(output_path = nil) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
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
157
158
159
160
161
162
163
164
165
166
# File 'lib/spoom/cli/srb/sigs.rb', line 63

def export(output_path = nil)
  gemspec = options[:gemspec]

  unless gemspec
    say("Locating gemspec file...")
    gemspec = Dir.glob("*.gemspec").first
    unless gemspec
      say_error("No gemspec file found")
      exit(1)
    end
    say("Using `#{gemspec}` as gemspec file")
  end

  spec = Gem::Specification.load(gemspec)

  # First, we copy the files to a temporary directory so we can rewrite them without messing with the
  # original ones.
  say("Copying files to a temporary directory...")
  copy_context = Spoom::Context.mktmp!
  FileUtils.cp_r(
    ["Gemfile", "Gemfile.lock", gemspec, "lib/"],
    copy_context.absolute_path,
  )

  # Then, we transform the copied files to translate all the RBS signatures into RBI signatures.
  say("Translating signatures from RBS to RBI...")
  files = collect_files([copy_context.absolute_path])
  transform_files(files) do |_file, contents|
    Spoom::Sorbet::Sigs.rbs_to_rbi(contents)
  end

  # We need to inject `extend T::Sig` to be sure all the classes can run the `sig{}` blocks.
  # For this we find the entry point of the gem and inject the `extend T::Sig` line at the top of the file.
  entry_point = "lib/#{spec.name}.rb"
  unless copy_context.file?(entry_point)
    say_error("No entry point found at `#{entry_point}`")
    exit(1)
  end

  say("Injecting `extend T::Sig` to `#{entry_point}`...")
  copy_context.write!(entry_point, <<~RB)
    require "sorbet-runtime"

    class Module; include T::Sig; end
    extend T::Sig

    #{copy_context.read(entry_point)}
  RB

  # Now we create a new context to import our modified gem and run tapioca
  say("Running Tapioca...")
  tapioca_context = Spoom::Context.mktmp!
  tapioca_context.write!("Gemfile", <<~RB)
    source "https://rubygems.org"

    gem "tapioca"
    gem "#{spec.name}", path: "#{copy_context.absolute_path}"
  RB
  exec(tapioca_context, "bundle install")
  exec(tapioca_context, "bundle exec tapioca gem #{spec.name} --no-loc --no-file-header")

  rbi_path = tapioca_context.glob("sorbet/rbi/gems/#{spec.name}@*.rbi").first
  unless rbi_path && tapioca_context.file?(rbi_path)
    say_error("No RBI file found at `sorbet/rbi/gems/#{spec.name}@*.rbi`")
    exit(1)
  end

  tapioca_context.write!(rbi_path, tapioca_context.read(rbi_path).gsub(/^# typed: true/, <<~RB.rstrip))
    # typed: true

    # DO NOT EDIT MANUALLY
    # This is an autogenerated file for types exported from the `#{spec.name}` gem.
    # Please instead update this file by running `spoom srb sigs export`.
  RB

  output_path ||= "rbi/#{spec.name}.rbi"
  generated_path = tapioca_context.absolute_path_to(rbi_path)

  if options[:check_sync]
    # If the check option is set, we just compare the generated RBI with the one in the gem.
    # If they are different, we exit with a non-zero exit code.
    unless system("diff -u -L 'generated' -L 'current' #{generated_path} #{output_path} >&2")
      say_error(<<~ERR, status: "\nError")
        The RBI file at `#{output_path}` is not up to date

        Please run `spoom srb sigs export` to update it.
      ERR
      exit(1)
    end

    say("The RBI file at `#{output_path}` is up to date")
    exit(0)
  else
    output_dir = File.dirname(output_path)
    FileUtils.rm_rf(output_dir)
    FileUtils.mkdir_p(output_dir)
    FileUtils.cp(generated_path, output_path)

    say("Exported signatures to `#{output_path}`")
  end
ensure
  copy_context&.destroy!
  tapioca_context&.destroy!
end

#strip(*paths) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spoom/cli/srb/sigs.rb', line 44

def strip(*paths)
  files = collect_files(paths)

  say("Stripping signatures from `#{files.size}` file#{files.size == 1 ? "" : "s"}...\n\n")

  transformed_files = transform_files(files) do |_file, contents|
    Spoom::Sorbet::Sigs.strip(contents)
  end

  say("Stripped signatures from `#{transformed_files}` file#{transformed_files == 1 ? "" : "s"}.")
end

#translate(*paths) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spoom/cli/srb/sigs.rb', line 15

def translate(*paths)
  from = options[:from]
  to = options[:to]

  if from == to
    say_error("Can't translate signatures from `#{from}` to `#{to}`")
    exit(1)
  end

  files = collect_files(paths)

  say("Translating signatures from `#{from}` to `#{to}` " \
    "in `#{files.size}` file#{files.size == 1 ? "" : "s"}...\n\n")

  case from
  when "rbi"
    transformed_files = transform_files(files) do |_file, contents|
      Spoom::Sorbet::Sigs.rbi_to_rbs(contents)
    end
  when "rbs"
    transformed_files = transform_files(files) do |_file, contents|
      Spoom::Sorbet::Sigs.rbs_to_rbi(contents)
    end
  end

  say("Translated signatures in `#{transformed_files}` file#{transformed_files == 1 ? "" : "s"}.")
end