Skip to content

Commit 4817e7d

Browse files
committedMar 1, 2025
Default to using spring when running commands and tasks with bin/rails
Previously, bin/rails only used spring for a subset of commands and rake tasks. However, most rake tasks can benefit from spring and do use spring when using bin/rake. This PR makes rake tasks run with bin/rails use spring, and introduces and opt-out system for certain Rails commands that should not use spring (like the server) instead of the previous opt-in system.
1 parent 1821287 commit 4817e7d

File tree

4 files changed

+49
-133
lines changed

4 files changed

+49
-133
lines changed
 

‎lib/spring/client/rails.rb

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
module Spring
22
module Client
33
class Rails < Command
4-
COMMANDS = %w(console runner generate destroy test)
4+
SPECIAL_COMMANDS = %w(test)
5+
EXCLUDED_COMMANDS = %w(server)
56

67
ALIASES = {
7-
"c" => "console",
8-
"r" => "runner",
9-
"g" => "generate",
10-
"d" => "destroy",
11-
"t" => "test"
8+
"t" => "test",
9+
"s" => "server"
1210
}
1311

1412
def self.description
@@ -18,15 +16,15 @@ def self.description
1816
def call
1917
command_name = ALIASES[args[1]] || args[1]
2018

21-
if COMMANDS.include?(command_name)
22-
Run.call(["rails_#{command_name}", *args.drop(2)])
23-
elsif command_name&.start_with?("db:") && !command_name.start_with?("db:system")
24-
Run.call(["rake", *args.drop(1)])
25-
else
19+
if SPECIAL_COMMANDS.include?(command_name)
20+
Run.call(["rails_#{command_name}", *args.drop(1)])
21+
elsif EXCLUDED_COMMANDS.include?(command_name)
2622
require "spring/configuration"
2723
ARGV.shift
2824
load Dir.glob(Spring.application_root_path.join("{bin,script}/rails")).first
2925
exit
26+
else
27+
Run.call(["rails", *args.drop(1)])
3028
end
3129
end
3230
end

‎lib/spring/commands/rails.rb

+7-83
Original file line numberDiff line numberDiff line change
@@ -2,111 +2,35 @@ module Spring
22
module Commands
33
class Rails
44
def call
5-
ARGV.unshift command_name
65
load Dir.glob(::Rails.root.join("{bin,script}/rails")).first
76
end
87

9-
def description
10-
nil
11-
end
12-
end
13-
14-
class RailsConsole < Rails
158
def env(args)
16-
return args.first if args.first && !args.first.index("-")
17-
189
environment = nil
1910

2011
args.each.with_index do |arg, i|
21-
if arg =~ /--environment=(\w+)/
22-
environment = $1
23-
elsif i > 0 && args[i - 1] == "-e"
12+
if arg =~ /(-e|--environment)=(\w+)/
13+
environment = $2
14+
elsif i > 0 && %w[-e --environment].include?(args[i - 1])
2415
environment = arg
2516
end
2617
end
2718

2819
environment
2920
end
3021

31-
def command_name
32-
"console"
33-
end
34-
end
35-
36-
class RailsGenerate < Rails
37-
def command_name
38-
"generate"
39-
end
40-
end
41-
42-
class RailsDestroy < Rails
43-
def command_name
44-
"destroy"
45-
end
46-
end
47-
48-
class RailsRunner < Rails
49-
def call
50-
ARGV.replace extract_environment(ARGV).first
51-
super
52-
end
53-
54-
def env(args)
55-
extract_environment(args).last
56-
end
57-
58-
def command_name
59-
"runner"
60-
end
61-
62-
def extract_environment(args)
63-
environment = nil
64-
65-
args = args.select.with_index { |arg, i|
66-
case arg
67-
when "-e"
68-
false
69-
when /--environment=(\w+)/
70-
environment = $1
71-
false
72-
else
73-
if i > 0 && args[i - 1] == "-e"
74-
environment = arg
75-
false
76-
else
77-
true
78-
end
79-
end
80-
}
81-
82-
[args, environment]
22+
def description
23+
nil
8324
end
8425
end
8526

8627
class RailsTest < Rails
8728
def env(args)
88-
environment = "test"
89-
90-
args.each.with_index do |arg, i|
91-
if arg =~ /--environment=(\w+)/
92-
environment = $1
93-
elsif i > 0 && args[i - 1] == "-e"
94-
environment = arg
95-
end
96-
end
97-
98-
environment
99-
end
100-
101-
def command_name
102-
"test"
29+
super || "test"
10330
end
10431
end
10532

106-
Spring.register_command "rails_console", RailsConsole.new
107-
Spring.register_command "rails_generate", RailsGenerate.new
108-
Spring.register_command "rails_destroy", RailsDestroy.new
109-
Spring.register_command "rails_runner", RailsRunner.new
33+
Spring.register_command "rails", Rails.new
11034
Spring.register_command "rails_test", RailsTest.new
11135
end
11236
end

‎test/support/acceptance_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,18 @@ class MyEngine < Rails::Engine
740740

741741
assert_failure app.spring_test_command, stderr: "omg (RuntimeError)"
742742
end
743+
744+
test "speeds up rake tasks with rails commands" do
745+
File.write(app.path("lib/tasks/env.rake"), <<-RUBY.strip_heredoc)
746+
namespace :foo do
747+
task :bar => :environment
748+
end
749+
RUBY
750+
751+
assert_speedup do
752+
2.times { app.run "bin/rails foo:bar" }
753+
end
754+
end
743755
end
744756
end
745757
end

‎test/unit/commands_test.rb

+21-39
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,31 @@
22
require "spring/commands"
33

44
class CommandsTest < ActiveSupport::TestCase
5-
test 'console command sets rails environment from command-line option' do
6-
command = Spring::Commands::RailsConsole.new
7-
assert_equal 'test', command.env(['test'])
8-
end
9-
10-
test 'console command sets rails environment from -e option' do
11-
command = Spring::Commands::RailsConsole.new
5+
test 'rails command sets rails environment from -e option' do
6+
command = Spring::Commands::Rails.new
127
assert_equal 'test', command.env(['-e', 'test'])
8+
assert_equal 'test', command.env(['-e=test'])
139
end
1410

15-
test 'console command sets rails environment from --environment option' do
16-
command = Spring::Commands::RailsConsole.new
11+
test 'rails command sets rails environment from --environment option' do
12+
command = Spring::Commands::Rails.new
13+
assert_equal 'test', command.env(['--environment', 'test'])
1714
assert_equal 'test', command.env(['--environment=test'])
1815
end
1916

20-
test 'console command ignores first argument if it is a flag except -e and --environment' do
21-
command = Spring::Commands::RailsConsole.new
17+
test 'rails command ignores first argument if it is a flag except -e and --environment' do
18+
command = Spring::Commands::Rails.new
2219
assert_nil command.env(['--sandbox'])
2320
end
2421

25-
test 'Runner#env sets rails environment from command-line option' do
26-
command = Spring::Commands::RailsRunner.new
27-
assert_equal 'test', command.env(['-e', 'test', 'puts 1+1'])
28-
end
29-
30-
test 'RailsRunner#env sets rails environment from long form of command-line option' do
31-
command = Spring::Commands::RailsRunner.new
32-
assert_equal 'test', command.env(['--environment=test', 'puts 1+1'])
22+
test 'rails command uses last environment option' do
23+
command = Spring::Commands::Rails.new
24+
assert_equal 'development', command.env(['-e', 'test', '--environment=development'])
3325
end
3426

35-
test 'RailsRunner#env ignores insignificant arguments' do
36-
command = Spring::Commands::RailsRunner.new
37-
assert_nil command.env(['puts 1+1'])
38-
end
39-
40-
test 'RailsRunner#extract_environment removes -e <env>' do
41-
command = Spring::Commands::RailsRunner.new
42-
args = ['-b', '-a', '-e', 'test', '-r']
43-
assert_equal [['-b', '-a', '-r'], 'test'], command.extract_environment(args)
44-
end
45-
46-
test 'RailsRunner#extract_environment removes --environment=<env>' do
47-
command = Spring::Commands::RailsRunner.new
48-
args = ['-b', '--environment=test', '-a', '-r']
49-
assert_equal [['-b', '-a', '-r'], 'test'], command.extract_environment(args)
27+
test 'rails command ignores additional arguments' do
28+
command = Spring::Commands::Rails.new
29+
assert_equal 'test', command.env(['-e', 'test', 'puts 1+1'])
5030
end
5131

5232
test "rake command has configurable environments" do
@@ -57,18 +37,20 @@ class CommandsTest < ActiveSupport::TestCase
5737
assert_nil command.env(["test_foo"])
5838
end
5939

60-
test 'RailsTest#command defaults to test rails environment' do
40+
test 'RailsTest#env defaults to test rails environment' do
6141
command = Spring::Commands::RailsTest.new
6242
assert_equal 'test', command.env([])
6343
end
6444

65-
test 'RailsTest#command sets rails environment from --environment option' do
45+
test 'RailsTest#env sets rails environment from --environment option' do
6646
command = Spring::Commands::RailsTest.new
67-
assert_equal 'foo', command.env(['--environment=foo'])
47+
assert_equal 'development', command.env(['--environment', 'development'])
48+
assert_equal 'development', command.env(['--environment=development'])
6849
end
6950

70-
test 'RailsTest#command sets rails environment from -e option' do
51+
test 'RailsTest#env sets rails environment from -e option' do
7152
command = Spring::Commands::RailsTest.new
72-
assert_equal 'foo', command.env(['-e', 'foo'])
53+
assert_equal 'development', command.env(['-e', 'development'])
54+
assert_equal 'development', command.env(['-e=development'])
7355
end
7456
end

0 commit comments

Comments
 (0)
Failed to load comments.