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
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
167
168
169
170
171
|
# File 'lib/spoom/cli/srb/sigs.rb', line 68
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::Sigs.rbs_to_rbi(contents)
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, " require \"sorbet-runtime\"\n\n class Module; include T::Sig; end\n extend T::Sig\n\n \#{copy_context.read(entry_point)}\n RB\n\n # Now we create a new context to import our modified gem and run tapioca\n say(\"Running Tapioca...\")\n tapioca_context = Spoom::Context.mktmp!\n tapioca_context.write!(\"Gemfile\", <<~RB)\n source \"https://rubygems.org\"\n\n gem \"tapioca\"\n gem \"\#{spec.name}\", path: \"\#{copy_context.absolute_path}\"\n RB\n exec(tapioca_context, \"bundle install\")\n exec(tapioca_context, \"bundle exec tapioca gem \#{spec.name} --no-loc --no-file-header\")\n\n rbi_path = tapioca_context.glob(\"sorbet/rbi/gems/\#{spec.name}@*.rbi\").first\n unless rbi_path && tapioca_context.file?(rbi_path)\n say_error(\"No RBI file found at `sorbet/rbi/gems/\#{spec.name}@*.rbi`\")\n exit(1)\n end\n\n tapioca_context.write!(rbi_path, tapioca_context.read(rbi_path).gsub(/^# typed: true/, <<~RB.rstrip))\n # typed: true\n\n # DO NOT EDIT MANUALLY\n # This is an autogenerated file for types exported from the `\#{spec.name}` gem.\n # Please instead update this file by running `spoom srb sigs export`.\n RB\n\n output_path ||= \"rbi/\#{spec.name}.rbi\"\n generated_path = tapioca_context.absolute_path_to(rbi_path)\n\n if options[:check_sync]\n # If the check option is set, we just compare the generated RBI with the one in the gem.\n # If they are different, we exit with a non-zero exit code.\n unless system(\"diff -u -L 'generated' -L 'current' \#{generated_path} \#{output_path} >&2\")\n say_error(<<~ERR, status: \"\\nError\")\n The RBI file at `\#{output_path}` is not up to date\n\n Please run `spoom srb sigs export` to update it.\n ERR\n exit(1)\n end\n\n say(\"The RBI file at `\#{output_path}` is up to date\")\n exit(0)\n else\n output_dir = File.dirname(output_path)\n FileUtils.rm_rf(output_dir)\n FileUtils.mkdir_p(output_dir)\n FileUtils.cp(generated_path, output_path)\n\n say(\"Exported signatures to `\#{output_path}`\")\n end\nensure\n copy_context&.destroy!\n tapioca_context&.destroy!\nend\n")
|
#strip(*paths) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/spoom/cli/srb/sigs.rb', line 49
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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/spoom/cli/srb/sigs.rb', line 20
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, positional_names: options[:positional_names])
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
|