3b49b1fa92531d4ce019c18a86081b6e1ef18b77
1 require File
.dirname(__FILE__
) + '/scm/scm'
2 require File
.dirname(__FILE__
) + '/scm/git'
3 require File
.dirname(__FILE__
) + '/scm/svn'
13 def initialize(template
, root
= '') # :nodoc:
14 @root = File
.expand_path(File
.directory
?(root
) ? root
: File
.join(Dir
.pwd
, root
))
16 log
'applying', "template: #{template}"
18 load_template(template
)
20 log
'applied', "#{template}"
23 def load_template(template
)
25 code
= open(template
).read
26 in_root
{ self.instance_eval(code
) }
27 rescue LoadError
, Errno
::ENOENT => e
28 raise "The template [#{template}] could not be loaded. Error: #{e}"
32 # Create a new file in the Rails project folder. Specify the
33 # relative path from RAILS_ROOT. Data is the return value of a block
38 # file("lib/fun_party.rb") do
39 # hostname = ask("What is the virtual hostname I should use?")
40 # "vhost.name = #{hostname}"
43 # file("config/apach.conf", "your apache config")
45 def file(filename
, data = nil, log_action
= true, &block
)
46 log
'file', filename
if log_action
47 dir
, file
= [File
.dirname(filename
), File
.basename(filename
)]
50 File
.open(file
, "w") do |f
|
60 # Install a plugin. You must provide either a Subversion url or Git url.
61 # For a Git-hosted plugin, you can specify if it should be added as a submodule instead of cloned.
65 # plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git'
66 # plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git', :submodule => true
67 # plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
69 def plugin(name
, options
)
72 if options
[:git] && options
[:submodule]
74 Git
.run("submodule add #{options[:git]} vendor/plugins/#{name}")
76 elsif options
[:git] || options
[:svn]
78 run_ruby_script("script/plugin install #{options[:svn] || options[:git]}", false)
81 log
"! no git or svn provided for #{name}. skipping..."
85 # Adds an entry into config/environment.rb for the supplied gem :
86 def gem(name
, options
= {})
88 env = options
.delete(:env)
90 gems_code
= "config.gem '#{name}'"
93 opts
= options
.inject([]) {|result
, h
| result
<< [":#{h[0]} => #{h[1].inspect.gsub('"',"'")}"] }.sort
.join(", ")
94 gems_code
<< ", #{opts}"
97 environment gems_code
, :env => env
100 # Adds a line inside the Initializer block for config/environment.rb. Used by #gem
101 # If options :env is specified, the line is appended to the corresponding
102 # file in config/environments/#{env}.rb
103 def environment(data = nil, options
= {}, &block
)
104 sentinel
= 'Rails::Initializer.run do |config|'
106 data = block
.call
if !data && block_given
?
109 if options
[:env].nil?
110 gsub_file
'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
111 "#{match}\n " << data
114 Array
.wrap(options
[:env]).each
do|env|
115 append_file
"config/environments/#{env}.rb", "\n#{data}"
121 # Run a command in git.
126 # git :add => "this.file that.rb"
127 # git :add => "onefile.rb", :rm => "badfile.cxx"
129 def git(command
= {})
131 if command
.is_a
?(Symbol
)
132 log
'running', "git #{command}"
133 Git
.run(command
.to_s
)
135 command
.each
do |command
, options
|
136 log
'running', "git #{command} #{options}"
137 Git
.run("#{command} #{options}")
143 # Create a new file in the vendor/ directory. Code can be specified
144 # in a block or a data string can be given.
148 # vendor("sekrit.rb") do
149 # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--"
150 # "salt = '#{sekrit_salt}'"
153 # vendor("foreign.rb", "# Foreign code is fun")
155 def vendor(filename
, data = nil, &block
)
156 log
'vendoring', filename
157 file("vendor/#{filename}", data, false, &block
)
160 # Create a new file in the lib/ directory. Code can be specified
161 # in a block or a data string can be given.
165 # lib("crypto.rb") do
166 # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'"
169 # lib("foreign.rb", "# Foreign code is fun")
171 def lib(filename
, data = nil, &block
)
173 file("lib/#{filename}", data, false, &block
)
176 # Create a new Rakefile with the provided code (either in a block or a string).
180 # rakefile("bootstrap.rake") do
181 # project = ask("What is the UNIX name of your project?")
184 # namespace :#{project} do
186 # puts "i like boots!"
192 # rakefile("seed.rake", "puts 'im plantin ur seedz'")
194 def rakefile(filename
, data = nil, &block
)
195 log
'rakefile', filename
196 file("lib/tasks/#{filename}", data, false, &block
)
199 # Create a new initializer with the provided code (either in a block or a string).
203 # initializer("globals.rb") do
206 # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do
207 # data << "#{const} = :entp"
213 # initializer("api.rb", "API_KEY = '123456'")
215 def initializer(filename
, data = nil, &block
)
216 log
'initializer', filename
217 file("config/initializers/#{filename}", data, false, &block
)
220 # Generate something using a generator from Rails or a plugin.
221 # The second parameter is the argument string that is passed to
222 # the generator or an Array that is joined.
226 # generate(:authenticated, "user session")
228 def generate(what
, *args
)
229 log
'generating', what
230 argument
= args
.map(&:to_s).flatten
.join(" ")
232 in_root
{ run_ruby_script("script/generate #{what} #{argument}", false) }
239 # inside('vendor') do
240 # run('ln -s ~/edge rails)
243 def run(command
, log_action
= true)
244 log
'executing', "#{command} from #{Dir.pwd}" if log_action
248 # Executes a ruby script (taking into account WIN32 platform quirks)
249 def run_ruby_script(command
, log_action
= true)
250 ruby_command
= RUBY_PLATFORM=~
/win32/ ? 'ruby ' : ''
251 run("#{ruby_command}#{command}", log_action
)
254 # Runs the supplied rake task
259 # rake("db:migrate", :env => "production")
260 # rake("gems:install", :sudo => true)
262 def rake(command
, options
= {})
264 env = options
[:env] || 'development'
265 sudo
= options
[:sudo] ? 'sudo ' : ''
266 in_root
{ run("#{sudo}rake #{command} RAILS_ENV=#{env}", false) }
269 # Just run the capify command in root
277 in_root
{ run('capify .', false) }
280 # Add Rails to /vendor/rails
286 def freeze
!(args
= {})
287 log
'vendor', 'rails edge'
288 in_root
{ run('rake rails:freeze:edge', false) }
291 # Make an entry in Rails routing file conifg/routes.rb
295 # route "map.root :controller => :welcome"
297 def route(routing_code
)
298 log
'route', routing_code
299 sentinel
= 'ActionController::Routing::Routes.draw do |map|'
302 gsub_file
'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
303 "#{match}\n #{routing_code}\n"
314 # answer = ask("Should I freeze the latest Rails?")
315 # freeze! if ask("Should I freeze the latest Rails?") == "yes"
322 # Do something in the root of the Rails application or
323 # a provided subfolder; the full path is yielded to the block you provide.
324 # The path is set back to the previous path when the method exits.
325 def inside(dir
= '', &block
)
326 folder
= File
.join(root
, dir
)
327 FileUtils
.mkdir_p(folder
) unless File
.exist
?(folder
)
328 FileUtils
.cd(folder
) { block
.arity
== 1 ? yield(folder
) : yield }
332 FileUtils
.cd(root
) { yield }
335 # Helper to test if the user says yes(y)?
339 # freeze! if yes?("Should I freeze the latest Rails?")
342 answer
= ask(question
).downcase
343 answer
== "y" || answer
== "yes"
346 # Helper to test if the user does NOT say yes(y)?
350 # capify! if no?("Will you be using vlad to deploy your application?")
356 # Run a regular expression replacement on a file
360 # gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
362 def gsub_file(relative_destination
, regexp
, *args
, &block
)
363 path
= destination_path(relative_destination
)
364 content
= File
.read(path
).gsub(regexp
, *args
, &block
)
365 File
.open(path
, 'wb') { |file
| file
.write(content
) }
368 # Append text to a file
372 # append_file 'config/environments/test.rb', 'config.gem "rspec"'
374 def append_file(relative_destination
, data)
375 path
= destination_path(relative_destination
)
376 File
.open(path
, 'ab') { |file
| file
.write(data) }
379 def destination_path(relative_destination
)
380 File
.join(root
, relative_destination
)
383 def log(action
, message
= '')
384 logger
.log(action
, message
)
388 @logger ||= Rails
::Generator::Base.logger
392 @logger ||= if defined?(Rails
::Generator::Base)
393 Rails
::Generator::Base.logger
395 require 'rails_generator/simple_logger'
396 Rails
::Generator::SimpleLogger.new(STDOUT)