-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
206 lines (174 loc) · 5.3 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
# Standard library
require 'rake'
require 'yaml'
require 'fileutils'
# Load the configuration file
config = YAML.load_file("_config.yml")
# Set "rake watch" as default task
task :default => :watch
# rake post["Post title"]
desc "Create a post in _posts"
task :post, :title do |t, args|
title = args[:title]
template = config["post"]["template"]
extension = config["post"]["extension"]
editor = config["editor"]
if title.nil? or title.empty?
raise "Please add a title to your post."
end
date = Time.now.strftime("%Y-%m-%d")
filename = "#{date}-#{title.gsub(/(\'|\!|\?|\:|\s\z)/,"").gsub(/\s/,"-").downcase}.#{extension}"
content = File.read(template)
if File.exists?("_posts/#{filename}")
raise "The post already exists."
else
parsed_content = "#{content.sub("title:", "title: \"#{title}\"")}"
File.write("_posts/#{filename}", parsed_content)
puts "#{filename} was created."
if editor && !editor.nil?
sleep 2
system "#{editor} _posts/#{filename}"
end
end
end
# rake draft["Post title"]
desc "Create a post in _drafts"
task :draft, :title do |t, args|
title = args[:title]
template = config["post"]["template"]
extension = config["post"]["extension"]
editor = config["editor"]
if title.nil? or title.empty?
raise "Please add a title to your post."
end
filename = "#{title.gsub(/(\'|\!|\?|\:|\s\z)/,"").gsub(/\s/,"-").downcase}.#{extension}"
content = File.read(template)
if File.exists?("_drafts/#{filename}")
raise "The post already exists."
else
parsed_content = "#{content.sub("title:", "title: \"#{title}\"")}"
File.write("_drafts/#{filename}", parsed_content)
puts "#{filename} was created."
if editor && !editor.nil?
sleep 2
system "#{editor} _drafts/#{filename}"
end
end
end
# rake publish
# rake publish["post-title"]
desc "Move a post from _drafts to _posts"
task :publish, :post do |t, args|
post = args[:post]
extension = config["post"]["extension"]
if post.nil? or post.empty?
Dir["_drafts/*.#{extension}"].each do |filename|
list = File.basename(filename, ".*")
puts list
end
else
date = Time.now.strftime("%Y-%m-%d")
filename = "#{post}.#{extension}"
FileUtils.mv("_drafts/#{filename}", "_posts/#{date}-#{filename}")
puts "#{filename} was moved to _posts."
end
end
# rake page["Page title"]
# rake page["Page title","Path/to/folder"]
desc "Create a page (with an optional filepath)"
task :page, :title, :path do |t, args|
title = args[:title]
filepath = args[:path]
template = config["page"]["template"]
extension = config["page"]["extension"]
editor = config["editor"]
if title.nil? or title.empty?
raise "Please add a title to your page."
end
if filepath.nil? or filepath.empty?
filepath = "./"
else
FileUtils.mkdir_p("#{filepath}")
end
filename = "#{title.gsub(/(\'|\!|\?|\:|\s\z)/,"").gsub(/\s/,"-").downcase}.#{extension}"
content = File.read(template)
if File.exists?("#{filepath}/#{filename}")
raise "The page aldready exists."
else
parsed_content = "#{content.sub("title:", "title: \"#{title}\"")}"
File.write("#{filepath}/#{filename}", parsed_content)
puts "#{filename} was created in #{filepath}."
if editor && !editor.nil?
sleep 2
system "#{editor} #{filepath}/#{filename}"
end
end
end
# rake build
desc "Generate the site (no server)"
task :build do
system "jekyll --no-server --no-auto"
end
# rake watch
# rake watch[number]
desc "Generate and watch the site (with an optional post limit)"
task :watch, :number do |t, args|
number = args[:number]
if number.nil? or number.empty?
system "jekyll --auto --server"
else
system "jekyll --auto --server --limit_posts=#{number}"
end
end
# rake preview
desc "Launch a preview of the site in the browser"
task :preview do
require 'Launchy'
Thread.new do
puts "Launching browser for preview..."
sleep 2
Launchy.open("http://localhost:4000/")
end
Rake::Task[:watch].invoke
end
# rake deploy["Commit message"]
desc "Deploy the site to a remote git repository"
task :deploy, :message do |t, args|
message = args[:message]
branch = config["git"]["branch"]
if message.nil? or message.empty?
raise "Please add a commit message."
end
if branch.nil? or branch.empty?
raise "Please add a branch."
else
Rake::Task[:build].invoke
system "git add ."
system "git commit -m \"#{message}\""
system "git push origin #{branch}"
end
end
# rake transfer
desc "Transfer the site to a remote server or a local git repository"
task :transfer do
command = config["transfer"]["command"]
source = config["transfer"]["source"]
destination = config["transfer"]["destination"]
settings = config["transfer"]["settings"]
if command.nil? or command.empty?
raise "Please choose a file transfer command."
elsif command == "robocopy"
Rake::Task[:build].invoke
system "robocopy #{source} #{destination} #{settings}"
puts "Your site was transfered."
elsif command == "rsync"
Rake::Task[:build].invoke
system "rsync #{settings} #{source} #{destination}"
puts "Your site was transfered."
elsif command == "jekyll-s3"
Rake::Task[:build].invoke
system "jekyll-s3"
else
raise "#{command} isn't a valid file transfer command."
end
end