qwandry/bin/qw

87 lines
2.1 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# Add qwandry's library to the load path
$:.unshift File.dirname(__FILE__) + '/../lib'
# Require it
require "qwandry.rb"
# Create launcher
@qwandry = Qwandry::Launcher.new
opts = OptionParser.new do |opts|
opts.banner = "Usage: qwandry [options] name"
opts.separator ""
opts.on("-r", "--repo LABELS", Array, "Search in LABELS, default: #{@qwandry.active.to_a.join(',')}","Available Repositories:", *@qwandry.repositories.keys.map{|k| " #{k}"}) do |labels|
@qwandry.active.replace(labels)
end
opts.separator ""
opts.on("-e", "--editor EDITOR", "Use EDITOR to open the package") do |editor|
@editor = editor
end
opts.separator "Additional Commands"
opts.on("--paths", "Prints all repositories and their paths") do
@qwandry.repositories.each do |label, entries|
puts "#{label} #{"[default]" if @qwandry.active.include? label}"
entries.each do |repo|
puts "\t#{repo.path} (#{repo.class.to_s.split('::').last})"
end
puts ""
end
exit(0)
end
opts.on("--customize", "Create and edit files for customizing Qwandry") do
dir = Qwandry.config_dir
if !dir
puts "HOME directory must be defined."
exit(1)
else
FileUtils.mkdir(dir, :verbose=>true) unless File.exist?(dir)
Dir[File.dirname(__FILE__) + '/../templates/*'].each do |path|
FileUtils.cp(path, dir, :verbose=>true, :preserve=>true)
end
@qwandry.launch dir
end
exit(0)
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
opts.parse! ARGV
if ARGV.length == 0
puts opts
exit(1)
end
# Configure default values
@qwandry.editor = @editor if @editor
name = ARGV.join(' ')
packages = @qwandry.find(*ARGV)
ARGV.clear # for the gets below
package = nil
case packages.length
when 0
puts "No packages matched '#{name}'"
exit 404 # Package not found -- hehe, super lame.
when 1
package = packages.first
else
packages.each_with_index do |package, index|
puts "%3d. %s" % [index+1, package.name]
end
print ">> "
index = gets.to_i - 1
package = packages[index]
end
@qwandry.launch(package) if package