Recipes are silly, undoing some excessive code.

This commit is contained in:
Adam Sanderson 2010-11-10 09:24:23 -08:00
parent aac88bcabc
commit 6c44db6e65
5 changed files with 32 additions and 97 deletions

33
bin/qw
View File

@ -4,14 +4,19 @@ $:.unshift File.dirname(__FILE__) + '/../lib'
# Require it
require "qwandry.rb"
recipes = Qwandry::Recipe.load_recipes
# Create launcher
@qwandry = Qwandry::Launcher.new
opts = OptionParser.new do |opts|
opts.banner = "Usage: qwandry [options] name"
opts.separator ""
opts.on("-r", "--recipe RECIPE", "Use paths from RECIPE","Recipes:", *recipes.map{|r| "* #{r.name}: #{r.description}"}) do |recipe|
@recipe_name = recipe
opts.on("-r", "--repo LABEL", "Only search in repository LABEL","Repositories:", *@qwandry.repositories.keys.map{|k| "* #{k}"}) do |label|
if @qwandry.repositories.has_key? label
@repository_label = label
else
STDERR.puts "Repository '#{label}' in not available, searching all repositories"
end
end
opts.separator ""
@ -31,31 +36,11 @@ if ARGV.length != 1
exit(1)
end
# Create launcher
@qwandry = Qwandry::Launcher.new
# Configure default values
@qwandry.editor = @editor if @editor
# Load recipe
@recipe_name ||= ENV['QW_DEFAULT'] || 'default'
@recipe = recipes.find{|r| r.name.downcase == @recipe_name }
unless @recipe
STDERR.puts "Could not find recipe '#{@recipe_name}'"
exit(1)
end
# Configure Qwandry
@recipe.new.configure(@qwandry)
# Warn if there are no repositories
if @qwandry.repositories.empty?
STDERR.puts "Warning: no repositories were defined in '#{@recipe_path}'"
end
name = ARGV.pop
packages = @qwandry.find(name)
packages = @qwandry.find(name, @repository_label)
package = nil
case packages.length

View File

@ -19,5 +19,4 @@ module Qwandry
autoload :FlatRepository, "qwandry/flat_repository"
autoload :LibraryRepository, "qwandry/library_repository"
autoload :Package, "qwandry/package"
autoload :Recipe, "qwandry/recipe"
end

View File

@ -10,6 +10,7 @@ module Qwandry
def initialize
@repositories = Hash.new{|h,k| h[k] = []}
configure_repositories!
end
# Adds a repository path to Qwandry's Launcher.
@ -25,9 +26,10 @@ module Qwandry
end
# Searches all of the loaded repositories for `name`
def find(name)
def find(name, required_label=nil)
packages = []
@repositories.each do |label, repos|
next if required_label && required_label != label
repos.each do |repo|
packages.concat(repo.scan(name))
end
@ -40,5 +42,25 @@ module Qwandry
editor ||= @editor || ENV['VISUAL'] || ENV['EDITOR']
system editor, *package.paths
end
private
def configure_repositories!
# Get all the paths on ruby's load path:
paths = $:
# Reject binary paths, we only want ruby sources:
paths = paths.reject{|path| path =~ /#{RUBY_PLATFORM}$/}
# Add ruby standard libraries:
paths.grep(/lib\/ruby/).each do |path|
add :ruby, path, Qwandry::LibraryRepository
end
# Add gem repositories:
($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path|
add :gem, path
end
end
end
end

View File

@ -1,44 +0,0 @@
class Qwandry::Recipe
class << self
def name(v=nil)
if v
@name = v
else
@name || self.to_s
end
end
def description(v=nil)
if v
@description = v
else
@description || ""
end
end
end
# Recipes should implement the `configure` method.
def configure(qw)
end
def self.load_recipes
# Load all required recipes:
built_in_path = File.dirname(__FILE__) + '/recipes/*.rb'
custom_path = ENV['HOME'] + '/.qwandry/*.rb' if ENV['HOME']
Dir[built_in_path, custom_path].compact.map do |recipe_path|
require recipe_path
class_name = File.basename(recipe_path,'.rb').split('_').map{|w| w.capitalize}.join
begin
Qwandry.const_get(class_name)
rescue Exception => e
STDERR.puts "Could not load recipe '#{recipe_path}'"
STDERR.puts e
end
end
end
end

View File

@ -1,27 +0,0 @@
# Default recipe, adds ruby sources to Qwandry.
# Ensure that rubygems is on the path so we can search it
require 'rubygems'
class Qwandry::Default < Qwandry::Recipe
name "Default"
description "Searches the ruby standard library and ruby gems"
def configure(qw)
# Get all the paths on ruby's load path:
paths = $:
# Reject binary paths, we only want ruby sources:
paths = paths.reject{|path| path =~ /#{RUBY_PLATFORM}$/}
# Add ruby standard libraries:
paths.grep(/lib\/ruby/).each do |path|
qw.add :ruby, path, Qwandry::LibraryRepository
end
# Add gem repositories:
($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path|
qw.add :gem, path
end
end
end