forked from neverstopbuilding/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
297 lines (257 loc) · 7.58 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Encoding: utf-8
require 'rubocop/rake_task'
require 'levenshtein'
require 'yaml'
# Settings
valid_categories = [
'robo garden',
'web development',
'devops',
'3d printing',
'productivity',
'test driven development',
'management',
'software engineering',
'hardware hacking'
]
minimum_tags = 5
posting_weekeday = 'Tuesday'
task default: 'assets:precompile'
namespace :build do
task :local do
Rake::Task[:clean].invoke
Rake::Task[:prepare].invoke
Rake::Task[:test].invoke
sh 'bundle exec jekyll build --config _config.yml,_development.yml'
end
task :staging do
Rake::Task[:clean].invoke
Rake::Task[:prepare].invoke
Rake::Task[:test].invoke
sh 'bundle exec jekyll build --config _config.yml,_staging.yml'
end
end
desc 'Create a new article'
task :new, :slug do |t, args|
fail 'Enter a slug for your post!' unless args.slug
post_date = calculate_next_post_date(posting_weekeday)
slug = args.slug.gsub(/\s/, '-').gsub(/[^\w-]/, '').downcase
filename = File.join('src', '_posts', "#{post_date}-#{slug}.md")
fail "#{filename} already exists!" if File.exist?(filename)
puts "Creating new article: #{filename}"
open(filename, 'w') do |file|
file.puts '---'
file.puts 'layout: post'
file.puts 'title: '
file.puts 'image: '
file.puts "date: #{post_date}"
file.puts 'category: '
file.puts 'tags: []'
file.puts 'description: ""'
file.puts 'promotion: ""'
file.puts '---'
end
end
def calculate_next_post_date(posting_weekeday)
latest_post_date = posts_data.to_a.last[1]['date']
today = Date.today
if latest_post_date > today
return latest_post_date + 7
else
date = Date.parse(posting_weekeday)
delta = date > Date.today ? 0 : 7
return date + delta
end
end
task :queue do
posts_data.each do |path, data|
# puts data.inspect
post_date = Date.parse(data['date'].to_s) if data['date']
if post_date && post_date > Date.today
puts "#{data['date']} - #{data['title'] || 'No Title'}"
end
end
end
task :social do
posts = Dir.glob(File.join('src', '_posts', '*.md')).sort.reverse
if posts[0] =~ /\d{4}-\d{2}-\d{2}-(.*)\.md/i
slug = $1
path = 'http://www.neverstopbuilding.com/' + slug
end
content = File.read(posts[0])
data = YAML.load($1) if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
puts "\nThe title is:\n"
puts "#{data['title']}"
links = {
'Hacker News' => 'hacker_news',
'Buffer' => 'buffer',
'Google Plus' => 'google_plus',
'Reddit' => 'reddit'
}
links.each do |type, source|
puts "\n#{type}\n"
puts path + "?utm_source=#{source}&medium=share&utm_campaign=#{slug}"
end
end
namespace :posts do
namespace :promotion do
task :validate do
posts_data.each do |path, data|
promotion = data['promotion']
if promotion && promotion.length > 115
fail ArgumentError, "ERROR: Promotion for \"#{data['title']}\" is too long! Keep under 115 characters."
end
end
puts 'No promotion errors found!'
end
end
namespace :categories do
task :list do
categories = []
posts_data.each do |path, data|
categories += data['categories'] if data['categories']
categories += [data['category']] if data['category']
end
puts categories.uniq.sort
end
task :validate do
errors = { total: 0, no_category: [], categories_found: [], not_in_list: [] }
posts_data.each do |path, data|
title = data['title']
unless data['category']
errors[:no_category] << title
errors[:total] += 1
end
if data['categories']
errors[:categories_found] << title
errors[:total] += 1
end
if data['category']
unless valid_categories.include? data['category']
errors[:not_in_list] << "#{title} -- #{data['category']}"
errors[:total] += 1
end
end
end
if errors[:total] > 0
display_errors errors, :no_category, 'The following posts lack a single category:'
display_errors errors, :categories_found, 'The following posts have multiple categories listed:'
display_errors errors, :not_in_list, 'The following have a unapproved category:'
fail "There were #{errors[:total]} errors found!"
else
puts 'No category errors found!'
end
end
end
namespace :tags do
desc 'Lists all the used tags ordered by frequency with amount used.'
task :list do
unique_tags.sort_by { |key, value| value }.reverse.each do | key, value|
puts "#{value} - #{key}\n"
end
end
desc 'Lists all the tags that are similar to other tags by letter match'
task :similar do
tags = unique_tags.keys
flagged = []
tags.each do |tag_a|
tags.each do |tag_b|
pair = [tag_a, tag_b].sort.join
unless tag_a == tag_b || flagged.include?(pair)
lnd = Levenshtein.normalized_distance(tag_a, tag_b, nil)
if lnd < 0.2
puts "#{tag_a} and #{tag_b}: #{(lnd * 100).round(2)}%"
flagged << pair
end
end
end
end
end
desc 'Searches for the query tag and opens the post.'
task :search, :query do |t, args|
posts_data.each do |path, data|
if data['tags'].include? args.query
system "subl #{File.join(Dir.pwd, path)}"
end
end
end
task :validate do
errors = { total: 0, no_tags: [], few_tags: [], overlap: [] }
posts_data.each do |path, data|
title = data['title']
unless data['tags']
errors[:no_tags] << title
errors[:total] += 1
end
if data['tags'].count < minimum_tags
errors[:few_tags] << "#{title} only has #{data['tags'].count} tags!"
errors[:total] += 1
end
if data['tags'].include? data['category']
errors[:overlap] << title
errors[:total] += 1
end
end
if errors[:total] > 0
display_errors errors, :no_tags, 'The following posts are untagged:'
display_errors errors, :few_tags, 'The following posts don\'t have enough tags:'
display_errors errors, :overlap, 'The following posts have a tag that is the same as the category:'
fail "There were #{errors[:total]} errors found!"
else
puts 'No tag errors found!'
end
end
end
end
desc 'Runs quality checks.'
task test: [:rubocop, 'posts:tags:validate', 'posts:categories:validate', 'posts:promotion:validate']
Rubocop::RakeTask.new
desc 'Removes the build directory.'
task :clean do
FileUtils.rm_rf 'build'
FileUtils.rm_rf ['.pygments-cache', '.sass-cache']
end
desc 'Adds the build tmp directory for test kit creation.'
task :prepare do
FileUtils.mkdir_p('build')
end
task deploy: [:clean, :prepare, :test] do
system 'git push origin master'
system 'git push heroku master'
Rake::Task[:social].execute
end
namespace :assets do
desc 'Precompile assets'
task :precompile do
sh 'bundle exec jekyll build'
end
end
# Helper functions
def posts_data
posts = Dir.glob(File.join('src', '_posts', '*.md'))
data = {}
posts.each do |path|
content = File.read(path)
if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
data["#{path}"] = YAML.load($1)
end
end
data
end
def display_errors(errors, key, prompt)
if errors[key].count > 0
puts "\nERRORS: #{prompt}\n\n"
puts errors[key].sort
end
end
def unique_tags
tags = Hash.new(0)
posts_data.each do |path, data|
if data['tags']
data['tags'].each do |tag|
tags[tag] += 1
end
end
end
tags
end