Class: Spoom::Cli::Srb::Sigs
- Inherits:
-
Thor
- Object
- Thor
- Spoom::Cli::Srb::Sigs
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
#set_color
Instance Method Details
#export(output_path = nil) ⇒ Object
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/spoom/cli/srb/sigs.rb', line 93
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)
say("Copying files to a temporary directory...")
copy_context = Spoom::Context.mktmp!
FileUtils.cp_r(
["Gemfile", "Gemfile.lock", gemspec, "lib/"],
copy_context.absolute_path,
)
say("Translating signatures from RBS to RBI...")
files = collect_files([copy_context.absolute_path])
transform_files(files) do |file, contents|
Spoom::Sorbet::Translate.(contents, file: file)
end
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
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-doc --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 `bundle exec spoom srb sigs export`.
RB
output_path ||= "rbi/#{spec.name}.rbi"
generated_path = tapioca_context.absolute_path_to(rbi_path)
if options[:check_sync]
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 `bundle exec 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
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/spoom/cli/srb/sigs.rb', line 74
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::Translate.strip_sorbet_sigs(contents, file: file)
end
say("Stripped signatures from `#{transformed_files}` file#{transformed_files == 1 ? "" : "s"}.")
end
|
#translate(*paths) ⇒ Object
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/spoom/cli/srb/sigs.rb', line 23
def translate(*paths)
from = options[:from]
to = options[:to]
max_line_length = options[:max_line_length]
if from == to
say_error("Can't translate signatures from `#{from}` to `#{to}`")
exit(1)
end
if max_line_length.nil? || max_line_length.zero?
max_line_length = nil
elsif max_line_length.negative?
say_error("--max-line-length can't be negative")
exit(1)
else
max_line_length = max_line_length.to_i
end
files = collect_files(paths, include_rbi_files: options[:include_rbi_files])
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::Translate.(
contents,
file: file,
positional_names: options[:positional_names],
max_line_length: max_line_length,
translate_generics: options[:translate_generics],
translate_helpers: options[:translate_helpers],
translate_abstract_methods: options[:translate_abstract_methods],
)
end
when "rbs"
transformed_files = transform_files(files) do |file, contents|
Spoom::Sorbet::Translate.(
contents,
file: file,
max_line_length: max_line_length,
)
end
end
say("Translated signatures in `#{transformed_files}` file#{transformed_files == 1 ? "" : "s"}.")
end
|