From c4830a11509a5b84636320220523b880a6eddf64 Mon Sep 17 00:00:00 2001 From: hardywu Date: Mon, 22 Jun 2020 04:50:52 -0400 Subject: [PATCH] fix: unzip wkhtmltopdf binary use gem extension --- Rakefile | 4 ++++ ext/wkhtmltopdf/extconf.rb | 45 +++++++++++++++++++++++++++++++++++ ext/wkhtmltopdf/wkhtmltopdf.c | 0 wkhtmltopdf-binary.gemspec | 1 + 4 files changed, 50 insertions(+) create mode 100644 ext/wkhtmltopdf/extconf.rb create mode 100644 ext/wkhtmltopdf/wkhtmltopdf.c diff --git a/Rakefile b/Rakefile index b461e91..e22c25a 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,7 @@ require 'rubygems' require 'rubygems/package_task' require 'rake/testtask' +require "rake/extensiontask" spec = eval(File.new("wkhtmltopdf-binary.gemspec").readlines.join("\n")) @@ -14,3 +15,6 @@ end desc 'Run tests' task default: :test + +Rake::ExtensionTask.new "wkhtmltopdf" do |ext| +end \ No newline at end of file diff --git a/ext/wkhtmltopdf/extconf.rb b/ext/wkhtmltopdf/extconf.rb new file mode 100644 index 0000000..c52f8fa --- /dev/null +++ b/ext/wkhtmltopdf/extconf.rb @@ -0,0 +1,45 @@ +require 'mkmf' +require 'rbconfig' +require 'zlib' + +suffix = case RbConfig::CONFIG['host_os'] + when /linux/ + os = `. /etc/os-release 2> /dev/null && echo ${ID}_${VERSION_ID}`.strip + + if os.start_with?('elementary') || os.start_with?('linuxmint') || os.start_with?('pop') + os = 'ubuntu_18.04' + end + + # CentOS 6 doesn't have `/etc/os-release`. + if (os.empty? && File.read('/etc/centos-release').start_with?('CentOS release 6')) || + os.start_with?('amzn_') + os = 'centos_6' + end + + # Deepin fallback + os = 'debian_9' if /deepin/.match?(os) + + architecture = RbConfig::CONFIG['host_cpu'] == 'x86_64' ? 'amd64' : 'i386' + + "#{os}_#{architecture}" + when /darwin/ + 'macos_cocoa' + else + 'unknown' + end + +binary = File.join(File.dirname(__FILE__), "../../bin/wkhtmltopdf_#{suffix}") +puts binary +if File.exist?("#{binary}.gz") && !File.exist?(binary) + File.open binary, 'wb', 0o755 do |file| + Zlib::GzipReader.open("#{binary}.gz") { |gzip| file << gzip.read } + end +end + +unless File.exist? binary + raise 'Invalid platform, must be running on Ubuntu 16.04/18.04/20.04 ' \ + 'CentOS 6/7/8, Debian 9/10, or intel-based Cocoa macOS ' \ + "(missing binary: #{binary})." +end + +create_makefile 'wkhtmltopdf/wkhtmltopdf' diff --git a/ext/wkhtmltopdf/wkhtmltopdf.c b/ext/wkhtmltopdf/wkhtmltopdf.c new file mode 100644 index 0000000..e69de29 diff --git a/wkhtmltopdf-binary.gemspec b/wkhtmltopdf-binary.gemspec index da6fd3a..6388162 100644 --- a/wkhtmltopdf-binary.gemspec +++ b/wkhtmltopdf-binary.gemspec @@ -11,6 +11,7 @@ Gem::Specification.new do |s| s.files = Dir['bin/*'] s.executables << "wkhtmltopdf" s.require_path = '.' + s.extensions = %w[ext/wkhtmltopdf/extconf.rb] s.add_development_dependency "minitest" end