-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·334 lines (278 loc) · 7.99 KB
/
build.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/bin/bash
#
# Copyright 2020-2023 SUSE Linux GmbH
#
# Dependencies
# Our main files are geekodoc-v?.rnc. It depends on:
# * transclusion.rnc
# a failed attempt to work with transclusions; can be probably removed
# * its.rnc
# integration of ITS 2.0 markup, see http://www.w3.org/TR/its20/
# * docbook.rnc/dbits.rnc
# the upstream schema files
#
# Process Flow
# geekodoc-v?.rnc # (1)
# +--> geekodoc-v?.rng #(2)
# +--> geekodoc-v?-flat.rni # (3)
# +--> geekodoc-v?-flat.rng # (4)
# +--> geekodoc-v?-flat.rnc # (5)
#
# (1) Our main files; all changes go here
# (2) We need to create the XML format (RNG) first to process it with
# our XML tools
# (3) The flat.rni file contains the "raw" representation of the
# schema in one file; it is an intermediate file (that's why it
# has an 'i' in the file extension) and is automatically
# removed
# (4) Apply cleanups to *-flat.rni to create *-flat.rng
# (5) The end result, which can be used independently from DocBook or Geekodoc
# source files
#
# Author: Thomas Schraitle
# Date: August 2021
# Add some precautions:
# Exit when the script tries to use undeclared variables.
set -o nounset
# set -x
# --- color codes
RED="\e[1;31m"
# VIOLET="\e[35m"
# BLUE="\e[94m"
YELLOW="\e[93m"
CYAN="\e[36m"
# GRAY="\e[37m"
BOLD="\e[1m"
# NORMAL=""
# REVERSE="\e[8m"
RESET="\e[0m"
# --- Global variables
ME="${0##*/}"
MYDIR=$(realpath $(dirname "$0"))
VERBOSITY=2
LOGGING_LEVEL="DEBUG"
declare -A LOGLEVELS=([DEBUG]=0 [INFO]=1 [WARN]=2 [ERROR]=3)
declare -A LEVEL2LOG=([0]="ERROR" [1]="WARN" [2]="INFO" [3]="DEBUG")
declare -A LOGCOLORS=([DEBUG]=$CYAN [INFO]=$BOLD [WARN]=$YELLOW [ERROR]=$RED)
LATEST_VERSION="2_5.2"
# -- Paths
GEEKODOC_DIR="geekodoc"
GEEKODOC_RNG_DIR=${GEEKODOC_DIR}/rng
GEEKODOC1_PATH=${GEEKODOC_RNG_DIR}/1_5.1
GEEKODOC2_PATH=${GEEKODOC_RNG_DIR}/${LATEST_VERSION}
# XSLT_DIR=${GEEKODOC_DIR}/xsl
BUILD_DIR="build"
DIST_DIR="dist"
EXTERNAL_DIR="external"
DOCBOOK5_SRC_DIR="$EXTERNAL_DIR/docbook5"
RNG_SRC_DIR="$EXTERNAL_DIR/relaxng"
RELAXNG_SCHEMA="$RNG_SRC_DIR/relaxng.rng"
# === Naming
# The naming was unfortunate, so geekodoc5 version 1 refers to DocBook5,
# not Geekodoc v5;
# we'll provide a compatibility link from geekodoc-v1 -> geekodoc5
GEEKODOC1_NAME="geekodoc-v1"
GEEKODOC2_NAME="geekodoc-v2"
GEEKODOC2_LINK_NAME="geekodoc-latest"
# -- Functions
function logger() {
local log_priority="$1"
local msg="$2"
local color
#check if level exists
[[ ${LOGLEVELS[$log_priority]} ]] || return 1
#check if level is enough
(( ${LOGLEVELS[$log_priority]} < ${LOGLEVELS[$LOGGING_LEVEL]} )) && return 2
color=${LOGCOLORS[$log_priority]}
#log here
echo -e "${color}${log_priority}:${RESET} ${msg}"
}
function logdebug {
logger "DEBUG" "$1"
}
function loginfo {
logger "INFO" "$1"
}
function logwarn {
logger "WARN" "$1"
}
function logerror {
logger "ERROR" "$1"
}
function exit_on_error {
logerror "$1" >&2
exit 1;
}
# Include some OS specific variables:
source /etc/os-release || exit_on_error "File /etc/os-release not found"
function usage {
cat << EOF
Build Geekodoc Schema
SYNOPSIS
$ME -h|--help
$ME [OPTIONS]
OPTIONS
-v, -vv, -vvv Log level, the more you add, the more messages
you get. Restricted to -vvv (=DEBUG)
(default: ${VERBOSITY})
-b DIR, --builddir=DIR
Set the directory where to build (default: "${BUILD_DIR}")
EOF
}
function requires {
loginfo "Check requirements..."
# * trang
# * jing
# * python3-rnginline (from obs://devel:languages:python/python3-rnginline)
# * docbook_5 (from obs://Publishing)
local SCRIPTS="trang jing rnginline"
for prog in $SCRIPTS; do
if ! command -v $prog >/dev/null; then
logerror "Command $prog not found."
exit 10
fi
logdebug "$prog found"
done
loginfo "All requirements are ok."
}
function create_build_env {
loginfo "Create build environment..."
# Dir structure
if [[ -d $BUILD_DIR ]]; then
rm -rf $BUILD_DIR $DIST_DIR 2>/dev/null
fi
mkdir -p {$BUILD_DIR,$DIST_DIR}/{$GEEKODOC1_PATH,$GEEKODOC2_PATH}
cp $GEEKODOC1_PATH/*.rnc $BUILD_DIR/$GEEKODOC1_PATH/
cp $GEEKODOC2_PATH/*.rnc $BUILD_DIR/$GEEKODOC2_PATH/
# Copy DocBook5 schema for GeekoDoc v1
cp $DOCBOOK5_SRC_DIR/*.rnc $BUILD_DIR/$GEEKODOC1_PATH/
# Copy DocBook5 schema and ITS for GeekoDoc v2
cp $DOCBOOK5_SRC_DIR/*.rnc $BUILD_DIR/$GEEKODOC2_PATH/
logdebug "Build environment created."
}
function rnc_to_rng {
local files="$1"
loginfo "Convert RNC -> RNG..."
for f in $files; do
rnc="$f.rnc"
rng="$f.rng"
logdebug "Converting $rnc -> $rng"
trang "$rnc" "$rng"
if [[ "$?" -ne 0 ]]; then
exit_on_error "Conversion from $rnc -> $rng failed"
fi
done
}
function make_flat {
local files="$1"
loginfo "Convert RNG -> RNI (flatten)..."
for f in $files; do
rng="$f.rng"
rni="$f-flat.rni"
logdebug "Converting $rng -> $rni"
rnginline "$rng" "$rni"
if [[ "$?" -ne 0 ]]; then
exit_on_error "Conversion from $rng -> $rni failed"
fi
done
}
function cleanup_xml {
local files="$1"
loginfo "Cleanup flat RNI..."
for f in $files; do
rng="$f-flat.rng"
rni="$f-flat.rni"
logdebug "Cleanup $rni -> $rng"
xmllint -o "$rng" --nsclean --format "$rni"
if [[ "$?" -ne 0 ]]; then
exit_on_error "Cleanup of $rni failed"
fi
done
}
function rngflat_to_rnc {
local files="$1"
loginfo "Convert flat RNG -> flat RNC..."
for f in $files; do
rng="$f-flat.rng"
rnc="$f-flat.rnc"
logdebug "Cleanup $rng -> $rnc"
trang "$rng" "$rnc"
if [[ "$?" -ne 0 ]]; then
exit_on_error "Conversion from $rng -> $rnc failed"
fi
done
}
function copy_flat_rnc {
local files="$1"
loginfo "Copy flat RNC files..."
for f in $files; do
rnc="$f-flat.rnc"
target="${f#$BUILD_DIR/}"
target="$DIST_DIR/${target%/*}"
logdebug "Copy $rnc -> $target"
sed -i 's/[[:blank:]]*$//' "$rnc"
cp "$rnc" "$target"
loginfo "Result: '$target/${rnc##*/}'"
done
}
function validate_result {
local files="$1"
loginfo "Validate GeekoDoc result..."
for f in $files; do
rng="$f.rng"
logdebug "Validating $rng..."
jing "$RELAXNG_SCHEMA" "$rng"
if [[ "$?" -ne 0 ]]; then
exit_on_error "Validation of $rng failed."
fi
done
}
function create_latest_link {
loginfo "Creating symbolic link ${LATEST_VERSION} -> latest"
ln -frs ${BUILD_DIR}/${GEEKODOC_RNG_DIR}/${LATEST_VERSION} \
${BUILD_DIR}/${GEEKODOC_RNG_DIR}/latest
ln -frs ${BUILD_DIR}/${GEEKODOC2_PATH}/${GEEKODOC2_NAME}-flat.rng \
${BUILD_DIR}/${GEEKODOC_RNG_DIR}/${GEEKODOC2_LINK_NAME}-flat.rng
ln -frs ${BUILD_DIR}/${GEEKODOC2_PATH}/${GEEKODOC2_NAME}-flat.rnc \
${BUILD_DIR}/${GEEKODOC_RNG_DIR}/${GEEKODOC2_LINK_NAME}-flat.rnc
}
# -- CLI parsing
ARGS=$(getopt -o h,:v,b: -l help,builddir: -n "$ME" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
--help|-h)
usage
exit 0
shift
;;
-b|--builddir)
BUILD_DIR="$2"
shift 2
[[ -z $BUILD_DIR ]] && exit_on_error "Need an argument for -b/--builddir"
;;
-v)
VERBOSITY=$((VERBOSITY+1))
shift
;;
--) shift ; break ;;
*) exit_on_error "Wrong parameter: $1" ;;
esac
done
# Fall back to 3 (=DEBUG) if we got more than 4
[[ $VERBOSITY -ge 3 ]] && VERBOSITY=3
LOGGING_LEVEL=${LEVEL2LOG[$VERBOSITY]}
# -- Process
cd "$MYDIR" || exit_on_error "Directory $MYDIR doesn't exist."
files="$BUILD_DIR/$GEEKODOC1_PATH/$GEEKODOC1_NAME \
$BUILD_DIR/$GEEKODOC2_PATH/$GEEKODOC2_NAME"
requires "$files"
create_build_env "$files"
rnc_to_rng "$files"
make_flat "$files"
cleanup_xml "$files"
rngflat_to_rnc "$files"
validate_result "$files"
copy_flat_rnc "$files"
create_latest_link
loginfo "Finished."