48 lines
1.3 KiB
Ruby
Raw Normal View History

2016-09-24 13:52:43 +02:00
module Hbc
module Scopes
def self.included(base)
base.extend(ClassMethods)
2016-08-18 22:11:42 +03:00
end
2016-09-24 13:52:43 +02:00
module ClassMethods
def all
@all_casks ||= {}
all_tokens.map { |t| @all_casks[t] ||= load(t) }
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def all_tapped_cask_dirs
Tap.map(&:cask_dir).compact
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def all_tokens
Tap.map { |t|
t.cask_files.map { |p|
"#{t.name}/#{File.basename(p, ".rb")}"
}
}.flatten
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def installed
# Hbc.load has some DWIM which is slow. Optimize here
# by spoon-feeding Hbc.load fully-qualified paths.
# TODO: speed up Hbc::Source::Tapped (main perf drag is calling Hbc.all_tokens repeatedly)
# TODO: ability to specify expected source when calling Hbc.load (minor perf benefit)
Pathname.glob(caskroom.join("*"))
.map { |caskroom_path|
token = caskroom_path.basename.to_s
path_to_cask = all_tapped_cask_dirs.find { |tap_dir|
tap_dir.join("#{token}.rb").exist?
}
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
if path_to_cask
Hbc.load(path_to_cask.join("#{token}.rb"))
else
Hbc.load(token)
end
}
end
2016-08-18 22:11:42 +03:00
end
end
end