From a809fcbdd5d274eafda9ad5bbae51074900ddaae Mon Sep 17 00:00:00 2001 From: Adam Sanderson Date: Thu, 5 Aug 2010 17:49:12 -0700 Subject: [PATCH] organizing my thoughts --- qwandry.rb | 103 ++++++++++++++++++++++++++++-------------------- repositories.rb | 2 +- 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/qwandry.rb b/qwandry.rb index 4c7a1c8..b4d88ed 100755 --- a/qwandry.rb +++ b/qwandry.rb @@ -1,56 +1,75 @@ #!/usr/bin/env ruby require 'optparse' -puts "Qwandry; a qwestionable tool" -opts = OptionParser.new do |opts| - opts.banner = "Usage: qwandry [options] [type] repository-name" +# Informal Spec: +# +# A User may have multiple Repositories +# A Repositories contains Packages +# +# A User will search for a repository giving a name and optional version +# Each Repository will be scanned for matching Packages +# If only one Package matches, that Package will be opened +# If more than one Package matches, then the user will be prompted to pick one +# While any two Packages share the same name their parent dir is appended +# If no Repository matches, then qwandry will exit with a -404 (repo not found) +# +module Qwandry + + class Repository + def initialize(path) + @path = path.chomp('/') + end + + def scan(name) + [] + end + end + + # Directories look like: + # Repository + # lib-0.1 + # lib-0.2 + class FlatRepository < Repository + def scan(name) + Dir["#{@path}/*"].select{|path| path if File.basename(path).start_with?(name)} + end + end - opts.separator "" - opts.separator "Known Repositories:" - # ... end -opts.parse! ARGV +if __FILE__ == $0 + opts = OptionParser.new do |opts| + opts.banner = "Usage: qwandry [options] name [version]" -if ARGV.length != 1 - puts opts - exit(-1) -end + opts.separator "" + opts.separator "Known Repositories:" + # ... + end -## -class Repository - def initialize(path) - @path = path.chomp('/') + opts.parse! ARGV + + if ARGV.length != 1 + puts opts + exit(-1) end - def scan(name) - [] + load('repositories.rb') + + name = ARGV.pop + candidates = [] + + @repositories.each do |set, repos| + repos.each do |repo| + candidates.concat(repo.scan(name)) + end end -end -class FlatRepository < Repository - def scan(name) - Dir["#{@path}/*"].select{|path| path if File.basename(path).start_with?(name)} + candidates.each_with_index do |path, index| + puts " #{index+1}. #{File.basename path}" end -end -## -load('repositories.rb') - -name = ARGV.pop -candidates = [] - -@repositories.each do |set, repos| - repos.each do |repo| - candidates.concat(repo.scan(name)) - end -end - -candidates.each_with_index do |path, index| - puts " #{index+1}. #{File.basename path}" -end - -print ">> " -index = gets.to_i-1 -path = candidates[index] -`mate #{path}` \ No newline at end of file + print ">> " + index = gets.to_i-1 + path = candidates[index] + `mate #{path}` +end \ No newline at end of file diff --git a/repositories.rb b/repositories.rb index 06e6afa..3c20cca 100644 --- a/repositories.rb +++ b/repositories.rb @@ -5,7 +5,7 @@ def which(bin) end def add(label, path) - @repositories[label.to_s] << FlatRepository.new(path) + @repositories[label.to_s] << Qwandry::FlatRepository.new(path) end if which('ruby') == '/Users/adam/.rvm/rubies/ruby-1.9.1-p378/bin/ruby'