Sensible mechanism for setting default repositories.

This commit is contained in:
Adam Sanderson 2010-11-10 18:56:30 -08:00
parent 39d2b456a7
commit 1668fc3a55
3 changed files with 22 additions and 12 deletions

10
bin/qw
View File

@ -11,12 +11,8 @@ opts = OptionParser.new do |opts|
opts.banner = "Usage: qwandry [options] name" opts.banner = "Usage: qwandry [options] name"
opts.separator "" opts.separator ""
opts.on("-r", "--repo LABEL", "Only search in repository LABEL","Repositories:", *@qwandry.repositories.keys.map{|k| "* #{k}"}) do |label| 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|
if @qwandry.repositories.has_key? label @qwandry.active.replace(labels)
@repository_label = label
else
STDERR.puts "Repository '#{label}' in not available, searching all repositories"
end
end end
opts.separator "" opts.separator ""
@ -40,7 +36,7 @@ end
@qwandry.editor = @editor if @editor @qwandry.editor = @editor if @editor
name = ARGV.pop name = ARGV.pop
packages = @qwandry.find(name, @repository_label) packages = @qwandry.find(name)
package = nil package = nil
case packages.length case packages.length

View File

@ -1,5 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'optparse' require 'optparse'
require 'set'
# Informal Spec: # Informal Spec:
# #

View File

@ -4,16 +4,20 @@ module Qwandry
class Launcher class Launcher
# The default editor to be used by Qwandry#launch. # The default editor to be used by Qwandry#launch.
attr_accessor :editor attr_accessor :editor
# The set of active repositories
attr_reader :active
# Returns the repositories the Launcher will use. # Returns the repositories the Launcher will use.
attr_reader :repositories attr_reader :repositories
def initialize def initialize
@repositories = Hash.new{|h,k| h[k] = []} @repositories = Hash.new{|h,k| h[k] = []}
@active = Set.new
configure_repositories! configure_repositories!
custom_configuration! custom_configuration!
end end
# Adds a repository path to Qwandry's Launcher. # Adds a repository path to Qwandry's Launcher.
# `label` is used to label packages residing in the folder `path`. # `label` is used to label packages residing in the folder `path`.
# The `repository_type` controls the class used to index the `path`. # The `repository_type` controls the class used to index the `path`.
@ -25,12 +29,19 @@ module Qwandry
@repositories[label] << repository_type.new(label, path) @repositories[label] << repository_type.new(label, path)
end end
end end
def activate(*labels)
labels.each{|label| @active.add label.to_s}
end
def deactivate(*labels)
labels.each{|label| @active.delete label.to_s}
end
# Searches all of the loaded repositories for `name` # Searches all of the loaded repositories for `name`
def find(name, required_label=nil) def find(name)
packages = [] packages = []
@repositories.each do |label, repos| @repositories.select{|label,_| @active.include? label }.each do |label, repos|
next if required_label && required_label != label
repos.each do |repo| repos.each do |repo|
packages.concat(repo.scan(name)) packages.concat(repo.scan(name))
end end
@ -61,6 +72,8 @@ module Qwandry
($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path| ($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path|
add :gem, path add :gem, path
end end
activate :ruby, :gem
end end
def custom_configuration! def custom_configuration!