Sha256: dbbf6b93c0200f07fd017902f641e9c9d19ad18d953cbb7c727b171c8bf1e908

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
require 'rbconfig'
require 'open-uri'
require 'imgkit/configuration'

def detect_architecture
  case Config::CONFIG['host_os']
  when /x86_64-linux/i
    'amd64'
  when /linux/i
    'i386'
  when /darwin/i
    'OS-X.i386'
  else
    raise "No binaries found for your system. Please install wkhtmltoimage by hand."
  end
end

def cleanup(install_to)
  `rm -rf wkhtmltoimage*`
  `rm #{install_to}`
end

def download_wkhtmltoimage(arch)
  page = open("http://code.google.com/p/wkhtmltopdf/downloads/list").read
  download = page.match(/href=".*name=(.*wkhtmltoimage-.*#{arch}.*?)&/) || raise("File not found..")
  download = download[1]
  url = "http://wkhtmltopdf.googlecode.com/files/#{download}"
  puts "Downloading #{download} from #{url}"
  
  `curl #{url} > #{download}`
  download
end

def install(download, arch, install_to)
  puts "Installing #{download} to #{install_to}"
  if download =~ /.tar.bz2$/
    `tar xjvf #{download}`
    `mv wkhtmltoimage-#{arch} #{install_to}`
  elsif download =~ /.tar.lzma$/
    raise "couldn't extract archive: lzcat not found" unless system("which lzcat > /dev/null 2>/dev/null")
    `lzcat #{download} | tar x`
    `mv wkhtmltoimage-#{arch} #{install_to}`
  else
    `mv #{download} #{install_to}`
  end
  `sudo chmod +x #{install_to}`
end

OptionParser.new do |parser|
  parser.banner = "IMGKit\n\nOptions are:"

  parser.on("--install-wkhtmltoimage", "Install wkhtmltoimage binaries (TO=/usr/local/bin ARCHITECTURE=i386)") do
    architecture = ENV['ARCHITECTURE'] || detect_architecture
    install_to = ENV['TO'] || IMGKit.configuration.wkhtmltoimage
    
    Dir.chdir '/tmp'
    
    cleanup(install_to)
    download = download_wkhtmltoimage(architecture)
    install(download, architecture, install_to)
  end

  parser.on("--version", "Show Version.") do
    root = File.dirname(File.dirname(__FILE__))
    puts File.read(File.join(root, 'VERSION'))
  end

  parser.on("-h", "--help", "Show this.") { puts parser; exit }
end.parse!

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
imgkit-1.0.0 bin/imgkit
imgkit-0.9.2 bin/imgkit