Skip to content

Commit

Permalink
Add "-m/--template" option to Rails generator to apply template to ge…
Browse files Browse the repository at this point in the history
…nerated application.

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
jm authored and lifo committed Dec 2, 2008
1 parent 2014d91 commit e8cc4b1
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 0 deletions.
43 changes: 43 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,48 @@
*2.3.0 [Edge]* *2.3.0 [Edge]*


* Add "-m/--template" option to Rails generator to apply a template to the generated application. [Jeremy McAnally]

This has been extracted from rg - http://github.com/jeremymcanally/rg

Example:

# template.rb

# Install plugins from git or svn
plugin "will-paginate", :git => "git://github.com/mislav/will_paginate.git"
plugin "old-restful-auth", :svn => "http://svn.techno-weenie.net/projects/plugins/restful_authentication/"

# Add gems to environment.rb
gem "jeremymcanally-context"
gem "bluecloth"

# Vendor file. Data in a string or...
vendor("borrowed.rb", <<CODE
def helpful_method
do_something_helpful_here
end
CODE

# ...file data from block return value.
# #initializer creates a new initializer file
initializer("crypto.rb") do
salt = "--#{Time.now}--#{rand}--#{srand(Time.now.to_i)}"

"SPECIAL_SALT = '#{salt}'"
end

Usage:

To use a template, provide a file path or URL:

1. Using a local file :

rails <application name> -m /path/to/my/template.rb

2. Or directly from a URL :

rails <application name> --template=http://gist.github.com/31208.txt

* Extracted the process scripts (inspector, reaper, spawner) into the plugin irs_process_scripts [DHH] * Extracted the process scripts (inspector, reaper, spawner) into the plugin irs_process_scripts [DHH]


* Changed Rails.root to return a Pathname object (allows for Rails.root.join('app', 'controllers') => "#{RAILS_ROOT}/app/controllers") #1482 [Damian Janowski/?] * Changed Rails.root to return a Pathname object (allows for Rails.root.join('app', 'controllers') => "#{RAILS_ROOT}/app/controllers") #1482 [Damian Janowski/?]
Expand Down
1 change: 1 addition & 0 deletions railties/bin/rails
Expand Up @@ -8,6 +8,7 @@ if %w(--version -v).include? ARGV.first
end end


freeze = ARGV.any? { |option| %w(--freeze -f).include?(option) } freeze = ARGV.any? { |option| %w(--freeze -f).include?(option) }

This comment has been minimized.

Copy link
@greatseth

greatseth Dec 2, 2008

BLOAT WTF BBQ

This comment has been minimized.

Copy link
@mislav

mislav Dec 2, 2008

Member

OMG you’re increasing Rails’ LOC, nooooo

This comment has been minimized.

Copy link
@amerine

amerine Dec 2, 2008

Contributor

ReallY? OMG ITS HUGER!

app_path = ARGV.first app_path = ARGV.first


require File.dirname(__FILE__) + '/../lib/rails_generator' require File.dirname(__FILE__) + '/../lib/rails_generator'
Expand Down
3 changes: 3 additions & 0 deletions railties/lib/rails_generator/base.rb
Expand Up @@ -154,6 +154,9 @@ def destination_path(relative_destination)
File.join(destination_root, relative_destination) File.join(destination_root, relative_destination)
end end


def after_generate
end

protected protected
# Convenience method for generator subclasses to record a manifest. # Convenience method for generator subclasses to record a manifest.
def record def record
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails_generator/commands.rb
Expand Up @@ -40,6 +40,7 @@ class Base < DelegateClass(Rails::Generator::Base)
# Replay action manifest. RewindBase subclass rewinds manifest. # Replay action manifest. RewindBase subclass rewinds manifest.
def invoke! def invoke!
manifest.replay(self) manifest.replay(self)
after_generate
end end


def dependency(generator_name, args, runtime_options = {}) def dependency(generator_name, args, runtime_options = {})
Expand Down
@@ -1,4 +1,5 @@
require 'rbconfig' require 'rbconfig'
require File.dirname(__FILE__) + '/template_runner'
require 'digest/md5' require 'digest/md5'
require 'active_support/secure_random' require 'active_support/secure_random'


Expand Down Expand Up @@ -37,6 +38,12 @@ def manifest
end end
end end


def after_generate
if options[:template]
Rails::TemplateRunner.new(@destination_root, options[:template])
end
end

protected protected
def banner def banner
"Usage: #{$0} /path/to/your/app [options]" "Usage: #{$0} /path/to/your/app [options]"
Expand All @@ -60,6 +67,11 @@ def add_options!(opt)
opt.on("-f", "--freeze", opt.on("-f", "--freeze",
"Freeze Rails in vendor/rails from the gems generating the skeleton", "Freeze Rails in vendor/rails from the gems generating the skeleton",
"Default: false") { |v| options[:freeze] = v } "Default: false") { |v| options[:freeze] = v }

opt.on("-m", "--template=path", String,
"Use an application template that lives at path (can be a filesystem path or URL).",
"Default: (none)") { |v| options[:template] = v }

end end




Expand Down
@@ -0,0 +1,16 @@
module Rails
class Git < Scm
def self.clone(repos, branch=nil)
`git clone #{repos}`

if branch
`cd #{repos.split('/').last}/`
`git checkout #{branch}`
end
end

def self.run(command)
`git #{command}`
end
end
end
@@ -0,0 +1,8 @@
module Rails
class Scm
private
def self.hash_to_parameters(hash)
hash.collect { |key, value| "--#{key} #{(value.kind_of?(String) ? value : "")}"}.join(" ")
end
end
end
@@ -0,0 +1,7 @@
module Rails
class Svn < Scm
def self.checkout(repos, branch = nil)
`svn checkout #{repos}/#{branch || "trunk"}`
end
end
end

8 comments on commit e8cc4b1

@matthewrudy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow.
I didn’t realise you could comment on individual lines.

Massiveness.

@chrisk
Copy link
Contributor

@chrisk chrisk commented on e8cc4b1 Dec 2, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is super cool, nice work j-mac

@Aupajo
Copy link
Contributor

@Aupajo Aupajo commented on e8cc4b1 Dec 3, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vendor("borrowed.rb", <<CODE
  ..snip..
+ CODE

Missing right-paren?

@jm
Copy link
Contributor Author

@jm jm commented on e8cc4b1 Dec 3, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oopsie.

@bumi
Copy link

@bumi bumi commented on e8cc4b1 Dec 3, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah! great to see RG go into core!
It feels like the old days when I’ve run the rails command for the first time ;)

@samgranieri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty cool. I wonder why RG was chosen over Suprails?

@NZKoz
Copy link
Member

@NZKoz NZKoz commented on e8cc4b1 Dec 3, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samgranieri: Jeremy reached out and suggested getting it included. We’ll be happy to take any patches to implement cool functionality in suprails but not in rails.

@listrophy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samgranieri Thanks for the verbal support for Suprails, but Jeremy did the more intelligent thing and got it included into rails. I didn't try to do that with Suprails because I thought it would never get pulled into core. oops. =)

Please sign in to comment.