From 22ec2aa2177fafa5056d2c15e6086b1d710e5bac Mon Sep 17 00:00:00 2001 From: Adam Sanderson Date: Mon, 8 Nov 2010 22:31:09 -0800 Subject: [PATCH] Adding recipes --- README | 8 ------- README.markdown | 31 +++++++++++++++++++++++++ TODO | 3 ++- bin/qw | 42 ++++++++++++++++++++++++---------- lib/qwandry/launcher.rb | 24 +------------------ lib/qwandry/recipes/default.rb | 20 ++++++++++++++++ 6 files changed, 84 insertions(+), 44 deletions(-) delete mode 100644 README create mode 100644 README.markdown create mode 100644 lib/qwandry/recipes/default.rb diff --git a/README b/README deleted file mode 100644 index eb95fce..0000000 --- a/README +++ /dev/null @@ -1,8 +0,0 @@ -Qwandry, a questionable tool -============================= -Why spend time trying to remember where libraries, projects, and packages are lurking, -when your computer already knows? - - qw activerecord # open the activerecord gem - qw matrix # open the ruby standard library matrix file - diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..e06a42f --- /dev/null +++ b/README.markdown @@ -0,0 +1,31 @@ +Qwandry, a questionable tool +============================= + +Why spend time trying to remember where libraries, projects, and packages are +lurking when your computer can do it faster? + + qw activerecord # open the activerecord gem + qw matrix # open the ruby standard library matrix file + +Installation +------------ +Qwandry is a standard ruby gem, on any system with ruby installed already +simply install with: + + gem install qwandry + +The gem will install the most recent version of Qwandry, and add the `qw` +script to your `PATH`. + +Usage +----- +Just type `qw` and the first few letters of what you're looking for. By +default Qwandry is set up for locating and editing ruby libraries. + + qw date # opens ruby's date library + +Contact +------- +Adam Sanderson, netghost@gmail.com + +Issues and Source: https://github.com/adamsanderson/qwandry \ No newline at end of file diff --git a/TODO b/TODO index 2d50e69..d9897bd 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,3 @@ - Differentiate multiple similar matches (show path or repository label) -- Recognize file idioms such as './file.rb' and matching "./file/" @done \ No newline at end of file +- Recognize file idioms such as './file.rb' and matching "./file/" @done +- Custom recipes \ No newline at end of file diff --git a/bin/qw b/bin/qw index e893a58..6747ade 100755 --- a/bin/qw +++ b/bin/qw @@ -1,26 +1,29 @@ #!/usr/bin/env ruby # Add qwandry's library to the load path -$: << File.dirname(__FILE__) + '/../lib' +$:.unshift File.dirname(__FILE__) + '/../lib' # Require it -require "qwandry" +require "qwandry.rb" -@qwandry = Qwandry::Launcher.new -load('~/.qwandry/repositories.rb') if File.exists?('~/.qwandry/repositories.rb') +recipe_paths = Dir[ + (ENV['HOME']||'')+'/.qwandry/*.rb', # custom recipes + File.dirname(__FILE__)+'/../lib/qwandry/recipes/*.rb', # built in recipes +] opts = OptionParser.new do |opts| opts.banner = "Usage: qwandry [options] name" opts.separator "" - 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 + recipe_names = recipe_paths.map{|path| File.basename(path,'.rb')}.uniq + opts.on("-r", "--recipe RECIPE", "Use paths from RECIPE","Recipes:", *recipe_names.map{|k| "* #{k}"}) do |recipe| + if recipe_names.include?(recipe) + @recipe = recipe else - STDERR.puts "Repository '#{label}' in not available, searching all repositories" + STDERR.puts "Recipe '#{recipe}' is not available, using default." end end - + opts.on("-e", "--editor EDITOR", "Use EDITOR to open the package") do |editor| - @qwandry.editor = editor + @editor = editor end opts.on_tail("-h", "--help", "Show this message") do @@ -30,14 +33,29 @@ opts = OptionParser.new do |opts| end opts.parse! ARGV - if ARGV.length != 1 puts opts exit(1) end +# Create launcher +@qwandry = Qwandry::Launcher.new + +# Configure default values +@qwandry.editor = @editor if @editor + +# Load recipe +@recipe ||= 'default' +@recipe_path = recipe_paths.find{|p| File.basename(p,'.rb') == @recipe } +unless @recipe_path + STDERR.puts "Could not find recipe '#{@recipe}'" + exit(1) +end +# Configure @qwandry +@qwandry.instance_eval(File.read(@recipe_path) ) + name = ARGV.pop -packages = @qwandry.find(name,@repository_label) +packages = @qwandry.find(name) package = nil case packages.length diff --git a/lib/qwandry/launcher.rb b/lib/qwandry/launcher.rb index 0fb4196..177713c 100644 --- a/lib/qwandry/launcher.rb +++ b/lib/qwandry/launcher.rb @@ -10,8 +10,6 @@ module Qwandry def initialize @repositories = Hash.new{|h,k| h[k] = []} - add_ruby_repositories - add_gem_repositories end # Adds a repository path to Qwandry's Launcher. @@ -23,10 +21,9 @@ module Qwandry end # Searches all of the loaded repositories for `name` - def find(name,repository_label=nil) + def find(name) packages = [] @repositories.each do |label, repos| - next if repository_label && repository_label != label repos.each do |repo| packages.concat(repo.scan(name)) end @@ -39,24 +36,5 @@ module Qwandry editor ||= @editor || ENV['VISUAL'] || ENV['EDITOR'] system editor, *package.paths end - - private - # Add ruby standard libraries, ignore the platform specific ones since they - # tend to contain only binaries - def add_ruby_repositories - ($:).grep(/lib\/ruby/).reject{|path| path =~ /#{RUBY_PLATFORM}$/}.each do |path| - add :ruby, path, Qwandry::LibraryRepository - end - end - - # Add gem repositories: - # Using the ruby load paths, determine the common gem root paths, and add those. - # This assumes gem paths look like: - def 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/recipes/default.rb b/lib/qwandry/recipes/default.rb new file mode 100644 index 0000000..2e2012f --- /dev/null +++ b/lib/qwandry/recipes/default.rb @@ -0,0 +1,20 @@ +# Default recipe, adds ruby sources to Qwandry. + +# Ensure that rubygems is on the path so we can search it +require 'rubygems' + +# 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 \ No newline at end of file