From 0306e71f4760ccafccafd8df2a2d8fc6e5d6e81c Mon Sep 17 00:00:00 2001 From: Adam Sanderson Date: Mon, 16 Aug 2010 22:53:40 -0700 Subject: [PATCH] Reorganizing files, cleaning up. --- bin/qw | 0 qwandry.rb => lib/qwandry.rb | 74 +++++----------------------------- lib/qwandry/flat_repository.rb | 18 +++++++++ lib/qwandry/launcher.rb | 61 ++++++++++++++++++++++++++++ lib/qwandry/package.rb | 13 ++++++ lib/qwandry/repository.rb | 18 +++++++++ repositories.rb | 19 --------- 7 files changed, 119 insertions(+), 84 deletions(-) create mode 100755 bin/qw rename qwandry.rb => lib/qwandry.rb (53%) create mode 100644 lib/qwandry/flat_repository.rb create mode 100644 lib/qwandry/launcher.rb create mode 100644 lib/qwandry/package.rb create mode 100644 lib/qwandry/repository.rb delete mode 100644 repositories.rb diff --git a/bin/qw b/bin/qw new file mode 100755 index 0000000..e69de29 diff --git a/qwandry.rb b/lib/qwandry.rb similarity index 53% rename from qwandry.rb rename to lib/qwandry.rb index 01333b9..aa3da1a 100755 --- a/qwandry.rb +++ b/lib/qwandry.rb @@ -14,73 +14,23 @@ require 'optparse' # If no Repository matches, then qwandry will exit with a 404 (repo not found) # module Qwandry - - class Repository - attr_reader :name - - def initialize(name, path) - @name = name - @path = path.chomp('/') - end - - def scan(name) - [] - end - - def package(name, paths) - Package.new(name, paths, self) - end - end - - # Directories look like: - # Repository - # lib-0.1 - # lib-0.2 - class FlatRepository < Repository - def scan(name) - results = [] - Dir["#{@path}/*"].select do |path| - if File.basename(path).start_with?(name) - results << package(File.basename(path), [path]) - end - end - - results - end - end - - - class Package - attr_reader :name - attr_reader :repository - attr_reader :paths - - def initialize(name, paths, repository) - @name = name - @repository = repository - @paths = paths - end - end - - def launch(package, editor=nil) - editor ||= ENV['VISUAL'] || ENV['EDITOR'] - system editor, *package.paths - end - module_function :launch - + autoload :Launcher, "qwandry/launcher" + autoload :Repository, "qwandry/repository" + autoload :FlatRepository, "qwandry/flat_repository" + autoload :Package, "qwandry/package" end if __FILE__ == $0 - load('repositories.rb') + @qwandry = Qwandry::Launcher.new load('~/.qwandry/repositories.rb') if File.exists?('~/.qwandry/repositories.rb') opts = OptionParser.new do |opts| opts.banner = "Usage: qwandry [options] name [version]" opts.separator "" - opts.separator "Known Repositories: #{@repositories.keys.join(", ")}" + opts.separator "Known Repositories: #{@qwandry.repositories.keys.join(", ")}" opts.on("-e", "--editor EDITOR", "Use EDITOR to open the package") do |editor| - @editor = editor + @qwandry.editor = editor end opts.on_tail("-h", "--help", "Show this message") do @@ -97,14 +47,8 @@ if __FILE__ == $0 end name = ARGV.pop - packages = [] + packages = @qwandry.find(name) - @repositories.each do |set, repos| - repos.each do |repo| - packages.concat(repo.scan(name)) - end - end - package = nil case packages.length when 0 @@ -122,5 +66,5 @@ if __FILE__ == $0 package = packages[index] end - Qwandry.launch(package, @editor) if package + @qwandry.launch(package) if package end \ No newline at end of file diff --git a/lib/qwandry/flat_repository.rb b/lib/qwandry/flat_repository.rb new file mode 100644 index 0000000..36b5f3b --- /dev/null +++ b/lib/qwandry/flat_repository.rb @@ -0,0 +1,18 @@ +module Qwandry + # Directories look like: + # Repository + # lib-0.1 + # lib-0.2 + class FlatRepository < Qwandry::Repository + def scan(name) + results = [] + Dir["#{@path}/*"].select do |path| + if File.basename(path).start_with?(name) + results << package(File.basename(path), [path]) + end + end + + results + end + end +end \ No newline at end of file diff --git a/lib/qwandry/launcher.rb b/lib/qwandry/launcher.rb new file mode 100644 index 0000000..5a25b3b --- /dev/null +++ b/lib/qwandry/launcher.rb @@ -0,0 +1,61 @@ +# Launcher is the core Qwandry class, it coordinates finding and launching +# a package. It is driven externaly by a UI. +module Qwandry + class Launcher + # The default editor to be used by Qwandry#launch. + attr_accessor :editor + + # Returns the repositories the Launcher will use. + attr_reader :repositories + + def initialize + @repositories = Hash.new{|h,k| h[k] = []} + add_ruby_repositories + add_gem_repositories + 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) + label = label.to_s + @repositories[label] << repository_type.new(label, path) + end + + # Searches all of the loaded repositories for `name` + def find(name) + packages = [] + @repositories.each do |label, repos| + repos.each do |repo| + packages.concat(repo.scan(name)) + end + end + packages + end + + # Launches a `package`, unless set, `editor` will check against the environment. + def launch(package, editor=nil) + 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 + 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/package.rb b/lib/qwandry/package.rb new file mode 100644 index 0000000..9ff602f --- /dev/null +++ b/lib/qwandry/package.rb @@ -0,0 +1,13 @@ +module Qwandry + class Package + attr_reader :name + attr_reader :repository + attr_reader :paths + + def initialize(name, paths, repository) + @name = name + @repository = repository + @paths = paths + end + end +end \ No newline at end of file diff --git a/lib/qwandry/repository.rb b/lib/qwandry/repository.rb new file mode 100644 index 0000000..8984cb1 --- /dev/null +++ b/lib/qwandry/repository.rb @@ -0,0 +1,18 @@ +module Qwandry + class Repository + attr_reader :name + + def initialize(name, path) + @name = name + @path = path.chomp('/') + end + + def scan(name) + [] + end + + def package(name, paths) + Package.new(name, paths, self) + end + end +end \ No newline at end of file diff --git a/repositories.rb b/repositories.rb deleted file mode 100644 index e0568e1..0000000 --- a/repositories.rb +++ /dev/null @@ -1,19 +0,0 @@ -@repositories = Hash.new{|h,k| h[k] = []} - -def add(label, path, repository_type=Qwandry::FlatRepository) - label = label.to_s - @repositories[label] << repository_type.new(label, path) -end - -# Add gem repositories: -# Using the ruby load paths, determine the common gem root paths, and add those. -# This assumes gem paths look like: -($:).grep(/gems/).map{|p| p[/.+\/gems\//]}.uniq.each do |path| - add :gem, path -end - -# Add ruby standard libraries, ignore the platform specific ones since they -# tend to contain only binaries -($:).grep(/lib\/ruby/).reject{|path| path =~ /#{RUBY_PLATFORM}$/}.each do |path| - add :ruby, path -end \ No newline at end of file