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
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/rbs/inline/cli.rb', line 72
def run(args)
base_paths = [Pathname("lib"), Pathname("app")]
output_path = nil
opt_in = true
unknown_type = nil
OptionParser.new do |opts|
opts.on("--base=BASE", "The path to calculate relative path of files (defaults to #{base_paths.join(File::PATH_SEPARATOR)})") do |str|
base_paths = str.split(File::PATH_SEPARATOR).map {|path| Pathname(path) }
end
opts.on("--output[=DEST]", "Save the generated RBS files under `sig/generated` or DEST if specified (defaults to output to STDOUT)") do
if _1
output_path = Pathname(_1)
else
output_path = Pathname("sig/generated")
end
end
opts.on("--opt-out", "Generates RBS files by default. Opt-out with `# rbs_inline: disabled` comment") do
opt_in = false
end
opts.on("--opt-in", "Generates RBS files only for files with `# rbs_inline: enabled` comment (default)") do
opt_in = true
end
opts.on("--unknown-type=TYPE", "Use given type instead of untyped for unknown types (default: untyped)") do
unknown_type = _1
end
opts.on("--verbose") do
logger.level = :DEBUG
end
end.parse!(args)
logger.debug { "base_paths = #{base_paths.join(File::PATH_SEPARATOR)}, output_path = #{output_path}" }
if output_path
calculator = PathCalculator.new(Pathname.pwd, base_paths, output_path)
end
targets = args.flat_map { Pathname.glob(_1) }.flat_map do |path|
if path.directory?
pattern = path + "**/*.rb"
Pathname.glob(pattern.to_s)
else
path
end
end
targets.sort!
targets.uniq!
count = 0
targets.each do |target|
absolute_path = Pathname.pwd + target
if output_path && calculator
output_file_path = calculator.calculate(absolute_path)
if output_file_path
output = output_file_path.sub_ext(".rbs")
else
raise "Cannot calculate the output file path for #{target} in #{output_path}: calculated = #{output}"
end
logger.debug { "Generating #{output} from #{target} ..." }
else
logger.debug { "Generating RBS declaration from #{target} ..." }
end
logger.debug { "Parsing ruby file #{target}..." }
if (uses, decls, rbs_decls = Parser.parse(Prism.parse_file(target.to_s), opt_in: opt_in))
writer = Writer.new()
if unknown_type
type = RBS::Parser.parse_type(unknown_type, require_eof: true)
type && writer.default_type = type
end
writer.("Generated from #{target.relative? ? target : target.relative_path_from(Pathname.pwd)} with RBS::Inline")
writer.write(uses, decls, rbs_decls)
if output
unless output.parent.directory?
logger.debug { "Making directory #{output.parent}..." }
output.parent.mkpath
end
if output.file? && output.read == writer.output
logger.debug { "Skip writing identical RBS file" }
else
logger.debug { "Writing RBS file to #{output}..." }
output.write(writer.output)
count += 1
end
else
stdout.puts writer.output
stdout.puts
count += 1
end
else
logger.debug { "Skipping #{target} because `# rbs_inline: enabled` comment not found" }
end
end
stderr.puts "🎉 Generated #{count} RBS files under #{output_path}"
0
end
|