From 6813edc7d926965e5644cd8befaf229a35b9d8ca Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sat, 28 May 2016 22:04:13 +0200 Subject: Initial command structure. --- .../lib/rails/commands/runner/runner_command.rb | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 railties/lib/rails/commands/runner/runner_command.rb (limited to 'railties/lib/rails/commands/runner/runner_command.rb') diff --git a/railties/lib/rails/commands/runner/runner_command.rb b/railties/lib/rails/commands/runner/runner_command.rb new file mode 100644 index 0000000000..8db6da8759 --- /dev/null +++ b/railties/lib/rails/commands/runner/runner_command.rb @@ -0,0 +1,45 @@ +module Rails + module Command + class RunnerCommand < Base + class_option :environment, aliases: "-e", type: :string, + default: Rails::Command.environment.dup, + desc: "The environment for the runner to operate under (test/development/production)" + + def help + super + puts self.class.desc + end + + def self.banner(*) + "#{super} [<'Some.ruby(code)'> | ]" + end + + def perform(code_or_file = nil) + unless code_or_file + help + exit 1 + end + + ENV["RAILS_ENV"] = options[:environment] + + require_application_and_environment! + Rails.application.load_runner + + if File.exist?(code_or_file) + $0 = code_or_file + Kernel.load code_or_file + else + begin + eval(code_or_file, binding, __FILE__, __LINE__) + rescue SyntaxError, NameError => error + $stderr.puts "Please specify a valid ruby command or the path of a script to run." + $stderr.puts "Run '#{self.class.executable} -h' for help." + $stderr.puts + $stderr.puts error + exit 1 + end + end + end + end + end +end -- cgit v1.2.3