lib/vendorificator/vendor/archive.rb in vendorificator-0.2.0 vs lib/vendorificator/vendor/archive.rb in vendorificator-0.3.0

- old
+ new

@@ -5,86 +5,90 @@ require 'escape' require 'vendorificator/vendor' -class Vendorificator::Vendor::Archive < Vendorificator::Vendor - arg_reader :url, :strip_root, :type, :checksum, :filename, :basename, :extname, :unpack - attr_reader :conjured_checksum +module Vendorificator + class Vendor::Archive < Vendor + arg_reader :url, :strip_root, :type, :checksum, :filename, :basename, :extname, :unpack + attr_reader :conjured_checksum - def initialize(environment, name, args={}, &block) - no_url_given = !args[:url] + def initialize(environment, name, args={}, &block) + no_url_given = !args[:url] - args[:url] ||= name - args[:filename] ||= URI::parse(args[:url]).path.split('/').last + args[:url] ||= name + args[:filename] ||= URI::parse(args[:url]).path.split('/').last - case args[:filename] - when /\.(tar\.|t)gz$/ - args[:type] ||= :targz - args[:unpack] ||= 'tar -xzf' - when /\.tar\.bz2$/ - args[:type] ||= :tarbz2 - args[:unpack] ||= 'tar -xjf' - when /\.zip$/ - args[:type] ||= :zip - args[:unpack] ||= 'unzip' - when /\.[^\.][^\.]?[^\.]?[^\.]?$/ - args[:type] ||= - begin - unless args[:unpack] - raise RuntimeError, - "Unknown file type #{$&.inspect}, please provide :unpack argument" + case args[:filename] + when /\.(tar\.|t)gz$/ + args[:type] ||= :targz + args[:unpack] ||= 'tar -xzf' + when /\.tar\.bz2$/ + args[:type] ||= :tarbz2 + args[:unpack] ||= 'tar -xjf' + when /\.zip$/ + args[:type] ||= :zip + args[:unpack] ||= 'unzip' + when /\.[^\.][^\.]?[^\.]?[^\.]?$/ + args[:type] ||= + begin + unless args[:unpack] + raise RuntimeError, + "Unknown file type #{$&.inspect}, please provide :unpack argument" + end + $& end - $& + else + args[:basename] ||= args[:filename] + args[:extname] ||= '' + unless args[:unpack] || [:targz, :tarbz2, :zip].include?(args[:type]) + raise RuntimeError, "Unknown file type for #{args[:filename].inspect}, please provide :unpack or :type argument" end - else - args[:basename] ||= args[:filename] - args[:extname] ||= '' - unless args[:unpack] || [:targz, :tarbz2, :zip].include?(args[:type]) - raise RuntimeError, "Unknown file type for #{args[:filename].inspect}, please provide :unpack or :type argument" end - end - args[:basename] ||= $` - args[:extname] ||= $& + args[:basename] ||= $` + args[:extname] ||= $& - name = args[:basename] if no_url_given + name = args[:basename] if no_url_given - super(environment, name, args, &block) - end + super(environment, name, args, &block) + end - def conjure! - shell.say_status :download, url - archive = Tempfile.new([basename, extname]) - archive.write( open(url).read ) - archive.close - @conjured_checksum = Digest::SHA256.file(archive.path).hexdigest - raise RuntimeError, "Checksum error" if checksum && checksum!=conjured_checksum - shell.say_status :unpack, filename - system "#{unpack} #{Escape.shell_single_word archive.path}" - if Dir.entries('.').length == 3 && !args[:no_strip_root] - root = (Dir.entries('.') - %w(.. .)).first - root_entries = Dir.entries(root) - %w(.. .) - while root_entries.include?(root) - FileUtils::mv root, root+"~" - root << "~" + def conjure! + shell.say_status :download, url + archive = Tempfile.new([basename, extname]) + archive.write( open(url).read ) + archive.close + @conjured_checksum = Digest::SHA256.file(archive.path).hexdigest + raise RuntimeError, "Checksum error" if checksum && checksum!=conjured_checksum + shell.say_status :unpack, filename + system "#{unpack} #{Escape.shell_single_word archive.path}" + if Dir.entries('.').length == 3 && !args[:no_strip_root] + root = (Dir.entries('.') - %w(.. .)).first + root_entries = Dir.entries(root) - %w(.. .) + while root_entries.include?(root) + FileUtils::mv root, root+"~" + root << "~" + end + FileUtils::mv root_entries.map { |e| File.join(root, e) }, '.' + FileUtils::rmdir root end - FileUtils::mv root_entries.map { |e| File.join(root, e) }, '.' - FileUtils::rmdir root + super + ensure + archive.close + archive.unlink end - super - ensure - archive.close - archive.unlink - end - def upstream_version - filename - end + def upstream_version + filename + end - def conjure_commit_message - rv = "Conjured archive #{name} from #{filename}\nOrigin: #{url}\nChecksum: #{conjured_checksum}\n" - rv << "Version: #{version}\n" if version - rv + def conjure_commit_message + rv = "Conjured archive #{name} from #{filename}\nOrigin: #{url}\nChecksum: #{conjured_checksum}\n" + rv << "Version: #{version}\n" if version + rv + end end - install! + class Config + register_module :archive, Vendor::Archive + end end