#!/usr/bin/env ruby require 'thor' $:.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'textmate' class TextmateInstaller < Thor desc "search [SEARCH]", "Lists all the matching remote bundles" def search(search = "") display_hash remote.bundles(search) end desc "list [SEARCH]", "lists all the bundles installed locally" def list(search = "") display_hash local.bundles(search) end desc "install NAME [SOURCE]", "install a bundle" def install(bundle) remotes = remote.bundles(bundle) case remotes.values.flatten.length when 0 then abort("Unknown bundle: #{bundle}") when 1 then local.install(bundle, remotes.keys.detect { |k| remotes[k].length == 1 }) else local.install(*select_remote(remotes)) end reload end desc "uninstall NAME", "uninstall a bundle" def uninstall(bundle) puts "Removing bundle..." local.uninstall(bundle) reload end desc "reload", "Reloads TextMate Bundles" def reload puts "Reloading bundles..." %x{ osascript -e 'tell app "TextMate" to reload bundles' } end end def local @local ||= Textmate::Local.new end def remote @remote ||= Textmate::Remote.new end def display_hash(hash) puts hash.keys.sort.each do |key| value = hash[key] key = key.name if key.respond_to?(:name) puts key puts '-' * key.length case value when Array then puts value.join("\n") unless value.empty? else puts value end puts end end def select_remote(remotes) puts "Please select a bundle from the following list:" puts bundle_index = {} remotes.keys.sort.inject(0) do |index, remote| bundles = remotes[remote] puts remote.name puts '-' * remote.name.length bundles.each do |bundle| bundle_index[index += 1] = [bundle, remote] puts "#{index}. #{bundle}" end puts index end index = ask('Which bundle would you like to install?').to_i puts abort("Unknown option: #{index}") unless bundle_index[index] bundle_index[index] end TextmateInstaller.start