-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestdata_embed.sh
executable file
·101 lines (78 loc) · 2.94 KB
/
testdata_embed.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
#!/usr/bin/env bash
# Copyright 2023 The flatgeobuf (Go) Authors. All rights reserved.
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file.
set -eo pipefail
if ! command -v bzip2 >/dev/null; then
>&2 << EOF cat
error: bzip2 not found
| This script uses bzip2 to compress test data files from
| https://github.com/flatgeobuf/flatgeobuf prior to embedding
| them in the Go code.
|
| Install bzip2 if you want to proceed.
EOF
exit 2
fi
function version() {
local version='$Id$'
version="${version##\$Id: }"
version="${version%% \$}"
echo $version
}
testdata_file="$1"
if ! [ -f "$testdata_file" ]; then
>&2 printf "unable to import %s: not found or not a file\n" "$testdata_file"
fi
# Derive the name of the string variable to contain the contents of the
# testdata file.
testdata_filename_root="$(basename "$testdata_file")"
declare -a testdata_varname_parts
testdata_filename_root="${testdata_filename_root%%.fgb}"
readarray -td $'\n' testdata_varname_parts <<<"${testdata_filename_root//_/$'\n'}"
testdata_varname=
for (( i=0; i<"${#testdata_varname_parts}"; i++ )); do
part="${testdata_varname_parts[$i]}"
testdata_varname+="${part^}"
done
testdata_varname+=FGB
# Create a temporary file to contain the input $GOFILE with generated
# code inserted. Set a trap to make sure the file gets deleted if we
# error out unexpectedly.
tmp_file=""
function cleanup() {
rm -f "$tmp_file"
}
trap cleanup EXIT
tmp_file=$(mktemp "$GOFILE.XXXXXXXX")
# Print status.
>&2 printf 'embedding %s into %s (%s) at line %d using %s\n' "$testdata_file" "$GOFILE" "var $testdata_varname" "$GOLINE" "$tmp_file"
# PREFIX: Echo every line of the Go file prior to the go:generate
# directive to the temporary file.
>"$tmp_file" head -n "$GOLINE" "$GOFILE"
# INFIX: Insert the generated code.
>>"$tmp_file" <<EOF cat
// DO NOT EDIT. Code generated by testdata_embed.sh version $(version).
//
// This string contains the base64-encoded, bzip2-compressed, contents of the
// file \`$testdata_file\`. For license information, please see
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
//
// Last generated by $USER on $(date). The raw source
// SHA-256 is $(sha256sum "$testdata_file" | cut -d' ' -f1).
//
// This silly byte-embedding trick is needed to access the example test code
// when running example code within the docs website,
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
var $testdata_varname = \`
EOF
<"$testdata_file" bzip2 | base64 -w 8192 >>"$tmp_file"
>>"$tmp_file" echo '`'
# Find the end of the string literal within the input file so we can
# skip past it.
offset="$(tail -n +"$GOLINE" "$GOFILE" | grep -n -m1 '\(^\|`\)`$' | cut -d: -f 1)"
# SUFFIX: Echo every line of the Go file after the go:generate directive
# to the temporary file.
>>"$tmp_file" tail -n +$((GOLINE + offset)) "$GOFILE"
# Swap the temporary file in for the original target Go file.
mv "$tmp_file" "$GOFILE"