668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
|
# File 'lib/rbs/cli.rb', line 668
def run_prototype_file(format, args)
availability = unless has_parser?
"\n** This command does not work on this interpreter (#{RUBY_ENGINE}) **\n"
end
output_dir = nil
base_dir = nil
force = false
opts = OptionParser.new
opts.banner = "Usage: rbs prototype \#{format} [files...]\n\#{availability}\nGenerate RBS prototype from source code.\nIt parses specified Ruby code and and generates RBS prototypes.\n\nIt only works on MRI because it parses Ruby code with `RubyVM::AbstractSyntaxTree`.\n\nExamples:\n\n $ rbs prototype rb lib/foo.rb\n $ rbs prototype rbi sorbet/rbi/foo.rbi\n\nYou can run the tool in *batch* mode by passing `--out-dir` option.\n\n $ rbs prototype rb --out-dir=sig lib/foo.rb\n $ rbs prototype rbi --out-dir=sig/models --base-dir=app/models app/models\n"
opts.on("--out-dir=DIR", "Specify the path to save the generated RBS files") do |path|
output_dir = Pathname(path)
end
opts.on("--base-dir=DIR", "Specify the path to calculate the relative path to save the generated RBS files") do |path|
base_dir = Pathname(path)
end
opts.on("--force", "Overwrite existing RBS files") do
force = true
end
opts.parse!(args)
unless has_parser?
stdout.puts "Not supported on this interpreter (#{RUBY_ENGINE})."
exit 1
end
if args.empty?
stdout.puts opts
return nil
end
new_parser = -> do
case format
when "rbi"
Prototype::RBI.new()
when "rb"
Prototype::RB.new()
else
raise
end
end
input_paths = args.map {|arg| Pathname(arg) }
if output_dir
skip_paths = []
input_paths.each do |path|
stdout.puts "Processing `#{path}`..."
ruby_files =
if path.file?
[path]
else
path.glob("**/*.rb").sort
end
ruby_files.each do |file_path|
stdout.puts " Generating RBS for `#{file_path}`..."
relative_path =
if base_dir
file_path.relative_path_from(base_dir)
else
if top = file_path.descend.first
case
when top == Pathname("lib")
file_path.relative_path_from(top)
when top == Pathname("app")
file_path.relative_path_from(top)
else
file_path
end
else
file_path
end
end
relative_path = relative_path.cleanpath()
if relative_path.absolute? || relative_path.descend.first&.to_s == ".."
stdout.puts " ⚠️ Cannot write the RBS to outside of the output dir: `#{relative_path}`"
next
end
output_path = (output_dir + relative_path).sub_ext(".rbs")
parser = new_parser[]
begin
parser.parse file_path.read()
rescue SyntaxError
stdout.puts " ⚠️ Unable to parse due to SyntaxError: `#{file_path}`"
next
end
if output_path.file?
if force
stdout.puts " - Writing RBS to existing file `#{output_path}`..."
else
stdout.puts " - Skipping existing file `#{output_path}`..."
skip_paths << file_path
next
end
else
stdout.puts " - Writing RBS to `#{output_path}`..."
end
(output_path.parent).mkpath
output_path.open("w") do |io|
writer = Writer.new(out: io)
writer.write(parser.decls)
end
end
end
unless skip_paths.empty?
stdout.puts
stdout.puts ">>>> Skipped existing #{skip_paths.size} files. Use `--force` option to update the files."
command = original_args.take(original_args.size - input_paths.size)
skip_paths.take(10).each do |path|
stdout.puts " #{defined?(Bundler) ? "bundle exec " : ""}rbs #{Shellwords.join(command)} --force #{Shellwords.escape(path.to_s)}"
end
if skip_paths.size > 10
stdout.puts " ..."
end
end
else
parser = new_parser[]
input_paths.each do |file|
parser.parse file.read()
end
writer = Writer.new(out: stdout)
writer.write parser.decls
end
end
|