Refactor railties console and dbconsole commands
fix minor convention problems
This commit is contained in:
parent
986fdc4e73
commit
117bb54125
@ -1,14 +1,13 @@
|
|||||||
require 'optparse'
|
require 'optparse'
|
||||||
require 'irb'
|
require 'irb'
|
||||||
require 'irb/completion'
|
require 'irb/completion'
|
||||||
|
require 'rails/commands/console_helper'
|
||||||
|
|
||||||
module Rails
|
module Rails
|
||||||
class Console
|
class Console
|
||||||
class << self
|
include ConsoleHelper
|
||||||
def start(*args)
|
|
||||||
new(*args).start
|
|
||||||
end
|
|
||||||
|
|
||||||
|
class << self
|
||||||
def parse_arguments(arguments)
|
def parse_arguments(arguments)
|
||||||
options = {}
|
options = {}
|
||||||
|
|
||||||
@ -21,23 +20,8 @@ def parse_arguments(arguments)
|
|||||||
opt.parse!(arguments)
|
opt.parse!(arguments)
|
||||||
end
|
end
|
||||||
|
|
||||||
if arguments.first && arguments.first[0] != '-'
|
set_options_env(arguments, options)
|
||||||
env = arguments.first
|
|
||||||
if available_environments.include? env
|
|
||||||
options[:environment] = env
|
|
||||||
else
|
|
||||||
options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
options
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def available_environments
|
|
||||||
Dir['config/environments/*.rb'].map { |fname| File.basename(fname, '.*') }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :options, :app, :console
|
attr_reader :options, :app, :console
|
||||||
@ -57,12 +41,9 @@ def sandbox?
|
|||||||
end
|
end
|
||||||
|
|
||||||
def environment
|
def environment
|
||||||
options[:environment] ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
options[:environment] ||= super
|
||||||
end
|
|
||||||
|
|
||||||
def environment?
|
|
||||||
environment
|
|
||||||
end
|
end
|
||||||
|
alias_method :environment?, :environment
|
||||||
|
|
||||||
def set_environment!
|
def set_environment!
|
||||||
Rails.env = environment
|
Rails.env = environment
|
||||||
|
34
railties/lib/rails/commands/console_helper.rb
Normal file
34
railties/lib/rails/commands/console_helper.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
require 'active_support/concern'
|
||||||
|
|
||||||
|
module Rails
|
||||||
|
module ConsoleHelper # :nodoc:
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def start(*args)
|
||||||
|
new(*args).start
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def set_options_env(arguments, options)
|
||||||
|
if arguments.first && arguments.first[0] != '-'
|
||||||
|
env = arguments.first
|
||||||
|
if available_environments.include? env
|
||||||
|
options[:environment] = env
|
||||||
|
else
|
||||||
|
options[:environment] = %w(production development test).detect { |e| e =~ /^#{env}/ } || env
|
||||||
|
end
|
||||||
|
end
|
||||||
|
options
|
||||||
|
end
|
||||||
|
|
||||||
|
def available_environments
|
||||||
|
Dir['config/environments/*.rb'].map { |fname| File.basename(fname, '.*') }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def environment
|
||||||
|
ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,13 +1,49 @@
|
|||||||
require 'erb'
|
require 'erb'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
require 'rails/commands/console_helper'
|
||||||
|
|
||||||
module Rails
|
module Rails
|
||||||
class DBConsole
|
class DBConsole
|
||||||
|
include ConsoleHelper
|
||||||
|
|
||||||
attr_reader :arguments
|
attr_reader :arguments
|
||||||
|
|
||||||
def self.start
|
class << self
|
||||||
new.start
|
def parse_arguments(arguments)
|
||||||
|
options = {}
|
||||||
|
|
||||||
|
OptionParser.new do |opt|
|
||||||
|
opt.banner = "Usage: rails dbconsole [environment] [options]"
|
||||||
|
opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
|
||||||
|
options['include_password'] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
|
||||||
|
"Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
|
||||||
|
options['mode'] = mode
|
||||||
|
end
|
||||||
|
|
||||||
|
opt.on("--header") do |h|
|
||||||
|
options['header'] = h
|
||||||
|
end
|
||||||
|
|
||||||
|
opt.on("-h", "--help", "Show this help message.") do
|
||||||
|
puts opt
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
|
opt.on("-e", "--environment=name", String,
|
||||||
|
"Specifies the environment to run this console under (test/development/production).",
|
||||||
|
"Default: development"
|
||||||
|
) { |v| options[:environment] = v.strip }
|
||||||
|
|
||||||
|
opt.parse!(arguments)
|
||||||
|
abort opt.to_s unless (0..1).include?(arguments.size)
|
||||||
|
end
|
||||||
|
|
||||||
|
set_options_env(arguments, options)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(arguments = ARGV)
|
def initialize(arguments = ARGV)
|
||||||
@ -15,7 +51,7 @@ def initialize(arguments = ARGV)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
options = parse_arguments(arguments)
|
options = self.class.parse_arguments(arguments)
|
||||||
ENV['RAILS_ENV'] = options[:environment] || environment
|
ENV['RAILS_ENV'] = options[:environment] || environment
|
||||||
|
|
||||||
case config["adapter"]
|
case config["adapter"]
|
||||||
@ -101,90 +137,37 @@ def config
|
|||||||
end
|
end
|
||||||
|
|
||||||
def environment
|
def environment
|
||||||
if Rails.respond_to?(:env)
|
Rails.respond_to?(:env) ? Rails.env : super
|
||||||
Rails.env
|
|
||||||
else
|
|
||||||
ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
def configurations
|
||||||
def configurations
|
require APP_PATH
|
||||||
require APP_PATH
|
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
||||||
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
ActiveRecord::Base.configurations
|
||||||
ActiveRecord::Base.configurations
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_arguments(arguments)
|
|
||||||
options = {}
|
|
||||||
|
|
||||||
OptionParser.new do |opt|
|
|
||||||
opt.banner = "Usage: rails dbconsole [environment] [options]"
|
|
||||||
opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
|
|
||||||
options['include_password'] = true
|
|
||||||
end
|
|
||||||
|
|
||||||
opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
|
|
||||||
"Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
|
|
||||||
options['mode'] = mode
|
|
||||||
end
|
|
||||||
|
|
||||||
opt.on("--header") do |h|
|
|
||||||
options['header'] = h
|
|
||||||
end
|
|
||||||
|
|
||||||
opt.on("-h", "--help", "Show this help message.") do
|
|
||||||
puts opt
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
|
|
||||||
opt.on("-e", "--environment=name", String,
|
|
||||||
"Specifies the environment to run this console under (test/development/production).",
|
|
||||||
"Default: development"
|
|
||||||
) { |v| options[:environment] = v.strip }
|
|
||||||
|
|
||||||
opt.parse!(arguments)
|
|
||||||
abort opt.to_s unless (0..1).include?(arguments.size)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if arguments.first && arguments.first[0] != '-'
|
def find_cmd_and_exec(commands, *args)
|
||||||
env = arguments.first
|
commands = Array(commands)
|
||||||
if available_environments.include? env
|
|
||||||
options[:environment] = env
|
dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
|
||||||
|
unless (ext = RbConfig::CONFIG['EXEEXT']).empty?
|
||||||
|
commands = commands.map{|cmd| "#{cmd}#{ext}"}
|
||||||
|
end
|
||||||
|
|
||||||
|
full_path_command = nil
|
||||||
|
found = commands.detect do |cmd|
|
||||||
|
dirs_on_path.detect do |path|
|
||||||
|
full_path_command = File.join(path, cmd)
|
||||||
|
File.file?(full_path_command) && File.executable?(full_path_command)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if found
|
||||||
|
exec full_path_command, *args
|
||||||
else
|
else
|
||||||
options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
|
abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
options
|
|
||||||
end
|
|
||||||
|
|
||||||
def available_environments
|
|
||||||
Dir['config/environments/*.rb'].map { |fname| File.basename(fname, '.*') }
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_cmd_and_exec(commands, *args)
|
|
||||||
commands = Array(commands)
|
|
||||||
|
|
||||||
dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
|
|
||||||
unless (ext = RbConfig::CONFIG['EXEEXT']).empty?
|
|
||||||
commands = commands.map{|cmd| "#{cmd}#{ext}"}
|
|
||||||
end
|
|
||||||
|
|
||||||
full_path_command = nil
|
|
||||||
found = commands.detect do |cmd|
|
|
||||||
dirs_on_path.detect do |path|
|
|
||||||
full_path_command = File.join(path, cmd)
|
|
||||||
File.file?(full_path_command) && File.executable?(full_path_command)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if found
|
|
||||||
exec full_path_command, *args
|
|
||||||
else
|
|
||||||
abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -99,19 +99,15 @@ def test_env
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_rails_env_is_development_when_argument_is_dev
|
def test_rails_env_is_development_when_argument_is_dev
|
||||||
dbconsole = Rails::DBConsole.new
|
Rails::DBConsole.stub(:available_environments, ['development', 'test']) do
|
||||||
|
options = Rails::DBConsole.send(:parse_arguments, ['dev'])
|
||||||
dbconsole.stub(:available_environments, ['development', 'test']) do
|
|
||||||
options = dbconsole.send(:parse_arguments, ['dev'])
|
|
||||||
assert_match('development', options[:environment])
|
assert_match('development', options[:environment])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
|
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
|
||||||
dbconsole = Rails::DBConsole.new
|
Rails::DBConsole.stub(:available_environments, ['dev']) do
|
||||||
|
options = Rails::DBConsole.send(:parse_arguments, ['dev'])
|
||||||
dbconsole.stub(:available_environments, ['dev']) do
|
|
||||||
options = dbconsole.send(:parse_arguments, ['dev'])
|
|
||||||
assert_match('dev', options[:environment])
|
assert_match('dev', options[:environment])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user