From 6c44db6e65278aee429917fa67dc0313bae1ec1c Mon Sep 17 00:00:00 2001 From: Adam Sanderson Date: Wed, 10 Nov 2010 09:24:23 -0800 Subject: [PATCH] Recipes are silly, undoing some excessive code. --- bin/qw | 33 +++++++------------------ lib/qwandry.rb | 1 - lib/qwandry/launcher.rb | 24 ++++++++++++++++++- lib/qwandry/recipe.rb | 44 ---------------------------------- lib/qwandry/recipes/default.rb | 27 --------------------- 5 files changed, 32 insertions(+), 97 deletions(-) delete mode 100644 lib/qwandry/recipe.rb delete mode 100644 lib/qwandry/recipes/default.rb diff --git a/bin/qw b/bin/qw index 5e21060..8bf1463 100755 --- a/bin/qw +++ b/bin/qw @@ -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 diff --git a/lib/qwandry.rb b/lib/qwandry.rb index 1d1084c..5a6dd47 100644 --- a/lib/qwandry.rb +++ b/lib/qwandry.rb @@ -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 \ No newline at end of file diff --git a/lib/qwandry/launcher.rb b/lib/qwandry/launcher.rb index 9e85e1d..852c70b 100644 --- a/lib/qwandry/launcher.rb +++ b/lib/qwandry/launcher.rb @@ -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 \ No newline at end of file diff --git a/lib/qwandry/recipe.rb b/lib/qwandry/recipe.rb deleted file mode 100644 index 5e6509b..0000000 --- a/lib/qwandry/recipe.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/lib/qwandry/recipes/default.rb b/lib/qwandry/recipes/default.rb deleted file mode 100644 index e0629ff..0000000 --- a/lib/qwandry/recipes/default.rb +++ /dev/null @@ -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 \ No newline at end of file