Skip to content

Commit 5c99a4b

Browse files
committed
AppGenerator: allow both 'rake' and 'rails'
This commit comes from the comments made by @matthewd at https://github.com/rails/rails/pull/23795/files#r54469637 and by @rafaelfranca at https://github.com/rails/rails/pull/23795/files#r54609364 The idea is that if you type (for example) "rake db:migrate" in an AppGenerator, then this should actually invoke `rake db:migrate` on the command line, whereas if you type "rails_command db:migrate", this should invoke `rails db:migrate`.
1 parent b165d73 commit 5c99a4b

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

railties/lib/rails/generators/actions.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,23 @@ def generate(what, *args)
207207
in_root { run_ruby_script("bin/rails generate #{what} #{argument}", verbose: false) }
208208
end
209209

210-
# Runs the supplied rake task
210+
# Runs the supplied rake task (invoked with 'rake ...')
211211
#
212212
# rake("db:migrate")
213213
# rake("db:migrate", env: "production")
214214
# rake("gems:install", sudo: true)
215215
def rake(command, options={})
216-
log :rake, command
217-
env = options[:env] || ENV["RAILS_ENV"] || 'development'
218-
sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : ''
219-
in_root { run("#{sudo}#{extify(:rails)} #{command} RAILS_ENV=#{env}", verbose: false) }
216+
execute_command :rake, command, options
217+
end
218+
219+
# Runs the supplied rake task (invoked with 'rails ...')
220+
#
221+
# rails("db:migrate")
222+
# rails("db:migrate", env: "production")
223+
# rails("gems:install", sudo: true)
224+
def rails_command(command, options={})
225+
execute_command :rails, command, options
220226
end
221-
alias :rails_command :rake
222227

223228
# Just run the capify command in root
224229
#
@@ -271,6 +276,16 @@ def log(*args)
271276
end
272277
end
273278

279+
280+
# Runs the supplied command using either "rake ..." or "rails ..."
281+
# based on the executor parameter provided.
282+
def execute_command(executor, command, options={})
283+
log executor, command
284+
env = options[:env] || ENV["RAILS_ENV"] || 'development'
285+
sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : ''
286+
in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", verbose: false) }
287+
end
288+
274289
# Add an extension to the given name based on the platform.
275290
def extify(name)
276291
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/

railties/test/generators/actions_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,37 +202,37 @@ def test_generate_should_run_script_generate_with_argument_and_options
202202
end
203203

204204
def test_rails_should_run_rake_command_with_default_env
205-
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false]) do
205+
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false]) do
206206
with_rails_env nil do
207207
action :rake, 'log:clear'
208208
end
209209
end
210210
end
211211

212212
def test_rails_with_env_option_should_run_rake_command_in_env
213-
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
213+
assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
214214
action :rake, 'log:clear', env: 'production'
215215
end
216216
end
217217

218218
test "rails command with RAILS_ENV variable should run rake command in env" do
219-
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
219+
assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
220220
with_rails_env "production" do
221221
action :rake, 'log:clear'
222222
end
223223
end
224224
end
225225

226226
test "env option should win over RAILS_ENV variable when running rake" do
227-
assert_called_with(generator, :run, ['rails log:clear RAILS_ENV=production', verbose: false]) do
227+
assert_called_with(generator, :run, ['rake log:clear RAILS_ENV=production', verbose: false]) do
228228
with_rails_env "staging" do
229229
action :rake, 'log:clear', env: 'production'
230230
end
231231
end
232232
end
233233

234234
test "rails command with sudo option should run rake command with sudo" do
235-
assert_called_with(generator, :run, ["sudo rails log:clear RAILS_ENV=development", verbose: false]) do
235+
assert_called_with(generator, :run, ["sudo rake log:clear RAILS_ENV=development", verbose: false]) do
236236
with_rails_env nil do
237237
action :rake, 'log:clear', sudo: true
238238
end

0 commit comments

Comments
 (0)