0
@@ -8,23 +8,24 @@ require 'fileutils'
0
def initialize(template, root = '') # :nodoc:
0
- @root = File.
join(Dir.pwd, root)
0
+ @root = File.
expand_path(File.directory?(root) ? root : File.join(Dir.pwd, root))
0
-
puts "applying template: #{template}"
0
+
log 'applying', "template: #{template}"
0
load_template(template)
0
-
puts "#{template} applied."
0
+
log 'applied', "#{template}"
0
def load_template(template)
0
code = open(template).read
0
in_root { self.instance_eval(code) }
0
- raise "The template [#{template}] could not be loaded."
0
+ rescue LoadError, Errno::ENOENT => e
0
+ raise "The template [#{template}] could not be loaded. Error: #{e}"
0
@@ -41,8 +42,8 @@ module Rails
0
# file("config/apach.conf", "your apache config")
0
- def file(filename, data = nil, &block)
0
- puts "creating file #{filename}"
0
+ def file(filename, data = nil, log_action = true, &block)
0
+ log 'file', filename if log_action
0
dir, file = [File.dirname(filename), File.basename(filename)]
0
@@ -66,7 +67,7 @@ module Rails
0
# plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
0
def plugin(name, options)
0
-
puts "installing plugin #{name}"0
if options[:git] && options[:submodule]
0
@@ -74,18 +75,17 @@ module Rails
0
elsif options[:git] || options[:svn]
0
-
`script/plugin install #{options[:svn] || options[:git]}`0
+
run("script/plugin install #{options[:svn] || options[:git]}", false)0
-
puts "! no git or svn provided for #{name}. skipping..."
0
+
log "! no git or svn provided for #{name}. skipping..."
0
# Adds an entry into config/environment.rb for the supplied gem :
0
def gem(name, options = {})
0
-
puts "adding gem #{name}"0
- sentinel = 'Rails::Initializer.run do |config|'
0
gems_code = "config.gem '#{name}'"
0
@@ -93,9 +93,18 @@ module Rails
0
gems_code << ", #{opts}"
0
+ # Adds a line inside the Initializer block for config/environment.rb. Used by #gem
0
+ def environment(data = nil, &block)
0
+ sentinel = 'Rails::Initializer.run do |config|'
0
+ data = block.call if !data && block_given?
0
gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
0
- "#{match}\n
#{gems_code}"0
@@ -111,11 +120,11 @@ module Rails
0
if command.is_a?(Symbol)
0
-
puts "running git #{command}"
0
+
log 'running', "git #{command}"
0
command.each do |command, options|
0
-
puts "running git #{command} #{options}"
0
+
log 'running', "git #{command} #{options}"
0
Git.run("#{command} #{options}")
0
@@ -135,16 +144,8 @@ module Rails
0
# vendor("foreign.rb", "# Foreign code is fun")
0
def vendor(filename, data = nil, &block)
0
- puts "vendoring file #{filename}"
0
- inside("vendor") do |folder|
0
- File.open("#{folder}/#{filename}", "w") do |f|
0
+ log 'vendoring', filename
0
+ file("vendor/#{filename}", data, false, &block)
0
# Create a new file in the lib/ directory. Code can be specified
0
@@ -158,17 +159,9 @@ module Rails
0
# lib("foreign.rb", "# Foreign code is fun")
0
- def lib(filename, data = nil)
0
- puts "add lib file #{filename}"
0
- inside("lib") do |folder|
0
- File.open("#{folder}/#{filename}", "w") do |f|
0
+ def lib(filename, data = nil, &block)
0
+ file("lib/#{filename}", data, false, &block)
0
# Create a new Rakefile with the provided code (either in a block or a string).
0
@@ -190,16 +183,8 @@ module Rails
0
# rakefile("seed.rake", "puts 'im plantin ur seedz'")
0
def rakefile(filename, data = nil, &block)
0
- puts "adding rakefile #{filename}"
0
- inside("lib/tasks") do |folder|
0
- File.open("#{folder}/#{filename}", "w") do |f|
0
+ log 'rakefile', filename
0
+ file("lib/tasks/#{filename}", data, false, &block)
0
# Create a new initializer with the provided code (either in a block or a string).
0
@@ -219,16 +204,8 @@ module Rails
0
# initializer("api.rb", "API_KEY = '123456'")
0
def initializer(filename, data = nil, &block)
0
- puts "adding initializer #{filename}"
0
- inside("config/initializers") do |folder|
0
- File.open("#{folder}/#{filename}", "w") do |f|
0
+ log 'initializer', filename
0
+ file("config/initializers/#{filename}", data, false, &block)
0
# Generate something using a generator from Rails or a plugin.
0
@@ -240,10 +217,10 @@ module Rails
0
# generate(:authenticated, "user session")
0
def generate(what, *args)
0
-
puts "generating #{what}"0
+
log 'generating', what0
argument = args.map(&:to_s).flatten.join(" ")
0
- in_root {
`#{root}/script/generate #{what} #{argument}` }
0
+ in_root {
run("script/generate #{what} #{argument}", false) }
0
@@ -254,8 +231,8 @@ module Rails
0
# run('ln -s ~/edge rails)
0
- puts "executing #{command} from #{Dir.pwd}"
0
+ def run(command, log_action = true)
0
+ log 'executing', "#{command} from #{Dir.pwd}" if log_action
0
@@ -268,10 +245,10 @@ module Rails
0
# rake("gems:install", :sudo => true)
0
def rake(command, options = {})
0
-
puts "running rake task #{command}"0
env = options[:env] || 'development'
0
sudo = options[:sudo] ? 'sudo ' : ''
0
- in_root {
`#{sudo}rake #{command} RAILS_ENV=#{env}` }
0
+ in_root {
run("#{sudo}rake #{command} RAILS_ENV=#{env}", false) }
0
# Just run the capify command in root
0
@@ -281,7 +258,8 @@ module Rails
0
- in_root { `capify .` }
0
+ in_root { run('capify .', false) }
0
# Add Rails to /vendor/rails
0
@@ -291,8 +269,8 @@ module Rails
0
- puts "vendoring rails edge"
0
- in_root { `rake rails:freeze:edge` }
0
+ log 'vendor', 'rails edge'
0
+ in_root { run('rake rails:freeze:edge', false) }
0
# Make an entry in Rails routing file conifg/routes.rb
0
@@ -302,6 +280,7 @@ module Rails
0
# route "map.root :controller => :welcome"
0
def route(routing_code)
0
+ log 'route', routing_code
0
sentinel = 'ActionController::Routing::Routes.draw do |map|'
0
@@ -321,7 +300,7 @@ module Rails
0
# freeze! if ask("Should I freeze the latest Rails?") == "yes"
0
@@ -368,5 +347,13 @@ module Rails
0
def destination_path(relative_destination)
0
File.join(root, relative_destination)
0
+ def log(action, message = '')
0
+ logger.log(action, message)
0
+ @logger ||= Rails::Generator::Base.logger
0
\ No newline at end of file
When running rake rails:template this breaks with uninitialized constant Rails::Generator
Its because of the logger – working on a fix right now.
Thanks for the catch @andrewtimberlake – I already put in a patch, should be picked up soon
Thanks, that helps