-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
106 lines (88 loc) · 2.47 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
require 'i18n'
require 'readline'
require 'socket'
I18n.available_locales = [:en, :fr]
# Helpers
# =======
# Execute a system command, but raise an exception in case of error.
def system!(*args)
result = system(*args)
if result.nil?
raise StandardError, "Command execution failed (#{$?})"
elsif result == false
raise StandardError, "Command returned a non-zero exit code (#{$?})"
end
end
# Retry a block during some time if the given exception is thrown.
def retriable(exceptions:, timeout:)
return_value = false
start = Time.now
begin
yield
rescue *exceptions => e
if (Time.now - start < timeout) then retry else raise StandardError.new("Timeout") end
end
end
# Tasks
# =====
desc "Run the website locally"
task :run do
port = 4000
# Wait for the local server to be ready, then open the default web browser
pid = Process.fork do
retriable(exceptions: [Errno::ECONNREFUSED, Errno::ETIMEDOUT], timeout: 10) do
Socket.tcp("127.0.0.1", port, connect_timeout: 1) {}
end
puts "Rake: localhost:#{port} is reachable. Opening the default web browser…"
system!("open http://localhost:#{port}/")
end
Process.detach(pid)
system!("bundle exec jekyll serve")
end
desc "Create a new empty post"
task :new do
title = Readline.readline("Title: ", false)
raise "Title must be provided" if title == ""
slug = ""
suggested_slug = I18n.transliterate(title).downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
answer = Readline.readline("Slug [#{suggested_slug}]: ", false)
if answer != ""
slug = answer
else
slug = suggested_slug
end
date = Time.now.strftime('%Y-%m-%d')
date_and_time = Time.now.strftime('%Y-%m-%d %H:%M')
post_file = "_posts/#{date}-#{slug}.md"
File.open(post_file, 'a+') do |f|
f.write <<-FILE.gsub(/^\s*/, '')
---
layout: post
title: "#{title}"
date: #{date_and_time}
---
FILE
end
editor = ENV['BLOG_EDITOR'] || ENV['EDITOR']
if editor
system!("#{editor} #{post_file}")
end
end
desc "Generate the static blog files"
task :build do
system!('bundle exec jekyll build -d public')
end
desc "Commit the changes (if any)"
task :commit do
has_changes = system('test -n "`git status --porcelain`"')
if has_changes
system!('git add -A && git commit')
end
end
desc "Push sources to Github (commiting the changes before if any)"
task :push => [:commit] do
system!('git push')
end
desc "Build, commit and push the site"
task :publish => [:build, :push] do
end