|
| 1 | +require "optparse" |
| 2 | +require "rbs_rails/cli/configuration" |
| 3 | + |
| 4 | +module RbsRails |
| 5 | + # @rbs &block: (CLI::Configuration) -> void |
| 6 | + def self.configure(&block) #: void |
| 7 | + CLI::Configuration.configure(&block) |
| 8 | + end |
| 9 | + |
| 10 | + class CLI |
| 11 | + attr_reader :config_file #: String? |
| 12 | + |
| 13 | + # @rbs argv: Array[String] |
| 14 | + def run(argv) #: Integer |
| 15 | + parser = create_option_parser |
| 16 | + |
| 17 | + begin |
| 18 | + args = parser.parse(argv) |
| 19 | + subcommand = args.shift || "help" |
| 20 | + |
| 21 | + case subcommand |
| 22 | + when "help" |
| 23 | + $stdout.puts parser.help |
| 24 | + 0 |
| 25 | + when "version" |
| 26 | + $stdout.puts "rbs_rails #{RbsRails::VERSION}" |
| 27 | + 0 |
| 28 | + when "all" |
| 29 | + load_application |
| 30 | + load_config |
| 31 | + generate_models |
| 32 | + generate_path_helpers |
| 33 | + 0 |
| 34 | + when "models" |
| 35 | + load_application |
| 36 | + load_config |
| 37 | + generate_models |
| 38 | + 0 |
| 39 | + when "path_helpers" |
| 40 | + load_application |
| 41 | + load_config |
| 42 | + generate_path_helpers |
| 43 | + 0 |
| 44 | + else |
| 45 | + $stdout.puts "Unknown command: #{subcommand}" |
| 46 | + $stdout.puts parser.help |
| 47 | + 1 |
| 48 | + end |
| 49 | + rescue OptionParser::InvalidOption => e |
| 50 | + $stderr.puts "Error: #{e.message}" |
| 51 | + $stdout.puts parser.help |
| 52 | + 1 |
| 53 | + end |
| 54 | + rescue StandardError => e |
| 55 | + $stderr.puts "Error: #{e.message}" |
| 56 | + 1 |
| 57 | + end |
| 58 | + |
| 59 | + private |
| 60 | + |
| 61 | + def config #: Configuration |
| 62 | + Configuration.instance |
| 63 | + end |
| 64 | + |
| 65 | + def load_config #: void |
| 66 | + if config_file |
| 67 | + load config_file |
| 68 | + else |
| 69 | + if File.exist?(".rbs_rails.rb") |
| 70 | + load ".rbs_rails.rb" |
| 71 | + elsif Rails.root.join("config/rbs_rails.rb").exist? |
| 72 | + load Rails.root.join("config/rbs_rails.rb").to_s |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def load_application #: void |
| 78 | + require_relative "#{Dir.getwd}/config/application" |
| 79 | + |
| 80 | + install_hooks |
| 81 | + |
| 82 | + Rails.application.initialize! |
| 83 | + rescue LoadError => e |
| 84 | + raise "Failed to load Rails application: #{e.message}" |
| 85 | + end |
| 86 | + |
| 87 | + def install_hooks #: void |
| 88 | + # Load inspectors. This is necessary to load earlier than Rails application. |
| 89 | + require 'rbs_rails/active_record/enum' |
| 90 | + end |
| 91 | + |
| 92 | + def generate_models #: void |
| 93 | + Rails.application.eager_load! |
| 94 | + |
| 95 | + dep_builder = DependencyBuilder.new |
| 96 | + |
| 97 | + ::ActiveRecord::Base.descendants.each do |klass| |
| 98 | + generate_single_model(klass, dep_builder) |
| 99 | + rescue => e |
| 100 | + puts "Error generating RBS for #{klass.name} model" |
| 101 | + raise e |
| 102 | + end |
| 103 | + |
| 104 | + if dep_rbs = dep_builder.build |
| 105 | + config.signature_root_dir.join('model_dependencies.rbs').write(dep_rbs) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + # @rbs klass: singleton(ActiveRecord::Base) |
| 110 | + # @rbs dep_builder: DependencyBuilder |
| 111 | + def generate_single_model(klass, dep_builder) #: bool |
| 112 | + return false if config.ignored_model?(klass) |
| 113 | + return false unless RbsRails::ActiveRecord.generatable?(klass) |
| 114 | + |
| 115 | + original_path, _line = Object.const_source_location(klass.name) rescue nil |
| 116 | + |
| 117 | + rbs_relative_path = if original_path && Pathname.new(original_path).fnmatch?("#{Rails.root}/**") |
| 118 | + Pathname.new(original_path) |
| 119 | + .relative_path_from(Rails.root) |
| 120 | + .sub_ext('.rbs') |
| 121 | + else |
| 122 | + "app/models/#{klass.name.underscore}.rbs" |
| 123 | + end |
| 124 | + |
| 125 | + path = config.signature_root_dir / rbs_relative_path |
| 126 | + path.dirname.mkpath |
| 127 | + |
| 128 | + sig = RbsRails::ActiveRecord.class_to_rbs(klass, dependencies: dep_builder.deps) |
| 129 | + path.write sig |
| 130 | + dep_builder.done << klass.name |
| 131 | + |
| 132 | + true |
| 133 | + end |
| 134 | + |
| 135 | + def generate_path_helpers #: void |
| 136 | + path = config.signature_root_dir.join 'path_helpers.rbs' |
| 137 | + path.dirname.mkpath |
| 138 | + |
| 139 | + sig = RbsRails::PathHelpers.generate |
| 140 | + path.write sig |
| 141 | + end |
| 142 | + |
| 143 | + def create_option_parser #: OptionParser |
| 144 | + OptionParser.new do |opts| |
| 145 | + opts.banner = <<~BANNER |
| 146 | + Usage: rbs_rails [command] [options] |
| 147 | +
|
| 148 | + Commands: |
| 149 | + help Show this help message |
| 150 | + version Show version |
| 151 | + all Generate all RBS files |
| 152 | + models Generate RBS files for models |
| 153 | + path_helpers Generate RBS for Rails path helpers |
| 154 | +
|
| 155 | + Options: |
| 156 | + BANNER |
| 157 | + |
| 158 | + opts.on("--signature-root-dir=DIR", "Specify the root directory for RBS signatures") do |dir| |
| 159 | + config.signature_root_dir = Pathname.new(dir) |
| 160 | + end |
| 161 | + |
| 162 | + opts.on("--config=FILE", "Load configuration from FILE") do |file| |
| 163 | + @config_file = file |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | + end |
| 168 | +end |
0 commit comments