diff options
Diffstat (limited to 'railties')
-rwxr-xr-x | railties/bin/gen | 27 | ||||
-rwxr-xr-x | railties/bin/rails | 2 | ||||
-rw-r--r-- | railties/lib/generator/base.rb | 34 | ||||
-rw-r--r-- | railties/lib/generator/generators/app/app_generator.rb | 8 | ||||
-rw-r--r-- | railties/lib/generator/generators/metal/USAGE | 8 | ||||
-rw-r--r-- | railties/lib/generator/generators/metal/metal_generator.rb | 17 | ||||
-rw-r--r-- | railties/lib/generator/generators/metal/templates/metal.rb | 12 | ||||
-rw-r--r-- | railties/test/generator/app_test.rb | 4 |
8 files changed, 100 insertions, 12 deletions
diff --git a/railties/bin/gen b/railties/bin/gen new file mode 100755 index 0000000000..0567ab6058 --- /dev/null +++ b/railties/bin/gen @@ -0,0 +1,27 @@ +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 diff --git a/railties/bin/rails b/railties/bin/rails index e13118500a..4d08ca392d 100755 --- a/railties/bin/rails +++ b/railties/bin/rails @@ -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 diff --git a/railties/lib/generator/base.rb b/railties/lib/generator/base.rb index 8751cb08fb..08f488932b 100644 --- a/railties/lib/generator/base.rb +++ b/railties/lib/generator/base.rb @@ -23,9 +23,16 @@ module Rails # 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 @@ module Rails # 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' diff --git a/railties/lib/generator/generators/app/app_generator.rb b/railties/lib/generator/generators/app/app_generator.rb index 6f5cb5f2c6..3eb9b8c79a 100644 --- a/railties/lib/generator/generators/app/app_generator.rb +++ b/railties/lib/generator/generators/app/app_generator.rb @@ -3,10 +3,8 @@ require 'digest/md5' 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 @@ module Rails::Generators 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 diff --git a/railties/lib/generator/generators/metal/USAGE b/railties/lib/generator/generators/metal/USAGE new file mode 100644 index 0000000000..123ec6c03f --- /dev/null +++ b/railties/lib/generator/generators/metal/USAGE @@ -0,0 +1,8 @@ +Description: + Cast some metal! + +Examples: + `./script/generate metal poller` + + This will create: + Metal: app/metal/poller.rb diff --git a/railties/lib/generator/generators/metal/metal_generator.rb b/railties/lib/generator/generators/metal/metal_generator.rb new file mode 100644 index 0000000000..1c59a4e90a --- /dev/null +++ b/railties/lib/generator/generators/metal/metal_generator.rb @@ -0,0 +1,17 @@ +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 diff --git a/railties/lib/generator/generators/metal/templates/metal.rb b/railties/lib/generator/generators/metal/templates/metal.rb new file mode 100644 index 0000000000..e94982b69a --- /dev/null +++ b/railties/lib/generator/generators/metal/templates/metal.rb @@ -0,0 +1,12 @@ +# 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 diff --git a/railties/test/generator/app_test.rb b/railties/test/generator/app_test.rb index 5329e27f9b..af1be03e59 100644 --- a/railties/test/generator/app_test.rb +++ b/railties/test/generator/app_test.rb @@ -122,11 +122,11 @@ class AppTest < GeneratorTestCase 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 |