Skip to content

Commit

Permalink
Adding test coverage and better logging to Rails::TemplateRunner [#1618
Browse files Browse the repository at this point in the history
… state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
quirkey authored and lifo committed Dec 27, 2008
1 parent fdaa9ed commit 9fd35fc
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 66 deletions.
Expand Up @@ -8,23 +8,24 @@
module Rails
class TemplateRunner
attr_reader :root
attr_writer :logger

def initialize(template, root = '') # :nodoc:
@root = File.join(Dir.pwd, root)
@root = File.expand_path(File.directory?(root) ? root : File.join(Dir.pwd, root))

puts "applying template: #{template}"
log 'applying', "template: #{template}"

load_template(template)

puts "#{template} applied."
log 'applied', "#{template}"
end

def load_template(template)
begin
code = open(template).read
in_root { self.instance_eval(code) }
rescue LoadError
raise "The template [#{template}] could not be loaded."
rescue LoadError, Errno::ENOENT => e
raise "The template [#{template}] could not be loaded. Error: #{e}"
end
end

Expand All @@ -41,8 +42,8 @@ def load_template(template)
#
# file("config/apach.conf", "your apache config")
#
def file(filename, data = nil, &block)
puts "creating file #{filename}"
def file(filename, data = nil, log_action = true, &block)
log 'file', filename if log_action
dir, file = [File.dirname(filename), File.basename(filename)]

inside(dir) do
Expand All @@ -66,36 +67,44 @@ def file(filename, data = nil, &block)
# plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
#
def plugin(name, options)
puts "installing plugin #{name}"
log 'plugin', name

if options[:git] && options[:submodule]
in_root do
Git.run("submodule add #{options[:git]} vendor/plugins/#{name}")
end
elsif options[:git] || options[:svn]
in_root do
`script/plugin install #{options[:svn] || options[:git]}`
run("script/plugin install #{options[:svn] || options[:git]}", false)
end
else
puts "! no git or svn provided for #{name}. skipping..."
log "! no git or svn provided for #{name}. skipping..."
end
end

# Adds an entry into config/environment.rb for the supplied gem :
def gem(name, options = {})
puts "adding gem #{name}"
log 'gem', name

sentinel = 'Rails::Initializer.run do |config|'
gems_code = "config.gem '#{name}'"

if options.any?
opts = options.inject([]) {|result, h| result << [":#{h[0]} => '#{h[1]}'"] }.join(", ")
gems_code << ", #{opts}"
end

environment gems_code
end

# Adds a line inside the Initializer block for config/environment.rb. Used by #gem
def environment(data = nil, &block)
sentinel = 'Rails::Initializer.run do |config|'

data = block.call if !data && block_given?

in_root do
gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
"#{match}\n #{gems_code}"
"#{match}\n " << data
end
end
end
Expand All @@ -111,11 +120,11 @@ def gem(name, options = {})
def git(command = {})
in_root do
if command.is_a?(Symbol)
puts "running git #{command}"
log 'running', "git #{command}"
Git.run(command.to_s)
else
command.each do |command, options|
puts "running git #{command} #{options}"
log 'running', "git #{command} #{options}"
Git.run("#{command} #{options}")
end
end
Expand All @@ -135,16 +144,8 @@ def git(command = {})
# vendor("foreign.rb", "# Foreign code is fun")
#
def vendor(filename, data = nil, &block)
puts "vendoring file #{filename}"
inside("vendor") do |folder|
File.open("#{folder}/#{filename}", "w") do |f|
if block_given?
f.write(block.call)
else
f.write(data)
end
end
end
log 'vendoring', filename
file("vendor/#{filename}", data, false, &block)
end

# Create a new file in the lib/ directory. Code can be specified
Expand All @@ -158,17 +159,9 @@ def vendor(filename, data = nil, &block)
#
# lib("foreign.rb", "# Foreign code is fun")
#
def lib(filename, data = nil)
puts "add lib file #{filename}"
inside("lib") do |folder|
File.open("#{folder}/#{filename}", "w") do |f|
if block_given?
f.write(block.call)
else
f.write(data)
end
end
end
def lib(filename, data = nil, &block)
log 'lib', filename
file("lib/#{filename}", data, false, &block)
end

# Create a new Rakefile with the provided code (either in a block or a string).
Expand All @@ -190,16 +183,8 @@ def lib(filename, data = nil)
# rakefile("seed.rake", "puts 'im plantin ur seedz'")
#
def rakefile(filename, data = nil, &block)
puts "adding rakefile #{filename}"
inside("lib/tasks") do |folder|
File.open("#{folder}/#{filename}", "w") do |f|
if block_given?
f.write(block.call)
else
f.write(data)
end
end
end
log 'rakefile', filename
file("lib/tasks/#{filename}", data, false, &block)
end

# Create a new initializer with the provided code (either in a block or a string).
Expand All @@ -219,16 +204,8 @@ def rakefile(filename, data = nil, &block)
# initializer("api.rb", "API_KEY = '123456'")
#
def initializer(filename, data = nil, &block)
puts "adding initializer #{filename}"
inside("config/initializers") do |folder|
File.open("#{folder}/#{filename}", "w") do |f|
if block_given?
f.write(block.call)
else
f.write(data)
end
end
end
log 'initializer', filename
file("config/initializers/#{filename}", data, false, &block)
end

# Generate something using a generator from Rails or a plugin.
Expand All @@ -240,10 +217,10 @@ def initializer(filename, data = nil, &block)
# generate(:authenticated, "user session")
#
def generate(what, *args)
puts "generating #{what}"
log 'generating', what
argument = args.map(&:to_s).flatten.join(" ")

in_root { `#{root}/script/generate #{what} #{argument}` }
in_root { run("script/generate #{what} #{argument}", false) }
end

# Executes a command
Expand All @@ -254,8 +231,8 @@ def generate(what, *args)
# run('ln -s ~/edge rails)
# end
#
def run(command)
puts "executing #{command} from #{Dir.pwd}"
def run(command, log_action = true)
log 'executing', "#{command} from #{Dir.pwd}" if log_action
`#{command}`
end

Expand All @@ -268,10 +245,10 @@ def run(command)
# rake("gems:install", :sudo => true)
#
def rake(command, options = {})
puts "running rake task #{command}"
log 'rake', command
env = options[:env] || 'development'
sudo = options[:sudo] ? 'sudo ' : ''
in_root { `#{sudo}rake #{command} RAILS_ENV=#{env}` }
in_root { run("#{sudo}rake #{command} RAILS_ENV=#{env}", false) }
end

# Just run the capify command in root
Expand All @@ -281,7 +258,8 @@ def rake(command, options = {})
# capify!
#
def capify!
in_root { `capify .` }
log 'capifying'
in_root { run('capify .', false) }
end

# Add Rails to /vendor/rails
Expand All @@ -291,8 +269,8 @@ def capify!
# freeze!
#
def freeze!(args = {})
puts "vendoring rails edge"
in_root { `rake rails:freeze:edge` }
log 'vendor', 'rails edge'
in_root { run('rake rails:freeze:edge', false) }
end

# Make an entry in Rails routing file conifg/routes.rb
Expand All @@ -302,6 +280,7 @@ def freeze!(args = {})
# route "map.root :controller => :welcome"
#
def route(routing_code)
log 'route', routing_code
sentinel = 'ActionController::Routing::Routes.draw do |map|'

in_root do
Expand All @@ -321,7 +300,7 @@ def route(routing_code)
# freeze! if ask("Should I freeze the latest Rails?") == "yes"
#
def ask(string)
puts string
log '', string
gets.strip
end

Expand Down Expand Up @@ -368,5 +347,13 @@ def gsub_file(relative_destination, regexp, *args, &block)
def destination_path(relative_destination)
File.join(root, relative_destination)
end

def log(action, message = '')
logger.log(action, message)
end

def logger
@logger ||= Rails::Generator::Base.logger
end
end
end

4 comments on commit 9fd35fc

@andrewtimberlake
Copy link
Contributor

Choose a reason for hiding this comment

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

When running rake rails:template this breaks with
uninitialized constant Rails::Generator

@quirkey
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its because of the logger – working on a fix right now.

@quirkey
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch @andrewtimberlake – I already put in a patch, should be picked up soon

@andrewtimberlake
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that helps

Please sign in to comment.