This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sometimes you need to calculate the rails_root from a subdirectory of a rails' app, eg, lib/capistrano/tasks/db.rake | |
# this code does it properly, but is verbose. golf it! | |
# | |
rails_root = File.expand_path(Dir.pwd) | |
until rails_root == '/' | |
if %w[ Gemfile app public ].all?{|entry| test(?e, "#{ rails_root }/#{ entry }")} | |
break | |
else | |
rails_root = File.dirname(rails_root) | |
end | |
rails_root = File.dirname(rails_root) | |
end | |
raise 'no rails_root!' if rails_root == '/' |
I'd feel confident enough if it had Gemfile
and app
dir.
require "pathname"
rails_root = Pathname.pwd.ascend.select do |pn|
File.file?(pn.join("Gemfile")) && File.directory?(pn.join("app"))
end
raise "Not found" if rails_root.nil?
@joelparkerhenderson - oh! Pathname.ascend is teh sweet!
@bradpauly - yes on both 'Gemfile' and 'app' !
based on your awesome suggestions:
Pathname.pwd.ascend{|d| f = d.join('config/application.rb') and f.exist? && f.read.match('Rails') && (break(d))}
# or
Pathname.pwd.ascend{|d| (d.join('config/application.rb').read.match('Rails') && (break(d))) rescue nil}
# or
Pathname.pwd.ascend{|d| (d.join('Gemfile').read.match('rails') && (break(d))) rescue 42}
@ahoward Nice, I like reading config/application.rb
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pathname.pwd.ascend do |x|; (x + "Gemfile").exist? and break x; end or raise "none"