public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Adding test coverage and better logging to Rails::TemplateRunner [#1618 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
quirkey (author)
Sat Dec 27 13:03:12 -0800 2008
lifo (committer)
Sat Dec 27 13:03:44 -0800 2008
commit  9fd35fc2d892393386ca9f522d25ba0bcb9c6764
tree    7a9d31c01c5917c41b506fae5f5c5d73fe2e388d
parent  fdaa9ed0336634c33b5a529dfe4f5ed506a1fc5e
...
8
9
10
 
11
12
13
 
14
15
 
16
17
18
19
 
20
21
22
23
24
25
26
27
 
 
28
29
30
...
41
42
43
44
45
 
 
46
47
48
...
66
67
68
69
 
70
71
72
...
74
75
76
77
 
78
79
80
 
81
82
83
84
85
86
 
87
88
89
90
91
...
93
94
95
 
 
 
 
 
 
 
 
 
96
97
98
 
99
100
101
...
111
112
113
114
 
115
116
117
118
 
119
120
121
...
135
136
137
138
139
140
141
142
143
144
145
146
147
 
 
148
149
150
...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 
 
 
172
173
174
...
190
191
192
193
194
195
196
197
198
199
200
201
202
 
 
203
204
205
...
219
220
221
222
223
224
225
226
227
228
229
230
231
 
 
232
233
234
...
240
241
242
243
 
244
245
246
 
247
248
249
...
254
255
256
257
258
 
 
259
260
261
...
268
269
270
271
 
272
273
274
 
275
276
277
...
281
282
283
284
 
 
285
286
287
...
291
292
293
294
295
 
 
296
297
298
...
302
303
304
 
305
306
307
...
321
322
323
324
 
325
326
327
...
368
369
370
 
 
 
 
 
 
 
 
371
372
373
...
8
9
10
11
12
13
 
14
15
 
16
17
18
19
 
20
21
22
23
24
25
26
 
 
27
28
29
30
31
...
42
43
44
 
 
45
46
47
48
49
...
67
68
69
 
70
71
72
73
...
75
76
77
 
78
79
80
 
81
82
83
84
85
86
 
87
88
 
89
90
91
...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 
107
108
109
110
...
120
121
122
 
123
124
125
126
 
127
128
129
130
...
144
145
146
 
 
 
 
 
 
 
 
 
 
147
148
149
150
151
...
159
160
161
 
 
 
 
 
 
 
 
 
 
 
162
163
164
165
166
167
...
183
184
185
 
 
 
 
 
 
 
 
 
 
186
187
188
189
190
...
204
205
206
 
 
 
 
 
 
 
 
 
 
207
208
209
210
211
...
217
218
219
 
220
221
222
 
223
224
225
226
...
231
232
233
 
 
234
235
236
237
238
...
245
246
247
 
248
249
250
 
251
252
253
254
...
258
259
260
 
261
262
263
264
265
...
269
270
271
 
 
272
273
274
275
276
...
280
281
282
283
284
285
286
...
300
301
302
 
303
304
305
306
...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
0
@@ -8,23 +8,24 @@ require 'fileutils'
0
 module Rails
0
   class TemplateRunner
0
     attr_reader :root
0
+    attr_writer :logger
0
 
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
 
0
-      puts "applying template: #{template}"
0
+      log 'applying', "template: #{template}"
0
 
0
       load_template(template)
0
 
0
-      puts "#{template} applied."
0
+      log 'applied', "#{template}"
0
     end
0
 
0
     def load_template(template)
0
       begin
0
         code = open(template).read
0
         in_root { self.instance_eval(code) }
0
-      rescue LoadError
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
       end
0
     end
0
 
0
@@ -41,8 +42,8 @@ module Rails
0
     #
0
     #   file("config/apach.conf", "your apache config")
0
     #
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
 
0
       inside(dir) do
0
@@ -66,7 +67,7 @@ module Rails
0
     #   plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
0
     #
0
     def plugin(name, options)
0
-      puts "installing plugin #{name}"
0
+      log 'plugin', name
0
 
0
       if options[:git] && options[:submodule]
0
         in_root do
0
@@ -74,18 +75,17 @@ module Rails
0
         end
0
       elsif options[:git] || options[:svn]
0
         in_root do
0
-          `script/plugin install #{options[:svn] || options[:git]}`
0
+          run("script/plugin install #{options[:svn] || options[:git]}", false)
0
         end
0
       else
0
-        puts "! no git or svn provided for #{name}.  skipping..."
0
+        log "! no git or svn provided for #{name}.  skipping..."
0
       end
0
     end
0
 
0
     # Adds an entry into config/environment.rb for the supplied gem :
0
     def gem(name, options = {})
0
-      puts "adding gem #{name}"
0
+      log 'gem', name
0
 
0
-      sentinel = 'Rails::Initializer.run do |config|'
0
       gems_code = "config.gem '#{name}'"
0
 
0
       if options.any?
0
@@ -93,9 +93,18 @@ module Rails
0
         gems_code << ", #{opts}"
0
       end
0
 
0
+      environment gems_code
0
+    end
0
+
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
+
0
+      data = block.call if !data && block_given?
0
+
0
       in_root do
0
         gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
0
-          "#{match}\n  #{gems_code}"
0
+          "#{match}\n " << data
0
         end
0
       end
0
     end
0
@@ -111,11 +120,11 @@ module Rails
0
     def git(command = {})
0
       in_root do
0
         if command.is_a?(Symbol)
0
-          puts "running git #{command}"
0
+          log 'running', "git #{command}"
0
           Git.run(command.to_s)
0
         else
0
           command.each do |command, options|
0
-            puts "running git #{command} #{options}"
0
+            log 'running', "git #{command} #{options}"
0
             Git.run("#{command} #{options}")
0
           end
0
         end
0
@@ -135,16 +144,8 @@ module Rails
0
     #   vendor("foreign.rb", "# Foreign code is fun")
0
     #
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
-          if block_given?
0
-            f.write(block.call)
0
-          else
0
-            f.write(data)
0
-          end
0
-        end
0
-      end
0
+      log 'vendoring', filename
0
+      file("vendor/#{filename}", data, false, &block)
0
     end
0
 
0
     # Create a new file in the lib/ directory. Code can be specified
0
@@ -158,17 +159,9 @@ module Rails
0
     #
0
     #   lib("foreign.rb", "# Foreign code is fun")
0
     #
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
-          if block_given?
0
-            f.write(block.call)
0
-          else
0
-            f.write(data)
0
-          end
0
-        end
0
-      end
0
+    def lib(filename, data = nil, &block)
0
+      log 'lib', filename
0
+      file("lib/#{filename}", data, false, &block)
0
     end
0
 
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
     #
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
-          if block_given?
0
-            f.write(block.call)
0
-          else
0
-            f.write(data)
0
-          end
0
-        end
0
-      end
0
+      log 'rakefile', filename
0
+      file("lib/tasks/#{filename}", data, false, &block)
0
     end
0
 
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
     #
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
-          if block_given?
0
-            f.write(block.call)
0
-          else
0
-            f.write(data)
0
-          end
0
-        end
0
-      end
0
+      log 'initializer', filename
0
+      file("config/initializers/#{filename}", data, false, &block)
0
     end
0
 
0
     # Generate something using a generator from Rails or a plugin.
0
@@ -240,10 +217,10 @@ module Rails
0
     #   generate(:authenticated, "user session")
0
     #
0
     def generate(what, *args)
0
-      puts "generating #{what}"
0
+      log 'generating', what
0
       argument = args.map(&:to_s).flatten.join(" ")
0
 
0
-      in_root { `#{root}/script/generate #{what} #{argument}` }
0
+      in_root { run("script/generate #{what} #{argument}", false) }
0
     end
0
 
0
     # Executes a command
0
@@ -254,8 +231,8 @@ module Rails
0
     #     run('ln -s ~/edge rails)
0
     #   end
0
     #
0
-    def run(command)
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
       `#{command}`
0
     end
0
 
0
@@ -268,10 +245,10 @@ module Rails
0
     #   rake("gems:install", :sudo => true)
0
     #
0
     def rake(command, options = {})
0
-      puts "running rake task #{command}"
0
+      log 'rake', 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
     end
0
 
0
     # Just run the capify command in root
0
@@ -281,7 +258,8 @@ module Rails
0
     #   capify!
0
     #
0
     def capify!
0
-      in_root { `capify .` }
0
+      log 'capifying'
0
+      in_root { run('capify .', false) }
0
     end
0
 
0
     # Add Rails to /vendor/rails
0
@@ -291,8 +269,8 @@ module Rails
0
     #   freeze!
0
     #
0
     def freeze!(args = {})
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
     end
0
 
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
     #
0
     def route(routing_code)
0
+      log 'route', routing_code
0
       sentinel = 'ActionController::Routing::Routes.draw do |map|'
0
 
0
       in_root do
0
@@ -321,7 +300,7 @@ module Rails
0
     #   freeze! if ask("Should I freeze the latest Rails?") == "yes"
0
     #
0
     def ask(string)
0
-      puts string
0
+      log '', string
0
       gets.strip
0
     end
0
 
0
@@ -368,5 +347,13 @@ module Rails
0
     def destination_path(relative_destination)
0
       File.join(root, relative_destination)
0
     end
0
+
0
+    def log(action, message = '')
0
+      logger.log(action, message)
0
+    end
0
+
0
+    def logger
0
+      @logger ||= Rails::Generator::Base.logger
0
+    end
0
   end
0
 end
0
\ No newline at end of file

Comments

andrewtimberlake Sat Dec 27 19:48:04 -0800 2008

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

quirkey Sat Dec 27 20:11:24 -0800 2008

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

quirkey Sat Dec 27 20:36:13 -0800 2008

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

andrewtimberlake Sun Dec 28 01:31:50 -0800 2008

Thanks, that helps