Created
July 22, 2012 21:53
-
-
Save xaviershay/3161134 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# Given an absolute path to a ruby file and an optional RVM directory, locates | |
# both an `.rvmrc` and a `Gemfile` and runs the script with both. | |
script = ARGV.shift | |
$rvm_path = ARGV.shift.to_s | |
rvm_script = "#{$rvm_path}/scripts/rvm" | |
$rvm_path = nil unless File.exists?(rvm_script) | |
bundler = nil | |
def locate_rvmrc | |
dir = '.' | |
while (dir = File.expand_path(dir)) != '/' do | |
rvmrc = File.join(dir, ".rvmrc") | |
if File.exists?(rvmrc) | |
return rvmrc | |
end | |
dir = File.join dir, '..' | |
end | |
nil | |
end | |
def run_ruby(cmd, exec = false) | |
raise unless $ruby_cmd.length > 0 | |
cmd = "bash -c '#{$ruby_cmd} #{cmd}'" | |
if exec | |
exec(cmd) | |
else | |
out = `#{cmd}` | |
[$?.exitstatus == 0, out] | |
end | |
end | |
cwd = File.dirname(script) | |
Dir.chdir(cwd) do | |
rvmrc = locate_rvmrc if $rvm_path | |
$ruby_cmd = if rvmrc | |
"export rvm_path=#{$rvm_path}; source #{rvm_script}; source #{rvmrc}; ruby" | |
else | |
"/usr/bin/ruby" | |
end | |
if !run_ruby("-S bundle --help")[0] | |
bundler = false | |
else | |
success, out = run_ruby("-S bundle check --no-color") | |
if success | |
bundler = true | |
else | |
if out =~ /Could not locate Gemfile/ | |
bundler = false | |
else | |
success, out = run_ruby("-S bundle --no-color") | |
if success | |
bundler = true | |
else | |
$stderr.puts out | |
exit 1 | |
end | |
end | |
end | |
end | |
cmd = if bundler | |
"-S bundle exec ruby" | |
else | |
"" | |
end | |
cmd += " " + script | |
puts cmd | |
run_ruby(cmd, true) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment