Add tap support to `edit` and `list`

Most notably, Cask.all returns an array of strings,
not of Cask instances. This makes things easier, as
well as faster, as there's no need to run map(&:to_s)
everywhere anymore.

self.path is a utility method which returns the path
of the cask from its title. There's something subtle
going in there:

 - If `cask_title` is fully qualified, e.g.
   "phinze-cask/alfred", it's straightforward.

 - If `cask_title` is only the name, e.g.
   "firefox-aurora", the name is matched from
   the full list (self.all) (which isn't sorted)
   and the first result is returned.

Hence, self.path with only the name is not precise.
There might be the possibility to apply heuristics
to do a better match (prefer phinze-cask, or maybe
installed casks?) but that's for another issue :-)

self.nice_listing is another utility method used
in `search` and `list`. It returns a list where
unique casks don't have a prefix, and duplicates
do. The prefix is the tap name. The list is then
sorted. For an example or two, look at the first
comment on phinze/#12.
This commit is contained in:
Félix Saparelli 2012-09-24 15:47:00 +12:00
parent 5b51542ccf
commit 4de758bca3
4 changed files with 29 additions and 20 deletions

View File

@ -41,6 +41,24 @@ class Cask
c
}
end
def self.nice_listing(cask_list)
casks = {}
cask_list.each { |c|
repo, name = c.split "/"
casks[name] ||= []
casks[name].push repo
}
list = []
casks.each { |name,repos|
if repos.length == 1
list.push name
else
repos.each { |r| list.push [r,name].join "/" }
end
}
list.sort
end
def self.init
HOMEBREW_CACHE.mkpath
@ -53,12 +71,16 @@ class Cask
def homepage; self.class.homepage; end
def self.installed
self.all.select(&:installed?)
self.all.select { |c| load(c).installed? }
end
def self.path(cask_title)
cask_title = all.grep(/#{cask_title}$/).first unless cask_title =~ /\//
tapspath.join(cask_title.sub("/", "/Casks/") + ".rb") unless cask_title.nil?
end
def self.load(cask_title)
path = tapspath.join cask_title.sub("/", "/Casks/")
require path
require path cask_title
const_get(cask_title.split('/').last.split('-').map(&:capitalize).join).new
end

View File

@ -1,8 +1,8 @@
class Cask::CLI::Edit
def self.run(*arguments)
cask_name, *rest = *arguments
cask_path = Cask.path.join("#{cask_name}.rb")
raise CaskUnavailableError, cask_path.basename('.rb').to_s unless cask_path.file?
cask_path = Cask.path(cask_name)
raise CaskUnavailableError, cask_name + ".rb" if cask_path.nil? || !cask_path.file?
exec_editor cask_path
end

View File

@ -1,6 +1,6 @@
class Cask::CLI::List
def self.run(*arguments)
puts Cask.installed.map(&:to_s).join("\n")
puts_columns Cask.nice_listing(Cask.installed)
end
def self.help

View File

@ -2,20 +2,7 @@ class Cask::CLI::Search
def self.run(*arguments)
search_term, *rest = *arguments
casks = {}
Cask.all.grep(/#{search_term}/).each { |c|
repo, name = c.split "/"
casks[name] ||= []
casks[name].push repo
}
list = []
casks.each { |name,repos|
if repos.length == 1
list.push name
else
repos.each { |r| list.push [r,name].join "/" }
end
}
puts_columns list.sort.join "\n"
puts_columns Cask.nice_listing(Cask.all.grep(/#{search_term}/))
end
def self.help