# # Rakefile to install kindlegen # (c) Copryright Toshihiko Ichida 2016 # require 'rbconfig' require 'fileutils' require 'open-uri' BINDIR = '../bin' def create_default_task(target) task :default => :install task :install => target do |t| mkdir BINDIR unless File.exist?(BINDIR) cp t.source, BINDIR end end def create_task_for_unix(config) require 'rubygems/package' require 'zlib' tarball = config[:tarball] target = config[:target] url = ENV['KINDLEGEN_TARBALL_URL'] || config[:url] create_default_task(target) file target => tarball do 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 curl(url, tarball) end end def curl(url, tarball) puts "open(#{url})" puts "save to #{tarball}" open(url) do |file| IO.copy_stream(file, tarball) end end # unzip for windows def unzip(tarball) puts "win-unzip #{tarball}" Zip::File.open(tarball).each do |entry| dir = File.dirname(entry.name) FileUtils.mkpath(dir) if dir != '.' && !File.exist?(dir) entry.extract(dest_path=entry.name) unless File.exist?(entry.name) end end def create_task_for_windows(config) require 'zip' tarball = config[:tarball] target = config[:target] url = ENV['KINDLEGEN_TARBALL_URL'] || config[:url] create_default_task(target) file target => tarball do unzip(tarball) end file tarball do curl(url, tarball) end end case RbConfig::CONFIG['host_os'] when /mac|darwin/i create_task_for_unix( { tarball: 'KindleGen_Mac_i386_v2_9.zip', 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', 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', 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