This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
204 lines (168 loc) · 6.52 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
require 'rake'
require 'html-proofer'
require 'date'
require 'yaml'
CONFIG = YAML.load(File.read('_config.yml'))
# remove generated site
def cleanup
sh 'rm -Rf _site'
end
# launch jekyll
def jekyll(directives = '')
sh 'bundle exec jekyll ' + directives
end
#############################################################################
# Development tasks
#############################################################################
# Usage: rake dev, rake dev:serve
task :dev => ["dev:serve"]
namespace :dev do
desc 'Serve development Jekyll site locally'
task :serve do
puts 'Starting up development Jekyll site server...'
jekyll('serve --future --config _config.yml,_config_dev.yml')
end
desc 'Build development Jekyll site'
task :build => :clean do
puts 'Building development Jekyll site...'
jekyll('build --future --config _config.yml,_config_dev.yml')
end
desc 'Check links: site needs to be running.'
task :test do
HTMLProofer.check_directories(["_site/"], {
:check_favicon => false,
:empty_alt_ignore => false,
:href_ignore => ["#", "http://localhost:4000", /^(https?\:\/\/)?(www\.)?youtube\.com\/.+$/],
:parallel => {:in_processes => 4},
:only_4xx => true,
:check_html => false,
:typhoeus => {
:timeout => 3 }
}).run
end
desc 'Clean up generated site'
task :clean do
cleanup
end
end
#############################################################################
# Production tasks
#############################################################################
# Usage: rake prod, rake prod:build, prod:deploy
task :prod => ['prod:build']
namespace :prod do
desc 'Build production Jekyll site'
task :build => :clean do
puts 'Building production Jekyll site...'
jekyll('build --future')
end
desc 'Build draft Jekyll site for Netlify'
task :buildnetlify => :clean do
puts 'Building Jekyll site for Netlify...'
jekyll('build --future --config _config.yml,_config_netlify.yml')
end
desc 'Generate the site and push changes to remote origin'
task :deploy do
# Configure git if this is run in Travis CI, see https://github.com/openingscience/book/blob/master/Rakefile
if ENV['TRAVIS']
puts 'Under TraviCI control...'
# Detect pull request
if ENV['TRAVIS_PULL_REQUEST'].to_s.to_i > 0
puts 'Pull request detected. Not proceeding with deploy.'
exit 0
end
puts 'setting user configs ...'
sh "git config --global user.name '#{ENV['GIT_NAME']}'"
sh "git config --global user.email '#{ENV['GIT_EMAIL']}'"
sh "git config --global push.default simple"
# deploy only if on master branch eventually
if ENV['TRAVIS_BRANCH'].to_s.scan(/^master$/).length > 0
puts 'Handling "master" branch'
# get the tag for HEAD
committag = `git describe --exact-match $(git rev-parse HEAD)`.delete("\n")
puts "here is the tag I found: -->#{committag}<--"
if committag.length == 0
puts "No tag! Hence not deploying to github.com/#{CONFIG['dest_user']}/#{CONFIG['dest_repo']}.github.io"
exit 0
else
puts "Building and deploying with tag '#{committag}'"
# Generate the site...it goes in _site
puts 'Generating site...'
jekyll('build --future')
# cloning destination repo
puts "Cloning repo #{CONFIG['dest_user']}/#{CONFIG['source_repo']} ..."
sh "git clone --depth=1 https://#{ENV['GIT_NAME']}:#{ENV['GH_TOKEN']}@github.com/#{CONFIG['dest_user']}/#{CONFIG['dest_repo']}.git > /dev/null"
# Commit and push to github
# - match the commit SHA
sha = `git rev-parse HEAD`.delete("\n")
puts 'Now moving to #{CONFIG["dest_repo"]...'
Dir.chdir("#{CONFIG['dest_repo']}") do
puts 'Removing all...'
sh "git rm -rf * > /dev/null"
puts 'Copying all newly generated pages from "../_site/"...'
sh "cp -r ../_site/* ."
puts 'Adding all files for a new commit...'
sh "git add --all ."
sh "git status"
puts 'Committing all...'
sh "git commit -m 'Updating to github.com/#{ENV['TRAVIS_REPO_SLUG']}/@#{sha} tagged #{committag}.'"
# Make sure to make the output quiet, or else the API token will leak!
puts '(Indirectly) Pushing to the Internet...'
sh "git push --force --quiet origin master"
puts "Destination repo, #{CONFIG['dest_user']}/#{CONFIG['dest_repo']}, pushed to GitHub Pages."
end
end
else
puts "Not in 'master' branch, hence no deployment to #{CONFIG['dest_user']}/#{CONFIG['dest_repo']}"
end
end
end
desc 'Clean up generated site'
task :clean do
cleanup
end
end
######################
# Notification tasks #
######################
# Notify the search engines to crawl again the newly published site
# Usage: rake notify
task :notify => ["notify:pingomatic", "notify:google", "notify:bing"]
desc "Notify various services that the site has been updated"
namespace :notify do
desc "Notify Ping-O-Matic"
task :pingomatic do
begin
require 'xmlrpc/client'
puts "* Notifying Ping-O-Matic that the site has updated"
XMLRPC::Client.new('rpc.pingomatic.com', '/').call('weblogUpdates.extendedPing',
'ansperformance.eu' ,
'http://ansperformance.eu',
'http://ansperformance.eu/atom.xml')
rescue LoadError
puts "! Could not ping ping-o-matic, because XMLRPC::Client could not be found."
end
end
desc "Notify Google of updated sitemap"
task :google do
begin
require 'net/http'
require 'uri'
puts "* Notifying Google that the site has updated"
Net::HTTP.get('www.google.com', '/webmasters/tools/ping?sitemap=' + URI.escape('http://ansperformance.eu/sitemap.xml'))
rescue LoadError
puts "! Could not ping Google about our sitemap, because Net::HTTP or URI could not be found."
end
end
desc "Notify Bing of updated sitemap"
task :bing do
begin
require 'net/http'
require 'uri'
puts '* Notifying Bing that the site has updated'
Net::HTTP.get('www.bing.com', '/webmaster/ping.aspx?siteMap=' + URI.escape('http://ansperformance.eu/sitemap.xml'))
rescue LoadError
puts "! Could not ping Bing about our sitemap, because Net::HTTP or URI could not be found."
end
end
end