Add a rakefile with some basics
[blog.git] / Rakefile
1 # == Dependencies ==============================================================
2
3 require 'rake'
4 require 'yaml'
5 require 'fileutils'
6 require 'rbconfig'
7
8 # == Configuration =============================================================
9
10 # Set "rake watch" as default task
11 task :default => :watch
12
13 # Load the configuration file
14 CONFIG = YAML.load_file("_rakeconfig.yml")
15
16 # Get and parse the date
17 DATE = Time.now.strftime("%Y-%m-%d")
18 TIME = Time.now.strftime("%H:%M:%S")
19 POST_TIME = DATE + ' ' + TIME
20
21 # Directories
22 POSTS = "_posts"
23 DRAFTS = "_drafts"
24
25 # == Helpers ===================================================================
26
27 # Execute a system command
28 def execute(command)
29 system "#{command}"
30 end
31
32 # Chech the title
33 def check_title(title)
34 if title.nil? or title.empty?
35 raise "Please add a title to your file."
36 end
37 end
38
39 # Transform the filename and date to a slug
40 def transform_to_slug(title, extension)
41 characters = /("|'|!|\?|:|\s\z)/
42 whitespace = /\s/
43 "#{title.gsub(characters,"").gsub(whitespace,"-").downcase}.#{extension}"
44 end
45
46 # Read the template file
47 def read_file(template)
48 File.read(template)
49 end
50
51 # Save the file with the title in the YAML front matter
52 def write_file(content, title, directory, filename)
53 parsed_content = "#{content.sub("title:", "title: \"#{title}\"")}"
54 parsed_content = "#{parsed_content.sub("date:", "date: #{POST_TIME}")}"
55 File.write("#{directory}/#{filename}", parsed_content)
56 puts "#{filename} was created in '#{directory}'."
57 end
58
59 # Create the file with the slug and open the default editor
60 def create_file(directory, filename, content, title, editor)
61 FileUtils.mkdir(directory) unless File.exists?(directory)
62 if File.exists?("#{directory}/#{filename}")
63 raise "The file already exists."
64 else
65 write_file(content, title, directory, filename)
66 if editor && !editor.nil?
67 sleep 1
68 execute("#{editor} #{directory}/#{filename}")
69 end
70 end
71 end
72
73 # Get the "open" command
74 def open_command
75 if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
76 "start"
77 elsif RbConfig::CONFIG["host_os"] =~ /darwin/
78 "open"
79 else
80 "xdg-open"
81 end
82 end
83
84 # == Tasks =====================================================================
85
86 # rake post["Title"]
87 desc "Create a post in _posts"
88 task :post, :title do |t, args|
89 title = args[:title]
90 template = CONFIG["post"]["template"]
91 extension = CONFIG["post"]["extension"]
92 editor = CONFIG["editor"]
93 check_title(title)
94 filename = "#{DATE}-#{transform_to_slug(title, extension)}"
95 content = read_file(template)
96 create_file(POSTS, filename, content, title, editor)
97 end
98
99 # rake draft["Title"]
100 desc "Create a post in _drafts"
101 task :draft, :title do |t, args|
102 title = args[:title]
103 template = CONFIG["post"]["template"]
104 extension = CONFIG["post"]["extension"]
105 editor = CONFIG["editor"]
106 check_title(title)
107 filename = transform_to_slug(title, extension)
108 content = read_file(template)
109 create_file(DRAFTS, filename, content, title, editor)
110 end
111
112 # rake publish
113 desc "Move a post from _drafts to _posts"
114 task :publish do
115 extension = CONFIG["post"]["extension"]
116 files = Dir["#{DRAFTS}/*.#{extension}"]
117 files.each_with_index do |file, index|
118 puts "#{index + 1}: #{file}".sub("#{DRAFTS}/", "")
119 end
120 print "> "
121 number = $stdin.gets
122 if number =~ /\D/
123 filename = files[number.to_i - 1].sub("#{DRAFTS}/", "")
124 FileUtils.mv("#{DRAFTS}/#{filename}", "#{POSTS}/#{DATE}-#{filename}")
125 puts "#{filename} was moved to '#{POSTS}'."
126 else
127 puts "Please choose a draft by the assigned number."
128 end
129 end
130
131 # rake page["Title"]
132 # rake page["Title","Path/to/folder"]
133 desc "Create a page (optional filepath)"
134 task :page, :title, :path do |t, args|
135 title = args[:title]
136 template = CONFIG["page"]["template"]
137 extension = CONFIG["page"]["extension"]
138 editor = CONFIG["editor"]
139 directory = args[:path]
140 if directory.nil? or directory.empty?
141 directory = "./"
142 else
143 FileUtils.mkdir_p("#{directory}")
144 end
145 check_title(title)
146 filename = transform_to_slug(title, extension)
147 content = read_file(template)
148 create_file(directory, filename, content, title, editor)
149 end
150
151 # rake build
152 desc "Build the site, optionally with changed destination path"
153 task :build, :option do |t, args|
154 option = args[:option]
155 if option.nil? or option.empty?
156 execute("jekyll build")
157 else
158 execute("jekyll build -d #{option}")
159 end
160 end
161
162 # rake watch
163 # rake watch[number]
164 # rake watch["drafts"]
165 desc "Serve and watch the site (with post limit or drafts)"
166 task :watch, :option do |t, args|
167 option = args[:option]
168 interrupted = false
169 trap("INT") { interrupted = true }
170 if option.nil? or option.empty?
171 execute("jekyll serve --incremental --watch")
172 else
173 if option == "drafts"
174 execute("jekyll serve --watch --drafts")
175 else
176 execute("jekyll serve --watch --limit_posts #{option}")
177 end
178 end
179 if interrupted
180 execute("rm -rf _site/")
181 end
182 end
183
184 # rake preview
185 desc "Launch a preview of the site in the browser"
186 task :preview do
187 port = CONFIG["port"]
188 if port.nil? or port.empty?
189 port = 4000
190 end
191 Thread.new do
192 puts "Launching browser for preview..."
193 sleep 1
194 execute("#{open_command} http://localhost:#{port}/")
195 end
196 Rake::Task[:watch].invoke
197 end
198
199 # rake deploy["Commit message"]
200 desc "Deploy the site to a remote git repo"
201 task :deploy, :message do |t, args|
202 message = args[:message]
203 branch = CONFIG["git"]["branch"]
204 if message.nil? or message.empty?
205 raise "Please add a commit message."
206 end
207 if branch.nil? or branch.empty?
208 raise "Please add a branch."
209 else
210 Rake::Task[:build].invoke
211 execute("git add .")
212 execute("git commit -m \"#{message}\"")
213 execute("git push origin #{branch}")
214 end
215 end