Skip to content

Commit 5a9473f

Browse files
committed
Revert "Merge pull request #733 from Shopify/default-to-using-spring-with-rails"
This reverts commit 4126e5e, reversing changes made to 046d50a.
1 parent 4126e5e commit 5a9473f

File tree

4 files changed

+133
-49
lines changed

4 files changed

+133
-49
lines changed

lib/spring/client/rails.rb

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

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

1214
def self.description
@@ -16,15 +18,15 @@ def self.description
1618
def call
1719
command_name = ALIASES[args[1]] || args[1]
1820

19-
if SPECIAL_COMMANDS.include?(command_name)
20-
Run.call(["rails_#{command_name}", *args.drop(1)])
21-
elsif EXCLUDED_COMMANDS.include?(command_name)
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
2226
require "spring/configuration"
2327
ARGV.shift
2428
load Dir.glob(Spring.application_root_path.join("{bin,script}/rails")).first
2529
exit
26-
else
27-
Run.call(["rails", *args.drop(1)])
2830
end
2931
end
3032
end

lib/spring/commands/rails.rb

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

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

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

1928
environment
2029
end
2130

22-
def description
23-
nil
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]
2483
end
2584
end
2685

2786
class RailsTest < Rails
2887
def env(args)
29-
super || "test"
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"
30103
end
31104
end
32105

33-
Spring.register_command "rails", Rails.new
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
34110
Spring.register_command "rails_test", RailsTest.new
35111
end
36112
end

test/support/acceptance_test.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -740,18 +740,6 @@ 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
755743
end
756744
end
757745
end

test/unit/commands_test.rb

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,51 @@
22
require "spring/commands"
33

44
class CommandsTest < ActiveSupport::TestCase
5-
test 'rails command sets rails environment from -e option' do
6-
command = Spring::Commands::Rails.new
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
712
assert_equal 'test', command.env(['-e', 'test'])
8-
assert_equal 'test', command.env(['-e=test'])
913
end
1014

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'])
15+
test 'console command sets rails environment from --environment option' do
16+
command = Spring::Commands::RailsConsole.new
1417
assert_equal 'test', command.env(['--environment=test'])
1518
end
1619

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

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'])
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'])
2528
end
2629

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'])
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'])
33+
end
34+
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)
3050
end
3151

3252
test "rake command has configurable environments" do
@@ -37,20 +57,18 @@ class CommandsTest < ActiveSupport::TestCase
3757
assert_nil command.env(["test_foo"])
3858
end
3959

40-
test 'RailsTest#env defaults to test rails environment' do
60+
test 'RailsTest#command defaults to test rails environment' do
4161
command = Spring::Commands::RailsTest.new
4262
assert_equal 'test', command.env([])
4363
end
4464

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

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

0 commit comments

Comments
 (0)