-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-website.sh
executable file
·152 lines (135 loc) · 4.37 KB
/
publish-website.sh
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
#!/usr/bin/env zsh
#------------------------------------------------------------------------
# Publish a new version of the site.
# This script can be used to publish a new version of this site, which in
# this case means bump the version number and corresponding new timestamp
# that appears on the index page (optionally), the footer of every page in
# the Just the Docs template, and the _config.yaml file, which is where
# the footer gets the value.
# NOTE: While recommended, it's not required to use this script...
#------------------------------------------------------------------------
tsformat="%Y-%m-%d %H:%M %z"
script=$0
dir=$(dirname $script)
cfg="$dir/docs/_config.yml"
index="$dir/docs/index.markdown"
work_branch=main
publish_branch=latest
help() {
cat << EOF
$script [-h|--help] [-n|--noop] [-v|--version V] [-t|--timestamp T]
Where the options are the following:
-h | --help Print this message and exit
-n | --noop Just print the commands but don't make changes.
-v | --version V Use version string "V", which should be of the format
"X.Y.Z". Without this option the current value of
"last_version" in _config.yml is extracted (e.g., 1.0.1)
and the last digit is incremented.
-t | --timestamp "T" Use this timestamp "T", which you'll need to quote on
the command line, because it must be of the form
"$tsformat". Without this option, the current
system time is used.
EOF
}
error() {
for arg in "$@"
do
echo "ERROR: $arg"
done
help
exit 1
}
while [[ $# -gt 0 ]]
do
case $1 in
-h|--h*)
help
exit 0
;;
-n|--n*)
NOOP=echo
;;
-v|--v*)
shift
version="$1"
;;
-t|--t*)
shift
timestamp="$1"
;;
*)
error "Unrecognized option: $1"
;;
esac
shift
done
[[ -z "$timestamp" ]] && timestamp=$(date +"$tsformat")
date -j -f "$tsformat" +"$tsformat" "$timestamp" > /dev/null 2>&1
[[ $? -ne 0 ]] && error "Invalid timestamp format for timestamp: $timestamp" "Required format: $tsformat"
old_version=$(grep last_version "$cfg" | sed -e 's/^[ ]*last_version:[ ]*//')
base=${old_version%.*}
ext=${old_version##*.}
let ext1=$ext+1
new_default_version=${base}.${ext1}
[[ -z "$version" ]] && version="$new_default_version"
echo "Using the following values:"
echo "Version: $version (previous version: $old_version)"
echo "Timestamp: $timestamp"
branch=$(git branch --show-current)
[[ "$branch" != "$work_branch" ]] && error "You must be on the $work_branch branch to run this command."
cfg_temp=${cfg}.$$
if [[ -z "$NOOP" ]]
then
sed -e "s/\s*last_version:.*/last_version: $version/" $cfg > $cfg_temp
sed -e "s/\s*last_modified_timestamp:.*/last_modified_timestamp: $timestamp/" $cfg_temp > $cfg
rm $cfg_temp
else
$NOOP "sed -e \"s/\s*last_version:.*/last_version: $version/\" $cfg > $cfg_temp"
$NOOP "sed -e \"s/\s*last_modified_timestamp:.*/last_modified_timestamp: $timestamp/\" $cfg_temp > $cfg"
$NOOP rm $cfg_temp
fi
# Sanity checks:
if [[ -z $NOOP ]]
then
grep "last_version: $version" $cfg -q || error "New version $version not found in $cfg"
grep "last_modified_timestamp: $timestamp" $cfg -q || error "New timestamp $timestamp not found in $cfg"
fi
# Update the index page table with versions.
latest_history_line=$(grep '\*\*History\*\*' "$index")
latest_history=$(echo $latest_history_line | sed -e 's/[^V]*V\([^, ]*\).*/\1/')
if [[ "$latest_history" = "$version" ]]
then
echo "New version ($version) is the same as the old version in $index. Not changing that file."
else
mv $index $index.$$
cat $index.$$ | while read line
do
case $line in
**History**.*)
ymd=$(date -j -f "$tsformat" +"%Y-%m-%d" "$timestamp")
echo "| **History** | V$version, $ymd |"
old_line=$(echo "$line" | sed -e 's/\*\*History\*\*/ /')
echo $old_line
;;
*)
echo "$line"
;;
esac
done > $index
rm $index.$$
fi
# Commit and push the updated config file:
if [[ "$latest_history" = "$version" ]]
then
$NOOP git commit -s -m "Updated version and timestamps in $cfg" $cfg
else
$NOOP git commit -s -m "Updated version and timestamps in $cfg and $index" $cfg $index
fi
$NOOP git push
# Merge to latest and push to publish. Also tag it!
$NOOP git checkout $publish_branch
$NOOP git merge $work_branch
$NOOP git push
$NOOP git tag v${version}
$NOOP git push --tags
$NOOP git checkout $work_branch