Pattern matching for library names

This commit is contained in:
Adam Sanderson 2010-11-19 15:01:28 -08:00
parent d085b92630
commit 0281d9a633
5 changed files with 17 additions and 12 deletions

5
TODO
View File

@ -1,6 +1,7 @@
TODO, or perhaps not...
- Differentiate multiple similar matches (show path or repository label) - Differentiate multiple similar matches (show path or repository label)
- Allow better customization of repositories - Allow better customization of repositories
- Filter by pattern, ie: `accept '*.rb'` or `reject '*.o'` @done ... but code be better
- Customize collection phase (see LibraryRepository) - Customize collection phase (see LibraryRepository)
- Customize naming phase - Customize naming phase
- Match multiword args, ie: activerec 3. => activerec*3.* => activerecord-3.0.3 - Match multiword args, ie: 'activerec 3.' => 'activerec*3.*' => 'activerecord-3.0.3' @done

9
bin/qw
View File

@ -55,7 +55,7 @@ opts = OptionParser.new do |opts|
end end
opts.parse! ARGV opts.parse! ARGV
if ARGV.length != 1 if ARGV.length == 0
puts opts puts opts
exit(1) exit(1)
end end
@ -63,8 +63,9 @@ end
# Configure default values # Configure default values
@qwandry.editor = @editor if @editor @qwandry.editor = @editor if @editor
name = ARGV.pop name = ARGV.join(' ')
packages = @qwandry.find(name) packages = @qwandry.find(*ARGV)
ARGV.clear # for the gets below
package = nil package = nil
case packages.length case packages.length
@ -79,7 +80,7 @@ else
end end
print ">> " print ">> "
index = gets.to_i-1 index = gets.to_i - 1
package = packages[index] package = packages[index]
end end

View File

@ -3,10 +3,10 @@ module Qwandry
# ./lib-0.1 # ./lib-0.1
# ./lib-0.2 # ./lib-0.2
class FlatRepository < Qwandry::Repository class FlatRepository < Qwandry::Repository
def scan(name) def scan(pattern)
results = [] results = []
all_paths.select do |path| all_paths.select do |path|
if File.basename(path).start_with?(name) if File.fnmatch?(pattern, File.basename(path))
results << package(File.basename(path), [path]) results << package(File.basename(path), [path])
end end
end end

View File

@ -46,11 +46,14 @@ module Qwandry
end end
# Searches all of the loaded repositories for `name` # Searches all of the loaded repositories for `name`
def find(name) def find(*pattern)
pattern = pattern.join('*')
pattern << '*' unless pattern =~ /\*$/
packages = [] packages = []
@repositories.select{|label,_| @active.include? label }.each do |label, repos| @repositories.select{|label,_| @active.include? label }.each do |label, repos|
repos.each do |repo| repos.each do |repo|
packages.concat(repo.scan(name)) packages.concat(repo.scan(pattern))
end end
end end
packages packages

View File

@ -5,11 +5,11 @@ module Qwandry
# lib2.py # lib2.py
# lib2/... # lib2/...
class LibraryRepository < Qwandry::Repository class LibraryRepository < Qwandry::Repository
def scan(name) def scan(pattern)
results = Hash.new{|h,k| h[k] = package(k)} results = Hash.new{|h,k| h[k] = package(k)}
all_paths.select do |path| all_paths.select do |path|
basename = File.basename(path) basename = File.basename(path)
if basename.start_with?(name) if File.fnmatch?(pattern, basename)
# strip any file extension # strip any file extension
basename.sub! /\.\w+$/,'' unless File.directory?(path) basename.sub! /\.\w+$/,'' unless File.directory?(path)
results[basename].paths << path results[basename].paths << path