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)
- 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 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

7
bin/qw
View File

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

View File

@ -3,10 +3,10 @@ module Qwandry
# ./lib-0.1
# ./lib-0.2
class FlatRepository < Qwandry::Repository
def scan(name)
def scan(pattern)
results = []
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])
end
end

View File

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

View File

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