forked from garethr/devopsweekly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
96 lines (84 loc) · 2.54 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
# Originally adopted from Raimonds Simanovskis Rakefile,
# http://github.com/rsim/blog.rayapps.com/blob/master/Rakefile
# which was in turn adopted from Tate Johnson's Rakefile
# http://github.com/tatey/tatey.com/blob/master/Rakefile
require 'webrick'
require 'directory_watcher'
require "term/ansicolor"
require "jekyll"
require "rdiscount"
require "yaml"
include Term::ANSIColor
include WEBrick
task :default => :develop
desc 'Build site with Jekyll.'
task :build do
printHeader "Compiling website..."
options = Jekyll.configuration({})
@site = Jekyll::Site.new(options)
@site.process
end
def globs(source)
Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) }
dirs -= ['_site']
dirs = dirs.map { |x| "#{x}/**/*" }
dirs += ['*']
end
end
desc 'Enter development mode.'
task :develop => :build do
printHeader "Auto-regenerating enabled."
directoryWatcher = DirectoryWatcher.new("./")
directoryWatcher.interval = 1
directoryWatcher.glob = globs(Dir.pwd)
directoryWatcher.add_observer do |*args| @site.process end
directoryWatcher.start
mimeTypes = WEBrick::HTTPUtils::DefaultMimeTypes
mimeTypes.store 'js', 'application/javascript'
server = HTTPServer.new(
:BindAddress => "localhost",
:Port => 4000,
:DocumentRoot => "_site",
:MimeTypes => mimeTypes,
:Logger => Log.new($stderr, Log::ERROR),
:AccessLog => [["/dev/null", AccessLog::COMBINED_LOG_FORMAT ]]
)
thread = Thread.new { server.start }
trap("INT") { server.shutdown }
printHeader "Development server started at http://localhost:4000/"
printHeader "Opening website in default web browser..."
%x[open http://localhost:4000/]
printHeader "Development mode entered."
thread.join()
end
desc 'Remove all built files.'
task :clean do
printHeader "Cleaning build directory..."
%x[rm -rf _site]
end
desc 'Create new post'
task :new do
title = ask("Title: ")
article = {"title" => title, "layout" => "post"}.to_yaml
article << "---"
fileName = title.gsub(/[\s \( \) \? \[ \] \, \: \< \>]/, '-').downcase
path = "_posts/#{Time.now.strftime("%Y-%m-%d")}#{'-' + fileName}.textile"
unless File.exist?(path)
File.open(path, "w") do |file|
file.write article
end
puts "A new article was created at #{path}."
sh "vim " + path
else
puts "There was an error creating the article, #{path} already exists."
end
end
def ask message
print message
STDIN.gets.chomp
end
def printHeader headerText
print bold + blue + "==> " + reset
print bold + headerText + reset + "\n"
end