Reorganizing files, cleaning up.

This commit is contained in:
Adam Sanderson 2010-08-16 22:53:40 -07:00
parent c694a1608b
commit 0306e71f47
7 changed files with 119 additions and 84 deletions

0
bin/qw Executable file
View File

View File

@ -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,13 +47,7 @@ if __FILE__ == $0
end
name = ARGV.pop
packages = []
@repositories.each do |set, repos|
repos.each do |repo|
packages.concat(repo.scan(name))
end
end
packages = @qwandry.find(name)
package = nil
case packages.length
@ -122,5 +66,5 @@ if __FILE__ == $0
package = packages[index]
end
Qwandry.launch(package, @editor) if package
@qwandry.launch(package) if package
end

View File

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

61
lib/qwandry/launcher.rb Normal file
View File

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

13
lib/qwandry/package.rb Normal file
View File

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

18
lib/qwandry/repository.rb Normal file
View File

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

View File

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