Class: RbsRails::CLI
- Inherits:
-
Object
- Object
- RbsRails::CLI
- Defined in:
- lib/rbs_rails/cli.rb,
lib/rbs_rails/cli/configuration.rb,
sig/rbs_rails/cli.rbs,
sig/rbs_rails/cli/configuration.rbs
Defined Under Namespace
Classes: Configuration
Instance Attribute Summary collapse
-
#config_file ⇒ String?
readonly
: String?.
Instance Method Summary collapse
-
#check_db_migrations! ⇒ void
Raise an error if database is not migrated to the latest version.
-
#config ⇒ Configuration
: Configuration.
-
#create_option_parser ⇒ OptionParser
: OptionParser.
-
#generate_models ⇒ void
: void.
-
#generate_path_helpers ⇒ void
: void.
- #generate_single_model(klass) ⇒ Boolean
-
#install_hooks ⇒ void
: void.
-
#load_application ⇒ void
: void.
-
#load_config ⇒ void
: void.
- #run(argv) ⇒ Integer
Instance Attribute Details
#config_file ⇒ String? (readonly)
: String?
11 12 13 |
# File 'lib/rbs_rails/cli.rb', line 11 def config_file @config_file end |
Instance Method Details
#check_db_migrations! ⇒ void
This method returns an undefined value.
Raise an error if database is not migrated to the latest version
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rbs_rails/cli.rb', line 105 def check_db_migrations! #: void return unless config.check_db_migrations if ::ActiveRecord::Migration.respond_to? :check_all_pending! # Rails 7.1 or later ::ActiveRecord::Migration.check_all_pending! # steep:ignore NoMethod else ::ActiveRecord::Migration.check_pending! end end |
#config ⇒ Configuration
: Configuration
61 62 63 |
# File 'lib/rbs_rails/cli.rb', line 61 def config #: Configuration Configuration.instance end |
#create_option_parser ⇒ OptionParser
: OptionParser
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rbs_rails/cli.rb', line 148 def create_option_parser #: OptionParser OptionParser.new do |opts| opts. = <<~BANNER Usage: rbs_rails [command] [options] Commands: help Show this help message version Show version all Generate all RBS files models Generate RBS files for models path_helpers Generate RBS for Rails path helpers Options: BANNER opts.on("--signature-root-dir=DIR", "Specify the root directory for RBS signatures") do |dir| config.signature_root_dir = Pathname.new(dir) end opts.on("--config=FILE", "Load configuration from FILE") do |file| @config_file = file end end end |
#generate_models ⇒ void
This method returns an undefined value.
: void
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rbs_rails/cli.rb', line 92 def generate_models #: void check_db_migrations! Rails.application.eager_load! ::ActiveRecord::Base.descendants.each do |klass| generate_single_model(klass) rescue => e puts "Error generating RBS for #{klass.name} model" raise e end end |
#generate_path_helpers ⇒ void
This method returns an undefined value.
: void
140 141 142 143 144 145 146 |
# File 'lib/rbs_rails/cli.rb', line 140 def generate_path_helpers #: void path = config.signature_root_dir.join 'path_helpers.rbs' path.dirname.mkpath sig = RbsRails::PathHelpers.generate Util::FileWriter.new(path).write sig end |
#generate_single_model(klass) ⇒ Boolean
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rbs_rails/cli.rb', line 117 def generate_single_model(klass) #: bool return false if config.ignored_model?(klass) return false unless RbsRails::ActiveRecord.generatable?(klass) original_path, _line = Object.const_source_location(klass.name) rescue nil rbs_relative_path = if original_path && Pathname.new(original_path).fnmatch?("#{Rails.root}/**") Pathname.new(original_path) .relative_path_from(Rails.root) .sub_ext('.rbs') else "app/models/#{klass.name.underscore}.rbs" end path = config.signature_root_dir / rbs_relative_path path.dirname.mkpath sig = RbsRails::ActiveRecord.class_to_rbs(klass) Util::FileWriter.new(path).write sig true end |
#install_hooks ⇒ void
This method returns an undefined value.
: void
87 88 89 90 |
# File 'lib/rbs_rails/cli.rb', line 87 def install_hooks #: void # Load inspectors. This is necessary to load earlier than Rails application. require 'rbs_rails/active_record/enum' end |
#load_application ⇒ void
This method returns an undefined value.
: void
77 78 79 80 81 82 83 84 85 |
# File 'lib/rbs_rails/cli.rb', line 77 def load_application #: void require_relative "#{Dir.getwd}/config/application" install_hooks Rails.application.initialize! rescue LoadError => e raise "Failed to load Rails application: #{e.}" end |
#load_config ⇒ void
This method returns an undefined value.
: void
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rbs_rails/cli.rb', line 65 def load_config #: void if config_file load config_file else if File.exist?(".rbs_rails.rb") load ".rbs_rails.rb" elsif Rails.root.join("config/rbs_rails.rb").exist? load Rails.root.join("config/rbs_rails.rb").to_s end end end |
#run(argv) ⇒ Integer
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rbs_rails/cli.rb', line 14 def run(argv) #: Integer parser = create_option_parser begin args = parser.parse(argv) subcommand = args.shift || "help" case subcommand when "help" $stdout.puts parser.help 0 when "version" $stdout.puts "rbs_rails #{RbsRails::VERSION}" 0 when "all" load_application load_config generate_models generate_path_helpers 0 when "models" load_application load_config generate_models 0 when "path_helpers" load_application load_config generate_path_helpers 0 else $stdout.puts "Unknown command: #{subcommand}" $stdout.puts parser.help 1 end rescue OptionParser::InvalidOption => e $stderr.puts "Error: #{e.}" $stdout.puts parser.help 1 end rescue StandardError => e $stderr.puts "Error: #{e.}" 1 end |