diff --git a/lib/knapsack/recipe.rb b/lib/knapsack/recipe.rb index e7bc909..ec4f4b2 100644 --- a/lib/knapsack/recipe.rb +++ b/lib/knapsack/recipe.rb @@ -54,7 +54,7 @@ def self.find_by_name name, *requirements dependency = Gem::Dependency.new(name, *requirements) candidates = _all.select { |r| - dependency.match? r.name, r.version + dependency.match? r.name, r.version.to_s.gsub(/[a-z]/i, "") }.sort_by { |r| r.version } candidates.last diff --git a/lib/knapsack/utils.rb b/lib/knapsack/utils.rb index e3cec85..8ab96c3 100644 --- a/lib/knapsack/utils.rb +++ b/lib/knapsack/utils.rb @@ -3,7 +3,10 @@ require "digest/md5" require "digest/sha2" require "fileutils" +require "open-uri" require "psych" +require 'zlib' +require 'rubygems/package' module Knapsack module Utils @@ -12,12 +15,9 @@ def download(url, filename) ensure_tree File.dirname(filename) - cmd = ["curl", "-L", "-s", "-S", url, "-o", filename] + tmpfile = open(url, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE) - pid = Process.spawn(*cmd, :err => [:child, :out], :out => IO::NULL) - _, status = Process.wait2(pid) - - status.success? + File.open(filename, "wb") { |file| file.write(tmpfile.read) } end module_function :download @@ -38,12 +38,32 @@ def extract(filename, target, options = {}) raise failure_message % ["MD5", options[:md5], digest] end - cmd = ["tar", "-xf", filename, "-C", target] + file = File.open(filename, "rb") + + Gem::Package::TarReader.new(Zlib::GzipReader.new(file)) do |tar| + symlinks = {} + + tar.each do |entry| + dest = File.join(target, entry.full_name) + + if entry.header.typeflag != '2' + FileUtils.mkdir_p(File.dirname(dest)) + else + src = File.dirname(dest) + "/" + entry.header.linkname + symlinks[src] = dest + end - pid = Process.spawn(*cmd, :err => [:child, :out], :out => IO::NULL) - _, status = Process.wait2(pid) + if entry.file? + File.open(dest, "wb") { |f| f.write(entry.read) } + end + end + + symlinks.each do |src, dest| + FileUtils.cp_r(src, dest) + end + end - status.success? || options[:ignore_extract_errors] + file.close end module_function :extract