-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
167 lines (139 loc) · 3.29 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'bundler/setup'
require 'launchy'
def revision
@revision ||= `git log -n 1 --format=%H`.chop
end
def branch
@branch ||= ENV["WERCKER_GIT_BRANCH"] || `git symbolic-ref HEAD | sed -e 's!refs/heads/!!'`.chop
end
def book_name
"great-h-book"
end
def types
%W[epub pdf]
end
def base_url
"http://great-h-book.eiel.info/"
end
def tmp_book
"#{base_url}tmp/#{book_name}-#{revision}"
end
def pdf_url
tmp_book + ".pdf"
end
def epub_url
tmp_book + ".epub"
end
def latest_book
"#{base_url}#{book_name}"
end
def latest_pdf
"#{latest_book}.pdf?#{Time.now.to_i}"
end
def latest_epub
"#{latest_book}.epub?#{Time.now.to_i}"
end
def s3
require 'aws/s3'
@s3 ||= AWS::S3.new(
access_key_id: ENV["AWS_ACCESS_KEY"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
region: ENV["AWS_REGION"],
)
end
def bucket
s3.buckets["great-h-book.eiel.info"]
end
def object_name_base
"tmp/#{book_name}-#{revision}"
end
task :copy do
sh "cp -a src/* ."
end
task :preprocess do
sh "bundle exec bin/get_statics"
sh "bundle exec bin/top-commit"
sh "bundle exec review-preproc *.re --replace"
end
desc 'create book'
task create: [:copy, :preprocess, :'create:epub', :'create:pdf']
namespace :create do
task :epub do
sh "bundle exec review-epubmaker config.yml"
end
task :pdf do
sh "rm -rf *pdf"
sh "bundle exec review-pdfmaker config.yml"
end
end
task :clean do
sh "rm -f *.re"
sh "rm -f *.epub"
sh "rm -rf tmp"
end
desc 'deploy book'
task deploy: [:clean, :create] do
types.each do |type|
object_name = "#{object_name_base}.#{type}"
object = bucket.objects[object_name]
if result = object.exists? rescue false
$stderr.puts "areaday objet: #{object_name}"
else
options = { acl: :public_read }
case type
when "epub"
options[:content_type] = 'application/epub+zip'
when "pdf"
options[:content_type] = 'application/pdf'
end
object.write(open("#{book_name}.#{type}"), options)
end
end
end
namespace :deploy do
desc 'deploy index.html'
task :index do
object = bucket.objects['index.html']
object.write(open("index.html"), acl: :public_read)
end
desc 'deploy latest'
task :latest do
types.each do |type|
object_name = "#{object_name_base}.#{type}"
object = bucket.objects[object_name]
name = "#{book_name}.#{type}"
object.copy_to name
bucket.objects[name].acl = :public_read
end
end
end
task :pry do
require 'pry'
Pry.start
end
namespace :open do
task :pdf do
Launchy.open(pdf_url)
end
task :epub do
Launchy.open(epub_url)
end
end
def twitter
return @twitter if @twitter
require 'twitter'
@twitter = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"]
config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
end
end
namespace :tweet do
task :latest do
twitter.update "すごい広島の本の最新版が更新されたよ。 #{latest_epub} #{latest_pdf} #すごい広島の本"
end
task :draft do
twitter.update "#{branch}ブランチを元にしたすごい広島の本が生成されたよ。 #{epub_url} #{pdf_url} #すごい広島の本"
end
end