-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rakefile
112 lines (92 loc) · 2.77 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
# This Rakefile is forked from: https://github.com/edgecase/middleman-gh-pages
require "fileutils"
require "tmpdir"
def remote_name
ENV.fetch("REMOTE_NAME", "origin")
end
def branch_name
ENV.fetch("BRANCH_NAME", "gh-pages")
end
def uncommitted_changes?
`git status --porcelain`.chomp.length > 0
end
def backup_and_restore(dir, file, &block)
return yield unless File.exist?(File.join(dir, file))
Dir.mktmpdir do |tmpdir|
mv File.join(dir, file), tmpdir
yield
mv File.join(tmpdir, file), dir
end
end
PROJECT_ROOT = ENV.fetch("PROJECT_ROOT", `git rev-parse --show-toplevel`.chomp)
BUILD_DIR = ENV.fetch("BUILD_DIR", File.join(PROJECT_ROOT, "build"))
GH_PAGES_REF = File.join(BUILD_DIR, ".git/refs/remotes/#{remote_name}/#{branch_name}")
directory BUILD_DIR
file GH_PAGES_REF => BUILD_DIR do
repo_url = nil
cd PROJECT_ROOT do
repo_url = `git config --get remote.#{remote_name}.url`.chomp
end
cd BUILD_DIR do
sh "git init"
sh "git remote add #{remote_name} #{repo_url}"
sh "git fetch #{remote_name} #{branch_name}"
if `git branch -r` =~ /#{branch_name}/
sh "git checkout #{branch_name}"
else
sh "git checkout --orphan #{branch_name}"
FileUtils.touch("index.json")
sh "git add ."
sh "git commit -m \"initial gh-pages commit\""
sh "git push #{remote_name} #{branch_name}"
end
end
end
# Alias to something meaningful
task :prepare_build_dir => GH_PAGES_REF
# Fetch upstream changes on gh-pages branch
task :sync_build_dir => :prepare_build_dir do
cd BUILD_DIR do
sh "git fetch #{remote_name} #{branch_name}"
sh "git reset --hard #{remote_name}/#{branch_name}"
end
end
# Prevent accidental publishing before committing changes
task :prevent_dirty_builds do
if uncommitted_changes?
puts "*** WARNING: You currently have uncommitted changes. ***"
fail "Build aborted, because project directory is not clean." unless ENV["ALLOW_DIRTY"]
end
end
desc "Remove all files from the build directory"
task :clean_json do
cd BUILD_DIR do
sh "rm -rf *"
end
end
desc "Compile all files into the build directory"
task :build => :clean_json do
backup_and_restore(BUILD_DIR, ".git") do
cd PROJECT_ROOT do
sh "bundle exec ruby generate_api.rb"
end
end
end
desc "Build and publish to Github Pages"
task :publish => [:prevent_dirty_builds, :sync_build_dir, :build] do
message = nil
suffix = ENV["COMMIT_MESSAGE_SUFFIX"]
cd PROJECT_ROOT do
head = `git log --pretty="%h" -n1`.chomp
message = ["Site updated to #{head}", suffix].compact.join("\n\n")
end
cd BUILD_DIR do
sh "git add --all"
if uncommitted_changes?
sh "git commit -m \"#{message}\""
else
puts "No changes to commit."
end
sh "git push #{remote_name} #{branch_name}"
end
end