Skip to content

Instantly share code, notes, and snippets.

@alexsancho
Created March 8, 2012 22:16
Show Gist options
  • Select an option

  • Save alexsancho/2003809 to your computer and use it in GitHub Desktop.

Select an option

Save alexsancho/2003809 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# DESCRIPTION: Tries to do something with unrecognized shell input.
# See the case statement for descriptions and examples.
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
require 'term/ansicolor'
include Term::ANSIColor
# Use all arguments
command = ARGV
# Prints and runs a command.
#
# @param [String, Array] cmd Command to run.
# Automatically joins Arrays with &&.
def run(*cmd)
$stderr.puts "Running '#{cmd.join(' ')}' instead".green
system(*cmd)
end
case command.join(' ')
when /^(?:git|https?):\/\/.+\.git$/
# Clone any full git repo URL.
# @example
# git://github.com/crafterm/jd.git
run "git", "clone", command.first
when /^(?:ftp|https?):\/\/.+\.t(?:ar\.)?gz$/
# Download and unzip a URL
run "wget #{command.first} -O- | tar xzv"
when /^[A-Za-z0-9_\-\/]+\.gem$/
# Install a gem
# @example
# haml.gem
gem_to_install = command.first.gsub(/\.gem$/, '')
run "gem", "install", gem_to_install
when /^[A-Za-z0-9_\-\/]+\.mate$/
# Open the gem in textmate
# @example
# haml.mate
gem_to_open = command.first.gsub(/\.mate$/, '')
run "gem which #{gem_to_open} | tail -1 | xargs dirname | sed -e's/$/\\/../' | xargs mate"
when /^[A-Za-z0-9_\-\/]+\.proj$/
# Open project with textmate or espresso
# @example
# tbb.proj
project = command.first.gsub(/\.proj/, '')
tmproj = `find $HOME/Code -maxdepth 3 -name #{project}.tmproj`
if tmproj.strip.length != 0
run "m #{tmproj}".strip
else
run "find $HOME/Code -type d -maxdepth 3 -name #{project} | xargs mate"
end
else
binfile = File.expand_path(command.first) rescue nil
unless binfile.nil? or File.exist? binfile
qry =`brew search #{command.first}`
if qry.empty?
warn "Error: No matching action for #{command.inspect}".red
else
run "brew", "install", command.first
system "#{command.join(' ')}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment