ext/Rakefile in kindlegen-3.0.5 vs ext/Rakefile in kindlegen-3.1.0

- old
+ new

@@ -2,12 +2,12 @@ # Rakefile to install kindlegen # (c) Copryright Toshihiko Ichida 2016 # require 'rbconfig' require 'fileutils' +require 'open-uri' -AMAZON = 'http://kindlegen.s3.amazonaws.com' BINDIR = '../bin' def create_default_task(target) task :default => :install @@ -16,33 +16,46 @@ cp t.source, BINDIR end end def create_task_for_unix(config) + require 'rubygems/package' + require 'zlib' + tarball = config[:tarball] - unzip = config[:unzip] target = config[:target] - url = "#{AMAZON}/#{tarball}" + url = ENV['KINDLEGEN_TARBALL_URL'] || config[:url] create_default_task(target) file target => tarball do - sh "#{unzip} #{tarball}" - sh "chmod +x #{target}" + Gem::Package::TarReader.new(Zlib::GzipReader.open(tarball)) do |tar| + tar.each do |entry| + next unless entry.file? + next if File.exist?(entry.full_name) + + dir = File.dirname(entry.full_name) + FileUtils.mkpath(dir) if dir != '.' && !File.exist?(dir) + File.open(entry.full_name, 'wb') do |f| + f.write(entry.read) + end + end + end + File.chmod(0o755, target) end file tarball do - sh "curl #{url} -L -o #{tarball}" + curl(url, tarball) end end -# curl for windows def curl(url, tarball) puts "open(#{url})" puts "save to #{tarball}" - data = open(url, 'rb').read - open(tarball, 'wb').write(data) + open(url) do |file| + IO.copy_stream(file, tarball) + end end # unzip for windows def unzip(tarball) puts "win-unzip #{tarball}" @@ -52,16 +65,15 @@ entry.extract(dest_path=entry.name) unless File.exist?(entry.name) end end def create_task_for_windows(config) - require 'open-uri' require 'zip' tarball = config[:tarball] target = config[:target] - url = "#{AMAZON}/#{tarball}" + url = ENV['KINDLEGEN_TARBALL_URL'] || config[:url] create_default_task(target) file target => tarball do unzip(tarball) @@ -74,20 +86,21 @@ case RbConfig::CONFIG['host_os'] when /mac|darwin/i create_task_for_unix( { tarball: 'KindleGen_Mac_i386_v2_9.zip', - unzip: 'unzip', - target: 'kindlegen' }) + target: 'kindlegen', + url: 'https://web.archive.org/web/20200814013519/https://kindlegen.s3.amazonaws.com/KindleGen_Mac_i386_v2_9.zip' }) when /linux|cygwin/i create_task_for_unix( { tarball: 'kindlegen_linux_2.6_i386_v2_9.tar.gz', - unzip: 'tar -zx --no-same-owner -f', - target: 'kindlegen' }) + target: 'kindlegen', + url: 'https://web.archive.org/web/20150803131026/https://kindlegen.s3.amazonaws.com/kindlegen_linux_2.6_i386_v2_9.tar.gz' }) when /mingw32|mswin32/i create_task_for_windows( { tarball: 'kindlegen_win32_v2_9.zip', - target: 'kindlegen.exe' }) + target: 'kindlegen.exe', + url: 'http://web.archive.org/web/20150407060917/http://kindlegen.s3.amazonaws.com/kindlegen_win32_v2_9.zip' }) else STDERR.puts "Host OS unsupported!" exit(1) end