Repository navigation
Expand file tree
/
Copy pathupdateVersionList.sh
More file actions
executable file
·313 lines (239 loc) · 9.14 KB
/
Copy pathupdateVersionList.sh
File metadata and controls
executable file
·313 lines (239 loc) · 9.14 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env bash
#
# Run to update the HTML page with the available versions.
#
# Used HTML classes:
#
# aside : for messages of lesser importance
# reponame : a code repository name
# GITbranch : GIT hash or branch tag
# version : a code version
# currentversion: the current code version
# updateline : (block) the "last updated" line
# warning : a message about possible failure
#
#
# Changes
# --------
#
# 202004xx (petrillo@slac.stanford.edu) [v1.0]
# original version
# 20210412 (petrillo@slac.stanford.edu) [v1.1]
# added support for command line options
#
# -- BEGIN -- boilerplate settings and library loading -------------------------
SCRIPTNAME="$(basename "$0")"
SCRIPTDIR="$(dirname "$0")"
declare LibraryToLoad
for LibraryToLoad in 'settings.sh' 'utilities.sh' ; do
source "${SCRIPTDIR%/}/${LibraryToLoad}" || exit $?
done
unset LibraryToLoad
# -- END -- boilerplate settings and library loading ---------------------------
# ------------------------------------------------------------------------------
SCRIPTVERSION="1.1"
# additional settings
declare -r RepoDir="./"
# template for the version list HTML file, relative to the script directory
declare -r DefaultTemplateFileName='versionlisttemplate.html'
# the first line with this tag in the template will be replaced with the list
declare -r DefaultVersionListTag='VERSIONLISTTAG'
# the first line with this tag in the template will be replaced with update info
declare -r DefaultUpdateTag='UPDATETAG'
# ------------------------------------------------------------------------------
function printHelp() {
cat <<EOH
Updates the HTML file presenting the list of available documentation of
${ExperimentName:-the currently configured experiment}.
Usage: ${SCRIPTNAME} [options]
Supported options:
--experiment=EXPERIMENTNAME [${ExperimentName:-autodetect}]
the name of the experiment (e.g. 'MicroBooNE')
--outputfile=OUTPUTHTML
sets the list file to be created (by default, the repository name under the
configured PublishBaseDir${PublishBaseDir:+", i.e. '${PublishBaseDir}'"})
--reponame=REPONAME [${ExperimentCodeName:-autodetect}]
the name of the repository (e.g. 'uboonecode')
--version , -V
prints the script version and exits (hint: it's version ${SCRIPTVERSION}
--help , -h , -?
prints this usage message and exits
EOH
} # printHelp()
function printVersion() {
echo "${SCRIPTNAME} version ${SCRIPTVERSION}."
} # printVersion()
# ------------------------------------------------------------------------------
function FillVersionsList() {
local -r DocDir="${1%/}/"
[[ -d "$DocDir" ]] || FATAL 2 "Can't find documentation base directory '${DocDir}'."
DocVersions=() # global
local VersionCandidatePath
for VersionCandidatePath in "${DocDir}"* ; do
[[ -d "$VersionCandidatePath" ]] || continue
local VersionCandidate="$(basename "$VersionCandidatePath")"
[[ "$VersionCandidate" =~ ^v[[:digit:]]{2}_[[:digit:]]{2}_[[:digit:]]{2}(_[[:digit:]]{2})?$ ]] || continue
DocVersions+=( "$VersionCandidate" )
done
} # FillVersionsList()
function MakeVersionItem() {
local -r DocVersion="$1"
local -r DocPath="$2"
local -r Indent="${3:-" "}"
local ExpCodeName="$ExperimentCodeName"
local Version="$DocVersion"
local DocDate
local DocGenExitCode
local -r MetadataFile="${DocPath%/}/${MetadataFileRelPath}"
if [[ -r "$MetadataFile" ]]; then
# metadata file is currently (version 1.0) created by UpdateFiles.sh
local MetadataVersion
MetadataVersion="$(ExtractFromMetadata 'MetadataVersion' "$MetadataFile")"
ExpCodeName="$(ExtractFromMetadata 'ExperimentCode' "$MetadataFile")" # maybe overwrite
Version="$(ExtractFromMetadata 'CodeVersion' "$MetadataFile")" # maybe overwrite
DocDate="$(ExtractFromMetadata 'Date' "$MetadataFile")" # maybe overwrite
DocGenExitCode="$(ExtractFromMetadata 'ExitCode' "$MetadataFile")" # maybe overwrite
DocCodeGITreference="$(ExtractFromMetadata 'GITreference' "$MetadataFile")" # maybe overwrite
fi
local Msg="<span class=\"reponame\">${ExpCodeName}</span> <span class=\"version\">${Version}</span>"
if [[ -z "$DocGenExitCode" ]]; then
Msg+=' <span class="aside">(no additional information available)</span>'
else
Msg+=" (documentation from ${DocDate}"
if [[ -n "$DocCodeGITreference" ]] && [[ "$DocCodeGITreference" != "$Version" ]]; then
Msg+=" for GIt reference <span class=\"GITbranch\">${DocCodeGITreference}</span>'"
fi
Msg+=')'
if [[ "$DocGenExitCode" != 0 ]]; then
Msg+=" <span class=\"warning\">[it may be incomplete: generation exited with code ${DocGenExitCode}]</span>"
fi
fi
echo "${Indent}<li><a href=\"${DocVersion}\">${Msg}</a></li>"
} # MakeVersionItem()
function MakeHTMLlist() {
local -r DocDir="$1"
[[ -d "$DocDir" ]] || FATAL 2 "Can't find documentation base directory '${DocDir}'."
# check for the special link
if [[ -h "${DocDir%/}/${LatestLinkName}" ]]; then
local -r LatestVersion="$(readlink "${DocDir%/}/${LatestLinkName}")"
echo " <li><a href=\"${LatestLinkName}\" class=\"currentversion\">latest documented version (${LatestVersion})</a></li>"
fi
# look for the actual versions
local VersionCandidatePath
for VersionCandidatePath in "${DocDir%/}/"* ; do
[[ -d "$VersionCandidatePath" ]] || continue
local VersionCandidate="$(basename "$VersionCandidatePath")"
[[ "$VersionCandidate" =~ ^v[[:digit:]]{2}_[[:digit:]]{2}_[[:digit:]]{2}(_[[:digit:]]{2})?$ ]] || continue
MakeVersionItem "$VersionCandidate" "$VersionCandidatePath"
done
} # MakeHTMLlist()
function UpdatingRemarks() {
local -r Indent="${1:-" "}"
cat <<EOF
${Indent}
${Indent}<hr>
${Indent}<div class="updateline">Last list refresh: $(date)</div>
${Indent}
EOF
} # UpdatingRemarks()
function MakeVersionListFile() {
# Writes a HTML file on screen
local -r TemplateFilePath="$1"
local -r DocDir="$2"
#
# write a header comment
#
cat <<EOC
<!--
This HTML file was automatically generated:
Date: $(date)
Host: $(hostname)
Working directory: '$(pwd)'
Command: ${0} ${@}
Script version: ${SCRIPTVERSION}
-->
EOC
#
# "parse" the template
#
# the horror...
local Line
local OldIFS="$IFS"
IFS=''
while read Line ; do
if [[ "$Line" =~ ${VersionListTag} ]]; then # replace this line
MakeHTMLlist "$DocDir"
elif [[ "$Line" =~ ${UpdateTag} ]]; then # replace this line
UpdatingRemarks
else
# trying hard to avoid any reinterpretation; `echo` *might* have sufficed
cat <<< "$Line"
fi
done < "$TemplateFilePath"
IFS="$OldIFS"
} # MakeVersionListFile()
# ------------------------------------------------------------------------------
#
# argument parsing
#
declare -i NoMoreOptions
declare -i DoVersion=0 DoHelp=0
declare -i CleanOutputArea=0 UpdateOutputArea=0
declare TemplateFileName="$DefaultTemplateFileName"
declare VersionListTag="$DefaultVersionListTag"
declare UpdateTag="$DefaultUpdateTag"
declare ExitWithCode
declare Param
for (( iParam = 1 ; iParam <= $# ; ++iParam )); do
Param="${!iParam}"
if isFlagUnset NoMoreOptions && [[ "${Param:0:1}" == '-' ]]; then
case "$Param" in
( '--experiment='* ) ExperimentName="${Param#--*=}" ;;
( '--outputfile='* ) DestFile="${Param#--*=}" ;;
( '--reponame='* ) ExperimentCodeName="${Param#--*=}" ;;
( '--version' | '-V' ) DoVersion=1 ;;
( '--help' | '-h' | '-?' ) DoHelp=1 ;;
( '-' | '--' ) NoMoreOptions=1 ;;
( * )
ERROR "Unknown option: '${Param}'."
ExitWithCode=1
esac
else
PositionalArguments+=( "$Param" )
fi
done
[[ "${#PositionalArguments[@]}" -gt 0 ]] && FATAL 1 "Found ${#PositionalArguments[@]} spurious arguments on command line: ${PositionalArguments[@]}."
: ${ExperimentName:="$(FindExperiment)"}
: ${ExperimentCodeName:="$(ExperimentCodeProduct "$ExperimentName")"}
if isFlagSet DoVersion ; then
printVersion
[[ -z "$ExitWithCode" ]] && ExitWithCode=0
fi
if isFlagSet DoHelp ; then
printHelp
[[ -z "$ExitWithCode" ]] && ExitWithCode=0
fi
[[ -n "$ExitWithCode" ]] && exit "$ExitWithCode"
#
# checks
#
# (some checks are delayed because they are not fatal when just printing help)
#
[[ -n "$ExperimentName" ]] || FATAL 1 "Can't detect the experiment name."
[[ -n "$ExperimentCodeName" ]] || FATAL 1 "Can't put together the experiment code repository name for '${ExperimentName}'."
declare TemplateFilePath="${SCRIPTDIR%/}/${TemplateFileName}"
[[ -r "$TemplateFilePath" ]] || FATAL 2 "Can't find template file '${TemplateFileName}' in '${SCRIPTDIR}'."
# not controlled by option because used only to compose `DestFile`:
PublishDir="${PublishBaseDir%/}/${ExperimentCodeName}"
[[ -r "$PublishDir" ]] || FATAL 2 "Can't find publishing base directory '${PublishDir}'."
# set a default destination if needed
: ${DestFile:="${PublishDir%/}/${VersionListFile}"}
#
# write the HTML file
#
if [[ -e "$DestFile" ]]; then
echo "Overwriting: '${DestFile}'"
else
echo "Writing: '${DestFile}'"
fi
MakeVersionListFile "$TemplateFilePath" "$PublishDir" > "$DestFile"