From 4a2dde846252706cd29668a8b3ad199be7d14faa Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Tue, 7 May 2024 20:30:31 +1000 Subject: [PATCH] Support and require Rails 7.2 --- lib/rails_console_commands.rb | 14 ++++++++++--- lib/rails_console_commands/arg_parser.rb | 20 +++++++++++++++++++ lib/rails_console_commands/commander.rb | 4 ++++ lib/rails_console_commands/destroy_command.rb | 14 +++++++++++++ .../generate_command.rb | 14 +++++++++++++ lib/rails_console_commands/rake_command.rb | 14 +++++++++++++ ...{console_delegation.rb => test_command.rb} | 14 ++++++------- lib/rails_console_commands/update_command.rb | 14 +++++++++++++ rails_console_commands.gemspec | 2 ++ 9 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 lib/rails_console_commands/arg_parser.rb create mode 100644 lib/rails_console_commands/destroy_command.rb create mode 100644 lib/rails_console_commands/generate_command.rb create mode 100644 lib/rails_console_commands/rake_command.rb rename lib/rails_console_commands/{console_delegation.rb => test_command.rb} (58%) create mode 100644 lib/rails_console_commands/update_command.rb diff --git a/lib/rails_console_commands.rb b/lib/rails_console_commands.rb index b679c10..86400a6 100644 --- a/lib/rails_console_commands.rb +++ b/lib/rails_console_commands.rb @@ -1,7 +1,15 @@ # frozen_string_literal: true -require 'rails/console/app' -require_relative './rails_console_commands/console_delegation' +require "irb" +require_relative './rails_console_commands/test_command' +require_relative './rails_console_commands/rake_command' +require_relative './rails_console_commands/generate_command' +require_relative './rails_console_commands/destroy_command' +require_relative './rails_console_commands/update_command' require_relative './rails_console_commands/version' -Rails::ConsoleMethods.send :include, RailsConsoleCommands::ConsoleDelegation +IRB::Command.register(:test, RailsConsoleCommands::TestCommand) +IRB::Command.register(:rake, RailsConsoleCommands::RakeCommand) +IRB::Command.register(:generate, RailsConsoleCommands::GenerateCommand) +IRB::Command.register(:destroy, RailsConsoleCommands::DestroyCommand) +IRB::Command.register(:update, RailsConsoleCommands::UpdateCommand) diff --git a/lib/rails_console_commands/arg_parser.rb b/lib/rails_console_commands/arg_parser.rb new file mode 100644 index 0000000..69fcb5a --- /dev/null +++ b/lib/rails_console_commands/arg_parser.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module RailsConsoleCommands + module ArgParser + def parse_arg(arg) + # IRB parses the arg differently for single and double quotes strings. E.g. + # test 'foo' -> "'foo'" + # test "foo" -> "\"foo\"" + # test 'foo', 10 -> "'foo', 10" + # test "foo", 10 -> "\"foo\", 10" + arg = arg.strip + arg = arg.delete("'") # handle single quote case + begin + JSON.parse(arg) # handle double quote case + rescue JSON::ParserError + arg + end + end + end +end diff --git a/lib/rails_console_commands/commander.rb b/lib/rails_console_commands/commander.rb index b2b8170..406607b 100644 --- a/lib/rails_console_commands/commander.rb +++ b/lib/rails_console_commands/commander.rb @@ -6,6 +6,10 @@ module RailsConsoleCommands class Commander + def self.commander + @commander ||= Commander.new + end + delegate :rake, to: :raker delegate :test, to: :tester delegate :generate, :destroy, :update, to: :generator diff --git a/lib/rails_console_commands/destroy_command.rb b/lib/rails_console_commands/destroy_command.rb new file mode 100644 index 0000000..7f44be2 --- /dev/null +++ b/lib/rails_console_commands/destroy_command.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require_relative './arg_parser' +require_relative './commander' + +module RailsConsoleCommands + class DestroyCommand < IRB::Command::Base + include ArgParser + + def execute(arg) + Commander.commander.destroy(parse_arg(arg)) + end + end +end diff --git a/lib/rails_console_commands/generate_command.rb b/lib/rails_console_commands/generate_command.rb new file mode 100644 index 0000000..0e7da73 --- /dev/null +++ b/lib/rails_console_commands/generate_command.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require_relative './arg_parser' +require_relative './commander' + +module RailsConsoleCommands + class GenerateCommand < IRB::Command::Base + include ArgParser + + def execute(arg) + Commander.commander.generate(parse_arg(arg)) + end + end +end diff --git a/lib/rails_console_commands/rake_command.rb b/lib/rails_console_commands/rake_command.rb new file mode 100644 index 0000000..7baecde --- /dev/null +++ b/lib/rails_console_commands/rake_command.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require_relative './arg_parser' +require_relative './commander' + +module RailsConsoleCommands + class RakeCommand < IRB::Command::Base + include ArgParser + + def execute(arg) + Commander.commander.rake(parse_arg(arg)) + end + end +end diff --git a/lib/rails_console_commands/console_delegation.rb b/lib/rails_console_commands/test_command.rb similarity index 58% rename from lib/rails_console_commands/console_delegation.rb rename to lib/rails_console_commands/test_command.rb index 3a8b747..67fea11 100644 --- a/lib/rails_console_commands/console_delegation.rb +++ b/lib/rails_console_commands/test_command.rb @@ -1,22 +1,20 @@ # frozen_string_literal: true +require_relative './arg_parser' require_relative './commander' module RailsConsoleCommands - module ConsoleDelegation - def commander - @commander ||= Commander.new - end + class TestCommand < IRB::Command::Base + include ArgParser - def test(*args) + def execute(arg) if Rails.env.test? - commander.test(*args) + what, line = arg.split(',').map{ |a| parse_arg(a) } + Commander.commander.test(what, line) else puts 'You can only run tests in a console started in the test environment. ' \ 'Use `rails console test` to start such a console' end end - - delegate :rake, :generate, :destroy, :update, to: :commander end end diff --git a/lib/rails_console_commands/update_command.rb b/lib/rails_console_commands/update_command.rb new file mode 100644 index 0000000..baf4cae --- /dev/null +++ b/lib/rails_console_commands/update_command.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require_relative './arg_parser' +require_relative './commander' + +module RailsConsoleCommands + class UpdateCommand < IRB::Command::Base + include ArgParser + + def execute(arg) + Commander.commander.update(parse_arg(arg)) + end + end +end diff --git a/rails_console_commands.gemspec b/rails_console_commands.gemspec index 37222b6..3ca422a 100644 --- a/rails_console_commands.gemspec +++ b/rails_console_commands.gemspec @@ -23,5 +23,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rubocop', '~> 0.49' + spec.add_dependency 'irb', '>= 1.13' + # TODO: Update this to '>= 7.2' once released spec.add_dependency 'railties', '>= 5' end