Made recipes normal classes, less magic.

This commit is contained in:
Adam Sanderson 2010-11-10 00:26:26 -08:00
parent 22ec2aa217
commit 4eaedbad12
6 changed files with 79 additions and 31 deletions

2
TODO
View File

@ -1,3 +1,3 @@
- Differentiate multiple similar matches (show path or repository label) - Differentiate multiple similar matches (show path or repository label)
- Recognize file idioms such as './file.rb' and matching "./file/" @done - Recognize file idioms such as './file.rb' and matching "./file/" @done
- Custom recipes - Custom recipes @done

34
bin/qw
View File

@ -4,24 +4,17 @@ $:.unshift File.dirname(__FILE__) + '/../lib'
# Require it # Require it
require "qwandry.rb" require "qwandry.rb"
recipe_paths = Dir[ recipes = Qwandry::Recipe.load_recipes
(ENV['HOME']||'')+'/.qwandry/*.rb', # custom recipes
File.dirname(__FILE__)+'/../lib/qwandry/recipes/*.rb', # built in recipes
]
opts = OptionParser.new do |opts| opts = OptionParser.new do |opts|
opts.banner = "Usage: qwandry [options] name" opts.banner = "Usage: qwandry [options] name"
opts.separator "" opts.separator ""
recipe_names = recipe_paths.map{|path| File.basename(path,'.rb')}.uniq opts.on("-r", "--recipe RECIPE", "Use paths from RECIPE","Recipes:", *recipes.map{|r| "* #{r.name}: #{r.description}"}) do |recipe|
opts.on("-r", "--recipe RECIPE", "Use paths from RECIPE","Recipes:", *recipe_names.map{|k| "* #{k}"}) do |recipe| @recipe_name = recipe
if recipe_names.include?(recipe)
@recipe = recipe
else
STDERR.puts "Recipe '#{recipe}' is not available, using default."
end
end end
opts.separator ""
opts.on("-e", "--editor EDITOR", "Use EDITOR to open the package") do |editor| opts.on("-e", "--editor EDITOR", "Use EDITOR to open the package") do |editor|
@editor = editor @editor = editor
end end
@ -45,14 +38,21 @@ end
@qwandry.editor = @editor if @editor @qwandry.editor = @editor if @editor
# Load recipe # Load recipe
@recipe ||= 'default' @recipe_name ||= ENV['QW_DEFAULT'] || 'default'
@recipe_path = recipe_paths.find{|p| File.basename(p,'.rb') == @recipe } @recipe = recipes.find{|r| r.name.downcase == @recipe_name }
unless @recipe_path
STDERR.puts "Could not find recipe '#{@recipe}'" unless @recipe
STDERR.puts "Could not find recipe '#{@recipe_name}'"
exit(1) exit(1)
end end
# Configure @qwandry
@qwandry.instance_eval(File.read(@recipe_path) ) # 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 name = ARGV.pop
packages = @qwandry.find(name) packages = @qwandry.find(name)

View File

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

View File

@ -16,8 +16,12 @@ module Qwandry
# `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`.
def add(label, path, repository_type=Qwandry::FlatRepository) def add(label, path, repository_type=Qwandry::FlatRepository)
label = label.to_s if path.is_a?(Array)
@repositories[label] << repository_type.new(label, path) path.each{|p| add label, p, repository_type}
else
label = label.to_s
@repositories[label] << repository_type.new(label, path)
end
end end
# Searches all of the loaded repositories for `name` # Searches all of the loaded repositories for `name`

36
lib/qwandry/recipe.rb Normal file
View File

@ -0,0 +1,36 @@
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:
Dir[File.dirname(__FILE__) + '/recipes/*.rb'].map do |recipe_path|
require recipe_path
class_name = File.basename(recipe_path,'.rb').split('_').map{|w| w.capitalize}.join
Qwandry.const_get(class_name)
end
end
end

View File

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