Skip to content

Commit

Permalink
Add support for templates for rails plugin new
Browse files Browse the repository at this point in the history
  • Loading branch information
drogus committed Nov 2, 2010
1 parent b8a0fab commit bcd414f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
18 changes: 18 additions & 0 deletions railties/lib/rails/generators/app_base.rb
Expand Up @@ -54,8 +54,26 @@ def create_root
valid_const?

empty_directory '.'
set_default_accessors!
FileUtils.cd(destination_root) unless options[:pretend]
end

def apply_rails_template
apply rails_template if rails_template
rescue Thor::Error, LoadError, Errno::ENOENT => e
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
end

def set_default_accessors!
self.rails_template = case options[:template]
when /^http:\/\//
options[:template]
when String
File.expand_path(options[:template], Dir.pwd)
else
options[:template]
end
end
end
end
end
16 changes: 1 addition & 15 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -217,7 +217,6 @@ def initialize(*args)
end

def create_root
set_default_accessors!
super
end

Expand Down Expand Up @@ -299,9 +298,7 @@ def finish_template
end

def apply_rails_template
apply rails_template if rails_template
rescue Thor::Error, LoadError, Errno::ENOENT => e
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
super
end

def bundle_if_dev_or_edge
Expand Down Expand Up @@ -338,17 +335,6 @@ def build(meth, *args)
builder.send(meth, *args) if builder.respond_to?(meth)
end

def set_default_accessors!
self.rails_template = case options[:template]
when /^http:\/\//
options[:template]
when String
File.expand_path(options[:template], Dir.pwd)
else
options[:template]
end
end

# Define file as an alias to create_file for backwards compatibility.
def file(*args, &block)
create_file(*args, &block)
Expand Down
Expand Up @@ -82,6 +82,9 @@ class PluginNewGenerator < AppBase
class_option :builder, :type => :string, :aliases => "-b",
:desc => "Path to a plugin builder (can be a filesystem path or URL)"

class_option :template, :type => :string, :aliases => "-m",
:desc => "Path to an application template (can be a filesystem path or URL)"

class_option :skip_gemfile, :type => :boolean, :default => false,
:desc => "Don't create a Gemfile"

Expand Down Expand Up @@ -147,6 +150,14 @@ def remove_uneeded_rails_files
build(:test_dummy_clean)
end

def finish_template
build(:leftovers)
end

def apply_rails_template
super
end

protected

def self.banner
Expand Down
20 changes: 20 additions & 0 deletions railties/test/generators/plugin_new_generator_test.rb
Expand Up @@ -110,6 +110,26 @@ def test_ensure_that_plugin_options_are_not_passed_app_generator
assert_match /STEP 2.*create Gemfile/m, output
end

def test_template_from_dir_pwd
FileUtils.cd(Rails.root)
assert_match /It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])
end

def test_template_raises_an_error_with_invalid_path
content = capture(:stderr){ run_generator([destination_root, "-m", "non/existant/path"]) }
assert_match /The template \[.*\] could not be loaded/, content
assert_match /non\/existant\/path/, content
end

def test_template_is_executed_when_supplied
path = "http://gist.github.com/103208.txt"
template = %{ say "It works!" }
template.instance_eval "def read; self; end" # Make the string respond to read

generator([destination_root], :template => path).expects(:open).with(path, 'Accept' => 'application/x-thor-template').returns(template)
assert_match /It works!/, silence(:stdout){ generator.invoke_all }
end

protected

def action(*args, &block)
Expand Down

0 comments on commit bcd414f

Please sign in to comment.