Skip to content
Newer
Older
100644 326 lines (293 sloc) 10.1 KB
748e1d6 @josevalim Require open-uri, we will need it.
josevalim authored Jun 17, 2009
1 require 'open-uri'
53c13f1 @anildigital Use Config::CONFIG['host_os'] instead of RUBY_PLATFORM [#4477 state:r…
anildigital authored Apr 26, 2010
2 require 'rbconfig'
d246da1 @carlosantoniodasilva Add missing require to Array#wrap in generators action methods
carlosantoniodasilva authored Jan 22, 2012
3 require 'active_support/core_ext/array/wrap'
748e1d6 @josevalim Require open-uri, we will need it.
josevalim authored Jun 17, 2009
4
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
5 module Rails
6 module Generators
7 module Actions
8
9 # Install a plugin. You must provide either a Subversion url or Git url.
bee3e09 @mrduncan Users can now pass :branch for git plugins and :revision for subversi…
mrduncan authored Aug 8, 2009
10 #
11 # For a Git-hosted plugin, you can specify a branch and
12 # whether it should be added as a submodule instead of cloned.
13 #
14 # For a Subversion-hosted plugin you can specify a revision.
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
15 #
16 # ==== Examples
17 #
18 # plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git'
bee3e09 @mrduncan Users can now pass :branch for git plugins and :revision for subversi…
mrduncan authored Aug 8, 2009
19 # plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git', :branch => 'stable'
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
20 # plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git', :submodule => true
21 # plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
bee3e09 @mrduncan Users can now pass :branch for git plugins and :revision for subversi…
mrduncan authored Aug 8, 2009
22 # plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk', :revision => 1234
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
23 #
24 def plugin(name, options)
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
25 log :plugin, name
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
26
27 if options[:git] && options[:submodule]
bee3e09 @mrduncan Users can now pass :branch for git plugins and :revision for subversi…
mrduncan authored Aug 8, 2009
28 options[:git] = "-b #{options[:branch]} #{options[:git]}" if options[:branch]
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
29 in_root do
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
30 run "git submodule add #{options[:git]} vendor/plugins/#{name}", :verbose => false
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
31 end
32 elsif options[:git] || options[:svn]
bee3e09 @mrduncan Users can now pass :branch for git plugins and :revision for subversi…
mrduncan authored Aug 8, 2009
33 options[:git] = "-b #{options[:branch]} #{options[:git]}" if options[:branch]
34 options[:svn] = "-r #{options[:revision]} #{options[:svn]}" if options[:revision]
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
35 in_root do
f44a0b1 @romdi fix usage examples and more to use new invocations
romdi authored Feb 6, 2010
36 run_ruby_script "script/rails plugin install #{options[:svn] || options[:git]}", :verbose => false
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
37 end
38 else
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
39 log "! no git or svn provided for #{name}. Skipping..."
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
40 end
41 end
42
7e075e6 Fixed many references to the old config/environment.rb and Rails::Ini…
Benjamin Quorning authored Jul 9, 2010
43 # Adds an entry into Gemfile for the supplied gem. If env
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
44 # is specified, add the gem to the given environment.
45 #
46 # ==== Example
47 #
74e1788 @radar For templates' gem method, :env is deprecated, use :group instead
radar authored Dec 1, 2010
48 # gem "rspec", :group => :test
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
49 # gem "technoweenie-restful-authentication", :lib => "restful-authentication", :source => "http://gems.github.com/"
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 3, 2009
50 # gem "rails", "3.0", :git => "git://github.com/rails/rails"
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
51 #
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
52 def gem(*args)
53 options = args.extract_options!
54 name, version = args
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
55
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
56 # Set the message to be shown in logs. Uses the git repo if one is given,
57 # otherwise use name (version).
58 parts, message = [ name.inspect ], name
59 if version ||= options.delete(:version)
9c65474 Fixes unescaped version passthrough of generator 'gem' command. [#3901
Michael Bleigh authored Feb 8, 2010
60 parts << version.inspect
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
61 message << " (#{version})"
62 end
63 message = options[:git] if options[:git]
64
65 log :gemfile, message
66
67 options.each do |option, value|
68 parts << ":#{option} => #{value.inspect}"
69 end
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
70
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
71 in_root do
95e9b5f @johnmdonahue Updating newline fix to maintain existing linebreaks and indentation …
johnmdonahue authored Nov 25, 2011
72 str = "gem #{parts.join(", ")}"
47bc5d0 @wojtekmach Add gem_group support to generators
wojtekmach authored Sep 4, 2011
73 str = " " + str if @in_group
95e9b5f @johnmdonahue Updating newline fix to maintain existing linebreaks and indentation …
johnmdonahue authored Nov 25, 2011
74 str = "\n" + str
47bc5d0 @wojtekmach Add gem_group support to generators
wojtekmach authored Sep 4, 2011
75 append_file "Gemfile", str, :verbose => false
76 end
77 end
78
79 # Wraps gem entries inside a group.
80 #
81 # ==== Example
82 #
83 # gem_group :development, :test do
84 # gem "rspec-rails"
85 # end
86 #
87 def gem_group(*names, &block)
88 name = names.map(&:inspect).join(", ")
89 log :gemfile, "group #{name}"
90
91 in_root do
95e9b5f @johnmdonahue Updating newline fix to maintain existing linebreaks and indentation …
johnmdonahue authored Nov 25, 2011
92 append_file "Gemfile", "\ngroup #{name} do", :force => true
47bc5d0 @wojtekmach Add gem_group support to generators
wojtekmach authored Sep 4, 2011
93
94 @in_group = true
b0b436e @arunagw warning removed : '&' interpreted as argument prefix
arunagw authored Sep 30, 2011
95 instance_eval(&block)
47bc5d0 @wojtekmach Add gem_group support to generators
wojtekmach authored Sep 4, 2011
96 @in_group = false
97
95e9b5f @johnmdonahue Updating newline fix to maintain existing linebreaks and indentation …
johnmdonahue authored Nov 25, 2011
98 append_file "Gemfile", "\nend\n", :force => true
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
99 end
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
100 end
101
102 # Add the given source to Gemfile
103 #
104 # ==== Example
105 #
b56b9ee @drnic #add_source: Fixed docco + added required newline to line added to Ge…
drnic authored Feb 22, 2010
106 # add_source "http://gems.github.com/"
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
107 def add_source(source, options={})
108 log :source, source
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
109
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
110 in_root do
b56b9ee @drnic #add_source: Fixed docco + added required newline to line added to Ge…
drnic authored Feb 22, 2010
111 prepend_file "Gemfile", "source #{source.inspect}\n", :verbose => false
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
112 end
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
113 end
114
7e075e6 Fixed many references to the old config/environment.rb and Rails::Ini…
Benjamin Quorning authored Jul 9, 2010
115 # Adds a line inside the Application class for config/application.rb.
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
116 #
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
117 # If options :env is specified, the line is appended to the corresponding
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
118 # file in config/environments.
119 #
120 def environment(data=nil, options={}, &block)
02c3c9d Fix the application.rb generator
Carl Lerche authored Nov 25, 2009
121 sentinel = /class [a-z_:]+ < Rails::Application/i
fcdcc87 @guilleiguaran Fix Rails::Generators::Actions#environment when env is passed. Closes #…
guilleiguaran authored Jul 17, 2011
122 env_file_sentinel = /::Application\.configure do/
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
123 data = block.call if !data && block_given?
124
125 in_root do
126 if options[:env].nil?
e61d6af @avakhov fix indent for generator environment method
avakhov authored Sep 2, 2011
127 inject_into_file 'config/application.rb', "\n #{data}", :after => sentinel, :verbose => false
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
128 else
fcdcc87 @guilleiguaran Fix Rails::Generators::Actions#environment when env is passed. Closes #…
guilleiguaran authored Jul 17, 2011
129 Array.wrap(options[:env]).each do |env|
130 inject_into_file "config/environments/#{env}.rb", "\n #{data}", :after => env_file_sentinel, :verbose => false
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
131 end
132 end
133 end
134 end
3b8e29f @josevalim Remove --freeze (since Rails will come bundled in all new apps) and u…
josevalim authored Nov 4, 2009
135 alias :application :environment
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
136
137 # Run a command in git.
138 #
139 # ==== Examples
140 #
141 # git :init
142 # git :add => "this.file that.rb"
143 # git :add => "onefile.rb", :rm => "badfile.cxx"
144 #
87329ef @gazay Removed warnings about shadowing variables
gazay authored Jun 20, 2011
145 def git(commands={})
146 if commands.is_a?(Symbol)
147 run "git #{commands}"
0fc5c59 @calavera Rails templates git command runs inside another directory [#2466 stat…
calavera authored Aug 30, 2009
148 else
34618e6 @arunagw Warnings removed for using shadow variable.
arunagw authored Sep 4, 2011
149 commands.each do |cmd, options|
150 run "git #{cmd} #{options}"
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
151 end
152 end
153 end
154
155 # Create a new file in the vendor/ directory. Code can be specified
156 # in a block or a data string can be given.
157 #
158 # ==== Examples
159 #
160 # vendor("sekrit.rb") do
161 # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--"
162 # "salt = '#{sekrit_salt}'"
163 # end
164 #
165 # vendor("foreign.rb", "# Foreign code is fun")
166 #
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
167 def vendor(filename, data=nil, &block)
168 log :vendor, filename
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
169 create_file("vendor/#{filename}", data, :verbose => false, &block)
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
170 end
171
172 # Create a new file in the lib/ directory. Code can be specified
173 # in a block or a data string can be given.
174 #
175 # ==== Examples
176 #
177 # lib("crypto.rb") do
178 # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'"
179 # end
180 #
181 # lib("foreign.rb", "# Foreign code is fun")
182 #
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
183 def lib(filename, data=nil, &block)
184 log :lib, filename
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
185 create_file("lib/#{filename}", data, :verbose => false, &block)
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
186 end
187
188 # Create a new Rakefile with the provided code (either in a block or a string).
189 #
190 # ==== Examples
191 #
192 # rakefile("bootstrap.rake") do
193 # project = ask("What is the UNIX name of your project?")
194 #
195 # <<-TASK
196 # namespace :#{project} do
197 # task :bootstrap do
198 # puts "i like boots!"
199 # end
200 # end
201 # TASK
202 # end
203 #
204 # rakefile("seed.rake", "puts 'im plantin ur seedz'")
205 #
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
206 def rakefile(filename, data=nil, &block)
207 log :rakefile, filename
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
208 create_file("lib/tasks/#{filename}", data, :verbose => false, &block)
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
209 end
210
211 # Create a new initializer with the provided code (either in a block or a string).
212 #
213 # ==== Examples
214 #
215 # initializer("globals.rb") do
216 # data = ""
217 #
b439c14 Fixing two minor errors in Rails::Generators::Actions initializer met…
Juan Maiz authored Oct 8, 2010
218 # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do |const|
219 # data << "#{const} = :entp\n"
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
220 # end
221 #
222 # data
223 # end
224 #
225 # initializer("api.rb", "API_KEY = '123456'")
226 #
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
227 def initializer(filename, data=nil, &block)
228 log :initializer, filename
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
229 create_file("config/initializers/#{filename}", data, :verbose => false, &block)
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
230 end
231
232 # Generate something using a generator from Rails or a plugin.
233 # The second parameter is the argument string that is passed to
234 # the generator or an Array that is joined.
235 #
236 # ==== Example
237 #
238 # generate(:authenticated, "user session")
239 #
240 def generate(what, *args)
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
241 log :generate, what
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
242 argument = args.map {|arg| arg.to_s }.flatten.join(" ")
243
f44a0b1 @romdi fix usage examples and more to use new invocations
romdi authored Feb 6, 2010
244 in_root { run_ruby_script("script/rails generate #{what} #{argument}", :verbose => false) }
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
245 end
246
247 # Runs the supplied rake task
248 #
249 # ==== Example
250 #
251 # rake("db:migrate")
252 # rake("db:migrate", :env => "production")
253 # rake("gems:install", :sudo => true)
254 #
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
255 def rake(command, options={})
256 log :rake, command
148e4a5 @dmitriy-kiriyenko Honour RAILS_ENV environment variable when running rake.
dmitriy-kiriyenko authored Sep 29, 2011
257 env = options[:env] || ENV["RAILS_ENV"] || 'development'
bb75c33 @spastorino Config is deprecated on 1.8.8 and 1.9.3 use RbConfig
spastorino authored Aug 23, 2010
258 sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : ''
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
259 in_root { run("#{sudo}#{extify(:rake)} #{command} RAILS_ENV=#{env}", :verbose => false) }
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
260 end
261
262 # Just run the capify command in root
263 #
264 # ==== Example
265 #
266 # capify!
267 #
268 def capify!
9b82fa1 @josevalim Freeze rails when required.
josevalim authored Jun 17, 2009
269 log :capify, ""
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
270 in_root { run("#{extify(:capify)} .", :verbose => false) }
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
271 end
272
db0530e @mhayes Fixed typo in Rails::Generators::Base [#5051 state:resolved]
mhayes authored Jul 5, 2010
273 # Make an entry in Rails routing file config/routes.rb
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
274 #
275 # === Example
276 #
b2ef6d1 @josh Fixed old routing mapper example in generated routes.rb
josh authored Dec 9, 2009
277 # route "root :to => 'welcome'"
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
278 #
279 def route(routing_code)
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
280 log :route, routing_code
e6331b1 @spastorino Makes rails destroy scaffold don't duplicate routes.draw do |map| |ma…
spastorino authored Jul 31, 2010
281 sentinel = /\.routes\.draw do(?:\s*\|map\|)?\s*$/
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
282
283 in_root do
b4ef958 @josevalim Change false to :verbose => false as in new Thor version.
josevalim authored Jul 15, 2009
284 inject_into_file 'config/routes.rb', "\n #{routing_code}\n", { :after => sentinel, :verbose => false }
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
285 end
286 end
287
d0454e5 @josevalim Add readme as method in Rails::Generators::Actions (as we had in 2.3)
josevalim authored Feb 17, 2010
288 # Reads the given file at the source root and prints it in the console.
289 #
290 # === Example
291 #
292 # readme "README"
293 #
294 def readme(path)
f00a398 @josevalim log and readme should respect --quiet
josevalim authored Mar 4, 2011
295 log File.read(find_in_source_paths(path))
d0454e5 @josevalim Add readme as method in Rails::Generators::Actions (as we had in 2.3)
josevalim authored Feb 17, 2010
296 end
297
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
298 protected
299
300 # Define log for backwards compatibility. If just one argument is sent,
f00a398 @josevalim log and readme should respect --quiet
josevalim authored Mar 4, 2011
301 # invoke say, otherwise invoke say_status. Differently from say and
302 # similarly to say_status, this method respects the quiet? option given.
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
303 #
304 def log(*args)
305 if args.size == 1
f00a398 @josevalim log and readme should respect --quiet
josevalim authored Mar 4, 2011
306 say args.first.to_s unless options.quiet?
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
307 else
d627c93 @josevalim Pass config.generators options along when RAILS_GENERATORS is set and…
josevalim authored Nov 4, 2009
308 args << (self.behavior == :invoke ? :green : :red)
32b9cef @amatsuda parenthesize the arguments with splat
amatsuda authored May 18, 2011
309 say_status(*args)
b9d8faf @josevalim Update templates to be backwards compatible.
josevalim authored Jun 17, 2009
310 end
311 end
312
66d1b96 @josevalim Make specs pass on Ruby 1.9.
josevalim authored Jul 6, 2009
313 # Add an extension to the given name based on the platform.
35925a8 @josevalim Ensure that rails templates methods are invoked with the proper exten…
josevalim authored Jul 4, 2009
314 #
315 def extify(name)
bb75c33 @spastorino Config is deprecated on 1.8.8 and 1.9.3 use RbConfig
spastorino authored Aug 24, 2010
316 if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
66d1b96 @josevalim Make specs pass on Ruby 1.9.
josevalim authored Jul 6, 2009
317 "#{name}.bat"
318 else
319 name
320 end
35925a8 @josevalim Ensure that rails templates methods are invoked with the proper exten…
josevalim authored Jul 4, 2009
321 end
322
d57b193 @josevalim Add base and port actions. Not functional though.
josevalim authored Jun 16, 2009
323 end
324 end
325 end
Something went wrong with that request. Please try again.