|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "optparse" |
| 4 | +require "next_rails" |
| 5 | +require "next_rails/bundle_report" |
| 6 | + |
| 7 | +class NextRails::BundleReport::CLI |
| 8 | + def initialize(argv) |
| 9 | + validate_arguments(argv) |
| 10 | + @argv = argv |
| 11 | + end |
| 12 | + |
| 13 | + def validate_arguments(argv) |
| 14 | + return unless argv.any? |
| 15 | + |
| 16 | + valid_report_types = %w[outdated compatibility ruby_check] |
| 17 | + report_type = argv.first |
| 18 | + |
| 19 | + unless valid_report_types.include?(report_type) |
| 20 | + raise ArgumentError, "Invalid report type '#{report_type}'. Valid types are: #{valid_report_types.join(', ')}." |
| 21 | + end |
| 22 | + |
| 23 | + argv.each do |arg| |
| 24 | + if arg.start_with?("--rails-version") && !arg.match?(/--rails-version=+\d+(\.\d+)*$/) |
| 25 | + raise ArgumentError, "Invalid Rails version format. Example: --rails-version=5.0.7" |
| 26 | + end |
| 27 | + |
| 28 | + if arg.start_with?("--ruby-version") && !arg.match?(/--ruby-version=+\d+(\.\d+)*$/) |
| 29 | + raise ArgumentError, "Invalid Ruby version format. Example: --ruby-version=3.3" |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + def run |
| 35 | + options = parse_options |
| 36 | + execute_report(@argv.first, options) |
| 37 | + end |
| 38 | + |
| 39 | + private |
| 40 | + |
| 41 | + def parse_options |
| 42 | + options = {} |
| 43 | + option_parser = OptionParser.new do |opts| |
| 44 | + opts.banner = <<-EOS |
| 45 | + Usage: #{$0} [report-type] [options] |
| 46 | +
|
| 47 | + report-type There are three report types available: `outdated`, `compatibility` and `ruby_check`. |
| 48 | +
|
| 49 | + Examples: |
| 50 | + #{$0} compatibility --rails-version 5.0 |
| 51 | + #{$0} compatibility --ruby-version 3.3 |
| 52 | + #{$0} outdated |
| 53 | + #{$0} outdated --json |
| 54 | +
|
| 55 | + ruby_check To find a compatible ruby version for the target rails version |
| 56 | +
|
| 57 | + Examples: |
| 58 | + #{$0} ruby_check --rails-version 7.0.0 |
| 59 | +
|
| 60 | + EOS |
| 61 | + |
| 62 | + opts.separator "" |
| 63 | + opts.separator "Options:" |
| 64 | + |
| 65 | + opts.on("--rails-version [STRING]", |
| 66 | + "Rails version to check compatibility against (defaults to 5.0)") do |rails_version| |
| 67 | + options[:rails_version] = rails_version |
| 68 | + end |
| 69 | + |
| 70 | + opts.on("--ruby-version [STRING]", |
| 71 | + "Ruby version to check compatibility against (defaults to 2.3)") do |ruby_version| |
| 72 | + options[:ruby_version] = ruby_version |
| 73 | + end |
| 74 | + |
| 75 | + opts.on("--include-rails-gems", "Include Rails gems in compatibility report (defaults to false)") do |
| 76 | + options[:include_rails_gems] = true |
| 77 | + end |
| 78 | + |
| 79 | + opts.on("--json", "Output JSON in outdated report (defaults to false)") do |
| 80 | + options[:format] = "json" |
| 81 | + end |
| 82 | + |
| 83 | + opts.on_tail("-h", "--help", "Show this message") do |
| 84 | + puts opts |
| 85 | + exit |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + begin |
| 90 | + option_parser.parse!(@argv) |
| 91 | + rescue OptionParser::ParseError => e |
| 92 | + warn Rainbow(e.message).red |
| 93 | + puts option_parser |
| 94 | + exit 1 |
| 95 | + end |
| 96 | + |
| 97 | + options |
| 98 | + end |
| 99 | + |
| 100 | + def execute_report(report_type, options) |
| 101 | + case report_type |
| 102 | + when "ruby_check" |
| 103 | + NextRails::BundleReport.compatible_ruby_version(rails_version: options.fetch(:rails_version)) |
| 104 | + when "outdated" |
| 105 | + NextRails::BundleReport.outdated(options.fetch(:format, nil)) |
| 106 | + else |
| 107 | + if options[:ruby_version] |
| 108 | + NextRails::BundleReport.ruby_compatibility(ruby_version: options.fetch(:ruby_version, "2.3")) |
| 109 | + else |
| 110 | + NextRails::BundleReport.rails_compatibility( |
| 111 | + rails_version: options.fetch(:rails_version, "5.0"), |
| 112 | + include_rails_gems: options.fetch(:include_rails_gems, false) |
| 113 | + ) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments