diff --git a/TODO b/TODO index 0711cdd..6a241f1 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,6 @@ - Differentiate multiple similar matches (show path or repository label) -- Recognize file idioms such as './file.rb' and matching "./file/" @done -- Custom recipes @done \ No newline at end of file +- 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 diff --git a/lib/qwandry/flat_repository.rb b/lib/qwandry/flat_repository.rb index 4a2c3a9..1371ba6 100644 --- a/lib/qwandry/flat_repository.rb +++ b/lib/qwandry/flat_repository.rb @@ -5,7 +5,7 @@ module Qwandry class FlatRepository < Qwandry::Repository def scan(name) results = [] - Dir["#{@path}/*"].select do |path| + all_paths.select do |path| if File.basename(path).start_with?(name) results << package(File.basename(path), [path]) end diff --git a/lib/qwandry/launcher.rb b/lib/qwandry/launcher.rb index 989e65d..f69bf63 100644 --- a/lib/qwandry/launcher.rb +++ b/lib/qwandry/launcher.rb @@ -18,15 +18,22 @@ module Qwandry custom_configuration! end - # Adds a repository path to Qwandry's Launcher. - # `label` is used to label packages residing in the folder `path`. - # The `repository_type` controls the class used to index the `path`. - def add(label, path, repository_type=Qwandry::FlatRepository) + # Adds a repository path to Qwandry's Launcher. `label` is used to label packages residing in the folder `path`. + # + # The `options` can be used to customize the repository. + # + # [:class] Repository class, defaults to Qwandry::FlatRepository + # [:accept] Filters paths, only keeping ones matching the accept option + # [:reject] Filters paths, rejecting any paths matching the reject option + # + # `:accept` and `:reject` take patterns such as '*.py[oc]', procs, and regular expressions. + def add(label, path, options={}) if path.is_a?(Array) - path.each{|p| add label, p, repository_type} + path.each{|p| add label, p, options} else + repository_class = options[:class] || Qwandry::FlatRepository label = label.to_s - @repositories[label] << repository_type.new(label, File.expand_path(path)) + @repositories[label] << repository_class.new(label, File.expand_path(path), options) end end @@ -67,7 +74,7 @@ module Qwandry # Add ruby standard libraries: paths.grep(/lib\/ruby/).each do |path| - add :ruby, path, Qwandry::LibraryRepository + add :ruby, path, :class=>Qwandry::LibraryRepository end # Add gem repositories: diff --git a/lib/qwandry/library_repository.rb b/lib/qwandry/library_repository.rb index bf6beb2..87b17ed 100644 --- a/lib/qwandry/library_repository.rb +++ b/lib/qwandry/library_repository.rb @@ -1,13 +1,13 @@ module Qwandry # Directories look like: # lib1.rb - # ./lib1 + # lib1/... # lib2.py - # ./lib2 + # lib2/... class LibraryRepository < Qwandry::Repository def scan(name) results = Hash.new{|h,k| h[k] = package(k)} - Dir["#{@path}/*"].select do |path| + all_paths.select do |path| basename = File.basename(path) if basename.start_with?(name) # strip any file extension diff --git a/lib/qwandry/package.rb b/lib/qwandry/package.rb index 9ff602f..729a087 100644 --- a/lib/qwandry/package.rb +++ b/lib/qwandry/package.rb @@ -1,8 +1,8 @@ module Qwandry class Package attr_reader :name - attr_reader :repository attr_reader :paths + attr_reader :repository def initialize(name, paths, repository) @name = name diff --git a/lib/qwandry/repository.rb b/lib/qwandry/repository.rb index 7ac9f87..eb16b3b 100644 --- a/lib/qwandry/repository.rb +++ b/lib/qwandry/repository.rb @@ -2,18 +2,38 @@ module Qwandry class Repository attr_reader :name attr_reader :path + attr_reader :options - def initialize(name, path) + def initialize(name, path, options={}) @name = name @path = path.chomp('/') + @options = options + end - + def scan(name) [] end + + def all_paths + paths = Dir["#{@path}/*"] + paths = paths.select(&matcher(options[:accept])) if options[:accept] + paths = paths.reject(&matcher(options[:reject])) if options[:reject] + paths + end def package(name, paths=[]) Package.new(name, paths, self) end + + private + def matcher(pattern) + case pattern + when Regexp then lambda{|p| p =~ pattern} + when String then lambda{|p| File.fnmatch?(pattern, p)} + when Proc then pattern + else raise ArgumentError, "Expected a Regexp, String, or Proc" + end + end end end \ No newline at end of file diff --git a/qwandry.gemspec b/qwandry.gemspec index b79fc01..615a696 100644 --- a/qwandry.gemspec +++ b/qwandry.gemspec @@ -9,18 +9,18 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Adam Sanderson"] - s.date = %q{2010-11-08} + s.date = %q{2010-11-19} s.default_executable = %q{qw} s.description = %q{ Open a gem or library's source directory with your default editor. } s.email = %q{netghost@gmail.com} s.executables = ["qw"] s.extra_rdoc_files = [ - "README", + "README.markdown", "TODO" ] s.files = [ - "README", + "README.markdown", "Rakefile", "TODO", "VERSION", diff --git a/templates/init.rb b/templates/init.rb index 3fcdf4f..7dad26c 100644 --- a/templates/init.rb +++ b/templates/init.rb @@ -34,8 +34,10 @@ # add :ghc, "/usr/local/lib/ghc/" # = Python -# Open python standard libraries: -# add :python, "/usr/lib/python2.6/", Qwandry::LibraryRepository +# Open python standard libraries, but ignore the *.pyo and *.pyc files: +# add :python, "/usr/lib/python2.6/", +# :class => Qwandry::LibraryRepository, +# :reject => '*.py[oc]' # = Ruby # Qwandry comes set up for ruby by default, however you may want to be able to @@ -45,5 +47,8 @@ # add :cruby, "~/.rvm/src/ruby-1.9.1-p378/" # = Javascript / Node -# Edit node.js and npm managed libraries: -# add :node, "/usr/local/lib/node/" \ No newline at end of file +# Edit node.js and npm managed libraries. +# Configure a :node repository showing only the active versions of each library: +# add :node, "/usr/local/lib/node/", :reject=> /@/ +# # Configure a :npm repository showing only the versioned libraries: +# add :npm, "/usr/local/lib/node/", :accept=> /@/ \ No newline at end of file