Created
July 16, 2010 00:38
-
-
Save filterfish/477760 to your computer and use it in GitHub Desktop.
Lightweigh version of rvm.
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 | |
require 'pp' | |
require 'pathname' | |
search_paths = ['/usr/local'] | |
Conf_file = "#{ENV['HOME']}/.lrvm" | |
def write_path(path) | |
File.open(Conf_file, 'w') do |fd| | |
fd.write(path.to_s) | |
end | |
end | |
def full_path | |
path = (File.exist?(Conf_file)) ? "#{File.read(Conf_file)}:#{ENV['PATH']}" : ENV['PATH'] | |
path.split(/:/).uniq.join(":") | |
end | |
def default_ruby_path | |
ENV['PATH'].split(/:/).map { |p| Pathname.new(p) }.detect do |path| | |
path.join("ruby").exist? unless path.fnmatch("*ruby*") | |
end.to_s | |
end | |
def find_ruby_paths(paths) | |
paths.inject([]) do |all_dirs, path| | |
all_dirs.tap do |_all_dirs| | |
_all_dirs << Pathname.new(path).entries.inject([]) do |dirs,dir| | |
dirs.tap do |_dirs| | |
_dirs << dir.expand_path(path).join("bin") if dir.fnmatch("*ruby*") | |
end | |
end | |
end | |
end.flatten | |
end | |
ruby_paths = find_ruby_paths(search_paths) | |
ruby_paths.insert(0, default_ruby_path) | |
ruby_paths.each_with_index do |path,n| | |
STDERR.puts "[%i] %s" % [n, path] | |
end | |
STDIN.each do |input| | |
index = Integer(input.strip) rescue nil | |
if index && index < ruby_paths.size | |
write_path(ruby_paths[index]) | |
puts "PATH=#{full_path}" | |
exit | |
else | |
puts "Please enter a number between 0 and #{ruby_paths.size - 1}" | |
end | |
end | |
STDERR.puts "Ok doing nothing as asked" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I personally like to install ruby versions system wide so this script looks at what versions are installed (it currently looks in /usr/local/bin but it's easy to add more) and prints the different options for you to select. It adds the path to $HOME/.lrvm so you can use it for other purposes such as adding the version to your prompt. You can then eval the output into your current environment:
You can add this to your shell config file (assumes sh compatible):
To add the version of ruby to your prompt (zsh):