forked from tdtds/kindlegen
/
Rakefile
93 lines (78 loc) · 1.93 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Rakefile to install kindlegen
# (c) Copryright Toshihiko Ichida 2016
#
require 'rbconfig'
require 'fileutils'
AMAZON = 'http://kindlegen.s3.amazonaws.com'
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)
tarball = config[:tarball]
unzip = config[:unzip]
target = config[:target]
url = "#{AMAZON}/#{tarball}"
create_default_task(target)
file target => tarball do
sh "#{unzip} #{tarball}"
sh "chmod +x #{target}"
end
file tarball do
sh "curl #{url} -L -o #{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)
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 'open-uri'
require 'zip'
tarball = config[:tarball]
target = config[:target]
url = "#{AMAZON}/#{tarball}"
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',
unzip: 'unzip',
target: 'kindlegen' })
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' })
when /mingw32|mswin32/i
create_task_for_windows(
{ tarball: 'kindlegen_win32_v2_9.zip',
target: 'kindlegen.exe' })
else
STDERR.puts "Host OS unsupported!"
exit(1)
end