|
3 | 3 | require 'benchmark' |
4 | 4 | require 'thor' |
5 | 5 | require 'yard' |
| 6 | +require 'sord' |
6 | 7 |
|
7 | 8 | module Solargraph |
8 | 9 | class Shell < Thor |
9 | 10 | include Solargraph::ServerMethods |
| 11 | + include ApiMap::SourceToYard |
10 | 12 |
|
11 | 13 | # Tell Thor to ensure the process exits with status 1 if any error happens. |
12 | 14 | def self.exit_on_failure? |
@@ -236,6 +238,67 @@ def list |
236 | 238 | puts "#{workspace.filenames.length} files total." |
237 | 239 | end |
238 | 240 |
|
| 241 | + desc 'cache', 'Cache a gem', hide: true |
| 242 | + # @return [void] |
| 243 | + # @param gem [String] |
| 244 | + # @param version [String, nil] |
| 245 | + def cache gem, version = nil |
| 246 | + spec = Gem::Specification.find_by_name(gem, version) |
| 247 | + pins = GemPins.build(spec) |
| 248 | + Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins) |
| 249 | + end |
| 250 | + |
| 251 | + desc 'gems', 'Cache documentation for installed gems' |
| 252 | + option :rebuild, type: :boolean, desc: 'Rebuild existing documentation', default: false |
| 253 | + # @return [void] |
| 254 | + def gems *names |
| 255 | + if names.empty? |
| 256 | + Gem::Specification.to_a.each do |spec| |
| 257 | + next unless options.rebuild || !Yardoc.cached?(spec) |
| 258 | + |
| 259 | + puts "Processing gem: #{spec.name} #{spec.version}" |
| 260 | + pins = GemPins.build(spec) |
| 261 | + Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins) |
| 262 | + end |
| 263 | + else |
| 264 | + names.each do |name| |
| 265 | + spec = Gem::Specification.find_by_name(name) |
| 266 | + if spec |
| 267 | + next unless options.rebuild || !Yardoc.cached?(spec) |
| 268 | + |
| 269 | + puts "Processing gem: #{spec.name} #{spec.version}" |
| 270 | + pins = GemPins.build(spec) |
| 271 | + Cache.save('gems', "#{spec.name}-#{spec.version}.ser", pins) |
| 272 | + else |
| 273 | + warn "Gem '#{name}' not found" |
| 274 | + end |
| 275 | + end |
| 276 | + end |
| 277 | + end |
| 278 | + |
| 279 | + desc 'rbs', 'Generate RBS definitions' |
| 280 | + option :inference, type: :boolean, desc: 'Enhance definitions with type inference', default: true |
| 281 | + def rbs |
| 282 | + api_map = Solargraph::ApiMap.load('.') |
| 283 | + pins = api_map.source_maps.flat_map(&:pins) |
| 284 | + store = Solargraph::ApiMap::Store.new(pins) |
| 285 | + if options[:inference] |
| 286 | + store.method_pins.each do |pin| |
| 287 | + if pin.return_type.undefined? |
| 288 | + type = pin.typify(api_map) |
| 289 | + type = pin.probe(api_map) if type.undefined? |
| 290 | + pin.docstring.add_tag YARD::Tags::Tag.new('return', nil, type.items.map(&:to_s)) |
| 291 | + pin.instance_variable_set(:@return_type, type) |
| 292 | + end |
| 293 | + end |
| 294 | + end |
| 295 | + rake_yard(store) |
| 296 | + YARD::Registry.save(false, '.yardoc') |
| 297 | + YARD::Registry.load('.yardoc') |
| 298 | + FileUtils.mkdir_p('sig') |
| 299 | + `sord gen sig/solargraph.rbs --rbs --no-regenerate` |
| 300 | + end |
| 301 | + |
239 | 302 | private |
240 | 303 |
|
241 | 304 | # @param pin [Solargraph::Pin::Base] |
|
0 commit comments