forked from hcai-mms/hcai-mms.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
214 lines (178 loc) · 5.65 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
207
208
209
210
211
212
213
214
require "rubygems"
require "bundler/setup"
require "stringex"
## -- Misc Configs -- ##
public_dir = "_site" # compiled site directory
deploy_dir = "_deploy"
server_port = "4000" # port for preview server eg. localhost:4000
target_repo = "git@github.com:Caleydo/caleydo.github.io.git" # TODO?? https://jku-vds-lab.github.io/ ??
if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
puts '## Set the codepage to 65001 for Windows machines'
`chcp 65001`
end
#######################
# Working with Jekyll #
#######################
desc "Generate jekyll site"
task :generate do
puts "## Generating Site with Jekyll"
system "jekyll build"
end
desc "Watch the site and regenerate when it changes"
task :watch do
puts "Starting to watch source with Jekyll"
jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll build --watch")
trap("INT") {
[jekyllPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
}
[jekyllPid].each { |pid| Process.wait(pid) }
end
desc "Serve the site and regenerate when it changes"
task :serve do
puts "Starting to watch source with Jekyll"
jekyllPid = Process.spawn({"OCTOPRESS_ENV"=>"preview"}, "jekyll serve --watch")
trap("INT") {
[jekyllPid].each { |pid| Process.kill(9, pid) rescue Errno::ESRCH }
exit 0
}
[jekyllPid].each { |pid| Process.wait(pid) }
end
desc "Clean out caches: .pygments-cache, .gist-cache, .sass-cache"
task :clean do
rm_rf [".pygments-cache/**", ".gist-cache/**", ".sass-cache/**"]
end
##############
# check links #
##############
desc "Check all external links"
task :check_links do
require 'link_checker'
puts "checking links"
raise "## Link Checker errors" unless LinkChecker.new(
:target => '_site',
# :options => { :no_warnings => true }
).check_uris == 0
end
##############
# Patch config file #
##############
desc "patch the config file with the right url setting"
task :patch_config do
repo_url = target_repo
branch = (repo_url.match(/\/[\w-]+\.github\.(?:io|com)/).nil?) ? 'gh-pages' : 'master'
project = (branch == 'gh-pages') ? repo_url.match(/\/([^\.]+)/)[1] : ''
#patch the _config file with the right url
jekyll_config = IO.read('_config.yml')
target_url = blog_url(project)
puts "patch config file to use url: #{target_url}"
jekyll_config.sub!(/^url:.*$/, "url: \"#{target_url}\"")
if project != ''
jekyll_config.sub!(/^baseurl:.*$/, "baseurl: \"/#{project}\"")
end
File.open('_config.yml', 'w') do |f|
f.write jekyll_config
end
end
desc "revert the config file to its original"
task :unpatch_config do
puts "revert _config.yml"
system "git checkout _config.yml"
end
##############
# Deploying #
##############
desc "Default deploy task"
task :deploy do
# Check if preview posts exist, which should not be published
if File.exists?(".preview-mode")
puts "## Found posts in preview mode, regenerating files ..."
File.delete(".preview-mode")
Rake::Task[:generate].execute
end
Rake::Task["deployImpl"].execute
end
desc "copy dot files for deployment"
task :copydot, :source, :dest do |t, args|
FileList["#{args.source}/**/.*"].exclude("**/.", "**/..", "**/.DS_Store", "**/._*").each do |file|
cp_r file, file.gsub(/#{args.source}/, "#{args.dest}") unless File.directory?(file)
end
end
desc "deploy public directory to github pages"
task :deployImpl do
repo_url = target_repo
branch = (repo_url.match(/\/[\w-]+\.github\.(?:io|com)/).nil?) ? 'gh-pages' : 'master'
puts "## Deploying branch to Github Pages "
puts "## Pulling any updates from Github Pages "
puts "delete existing deployment dir"
FileUtils.remove_dir("#{deploy_dir}",true)
system "git clone #{repo_url} #{deploy_dir}"
(Dir["#{deploy_dir}/*"]).each { |f| rm_rf(f) }
puts "\n## Copying #{public_dir} to #{deploy_dir}"
Rake::Task[:copydot].invoke(public_dir, deploy_dir)
cp_r "#{public_dir}/.", deploy_dir
#remove CNAME entry for project specific repos
if branch == 'gh-pages' and File.exist?("#{deploy_dir}/CNAME")
FileUtils.rm("#{deploy_dir}/CNAME")
end
#find out the last commit date
lastcommit = ''
cd "#{deploy_dir}" do
#when was the new clone commited last
lastcommit = `git log -1 --format=%cd`.strip
end
#find out what happens inbetween
commits = `git log --since "#{lastcommit}" --format=\"%h %cd %cn: %s"`
cd "#{deploy_dir}" do
system "git add -A"
message = "Site updated at #{Time.now.utc}:\n#{commits}"
puts "\n## Committing: #{message}"
system "git commit -m \"#{message}\""
puts "\n## Pushing generated #{deploy_dir} website"
system "git push origin #{branch}"
puts "\n## Github Pages deploy complete"
end
end
##############
# Publish Chain #
##############
desc "Generate website and deploy"
task :publish => [:patch_config, :generate, :unpatch_config, :deploy] do #, :check_links
end
########################################################
def ok_failed(condition)
if (condition)
puts "OK"
else
puts "FAILED"
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def blog_url(project)
if (project == '')
url = if File.exists?('CNAME')
"https://#{IO.read('CNAME').strip}"
else
"https://jku-vds-lab.github.io/"
end
else
url = "https://jku-vds-lab.github.io/#{project}"
end
url
end
desc "list tasks"
task :list do
puts "Tasks: #{(Rake::Task.tasks - [Rake::Task[:list]]).join(', ')}"
puts "(type rake -T for more detail)\n\n"
end