Made recipes normal classes, less magic.
This commit is contained in:
parent
22ec2aa217
commit
4eaedbad12
2
TODO
2
TODO
|
@ -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
34
bin/qw
|
@ -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)
|
||||||
|
|
|
@ -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
|
|
@ -16,9 +16,13 @@ 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)
|
||||||
|
if path.is_a?(Array)
|
||||||
|
path.each{|p| add label, p, repository_type}
|
||||||
|
else
|
||||||
label = label.to_s
|
label = label.to_s
|
||||||
@repositories[label] << repository_type.new(label, path)
|
@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`
|
||||||
def find(name)
|
def find(name)
|
||||||
|
|
|
@ -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
|
|
@ -3,6 +3,11 @@
|
||||||
# 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'
|
||||||
|
|
||||||
|
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:
|
# Get all the paths on ruby's load path:
|
||||||
paths = $:
|
paths = $:
|
||||||
|
|
||||||
|
@ -11,10 +16,12 @@ paths = paths.reject{|path| path =~ /#{RUBY_PLATFORM}$/}
|
||||||
|
|
||||||
# Add ruby standard libraries:
|
# Add ruby standard libraries:
|
||||||
paths.grep(/lib\/ruby/).each do |path|
|
paths.grep(/lib\/ruby/).each do |path|
|
||||||
add :ruby, path, Qwandry::LibraryRepository
|
qw.add :ruby, path, Qwandry::LibraryRepository
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add gem repositories:
|
# Add gem repositories:
|
||||||
($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path|
|
($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path|
|
||||||
add :gem, path
|
qw.add :gem, path
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue