-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathRakefile
201 lines (165 loc) · 5.35 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require "rubygems"
require "bundler/setup"
require "date"
require "json"
require "dotenv/tasks"
require "createsend"
require "pry"
require "uri"
require_relative "lib/core_ext/date"
require_relative "lib/core_ext/integer"
require_relative "lib/bq_client"
require_relative "lib/db"
require_relative "lib/issue"
require_relative "lib/repo"
require_relative "lib/template"
require_relative "lib/buffer"
DATE = Date.parse(ENV.fetch("DATE")) rescue Date.today
DIST_DIR = "dist"
ISSUE_DIR = File.join(DIST_DIR, DATE.path)
ISSUE_URL = URI.join(ENV.fetch("URL", "https://nightly.changelog.com/"), DATE.path)
DATA_FILE = File.join(ISSUE_DIR, "data.json")
THEMES = %w(night day)
MAX_REPOS = 15
def each_issue &block
Dir["#{DIST_DIR}/**/*/"].each do |path|
if match = path.match(/(\d{4})\/(\d{2})\/(\d{2})/)
y, m, d = match.captures.map(&:to_i)
block.call path, Date.new(y, m, d)
end
end
end
desc "Launches local HTTP server on DIST_DIR"
task :preview do
system("cd #{DIST_DIR} && python -m SimpleHTTPServer") ||
system("cd #{DIST_DIR} && python3 -m http.server")
end
desc "Performs all operations for DATE except delivering the email"
task generate: [:sass, :images, :issue, :index]
task console: [:dotenv] do
binding.pry
end
task :dist do
FileUtils.mkdir_p DIST_DIR
end
desc "Takes dat scss and makes it dat css"
task sass: [:dist] do
Dir["styles/*.scss"].each do |infile|
outfile = File.basename(infile).gsub ".scss", ".css"
next if outfile.start_with? "_"
system "sass --sourcemap=none #{infile} #{DIST_DIR}/#{outfile}"
end
end
desc "Copies the images directory to DIST_DIR"
task images: [:dist] do
FileUtils.cp_r "images", "dist", preserve: false
end
desc "Processes the site's index w/ current linked list"
task index: [:dist] do
File.write "#{DIST_DIR}/index.html", Template.new("html/index").render
end
desc "Runs all tasks to generate DATE's issue"
task issue: ["issue:data", "issue:html", "issue:text"]
desc "Runs all design-related tasks across all issues in DIST_DIR"
task redesign: [:sass, :images] do
each_issue do |path, date|
puts "Redesigning #{path}..."
system "DATE=#{date} rake issue:html"
end
end
namespace :issue do
task dir: [:dist] do
FileUtils.mkdir_p ISSUE_DIR
end
desc "Generates DATA_FILE file for DATE. No-op if file exists"
task data: [:dotenv, :dir] do
if File.exist? DATA_FILE
next
end
bq = BqClient.new DATE
data = {
top_new: bq.top_new.first(MAX_REPOS),
top_all: bq.top_all.first(MAX_REPOS)
}
data[:top_all].each do |repo|
DB.insert DATE, repo
end
File.write DATA_FILE, JSON.dump(data)
end
desc "Buffers issue's tweets for DATE"
task buffer: [:data] do
json = JSON.load File.read DATA_FILE
issue = Issue.new DATE, json
gotime = Buffer.new ENV.fetch("BUFFER_GO_TIME"), %w(Go), "#golang"
jsparty = Buffer.new ENV.fetch("BUFFER_JS_PARTY"), %w(CSS JavaScript JSX PureScript TypeScript Vue)
[gotime, jsparty].each do |buffer|
buffer.injest issue.top_new
buffer.injest issue.top_all_firsts
buffer.queue
end
end
desc "Generates index.html file for DATE"
task html: [:data] do
template = Template.new "html/issue"
json = JSON.load File.read DATA_FILE
issue = Issue.new DATE, json
File.write "#{ISSUE_DIR}/index.html", template.render({
issue: issue,
web_version: true,
theme: "night"
})
THEMES.each do |theme|
File.write "#{ISSUE_DIR}/email-#{theme}.html", template.render({
issue: issue,
web_version: false,
theme: theme
})
end
end
desc "Generates email.text file for DATE"
task text: [:data] do
template = Template.new "text/issue"
json = JSON.load File.read DATA_FILE
issue = Issue.new DATE, json
File.write "#{ISSUE_DIR}/email.text", template.render({issue: issue})
end
desc "Delivers DATE's email to Campaign Monitor"
task deliver: [:dotenv] do
json = JSON.load File.read DATA_FILE
next unless json["top_new"].any? || json["top_all"].any?
auth = {api_key: ENV.fetch("CAMPAIGN_MONITOR_KEY")}
CreateSend::List.new(auth, ENV.fetch("CAMPAIGN_MONITOR_LIST")).segments.each do |segment|
theme_name = segment.Title.downcase
theme_id = segment.SegmentID
next unless THEMES.include? theme_name
campaign_id = CreateSend::Campaign.create(
auth,
ENV.fetch("CAMPAIGN_MONITOR_ID"), # client id
"The Hottest Repos on GitHub - #{DATE.day_month_abbrev}", # subject
"Nightly – #{DATE} (#{theme_name} theme)", # campaign name
"Changelog Nightly", # from name
"[email protected]", # from email
"[email protected]", # reply to
"#{ISSUE_URL}/email-#{theme_name}.html", # html url
"#{ISSUE_URL}/email.text", # text url
[], # list ids
[theme_id] # segment ids
)
CreateSend::Campaign.new(auth, campaign_id).send "[email protected]" # send + confirmation email
end
end
end
namespace :db do
desc "Seed the database with historic data files"
task seed: [:dotenv] do
puts "Seeding the db..."
each_issue do |path, date|
data = JSON.load File.read File.expand_path("#{path}/data.json")
data["top_all"].each do |json|
repo = Repo.from_json json
DB.insert date, repo
end
end
puts "There are now #{DB.count} listings in the db."
end
end