提交 18456758 编写于 作者: J José Valim

Added metal generator.

上级 f03890e1
require File.dirname(__FILE__) + '/../lib/ruby_version_check'
Signal.trap("INT") { puts; exit }
require File.dirname(__FILE__) + '/../lib/rails/version'
if %w(--version -v).include? ARGV.first
puts "Rails #{Rails::VERSION::STRING}"
exit(0)
end
if ARGV.size == 0
puts "Please select a generator. Options: foo, bar"
return
else ARGV.size == 1
ARGV << "--help"
end
Dir[File.dirname(__FILE__) + '/../lib/generator/generators/*/*_generator.rb'].each do |file|
require file
end
name = ARGV.shift
if klass = Thor::Util.find_by_namespace("rails:generators:#{name}")
klass.start
else
puts "Could not find generator #{name}."
end
......@@ -10,4 +10,4 @@ end
ARGV << "--help" if ARGV.empty?
require File.dirname(__FILE__) + '/../lib/generator/generators/app/app_generator'
Rails::Generators::App.start
Rails::Generators::AppGenerator.start
......@@ -23,9 +23,16 @@ class Base < Thor::Group
# Automatically sets the source root based on the class name.
#
def self.source_root
@source_root ||= begin
klass_name = self.name.gsub(/^Rails::Generators::/, '')
File.expand_path(File.join(File.dirname(__FILE__), 'generators', klass_name.underscore, 'templates'))
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'generators', generator_name, 'templates'))
end
# Convenience method to get the namespace from the class name.
#
def self.namespace(name=nil)
if name
super
else
@namespace ||= "rails:generators:#{generator_name}"
end
end
......@@ -34,11 +41,26 @@ def self.source_root
# Use Rails default banner.
#
def self.banner
"#{$0} #{self.arguments.map(&:usage).join(' ')} [options]"
"#{$0} #{generator_name} #{self.arguments.map(&:usage).join(' ')} [options]"
end
# Removes the namespaces and get the generator name. For example,
# Rails::Generators::MetalGenerator will return "metal" as generator name.
#
# The name is used to set the namespace (in this case "rails:generators:metal")
# and to set the source root ("generators/metal/templates").
#
def self.generator_name
@generator_name ||= begin
klass_name = self.name
klass_name.gsub!(/^Rails::Generators::/, '')
klass_name.gsub!(/Generator$/, '')
klass_name.underscore
end
end
# Small macro to ruby as an option to the generator with proper default
# value plus an instance helper method.
# Small macro to add ruby as an option to the generator with proper
# default value plus an instance helper method called shebang.
#
def self.add_shebang_option!
require 'rbconfig'
......
......@@ -3,10 +3,8 @@
require 'active_support/secure_random'
module Rails::Generators
class App < Base
class AppGenerator < Base
DATABASES = %w( mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db )
namespace "rails:app"
add_shebang_option!
argument :app_path, :type => :string
......@@ -182,6 +180,10 @@ def app_secret
ActiveSupport::SecureRandom.hex(64)
end
def self.banner
"#{$0} #{self.arguments.map(&:usage).join(' ')} [options]"
end
def mysql_socket
@mysql_socket ||= [
"/tmp/mysql.sock", # default
......
Description:
Cast some metal!
Examples:
`./script/generate metal poller`
This will create:
Metal: app/metal/poller.rb
require File.dirname(__FILE__) + '/../../base'
module Rails::Generators
class MetalGenerator < Base
argument :file_name, :type => :string
def create_file
template "metal.rb", "app/metal/#{file_name}.rb"
end
protected
def class_name
file_name.classify
end
end
end
# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
class <%= class_name %>
def self.call(env)
if env["PATH_INFO"] =~ /^\/<%= file_name %>/
[200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
end
......@@ -122,11 +122,11 @@ def test_template_is_executed_when_supplied
protected
def run_generator(args=[])
silence(:stdout) { Rails::Generators::App.start [destination_root].concat(args) }
silence(:stdout) { Rails::Generators::AppGenerator.start [destination_root].concat(args) }
end
def generator(options={})
@generator ||= Rails::Generators::App.new([destination_root], options, :root => destination_root)
@generator ||= Rails::Generators::AppGenerator.new([destination_root], options, :root => destination_root)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册