Skip to content

Instantly share code, notes, and snippets.

@lastobelus
Created April 13, 2011 23:18
Show Gist options
  • Save lastobelus/918640 to your computer and use it in GitHub Desktop.
Save lastobelus/918640 to your computer and use it in GitHub Desktop.
rvm compatible editgem in textmate command
#!/usr/bin/env ruby
editor = ENV['EDITOR'] || 'mate'
begin
require 'highline/import'
rescue Exception
puts "\e[33minstalling highline...(#{Process.pid})\e[0m"
require 'rubygems/command.rb'
require 'rubygems/dependency_installer.rb'
inst = Gem::DependencyInstaller.new
inst.install('highline')
pid = fork{exec"editgem #{ARGV.join(' ')}"}
Process.wait(pid)
exit
end
colors = HighLine::ColorScheme.new do |cs|
cs[:list_item] = [:bold, :red]
end
HighLine.color_scheme = colors
gempaths = `rvm gempath`.chomp
gemname = "*#{ARGV.join('*')}*" if ARGV.count > 0
if gemname.nil?
gemname = ask "<%= color('What gem do you want to edit? ', GREEN) %>"
end
gempaths.split(':').reverse.each do |raw_gempath|
gempath = File.join(raw_gempath,'gems')
say "<%= color('looking in ##{gempath} for #{gemname}...', BLACK) %>"
matches = Dir.glob(File.join(gempath, gemname))
if matches.length > 0
if matches.length == 1
exec "#{editor} #{matches.first}"
exit
else
pretty_choices = []
matches.each_with_index do |m, i|
path,gemset = raw_gempath.split('@')
out = "<%= color('#{i+1}. ', :list_item) %>"
out += "<%= color('#{path}', BLACK) %>"
out += "<%= color('@#{gemset}', RED) %>" if gemset
out += "<%= color('/gems/', BLACK) %>"
out += File.basename(m)
pretty_choices << out
end
choose do |menu|
menu.layout = <<-EOPROMPT
<%= color('Which gem do you want to edit:', GREEN) %>
#{pretty_choices.join("\n") }
EOPROMPT
menu.choices(*matches) do |choice|
exec "#{editor} #{choice}"
exit
end
end
end
end
end
say "<%= color('could not find a match for #{gemname} ', RED) %>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment