lib/kosmos/package.rb in kosmos-0.0.1 vs lib/kosmos/package.rb in kosmos-0.0.2.pre.test2
- old
+ new
@@ -1,53 +1,49 @@
require 'pathname'
require 'httparty'
require 'zip'
require 'tmpdir'
+require 'damerau-levenshtein'
module Kosmos
class Package
include PackageDsl
+ include PackageAttrs
attr_reader :ksp_path, :download_dir
- [:title, :url].each do |param|
- define_singleton_method(param) do |value = nil|
- if value
- instance_variable_set("@#{param}", value)
- else
- instance_variable_get("@#{param}")
- end
- end
- end
-
- # Internal version of the `install` method, which saves before actually
- # performing the installation.
+ # Internal version of the `install` method, which handles procedures commong
+ # to all packages, such as saving work before and after installation, as
+ # well as downloading and unzipping packages and running post-processors.
def install!(ksp_path)
@ksp_path = ksp_path
+
+ install_prerequisites!
+
@download_dir = self.class.unzip!
Util.log "Saving your work before installing ..."
- Versioner.mark_preinstall(ksp_path, self.class)
+ Versioner.mark_preinstall(ksp_path, self)
- Util.log "Installing #{self.class.title} ..."
+ Util.log "Installing #{title} ..."
install
Util.log "Cleaning up ..."
- Kosmos.config.post_processors.each { |p| p.post_process(ksp_path) }
+ Util.run_post_processors!(ksp_path)
- Versioner.mark_postinstall(ksp_path, self.class)
+ Versioner.mark_postinstall(ksp_path, self)
+
+ install_postrequisites!
end
class << self
- def aliases(*aliases)
- @aliases ||= []
+ def unzip!
+ PackageDownloads.download_and_unzip_package(self)
+ end
- if aliases.any?
- @aliases = aliases
- else
- @aliases
- end
+ def download!
+ PackageDownloads.download_package(self)
end
# a callback for when a subclass of this class is created
def inherited(package)
(@@packages ||= []) << package
@@ -55,55 +51,43 @@
def normalize_for_find(name)
name.downcase.gsub(' ', "-")
end
+ def normalized_title
+ normalize_for_find(title)
+ end
+
def find(name)
@@packages.find do |package|
- [package.title, package.aliases].flatten.any? do |candidate_name|
+ package.names.any? do |candidate_name|
normalize_for_find(candidate_name) == normalize_for_find(name)
end
end
end
- def unzip!
- download_file = download!
-
- Util.log "Unzipping ..."
-
- output_path = Pathname.new(download_file.path).parent.to_s
-
- Zip::File.open(download_file.path) do |zip_file|
- zip_file.each do |entry|
- destination = File.join(output_path, entry.name)
- parent_dir = File.expand_path('..', destination)
-
- FileUtils.mkdir_p(parent_dir) unless File.exists?(parent_dir)
-
- entry.extract(destination)
- end
+ def search(name)
+ @@packages.min_by do |package|
+ package.names.map do |candidate_name|
+ DamerauLevenshtein.distance(name, candidate_name)
+ end.min
end
+ end
+ end
- File.delete(File.absolute_path(download_file))
+ private
- output_path
+ def install_prerequisites!
+ resolve_prerequisites.each do |package|
+ Util.log "#{title} has prerequisite #{package.title}."
+ package.new.install!(ksp_path)
end
+ end
- def download!
- Util.log "The package is found at #{url}. Finding the download URL ..."
- download_url = DownloadUrl.new(url).resolve_download_url
-
-
- Util.log "Found it. Downloading from #{download_url} ..."
- downloaded_file = HTTParty.get(download_url)
-
- tmpdir = Dir.mktmpdir
-
- download_file = File.new(File.join(tmpdir, 'download'), 'w+')
- download_file.write(downloaded_file)
- download_file.close
-
- download_file
+ def install_postrequisites!
+ resolve_postrequisites.each do |package|
+ Util.log "#{title} has postrequisite #{package.title}."
+ package.new.install!(ksp_path)
end
end
# Now, let's include all the known packages.
Dir[File.join(File.dirname(__FILE__), 'packages', '*.rb')].each do |file|