-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_gen
executable file
·280 lines (257 loc) · 7.21 KB
/
report_gen
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
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2022 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# See the output of "$0 -h" for details.
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail
#set -Eeu
APP_NAME="OSH-Tool HTML Report Creator"
SCRIPT_NAME="$(basename "$0")"
SCRIPT_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
OUT_DIR_DEFAULT="public"
OUT_FILE_NAME_MD="osh-report.md"
OUT_FILE_NAME_JSON="osh-report.json"
OUT_FILE_NAME_HTML="osh-report.html"
BADGE_FILE_NAME_PREFIX="osh-badge-"
RATINGS="compliance openness hardware quality machineReadability"
# parameters
repo="$(pwd)"
title=""
output_dir="$OUT_DIR_DEFAULT"
force=false
download_badges=false
function print_help() {
echo -e ""
echo -e "$APP_NAME - Executes the OSH CLI tool"
echo -e "on a local (git) repo, and generates an HTML report"
echo -e "from the results."
echo -e "By default, all output goes into a single folder: 'public'"
echo -e ""
echo -e "Usage:"
echo -e "\t$SCRIPT_NAME [OPTION...]"
echo -e "Options:"
echo -e "\t-C, --repo [PATH]"
echo -e "\t\tDefines the path to the repo to check (default: '.')"
echo -e "\t-o, --output-dir [PATH]"
echo -e "\t\tWhere to write the resulting HTML, Markdown and JSON reports to"
echo -e "\t\t(default: '$OUT_DIR_DEFAULT')"
echo -e "\t-f, --force"
echo -e "\t\tForces overwriting index.html"
echo -e "\t-d, --download-badges"
echo -e "\t\tInstead of linking to remote badges,"
echo -e "\t\tdownload them as files and link to them locally."
echo -e "\t-h, --help"
echo -e "\t\tPrint this usage help and exit"
echo -e ""
}
# Process command line arguments
while [[ $# -gt 0 ]]
do
arg="$1"
shift # $2 -> $1, $3 -> $2, ...
case "$arg" in
-C|--repo)
repo="$1"
shift
;;
-o|--output-dir)
output_dir="$1"
shift
;;
-f|--force)
force=true
;;
-d|--download-badges)
download_badges=true
;;
-h|--help)
print_help
exit 0
;;
*) # non-/unknown option
>&2 echo "Unknown flag: '$arg'"
exit 1
;;
esac
done
output_md="$output_dir/$OUT_FILE_NAME_MD"
output_json="$output_dir/$OUT_FILE_NAME_JSON"
output_html="$output_dir/$OUT_FILE_NAME_HTML"
badge_file_prefix="$output_dir/$BADGE_FILE_NAME_PREFIX"
if ! [ -d "$repo" ]
then
>&2 echo "ERROR: Not an existing repository: '$repo'"
exit 1
fi
mkdir -p "$output_dir"
# Creates the OSH report as Markdown table
osh \
-C "$repo" \
check \
--force \
--report-md-table "$output_md" \
--report-json "$output_json"
# Prettify JSON
jq < "$output_json" > "${output_json}_TMP"
mv "${output_json}_TMP" "$output_json"
tool_homepage="$(jq -r '.prelude.homepage' < "$output_json")"
if $download_badges
then
for rating in $RATINGS
do
badge_file="${badge_file_prefix}${rating}.svg"
rm -f "$badge_file"
badge_url="$(jq -r '.stats.ratings.'"$rating"'.badgeUrl' < "$output_json")"
wget "$badge_url" --quiet -O "$badge_file"
done
fi
function projvar_get() {
local key
local value
key="$1"
value="$(jq -r '.prelude.projVars["'"$key"'"]' < "$output_json")"
printf '%s' "$value"
if [ "$value" == "null" ]
then
return 1
else
return 0
fi
}
function projvar_get_or_default() {
local key
local default
local value
local ret
key="$1"
default="$2"
value="$(projvar_get "$key")"
ret=$?
if [ $ret -eq 0 ]
then
printf '%s' "$value"
else
printf '%s' "$default"
fi
return $ret
}
if [ -z "$title" ]
then
proj_name="$(projvar_get_or_default 'NAME' 'Project name N/A' || true)"
proj_repo_web_url="$(projvar_get_or_default 'REPO_WEB_URL' '???' || true)"
title="[$proj_name]($proj_repo_web_url) - [OSH](https://en.wikipedia.org/wiki/Open-source_hardware) quality report"
fi
subtitle="Produced by the [\`osh\`-tool]($tool_homepage)"
# Prepends meta-data to Markdown report
prefix_file="/tmp/report_gen-prefix-$RANDOM"
tmp_file="/tmp/report_gen-tmp-$RANDOM"
{
echo "---"
echo "title: '$title'"
echo "subtitle: '$subtitle'"
echo "---"
echo
echo "<details>"
echo
echo "<summary>Badges</summary>"
echo
echo "| Dimension | Badge | Markdown Code | HTML Code |"
echo "| - | --: | ----- | ----- |"
for rating in $RATINGS
do
if $download_badges
then
badge_file_or_url="$(basename "${badge_file_prefix}${rating}.svg")"
else
badge_file_or_url="$(jq -r '.stats.ratings.'"$rating"'.badgeUrl' < "$output_json")"
fi
echo -n "| $rating "
echo -n "| ![OSH - $rating]($badge_file_or_url) "
echo -n "| <code id=\"md-code-$rating\">\`[![OSH - $rating](\$BASE_URL/$badge_file_or_url)](<\$REPORT_URL>)\`</code> "
echo -n "| <code id=\"html-code-$rating\">\`<a href=\"\$REPORT_URL\"><img alt=\"OSH - $rating\" src=\"\$BASE_URL/$badge_file_or_url\"/></a>\`</code> "
echo "|"
done
echo
echo "<p id=\"no-script-indicator\">"
echo "Please enabled JavaScript for this page, to see the correct URLs above,"
echo "without the <code>\$BASE_URL</code> placeholders."
echo "</p>"
echo
echo "<script>"
echo "function repl_url(id) {"
echo " let elem = document.getElementById(id);"
echo " let report_url = window.location.href;"
echo " let base_url = report_url.replace(\"/$OUT_FILE_NAME_HTML\", \"\");"
echo " elem.innerHTML = elem.innerHTML.replace(\"\$REPORT_URL\", report_url);"
echo " elem.innerHTML = elem.innerHTML.replace(\"\$BASE_URL\", base_url);"
echo "}"
echo "function clear_cont(id) {"
echo " let elem = document.getElementById(id);"
echo " elem.innerHTML = \"\";"
echo "}"
for rating in $RATINGS
do
echo
echo "repl_url(\"md-code-$rating\");"
echo "repl_url(\"html-code-$rating\");"
done
echo "clear_cont(\"no-script-indicator\");"
echo "</script>"
echo
echo "</details>"
echo
echo "<details>"
echo
echo "<summary>This report in different formats</summary>"
echo
echo "* [Markdown]($OUT_FILE_NAME_MD)"
echo "* [HTML]($OUT_FILE_NAME_HTML)"
echo "* [JSON]($OUT_FILE_NAME_JSON)"
echo
echo "</details>"
echo
} >> "$prefix_file"
# Splice-in pandoc and its version into the tools section
pandoc_version="$(pandoc --version | head -n 1 | sed -e 's/^pandoc //')"
# shellcheck disable=SC2016
sed -i -e 's/| \[`osh-dir-std`\](/| [`pandoc`](https:\/\/pandoc.org) | '"$pandoc_version"' |\n| [`osh-dir-std`](/' "$output_md"
cat "$prefix_file" "$output_md" > "$tmp_file"
mv "$tmp_file" "$output_md"
echo "INFO Written Markdown report file '$output_md'."
rm "$prefix_file"
# Generates HTML report
cwd="$PWD"
cd "$output_dir"
pandoc \
-t html \
-o "$OUT_FILE_NAME_HTML" \
--standalone \
--self-contained \
-f markdown-smart \
--css="$SCRIPT_DIR/report_gen_additional.css" \
-M document-css=true \
"$OUT_FILE_NAME_MD"
cd "$cwd"
echo "INFO Written HTML report file '$output_html'."
# Generate an index.html if none exists yet
index_file="$output_dir/index.html"
if ! [ -f "$index_file" ] || $force
then
echo '<!DOCTYPE html>
<html>
<head>
<title>Redirecting to OSH report ...</title>
<meta http-equiv="refresh" content="1; url = osh-report.html" />
</head>
<body>
<p>If you are not redirected automatically,
click on <a href="osh-report.html">osh-report.html</a>.</p>
</body>
</html>' > "$index_file"
echo "INFO Written index file '$index_file'."
else
>&2 printf 'WARN Skipped writing index file "%s",\n because it already exists and --force was not given.\n' \
"$index_file"
fi