lib/menagerie/artifact.rb in menagerie-0.1.3 vs lib/menagerie/artifact.rb in menagerie-1.0.0
- old
+ new
@@ -1,32 +1,35 @@
-require 'pathname'
require 'open-uri'
module Menagerie
##
# Artifacts are a stored file for a particular version of a project
class Artifact
- attr_reader :version, :name, :path
+ attr_reader :name, :version
def initialize(params = {})
- @config = params
- @config[:artifact] ? create : parse
+ @options = params
+ @logger = @options[:logger] || Menagerie.get_logger
+ @options[:artifact] ? create : parse
end
+ def path
+ @path ||= "#{@options[:paths][:artifacts]}/#{@name}/#{@version}"
+ end
+
private
def create
- @name, @version, url = @config[:artifact].values_at :name, :version, :url
- @path = "#{@config[:paths][:artifacts]}/#{@name}/#{@version}"
- download url, path unless File.exist? path
- File.chmod(@config[:artifact][:mode], path) if @config[:artifact][:mode]
+ artifact = @options[:artifact]
+ @name, @version = artifact.values_at(:name, :version)
+ @logger.info "Downloading artifact: #{path}"
+ download(artifact[:url], path) unless File.exist? path
+ File.chmod(artifact[:mode], path) if artifact[:mode]
end
def parse
- link_path = @config[:path]
- @name = Pathname.new(link_path).basename
- @version = File.basename File.readlink(link_path)
- @path = "#{@config[:paths][:artifacts]}/#{@name}/#{@version}"
+ @name = File.basename @options[:path]
+ @version = File.basename File.readlink(@options[:path])
end
def download(url, path)
FileUtils.mkdir_p File.dirname(path)
File.open(path, 'wb') do |fh|