Permalink
Browse files

Add "-m/--template" option to Rails generator to apply template to ge…

…nerated application.

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent 2014d91 commit e8cc4b116c460c524961a07da92da3f323854c15 @jm jm committed with lifo Dec 2, 2008
View
@@ -1,5 +1,48 @@
*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]
* Changed Rails.root to return a Pathname object (allows for Rails.root.join('app', 'controllers') => "#{RAILS_ROOT}/app/controllers") #1482 [Damian Janowski/?]
View
@@ -8,6 +8,7 @@ if %w(--version -v).include? ARGV.first
end
freeze = ARGV.any? { |option| %w(--freeze -f).include?(option) }
+
@mislav
mislav Dec 2, 2008 Ruby on Rails member

OMG you’re increasing Rails’ LOC, nooooo

@amerine
amerine Dec 2, 2008

ReallY? OMG ITS HUGER!

app_path = ARGV.first
require File.dirname(__FILE__) + '/../lib/rails_generator'
@@ -154,6 +154,9 @@ def destination_path(relative_destination)
File.join(destination_root, relative_destination)
end
+ def after_generate
+ end
+
protected
# Convenience method for generator subclasses to record a manifest.
def record
@@ -40,6 +40,7 @@ class Base < DelegateClass(Rails::Generator::Base)
# Replay action manifest. RewindBase subclass rewinds manifest.
def invoke!
manifest.replay(self)
+ after_generate
end
def dependency(generator_name, args, runtime_options = {})
@@ -1,4 +1,5 @@
require 'rbconfig'
+require File.dirname(__FILE__) + '/template_runner'
require 'digest/md5'
require 'active_support/secure_random'
@@ -37,6 +38,12 @@ def manifest
end
end
+ def after_generate
+ if options[:template]
+ Rails::TemplateRunner.new(@destination_root, options[:template])
+ end
+ end
+
protected
def banner
"Usage: #{$0} /path/to/your/app [options]"
@@ -60,6 +67,11 @@ def add_options!(opt)
opt.on("-f", "--freeze",
"Freeze Rails in vendor/rails from the gems generating the skeleton",
"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
@@ -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
Oops, something went wrong. Retry.

8 comments on commit e8cc4b1

@matthewrudy

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

Massiveness.

@chrisk
chrisk commented on e8cc4b1 Dec 2, 2008

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

@Aupajo
Aupajo commented on e8cc4b1 Dec 3, 2008
vendor("borrowed.rb", <<CODE
  ..snip..
+ CODE

Missing right-paren?

@jm
jm commented on e8cc4b1 Dec 3, 2008

Oopsie.

@bumi
bumi commented on e8cc4b1 Dec 3, 2008

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

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

@NZKoz
Member
NZKoz commented on e8cc4b1 Dec 3, 2008

@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

@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.