Class: RBS::CLI
- Inherits:
-
Object
- Object
- RBS::CLI
- Defined in:
- lib/rbs/cli.rb
Defined Under Namespace
Classes: LibraryOptions
Constant Summary collapse
- COMMANDS =
[:ast, :list, :ancestors, :methods, :method, :validate, :constant, :paths, :prototype, :vendor, :version, :parse]
Instance Attribute Summary collapse
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
-
#initialize(stdout:, stderr:) ⇒ CLI
constructor
A new instance of CLI.
- #library_parse(opts, options:) ⇒ Object
- #parse_logging_options(opts) ⇒ Object
- #parse_type_name(string) ⇒ Object
- #run(args) ⇒ Object
- #run_ancestors(args, options) ⇒ Object
- #run_ast(args, options) ⇒ Object
- #run_constant(args, options) ⇒ Object
- #run_list(args, options) ⇒ Object
- #run_method(args, options) ⇒ Object
- #run_methods(args, options) ⇒ Object
- #run_parse(args, options) ⇒ Object
- #run_paths(args, options) ⇒ Object
- #run_prototype(args, options) ⇒ Object
- #run_prototype_file(format, args) ⇒ Object
- #run_validate(args, options) ⇒ Object
- #run_vendor(args, options) ⇒ Object
- #run_version(args, options) ⇒ Object
Constructor Details
#initialize(stdout:, stderr:) ⇒ CLI
Returns a new instance of CLI.
34 35 36 37 |
# File 'lib/rbs/cli.rb', line 34 def initialize(stdout:, stderr:) @stdout = stdout @stderr = stderr end |
Instance Attribute Details
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
32 33 34 |
# File 'lib/rbs/cli.rb', line 32 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
31 32 33 |
# File 'lib/rbs/cli.rb', line 31 def stdout @stdout end |
Instance Method Details
#library_parse(opts, options:) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rbs/cli.rb', line 41 def library_parse(opts, options:) opts.on("-r LIBRARY") do |lib| .libs << lib end opts.on("-I DIR") do |dir| .dirs << dir end opts.on("--no-stdlib") do .no_stdlib = true end opts end |
#parse_logging_options(opts) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rbs/cli.rb', line 57 def (opts) opts.on("--log-level=LEVEL", "Specify log level (defaults to `warn`)") do |level| RBS.logger_level = level end opts.on("--log-output=OUTPUT", "Specify the file to output log (defaults to stderr)") do |output| RBS.logger_output = File.open(output, "a") end opts end |
#parse_type_name(string) ⇒ Object
551 552 553 554 555 556 |
# File 'lib/rbs/cli.rb', line 551 def parse_type_name(string) Namespace.parse(string).yield_self do |namespace| last = namespace.path.last TypeName.new(name: last, namespace: namespace.parent) end end |
#run(args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rbs/cli.rb', line 69 def run(args) = LibraryOptions.new opts = OptionParser.new opts. = <<~USAGE Usage: rbs [options] COMMAND Available commands: #{COMMANDS.join(", ")} USAGE library_parse(opts, options: ) (opts) opts.order!(args) command = args.shift&.to_sym if COMMANDS.include?(command) __send__ :"run_#{command}", args, else stdout.puts opts.help end end |
#run_ancestors(args, options) ⇒ Object
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 |
# File 'lib/rbs/cli.rb', line 139 def run_ancestors(args, ) kind = :instance OptionParser.new do |opts| opts.on("--instance") { kind = :instance } opts.on("--singleton") { kind = :singleton } end.order!(args) loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) builder = DefinitionBuilder.new(env: env) type_name = parse_type_name(args[0]).absolute! if env.class?(type_name) ancestor = case kind when :instance decl = env.find_class(type_name) Definition::Ancestor::Instance.new(name: type_name, args: Types::Variable.build(decl.type_params.each.map(&:name))) when :singleton Definition::Ancestor::Singleton.new(name: type_name) end ancestors = builder.build_ancestors(ancestor) ancestors.each do |ancestor| case ancestor when Definition::Ancestor::Singleton stdout.puts "singleton(#{ancestor.name})" when Definition::Ancestor::ExtensionSingleton stdout.puts "singleton(#{ancestor.name} (#{ancestor.extension_name}))" when Definition::Ancestor::Instance if ancestor.args.empty? stdout.puts ancestor.name.to_s else stdout.puts "#{ancestor.name}[#{ancestor.args.join(", ")}]" end when Definition::Ancestor::ExtensionInstance if ancestor.args.empty? stdout.puts "#{ancestor.name} (#{ancestor.extension_name})" else stdout.puts "#{ancestor.name}[#{ancestor.args.join(", ")}] (#{ancestor.extension_name})" end end end else stdout.puts "Cannot find class: #{type_name}" end end |
#run_ast(args, options) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rbs/cli.rb', line 91 def run_ast(args, ) loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) stdout.print JSON.generate(env.declarations) stdout.flush end |
#run_constant(args, options) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/rbs/cli.rb', line 338 def run_constant(args, ) context = nil OptionParser.new do |opts| opts.on("--context CONTEXT") {|c| context = c } end.order!(args) unless args.size == 1 stdout.puts "Expected one argument." return end loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) builder = DefinitionBuilder.new(env: env) table = ConstantTable.new(builder: builder) namespace = context ? Namespace.parse(context).absolute! : Namespace.root stdout.puts "Context: #{namespace}" name = Namespace.parse(args[0]).to_type_name stdout.puts "Constant name: #{name}" constant = table.resolve_constant_reference(name, context: namespace.ascend.to_a) if constant stdout.puts " => #{constant.name}: #{constant.type}" else stdout.puts " => [no constant]" end end |
#run_list(args, options) ⇒ Object
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 |
# File 'lib/rbs/cli.rb', line 103 def run_list(args, ) list = [] OptionParser.new do |opts| opts.on("--class") { list << :class } opts.on("--module") { list << :module } opts.on("--interface") { list << :interface } end.order!(args) list.push(:class, :module, :interface) if list.empty? loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) env.each_decl.sort_by {|name,| name.to_s }.each do |type_name, decl| case decl when AST::Declarations::Class if list.include?(:class) stdout.puts "#{type_name} (class)" end when AST::Declarations::Module if list.include?(:module) stdout.puts "#{type_name} (module)" end when AST::Declarations::Interface if list.include?(:interface) stdout.puts "#{type_name} (interface)" end end end end |
#run_method(args, options) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/rbs/cli.rb', line 239 def run_method(args, ) kind = :instance OptionParser.new do |opts| opts.on("--instance") { kind = :instance } opts.on("--singleton") { kind = :singleton } end.order!(args) unless args.size == 2 stdout.puts "Expected two arguments, but given #{args.size}." return end loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) builder = DefinitionBuilder.new(env: env) type_name = parse_type_name(args[0]).absolute! method_name = args[1].to_sym unless env.class?(type_name) stdout.puts "Cannot find class: #{type_name}" return end definition = case kind when :instance builder.build_instance(type_name) when :singleton builder.build_singleton(type_name) end method = definition.methods[method_name] unless method stdout.puts "Cannot find method: #{method_name}" return end stdout.puts "#{type_name}#{kind == :instance ? "#" : "."}#{method_name}" stdout.puts " defined_in: #{method.defined_in&.name&.absolute!}" stdout.puts " implementation: #{method.implemented_in.name.absolute!}" stdout.puts " accessibility: #{method.accessibility}" stdout.puts " types:" separator = " " for type in method.method_types stdout.puts " #{separator} #{type}" separator = "|" end end |
#run_methods(args, options) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rbs/cli.rb', line 194 def run_methods(args, ) kind = :instance inherit = true OptionParser.new do |opts| opts.on("--instance") { kind = :instance } opts.on("--singleton") { kind = :singleton } opts.on("--inherit") { inherit = true } opts.on("--no-inherit") { inherit = false } end.order!(args) unless args.size == 1 stdout.puts "Expected one argument." return end loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) builder = DefinitionBuilder.new(env: env) type_name = parse_type_name(args[0]).absolute! if env.class?(type_name) definition = case kind when :instance builder.build_instance(type_name) when :singleton builder.build_singleton(type_name) end definition.methods.keys.sort.each do |name| method = definition.methods[name] if inherit || method.implemented_in == definition.declaration stdout.puts "#{name} (#{method.accessibility})" end end else stdout.puts "Cannot find class: #{type_name}" end end |
#run_parse(args, options) ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/rbs/cli.rb', line 530 def run_parse(args, ) loader = EnvironmentLoader.new() syntax_error = false args.each do |path| path = Pathname(path) loader.each_signature(path) do |sig_path| Parser.parse_signature(sig_path.read) rescue RBS::Parser::SyntaxError => ex loc = ex.error_value.location stdout.puts "#{sig_path}:#{loc.start_line}:#{loc.start_column}: parse error on value: (#{ex.token_str})" syntax_error = true rescue RBS::Parser::SemanticsError => ex loc = ex.location stdout.puts "#{sig_path}:#{loc.start_line}:#{loc.start_column}: #{ex.}" syntax_error = true end end exit 1 if syntax_error end |
#run_paths(args, options) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/rbs/cli.rb', line 378 def run_paths(args, ) loader = EnvironmentLoader.new() .setup(loader) kind_of = -> (path) { case when path.file? "file" when path.directory? "dir" when !path.exist? "absent" else "unknown" end } if loader.stdlib_root path = loader.stdlib_root stdout.puts "#{path}/builtin (#{kind_of[path]}, stdlib)" end loader.paths.each do |path| case path when Pathname stdout.puts "#{path} (#{kind_of[path]})" when EnvironmentLoader::GemPath stdout.puts "#{path.path} (#{kind_of[path.path]}, gem, name=#{path.name}, version=#{path.version})" when EnvironmentLoader::LibraryPath stdout.puts "#{path.path} (#{kind_of[path.path]}, library, name=#{path.name})" end end end |
#run_prototype(args, options) ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/rbs/cli.rb', line 413 def run_prototype(args, ) format = args.shift case format when "rbi", "rb" decls = run_prototype_file(format, args) when "runtime" require_libs = [] relative_libs = [] merge = false owners_included = [] OptionParser.new do |opts| opts.on("--require LIB") do |lib| require_libs << lib end opts.on("--require-relative LIB") do |lib| relative_libs << lib end opts.on("--merge") do merge = true end opts.on("--method-owner CLASS") do |klass| owners_included << klass end end.parse!(args) loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) require_libs.each do |lib| require(lib) end relative_libs.each do |lib| require(lib) end decls = Prototype::Runtime.new(patterns: args, env: env, merge: merge, owners_included: owners_included).decls else stdout.puts "Supported formats: rbi, rb, runtime" exit 1 end writer = Writer.new(out: stdout) writer.write decls end |
#run_prototype_file(format, args) ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/rbs/cli.rb', line 465 def run_prototype_file(format, args) parser = case format when "rbi" Prototype::RBI.new() when "rb" Prototype::RB.new() end args.each do |file| parser.parse Pathname(file).read end parser.decls end |
#run_validate(args, options) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/rbs/cli.rb', line 294 def run_validate(args, ) loader = EnvironmentLoader.new() .setup(loader) env = Environment.new() loader.load(env: env) builder = DefinitionBuilder.new(env: env) env.each_decl do |name, decl| case decl when AST::Declarations::Class, AST::Declarations::Module stdout.puts "#{Location.to_string decl.location}:\tValidating class/module definition: `#{name}`..." builder.build_instance(decl.name.absolute!).each_type do |type| env.validate type, namespace: Namespace.root end builder.build_singleton(decl.name.absolute!).each_type do |type| env.validate type, namespace: Namespace.root end when AST::Declarations::Interface stdout.puts "#{Location.to_string decl.location}:\tValidating interface: `#{name}`..." builder.build_interface(decl.name.absolute!, decl).each_type do |type| env.validate type, namespace: Namespace.root end end end env.each_constant do |name, const| stdout.puts "#{Location.to_string const.location}:\tValidating constant: `#{name}`..." env.validate const.type, namespace: name.namespace end env.each_global do |name, global| stdout.puts "#{Location.to_string global.location}:\tValidating global: `#{name}`..." env.validate global.type, namespace: Namespace.root end env.each_alias do |name, decl| stdout.puts "#{Location.to_string decl.location}:\tValidating alias: `#{name}`..." env.validate decl.type, namespace: name.namespace end end |
#run_vendor(args, options) ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/rbs/cli.rb', line 480 def run_vendor(args, ) clean = false vendor_stdlib = false vendor_dir = Pathname("vendor/sigs") OptionParser.new do |opts| opts. = <<~EOB Usage: rbs vendor [options] GEMS... Vendor signatures in the project directory. EOB opts.on("--[no-]clean", "Clean vendor directory (default: no)") do |v| clean = v end opts.on("--[no-]stdlib", "Vendor stdlib signatures or not (default: no)") do |v| vendor_stdlib = v end opts.on("--vendor-dir [DIR]", "Specify the directory for vendored signatures (default: vendor/sigs)") do |path| vendor_dir = Pathname(path) end end.parse!(args) stdout.puts "Vendoring signatures to #{vendor_dir}..." vendorer = Vendorer.new(vendor_dir: vendor_dir) if clean stdout.puts " Deleting #{vendor_dir}..." vendorer.clean! end if vendor_stdlib stdout.puts " Vendoring standard libraries..." vendorer.stdlib! end args.each do |gem| name, version = EnvironmentLoader.parse_library(gem) unless EnvironmentLoader.gem_sig_path(name, version) stdout.puts " ⚠️ Cannot find rubygem: name=#{name}, version=#{version} 🚨" else stdout.puts " Vendoring gem: name=#{name}, version=#{version}..." vendorer.gem!(name, version) end end end |
#run_version(args, options) ⇒ Object
374 375 376 |
# File 'lib/rbs/cli.rb', line 374 def run_version(args, ) stdout.puts "ruby-signature #{VERSION}" end |