提交 b37938ef 编写于 作者: P Piotr Sarnacki

Refactored AppGenerator and PluginNewGenerator to inherit from AppBase.

上级 cfcea1d5
require 'digest/md5'
require 'active_support/secure_random'
require 'rails/version' unless defined?(Rails::VERSION)
require 'rbconfig'
require 'open-uri'
require 'uri'
module Rails
module Generators
class AppBase < Base
def self.say_step(message)
@step = (@step || 0) + 1
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def step_#{@step}
#{"puts" if @step > 1}
say_status "STEP #{@step}", #{message.inspect}
end
METHOD
end
def initialize(*args)
@original_wd = Dir.pwd
super
end
protected
def builder
@builder ||= begin
if path = options[:builder]
if URI(path).is_a?(URI::HTTP)
contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
else
contents = open(File.expand_path(path, @original_wd)) {|io| io.read }
end
prok = eval("proc { #{contents} }", TOPLEVEL_BINDING, path, 1)
instance_eval(&prok)
end
builder_class = get_builder_class
builder_class.send(:include, ActionMethods)
builder_class.new(self)
end
end
def build(meth, *args)
builder.send(meth, *args) if builder.respond_to?(meth)
end
def create_root
self.destination_root = File.expand_path(app_path, destination_root)
valid_const?
empty_directory '.'
FileUtils.cd(destination_root) unless options[:pretend]
end
end
end
end
require 'digest/md5'
require 'active_support/secure_random'
require 'rails/version' unless defined?(Rails::VERSION)
require 'rbconfig'
require 'open-uri'
require 'uri'
require 'rails/generators/app_base'
module Rails
module ActionMethods
......@@ -158,7 +153,7 @@ module Generators
RESERVED_NAMES = %w[application destroy benchmarker profiler
plugin runner test]
class AppGenerator < Base
class AppGenerator < AppBase
DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
JAVASCRIPTS = %w( prototype jquery )
......@@ -210,8 +205,6 @@ class AppGenerator < Base
def initialize(*args)
raise Error, "Options should be given after the application name. For details run: rails --help" if args[0].blank?
@original_wd = Dir.pwd
super
if !options[:skip_active_record] && !DATABASES.include?(options[:database])
......@@ -224,12 +217,8 @@ def initialize(*args)
end
def create_root
self.destination_root = File.expand_path(app_path, destination_root)
valid_app_const?
empty_directory '.'
set_default_accessors!
FileUtils.cd(destination_root) unless options[:pretend]
super
end
def create_root_files
......@@ -339,7 +328,7 @@ def builder
instance_eval(&prok)
end
builder_class = defined?(::AppBuilder) ? ::AppBuilder : Rails::AppBuilder
builder_class = get_builder_class
builder_class.send(:include, ActionMethods)
builder_class.new(self)
end
......@@ -388,7 +377,7 @@ def app_const
@app_const ||= "#{app_const_base}::Application"
end
def valid_app_const?
def valid_const?
if app_const =~ /^\d/
raise Error, "Invalid application name #{app_name}. Please give a name which does not start with numbers."
elsif RESERVED_NAMES.include?(app_name)
......@@ -442,6 +431,10 @@ def empty_directory_with_gitkeep(destination, config = {})
empty_directory(destination, config)
create_file("#{destination}/.gitkeep") unless options[:skip_git]
end
def get_builder_class
defined?(::AppBuilder) ? ::AppBuilder : Rails::AppBuilder
end
end
end
end
require 'rails/generators/app_base'
require "rails/generators/rails/app/app_generator"
module Rails
......@@ -70,12 +71,13 @@ def script
end
module Generators
class PluginNewGenerator < Base
class PluginNewGenerator < AppBase
attr_accessor :rails_template
add_shebang_option!
argument :plugin_path, :type => :string
alias_method :app_path, :plugin_path
class_option :builder, :type => :string, :aliases => "-b",
:desc => "Path to a plugin builder (can be a filesystem path or URL)"
......@@ -89,32 +91,17 @@ class PluginNewGenerator < Base
class_option :help, :type => :boolean, :aliases => "-h", :group => :rails,
:desc => "Show this help message and quit"
def self.say_step(message)
@step = (@step || 0) + 1
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def step_#{@step}
#{"puts" if @step > 1}
say_status "STEP #{@step}", #{message.inspect}
end
METHOD
end
def initialize(*args)
raise Error, "Options should be given after plugin name. For details run: rails plugin --help" if args[0].blank?
@original_wd = Dir.pwd
super
end
say_step "Creating gem skeleton"
def create_root
self.destination_root = File.expand_path(plugin_path, destination_root)
valid_plugin_const?
empty_directory '.'
FileUtils.cd(destination_root) unless options[:pretend]
super
end
def create_root_files
......@@ -166,29 +153,6 @@ def self.banner
"rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]"
end
def builder
@builder ||= begin
if path = options[:builder]
if URI(path).is_a?(URI::HTTP)
contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
else
contents = open(File.expand_path(path, @original_wd)) {|io| io.read }
end
prok = eval("proc { #{contents} }", TOPLEVEL_BINDING, path, 1)
instance_eval(&prok)
end
builder_class = defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
builder_class.send(:include, ActionMethods)
builder_class.new(self)
end
end
def build(meth, *args)
builder.send(meth, *args) if builder.respond_to?(meth)
end
def name
@name ||= File.basename(destination_root)
end
......@@ -197,7 +161,7 @@ def camelized
@camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize
end
def valid_plugin_const?
def valid_const?
if camelized =~ /^\d/
raise Error, "Invalid plugin name #{name}. Please give a name which does not start with numbers."
elsif RESERVED_NAMES.include?(name)
......@@ -220,6 +184,10 @@ def application_definition
end
end
alias :store_application_definition! :application_definition
def get_builder_class
defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册