This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
153 lines (126 loc) · 3.98 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
require 'rake-jekyll'
require 'yaml'
# Load the configuration file.
config = YAML.load_file '_config.yml'
# Set environment config file combinations.
testing_config = '_config.yml,_config.testing.yml'
dev_config = '_config.yml,_config.dev.yml'
# Set staging environment config file name.
staging_config_file = '_config.staging.yml'
# Spawn a server and kill it gracefully when interrupt is received.
def spawn *cmd
pid = Process.spawn 'bundle', 'exec', *cmd
switch = true
Signal.trap 'SIGINT' do
Process.kill(:QUIT, pid) && Process.wait
switch = false
end
while switch do sleep 1 end
end
# Command to build static site to destination (as an Array).
def build_site_command(destination=nil)
staging_config_file = '_config.staging.yml'
args = []
args.concat ['--destination', destination] unless destination.nil?
if File.exists? staging_config_file
args.concat ['--config', "_config.yml,#{staging_config_file}"]
end
['bundle', 'exec', 'jekyll', 'build', *args]
end
# Clean modifications from the staging environment.
def clean_staging
staging_config_file = '_config.staging.yml'
File.delete staging_config_file if File.exists? staging_config_file
sh 'git checkout robots.txt'
end
# Set `rake draft` as default task.
task default: [:draft]
# rake build
desc 'Generate the site'
task build: [:staging_env] do
clean_staging if ENV['STAGING_URL'].to_s.empty?
sh(*build_site_command)
clean_staging
end
# rake test
desc 'Generate the site and start a server (no auto-regeneration)'
task :test do
spawn 'jekyll', 'serve', '--no-watch', '--config', testing_config
end
# rake watch
desc 'Start a server and watch the site for changes'
task :watch do
spawn 'jekyll', 'serve', '--config', testing_config
end
# rake dev
desc 'Start a server and watch the site for changes:' + "\n" +
'loads _config.dev.yml as an additional config file'
task :dev do
spawn 'jekyll', 'serve', '--config', dev_config
end
# rake draft
desc 'Start a server and watch the site for changes:' + "\n" +
'include all drafts in site'
task :draft do
spawn 'jekyll', 'serve', '--drafts', '--config', testing_config
end
# rake deploy
desc 'Deploy the site using rsync'
task deploy: [:build] do
fail 'Error: must add :depoly: section to _config.yml.' if config['deploy'].nil?
# Load or set destination.
destination = File.join(
config[:destination] ? config[:destination] : '_site', '/')
# Load deploy settings.
local = File.expand_path destination
protocol = config['deploy']['protocol']
server = config['deploy']['server']
user = config['deploy']['user']
port = config['deploy']['port']
path = config['deploy']['path']
upload_only = config['deploy']['upload_only']
case protocol
when :rsync
flags = %w{-r -t -z -v}
if port.to_i != 22
flags << '-e'
flags << %Q{"ssh -p #{port}"}
end
excludes = upload_only.nil? ? [] : upload_only.collect { |e| "--exclude='#{e}'" }
rsync = ['rsync', *flags, '--del', *excludes,
"#{local}/", "#{user}@#{server}:#{path}"].join(' ')
sh rsync
if upload_only
rsync_uploads = [
'rsync', *flags,
"#{local}/", "#{user}@#{server}:#{path}"
].join(' ')
sh rsync_uploads
end
end
end
# rake publish
Rake::Jekyll::GitDeployTask.new(:publish) do |t|
t.description = 'Generate the site and push changes to remote repository'
t.jekyll_build = -> (dest_dir) {
Rake.sh(*build_site_command(dest_dir))
}
end
# rake staging_env
desc 'Prepare the staging environment'
task :staging_env do
url = ENV['STAGING_URL'].to_s
next if url.empty?
staging_config = {
'domain' => url, 'baseurl' => url,
'assets' => {'baseurl' => "#{url}/assets"}
}
File.open(staging_config_file, 'w') { |f| f.write staging_config.to_yaml }
File.open('robots.txt', 'w') { |f| f.write "User-agent: *\nDisallow: /" }
cname = ENV['CNAME'].to_s
if cname == 'false'
File.delete('CNAME') if File.exists?('CNAME')
elsif !cname.empty?
File.open('CNAME', 'w') { |f| f.write cname }
end
end