|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../ruby_version_adder" |
| 4 | +require_relative "console" |
| 5 | + |
| 6 | +module Commands |
| 7 | + # Add Ruby Version Command (Presentation Layer) |
| 8 | + # |
| 9 | + # This class provides a CLI-friendly interface for adding Ruby versions. |
| 10 | + # It wraps RubyVersionAdder with formatted output including colors and emojis. |
| 11 | + # |
| 12 | + # For silent/programmatic usage, use RubyVersionAdder directly. |
| 13 | + class AddRubyVersion |
| 14 | + include Console |
| 15 | + |
| 16 | + class << self |
| 17 | + # Main entry point for adding a Ruby version with formatted output |
| 18 | + # |
| 19 | + # @param version [String] The Ruby version to add (e.g., "3.4.0") |
| 20 | + # @param working_dir [String] The directory to operate in |
| 21 | + # @param output [IO] Output stream for messages (default: $stdout) |
| 22 | + # @return [Hash] Result with :success, :files_modified, and :message keys |
| 23 | + def call(version, working_dir:, output: $stdout) |
| 24 | + new(version, working_dir: working_dir, output: output).call |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + attr_reader :version, :working_dir, :output |
| 29 | + |
| 30 | + def initialize(version, working_dir:, output: $stdout) |
| 31 | + @version = version |
| 32 | + @working_dir = working_dir |
| 33 | + @output = output |
| 34 | + end |
| 35 | + |
| 36 | + def call |
| 37 | + log_checking_configuration |
| 38 | + |
| 39 | + # Get current default before running the service |
| 40 | + service = RubyVersionAdder::Service.new(version, working_dir: working_dir) |
| 41 | + |
| 42 | + begin |
| 43 | + current_default = service.current_default_version |
| 44 | + log_version_info(current_default) |
| 45 | + rescue RubyVersionAdder::Error |
| 46 | + # Will be handled by the service call |
| 47 | + end |
| 48 | + |
| 49 | + # Call the business logic service |
| 50 | + result = RubyVersionAdder.call(version, working_dir: working_dir) |
| 51 | + |
| 52 | + if result[:success] |
| 53 | + log_success_result(result) |
| 54 | + else |
| 55 | + log_error(result[:error]) |
| 56 | + end |
| 57 | + |
| 58 | + result |
| 59 | + end |
| 60 | + |
| 61 | + private |
| 62 | + |
| 63 | + def log_checking_configuration |
| 64 | + log("Checking current configuration...", :cyan, emoji: :search) |
| 65 | + end |
| 66 | + |
| 67 | + def log_version_info(current_default) |
| 68 | + log("Current default version: #{current_default}") |
| 69 | + log("New version to add: #{version}") |
| 70 | + end |
| 71 | + |
| 72 | + def log_success_result(result) |
| 73 | + log("") |
| 74 | + log("Adding #{version} to #{RubyVersionAdder::VERSIONS_JSON_FILE}...", :blue, emoji: :edit) |
| 75 | + log("Successfully added to #{RubyVersionAdder::VERSIONS_JSON_FILE}", :green, emoji: :check) |
| 76 | + |
| 77 | + if result[:default_updated] |
| 78 | + log_default_updated(result) |
| 79 | + else |
| 80 | + log_default_unchanged(result[:previous_default]) |
| 81 | + end |
| 82 | + |
| 83 | + log_final_success(result[:files_modified]) |
| 84 | + end |
| 85 | + |
| 86 | + def log_default_updated(result) |
| 87 | + log("") |
| 88 | + log("New version #{version} is newer than current default #{result[:previous_default]}", :yellow, emoji: :update) |
| 89 | + log("Updated default version to #{version}", :green, emoji: :check) |
| 90 | + log("Updated README default version to #{version}", :green, emoji: :check) |
| 91 | + |
| 92 | + log("") |
| 93 | + log("Bumping feature version...", :yellow, emoji: :update) |
| 94 | + log("Feature version bumped from #{result[:old_feature_version]} to #{result[:new_feature_version]}", :green, emoji: :check) |
| 95 | + |
| 96 | + log("") |
| 97 | + log("Updating test files...", :blue, emoji: :edit) |
| 98 | + RubyVersionAdder::TEST_FILES.each do |test_file| |
| 99 | + log("Updated #{test_file}", :green, emoji: :check) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def log_default_unchanged(previous_default) |
| 104 | + log("") |
| 105 | + log("New version #{version} is not newer than current default #{previous_default}", :cyan, emoji: :info) |
| 106 | + log("Default version remains unchanged") |
| 107 | + log("") |
| 108 | + log("Skipping test file updates (new version #{version} is not newer than current default #{previous_default})", :cyan, emoji: :info) |
| 109 | + end |
| 110 | + |
| 111 | + def log_final_success(files_modified) |
| 112 | + log("") |
| 113 | + log("Successfully added Ruby version #{version}!", :green, emoji: :party) |
| 114 | + log("") |
| 115 | + log("Files modified:", :blue, emoji: :file) |
| 116 | + files_modified.each { |file| log(" • #{file}") } |
| 117 | + |
| 118 | + log("") |
| 119 | + log("Next steps:", :magenta, emoji: :bulb) |
| 120 | + log(" 1. Review the changes: git diff") |
| 121 | + log(" 2. Commit the changes: git add . && git commit -m 'Add Ruby #{version}'") |
| 122 | + log(" 3. Push changes: git push") |
| 123 | + end |
| 124 | + |
| 125 | + def log_error(error_message) |
| 126 | + log("Error: #{error_message}", :red, emoji: :error) |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments