Skip to content

Instantly share code, notes, and snippets.

@alltom
Created January 30, 2011 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alltom/803281 to your computer and use it in GitHub Desktop.
Save alltom/803281 to your computer and use it in GitHub Desktop.
oo
#!/usr/bin/env ruby
# context-sensitive "open" alternative
# just go into a directory and run "oo"
# the correct project editor be used to open it
# usage:
# $ oo guess what to do with current directory
# $ oo [file or directory] guess what to do with the given file/directory
# from: http://alltom.com/pages/oo
# gist: https://gist.github.com/803281
@file = "."
# if a filename was given, let's operate from there
# instead of the current directory
if ARGV.first
unless File.exist?(ARGV.first)
puts "\"#{ARGV.first}\" doesn't exist"
exit 1
end
# if it's a file, operate on that
# otherwise, operate on that directory
if File.file?(ARGV.first)
Dir.chdir(File.dirname ARGV.first)
@file = File.basename ARGV.first
else
Dir.chdir(ARGV.first)
end
end
# does this glob match anything in the directory?
def has(glob)
Dir[glob].length > 0
end
# is @file included in the glob?
# (I'd check the glob against the string directly, but I don't know how)
def is(glob)
Dir[glob].include? @file
end
dir_matchers = [
{ :matcher => lambda { has "*.xcodeproj" }, :open => "*.xcodeproj" },
{ :matcher => lambda { has "*.pbxproj" }, :open => "." }, # inside *.xcodeproj dir
{ :matcher => lambda { has "*.fla" }, :open => "*.fla" },
{ :matcher => lambda { has "app" }, :cmd => "mate" }, # rails
{ :matcher => lambda { true }, :cmd => "mate" } # default
]
file_matchers = [
{ :matcher => lambda { is "*.fla" }, :cmd => "open" },
{ :matcher => lambda { true }, :cmd => "mate" } # default
]
(@file == "." ? dir_matchers : file_matchers).each do |m|
if m[:matcher][]
exec(m[:cmd] || "open",
m[:open] ? Dir[m[:open]].first : @file)
exit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment