-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
146 lines (129 loc) · 3.85 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
# Rakefile to help with standard blog activities
# Inspired by https://github.com/Bilalh/bilalh.github.com/blob/source/Rakefile
require 'fileutils'
##################################################################
##### Constants
##################################################################
#pid file for the jekyll process
PID_FILE = '/tmp/jekyll.pid'
ERR_FILE = '/tmp/jekyll.error'
##################################################################
##### Helper Methods
##################################################################
# Get the jekyll pid, if its running
# if its not running, throws Errno::ESRCH
def getJekyllPid
if File.exists? PID_FILE
pid = Integer(File.read(PID_FILE))
#if jekyll is alread running, then we are done
Process.getpgid( pid )
return pid
else
throw Errno::ESRCH
end
end
##################################################################
###### Tasks
##################################################################
task :default => :start
task :start => :build
desc "Build from source and start the jekyll server (if not running already)"
task :build do
#grep for a jekyll process
begin
getJekyllPid
started = true
rescue
#NOOP - not started
end
#if the process isn't started, start it
unless started
# cleanup old site files
FileUtils.rm_rf('_site')
puts "Starting jeykll server http://0.0.0.0:4000/..."
#Store the pid of jeykll server
File.open(PID_FILE, 'w+') do |f|
# Spawn a new process and run the rake command
puts "Writing errors to #{ERR_FILE}"
pid = Process.spawn("jekyll server --incremental --watch --drafts", :out => '/dev/null', :err => ERR_FILE)
puts "Started with pid ", pid
f.puts pid
# Detach the spawned process
Process.detach pid
end
end
end
desc "Get the stored pid on which jekyll should be running"
task :pid do
pid = getJekyllPid()
puts "Jekyll expected on pid: \t#{pid}"
actual = `ps aux | grep jekyll | grep -v grep | awk '{print $2}'`
puts "Actually: \t\t\t#{actual}"
end
desc "Stop the jekyll server, if it is running already"
task :stop do
begin
pid = getJekyllPid()
puts "Jekyll running on pid: #{pid}, stopping..."
Process.kill("TERM", pid)
File.delete(PID_FILE)
puts "Killed jekyll"
rescue Exception => e
puts "\t#{e.backtrace.first}: #{e.message} (#{e.class})"
puts "Jekyll already stopped."
end
end
def createPost(path, title)
File.open(path, "w") do |f|
f.puts "---"
f.puts "layout: post"
f.puts "title: #{title}"
f.puts "location: San Francisco, CA"
f.puts "subtitle:"
f.puts "tags:"
f.puts "---"
end
end
def sanitizeTitle(title)
return title.downcase.gsub(/[\s\.]/, '-').gsub(/[^\w\d\-]/, '')
end
def getPostPath(title)
now = Time.now
return "_posts/#{now.strftime('%F')}-#{title}.md"
end
desc "Makes a new post - rake new <post title>"
task :newPost do
throw "No title given" unless ARGV[1]
title = ""
ARGV[1..ARGV.length - 1].each { |v| title += " #{v}" }
title.strip!
path = getPostPath(sanitizeTitle(title))
createPost(path, title)
exec("vim +4 #{path}")
exit
end
desc "Create a new draft post - rake draft <post title>"
task :draft do
throw "No title given" unless ARGV[1]
title = ""
ARGV[1..ARGV.length - 1].each { |v| title += " #{v}" }
title.strip!
path = "_drafts/#{sanitizeTitle(title)}.md"
createPost(path, title)
exec("vim +4 #{path}")
exit
end
desc "Move a draft to full post - rake publish <path to file>"
task :publish do
title = ARGV.last
# Cleanup the title to just the name of the post
cleanTitle = title.gsub("_drafts/", '')
offset = cleanTitle.rindex(".md")
cleanTitle = cleanTitle.slice(0, offset) unless offset.nil?
# find to whence we should move the path for a post
postPath = getPostPath(cleanTitle)
# do the actual move
FileUtils.mv(title, postPath)
# exit because Rake will find the '.' in the file name as somehow being a new rake task
exit
end