Skip to content

Commit dcc758a

Browse files
chadDavid Heinemeier Hansson
authored andcommitted
detect being inside a rails application even from a subdirectory
Signed-off-by: David Heinemeier Hansson <[email protected]>
1 parent 330a890 commit dcc758a

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

railties/CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
*Rails 3.0.0 [Release Candidate] (unreleased)*
2+
3+
* Made the rails command work even when you're in a subdirectory [Chad Fowler]
4+
5+
16
*Rails 3.0.0 [beta 4] (June 8th, 2010)*
27

38
* Version bump

railties/lib/rails/cli.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
require 'rbconfig'
2-
3-
module Rails
4-
module ScriptRailsLoader
5-
RUBY = File.join(*RbConfig::CONFIG.values_at("bindir", "ruby_install_name")) + RbConfig::CONFIG["EXEEXT"]
6-
SCRIPT_RAILS = File.join('script', 'rails')
7-
8-
def self.exec_script_rails!
9-
cwd = Dir.pwd
10-
exec RUBY, SCRIPT_RAILS, *ARGV if File.exists?(SCRIPT_RAILS)
11-
Dir.chdir("..") do
12-
# Recurse in a chdir block: if the search fails we want to be sure
13-
# the application is generated in the original working directory.
14-
exec_script_rails! unless cwd == Dir.pwd
15-
end
16-
rescue SystemCallError
17-
# could not chdir, no problem just return
18-
end
19-
end
20-
end
2+
require 'rails/script_rails_loader'
213

224
Rails::ScriptRailsLoader.exec_script_rails!
235

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'pathname'
2+
3+
module Rails
4+
module ScriptRailsLoader
5+
RUBY = File.join(*RbConfig::CONFIG.values_at("bindir", "ruby_install_name")) + RbConfig::CONFIG["EXEEXT"]
6+
SCRIPT_RAILS = File.join('script', 'rails')
7+
8+
def self.exec_script_rails!
9+
cwd = Dir.pwd
10+
exec RUBY, SCRIPT_RAILS, *ARGV if in_rails_application?
11+
Dir.chdir("..") do
12+
# Recurse in a chdir block: if the search fails we want to be sure
13+
# the application is generated in the original working directory.
14+
exec_script_rails! unless cwd == Dir.pwd
15+
end
16+
rescue SystemCallError
17+
# could not chdir, no problem just return
18+
end
19+
20+
def self.in_rails_application?
21+
File.exists?(SCRIPT_RAILS) || in_rails_application_subdirectory?
22+
end
23+
24+
def self.in_rails_application_subdirectory?(path = Pathname.new(Dir.pwd))
25+
File.exists?(File.join(path, SCRIPT_RAILS)) || !path.root? && in_rails_application_subdirectory?(path.parent)
26+
end
27+
end
28+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'abstract_unit'
2+
require 'rails/script_rails_loader'
3+
4+
class ScriptRailsLoaderTest < ActiveSupport::TestCase
5+
6+
test "is in a rails application if script/rails exists" do
7+
File.stubs(:exists?).returns(true)
8+
assert Rails::ScriptRailsLoader.in_rails_application?
9+
end
10+
11+
test "is in a rails application if parent directory has script/rails" do
12+
File.stubs(:exists?).with("/foo/bar/script/rails").returns(false)
13+
File.stubs(:exists?).with("/foo/script/rails").returns(true)
14+
assert Rails::ScriptRailsLoader.in_rails_application_subdirectory?(Pathname.new("/foo/bar"))
15+
end
16+
17+
test "is not in a rails application if at the root directory and doesn't have script/rails" do
18+
Pathname.any_instance.stubs(:root?).returns true
19+
assert !Rails::ScriptRailsLoader.in_rails_application?
20+
end
21+
22+
end

0 commit comments

Comments
 (0)