<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>railties/test/generators/rails_template_runner_test.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -8,23 +8,24 @@ require 'fileutils'
 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 &quot;applying template: #{template}&quot;
+      log 'applying', &quot;template: #{template}&quot;
 
       load_template(template)
 
-      puts &quot;#{template} applied.&quot;
+      log 'applied', &quot;#{template}&quot;
     end
 
     def load_template(template)
       begin
         code = open(template).read
         in_root { self.instance_eval(code) }
-      rescue LoadError
-        raise &quot;The template [#{template}] could not be loaded.&quot;
+      rescue LoadError, Errno::ENOENT =&gt; e
+        raise &quot;The template [#{template}] could not be loaded. Error: #{e}&quot;
       end
     end
 
@@ -41,8 +42,8 @@ module Rails
     #
     #   file(&quot;config/apach.conf&quot;, &quot;your apache config&quot;)
     #
-    def file(filename, data = nil, &amp;block)
-      puts &quot;creating file #{filename}&quot;
+    def file(filename, data = nil, log_action = true, &amp;block)
+      log 'file', filename if log_action
       dir, file = [File.dirname(filename), File.basename(filename)]
 
       inside(dir) do
@@ -66,7 +67,7 @@ module Rails
     #   plugin 'restful-authentication', :svn =&gt; 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
     #
     def plugin(name, options)
-      puts &quot;installing plugin #{name}&quot;
+      log 'plugin', name
 
       if options[:git] &amp;&amp; options[:submodule]
         in_root do
@@ -74,18 +75,17 @@ module Rails
         end
       elsif options[:git] || options[:svn]
         in_root do
-          `script/plugin install #{options[:svn] || options[:git]}`
+          run(&quot;script/plugin install #{options[:svn] || options[:git]}&quot;, false)
         end
       else
-        puts &quot;! no git or svn provided for #{name}.  skipping...&quot;
+        log &quot;! no git or svn provided for #{name}.  skipping...&quot;
       end
     end
 
     # Adds an entry into config/environment.rb for the supplied gem :
     def gem(name, options = {})
-      puts &quot;adding gem #{name}&quot;
+      log 'gem', name
 
-      sentinel = 'Rails::Initializer.run do |config|'
       gems_code = &quot;config.gem '#{name}'&quot;
 
       if options.any?
@@ -93,9 +93,18 @@ module Rails
         gems_code &lt;&lt; &quot;, #{opts}&quot;
       end
 
+      environment gems_code
+    end
+
+    # Adds a line inside the Initializer block for config/environment.rb. Used by #gem
+    def environment(data = nil, &amp;block)
+      sentinel = 'Rails::Initializer.run do |config|'
+
+      data = block.call if !data &amp;&amp; block_given?
+
       in_root do
         gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
-          &quot;#{match}\n  #{gems_code}&quot;
+          &quot;#{match}\n &quot; &lt;&lt; data
         end
       end
     end
@@ -111,11 +120,11 @@ module Rails
     def git(command = {})
       in_root do
         if command.is_a?(Symbol)
-          puts &quot;running git #{command}&quot;
+          log 'running', &quot;git #{command}&quot;
           Git.run(command.to_s)
         else
           command.each do |command, options|
-            puts &quot;running git #{command} #{options}&quot;
+            log 'running', &quot;git #{command} #{options}&quot;
             Git.run(&quot;#{command} #{options}&quot;)
           end
         end
@@ -135,16 +144,8 @@ module Rails
     #   vendor(&quot;foreign.rb&quot;, &quot;# Foreign code is fun&quot;)
     #
     def vendor(filename, data = nil, &amp;block)
-      puts &quot;vendoring file #{filename}&quot;
-      inside(&quot;vendor&quot;) do |folder|
-        File.open(&quot;#{folder}/#{filename}&quot;, &quot;w&quot;) do |f|
-          if block_given?
-            f.write(block.call)
-          else
-            f.write(data)
-          end
-        end
-      end
+      log 'vendoring', filename
+      file(&quot;vendor/#{filename}&quot;, data, false, &amp;block)
     end
 
     # Create a new file in the lib/ directory. Code can be specified
@@ -158,17 +159,9 @@ module Rails
     #
     #   lib(&quot;foreign.rb&quot;, &quot;# Foreign code is fun&quot;)
     #
-    def lib(filename, data = nil)
-      puts &quot;add lib file #{filename}&quot;
-      inside(&quot;lib&quot;) do |folder|
-        File.open(&quot;#{folder}/#{filename}&quot;, &quot;w&quot;) do |f|
-          if block_given?
-            f.write(block.call)
-          else
-            f.write(data)
-          end
-        end
-      end
+    def lib(filename, data = nil, &amp;block)
+      log 'lib', filename
+      file(&quot;lib/#{filename}&quot;, data, false, &amp;block)
     end
 
     # Create a new Rakefile with the provided code (either in a block or a string).
@@ -190,16 +183,8 @@ module Rails
     #   rakefile(&quot;seed.rake&quot;, &quot;puts 'im plantin ur seedz'&quot;)
     #
     def rakefile(filename, data = nil, &amp;block)
-      puts &quot;adding rakefile #{filename}&quot;
-      inside(&quot;lib/tasks&quot;) do |folder|
-        File.open(&quot;#{folder}/#{filename}&quot;, &quot;w&quot;) do |f|
-          if block_given?
-            f.write(block.call)
-          else
-            f.write(data)
-          end
-        end
-      end
+      log 'rakefile', filename
+      file(&quot;lib/tasks/#{filename}&quot;, data, false, &amp;block)
     end
 
     # Create a new initializer with the provided code (either in a block or a string).
@@ -219,16 +204,8 @@ module Rails
     #   initializer(&quot;api.rb&quot;, &quot;API_KEY = '123456'&quot;)
     #
     def initializer(filename, data = nil, &amp;block)
-      puts &quot;adding initializer #{filename}&quot;
-      inside(&quot;config/initializers&quot;) do |folder|
-        File.open(&quot;#{folder}/#{filename}&quot;, &quot;w&quot;) do |f|
-          if block_given?
-            f.write(block.call)
-          else
-            f.write(data)
-          end
-        end
-      end
+      log 'initializer', filename
+      file(&quot;config/initializers/#{filename}&quot;, data, false, &amp;block)
     end
 
     # Generate something using a generator from Rails or a plugin.
@@ -240,10 +217,10 @@ module Rails
     #   generate(:authenticated, &quot;user session&quot;)
     #
     def generate(what, *args)
-      puts &quot;generating #{what}&quot;
+      log 'generating', what
       argument = args.map(&amp;:to_s).flatten.join(&quot; &quot;)
 
-      in_root { `#{root}/script/generate #{what} #{argument}` }
+      in_root { run(&quot;script/generate #{what} #{argument}&quot;, false) }
     end
 
     # Executes a command
@@ -254,8 +231,8 @@ module Rails
     #     run('ln -s ~/edge rails)
     #   end
     #
-    def run(command)
-      puts &quot;executing #{command} from #{Dir.pwd}&quot;
+    def run(command, log_action = true)
+      log 'executing',  &quot;#{command} from #{Dir.pwd}&quot; if log_action
       `#{command}`
     end
 
@@ -268,10 +245,10 @@ module Rails
     #   rake(&quot;gems:install&quot;, :sudo =&gt; true)
     #
     def rake(command, options = {})
-      puts &quot;running rake task #{command}&quot;
+      log 'rake', command
       env = options[:env] || 'development'
       sudo = options[:sudo] ? 'sudo ' : ''
-      in_root { `#{sudo}rake #{command} RAILS_ENV=#{env}` }
+      in_root { run(&quot;#{sudo}rake #{command} RAILS_ENV=#{env}&quot;, false) }
     end
 
     # Just run the capify command in root
@@ -281,7 +258,8 @@ module Rails
     #   capify!
     #
     def capify!
-      in_root { `capify .` }
+      log 'capifying'
+      in_root { run('capify .', false) }
     end
 
     # Add Rails to /vendor/rails
@@ -291,8 +269,8 @@ module Rails
     #   freeze!
     #
     def freeze!(args = {})
-      puts &quot;vendoring rails edge&quot;
-      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
@@ -302,6 +280,7 @@ module Rails
     #   route &quot;map.root :controller =&gt; :welcome&quot;
     #
     def route(routing_code)
+      log 'route', routing_code
       sentinel = 'ActionController::Routing::Routes.draw do |map|'
 
       in_root do
@@ -321,7 +300,7 @@ module Rails
     #   freeze! if ask(&quot;Should I freeze the latest Rails?&quot;) == &quot;yes&quot;
     #
     def ask(string)
-      puts string
+      log '', string
       gets.strip
     end
 
@@ -368,5 +347,13 @@ module Rails
     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
\ No newline at end of file</diff>
      <filename>railties/lib/rails_generator/generators/applications/app/template_runner.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>fdaa9ed0336634c33b5a529dfe4f5ed506a1fc5e</id>
    </parent>
  </parents>
  <author>
    <name>Aaron Quint</name>
    <login>quirkey</login>
    <email>aaron@quirkey.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/9fd35fc2d892393386ca9f522d25ba0bcb9c6764</url>
  <id>9fd35fc2d892393386ca9f522d25ba0bcb9c6764</id>
  <committed-date>2008-12-27T13:03:44-08:00</committed-date>
  <authored-date>2008-12-27T13:03:12-08:00</authored-date>
  <message>Adding test coverage and better logging to Rails::TemplateRunner [#1618 state:resolved]

Signed-off-by: Pratik Naik &lt;pratiknaik@gmail.com&gt;</message>
  <tree>7a9d31c01c5917c41b506fae5f5c5d73fe2e388d</tree>
  <committer>
    <name>Pratik Naik</name>
    <login>lifo</login>
    <email>pratiknaik@gmail.com</email>
  </committer>
</commit>
