-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
213 lines (171 loc) · 9.55 KB
/
build.sh
File metadata and controls
213 lines (171 loc) · 9.55 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
#!/bin/bash
set -euo pipefail
shopt -s nullglob
REPO_SLUG=${1:?Error: Repository slug not provided. Exiting.}
BASE_URL="https://github.com/$REPO_SLUG"
BASE_URL_RAW="https://raw.githubusercontent.com/$REPO_SLUG/main"
SRC_DIR='userscripts/src'
META_DIR='userscripts/meta'
DIST_DIR='userscripts/dist'
DOCS_DIR='userscripts/docs'
SCREENSHOTS_DIR='userscripts/docs/screenshots'
printf 'Initializing build environment\n'
npm install --save-dev esbuild cloc > /dev/null
printf 'Removing old build artifacts\n'
mkdir -p "$META_DIR" "$DIST_DIR" "$DOCS_DIR" "$SCREENSHOTS_DIR"
find "$META_DIR" "$DIST_DIR" "$DOCS_DIR" -maxdepth 1 -type f -delete
printf 'Creating temp files for README.md update\n'
BEFORE_TABLE_FILE=$(mktemp)
AFTER_TABLE_FILE=$(mktemp)
TABLE_CONTENT_FILE=$(mktemp)
start_line=$(grep -n -m 1 '## Userscripts' README.md | cut -d : -f 1)
head -n "$start_line" README.md > "$BEFORE_TABLE_FILE"
end_line=$(tail -n +$((start_line + 1)) README.md | grep -n -m 1 -v '^|' | cut -d : -f 1)
[[ -n "$end_line" ]] && tail -n +$((start_line + end_line)) README.md > "$AFTER_TABLE_FILE"
( printf '| *NAME* | *VERSION* | *LOC* | *INSTALL* |\n'
printf '|:---|:---|:---|:---|\n'
) > "$TABLE_CONTENT_FILE"
printf 'Starting to process userscripts in %s...\n' "$SRC_DIR"
declare -i userscript_count=$(find "$SRC_DIR" -name "*.user.js" | wc -l)-1
declare -i loc_count_total=0
declare -a ms_ids
declare -A ms_store
for file in "$SRC_DIR"/*.user.js; do
id=$(basename "$file" .user.js)
printf '>> Processing ID: %s\n' "$id"
if [[ "$id" = 'zzzzzzzz' ]]; then
printf ' 0. Generating src file for megascript\n'
ms_old="$(< "$file")"
cat > "$file" << EOF
// ==UserScript==
// @name Trakt.tv | Megascript
// @description My $userscript_count trakt.tv userscripts merged into one for convenience:\
$(for id in "${ms_ids[@]}"; do sed 's#^Trakt.tv |##' <<< "${ms_store["$id.script_name"]}"; done | sort | paste -sd, -).
// @version $(date '+%Y-%m-%d_%H-%M')
// @updateURL https://update.greasyfork.org/scripts/557305.meta.js
// @namespace zzzzzzzz
$(grep -m 1 '// @icon' <<< "${ms_store["${ms_ids[0]}.header"]}")
$(grep '// @match' <<< "${ms_store["${ms_ids[0]}.header"]}")
$(grep -m 1 '// @run-at' <<< "${ms_store["${ms_ids[0]}.header"]}")
$(for id in "${ms_ids[@]}"; do printf '%s\n' "${ms_store["$id.header"]}"; done \
| grep -E '^// @(resource|require|grant|connect)' \
| sort -u \
| awk '
/^\/\/ @resource/ {key="a"}
/^\/\/ @require/ {key="b"}
/^\/\/ @grant/ {key="ca"; if ($0 ~ /GM_/) key="cb"; if ($0 ~ /GM\./) key="cc"}
/^\/\/ @connect/ {key="d"}
{print key $0}
' \
| sort \
| sed -E 's|^[a-z]+||')
// ==/UserScript==
/* README
### General
- You can disable individual modules by setting the corresponding id to \`false\` in the userscript storage tab *(note: only displayed after first run and with "Config mode" set to "Advanced" in TM settings)*.
- Each enabled module will conflict with the corresponding standalone userscript. Either uninstall the standalone version (suggested) or disable the respective module.
- Newly added modules will automatically get enabled when the script is updated. Modules that you already disabled will stay disabled.
- As VIP user you should disable: \`2dz6ub1t\`, \`fyk2l3vj\`, \`x70tru7b\`, \`2hc6zfyy\`
- This userscript is automatically generated. YMMV.
| *NAME* | *ID* |
| :----- | :---------- |
$(for id in "${ms_ids[@]}"; do
printf '| [%s](%s#StickyHeader "%s") | `%s` |\n' \
"$(sed 's#|#\\|#g' <<< "${ms_store["$id.script_name"]}")" "$id.md" "$(sed 's#|#\\|#g; s|"|\\"|g' <<< "${ms_store["$id.script_description"]}")" \
"$id"
done | sort)
*/
$(for id in "${ms_ids[@]}"; do
printf '/* [%s]\n' "${ms_store["$id.script_name"]}"
printf '%s\n' "${ms_store["$id.script_description"]}"
[[ -n "${ms_store["$id.readme_comment"]}" ]] && printf '\n%s\n' "$(sed '1d;$d' <<< "${ms_store["$id.readme_comment"]}")"
printf '*/\n\n'
done)
'use strict';
const gmStorage = { $(for id in "${ms_ids[@]}"; do printf "'%s': true, " "$id"; done)...(GM_getValue('megascript')) };
GM_setValue('megascript', gmStorage);
$(for id in "${ms_ids[@]}"; do printf "\n\ngmStorage['%s'] && (async (moduleName) => {\n%s\n})('%s');\n" "$id" "${ms_store["$id.body"]}" "${ms_store["$id.script_name"]}"; done)
EOF
if diff <(grep -v '^// @version' <<< "$ms_old") <(grep -v '^// @version' "$file") > /dev/null; then
printf '%s\n' "$ms_old" > "$file"
fi
fi
printf ' 1. Parsing src file\n'
clean_content="$(tr -d '\r' < "$file")"
script_name="$(grep -m 1 '^// @name' <<< "$clean_content" | sed -E 's|// @name\s+||')"
script_description="$(grep -m 1 '^// @description' <<< "$clean_content" | sed -E 's|// @description\s+||')"
script_version="$(grep -m 1 '^// @version' <<< "$clean_content" | sed -E 's|// @version\s+||')"
script_namespace="$(grep -m 1 '^// @namespace' <<< "$clean_content" | sed -E 's|// @namespace\s+||')"
header="$(sed -n '\|^// ==UserScript==$|,\|^// ==/UserScript==$|p' <<< "$clean_content")"
readme_comment="$(sed -n '\|^/\* README$|,\|^\*/$|p' <<< "$clean_content")"
body="$(sed '\|^// ==UserScript==$|,\|^// ==/UserScript==$|d; \|^/\* README$|,\|^\*/$|d' <<< "$clean_content" | sed -n '\|[^\s]|,$p')"
screenshots="$(find "$SCREENSHOTS_DIR" -type f -name "$id-*.*" -printf '%f\n' | sort)"
[[ "$id" != "${script_namespace##*/}" ]] && { printf 'Error: Userscript ID from filename does not match ID from namespace. Exiting.\n' >&2; exit 1; }
if [[ "$id" != 'zzzzzzzz' ]]; then
ms_ids+=("$id")
ms_store["$id.script_name"]="$script_name"
ms_store["$id.script_description"]="$script_description"
ms_store["$id.script_version"]="$script_version"
ms_store["$id.header"]="$header"
ms_store["$id.readme_comment"]="$readme_comment"
ms_store["$id.body"]="$body"
fi
printf ' 2. Generating dist and meta files\n'
header="$(sed \
-e '\|// @description| s|$|'"$([[ -n "$readme_comment$screenshots" ]] && printf '%s' ' See README for details.')"'|' \
-e '\|// @namespace|d' \
-e '\|// @author|d' \
-e '\|// @license|d' \
-e '\|// @homepageURL|d' \
-e '\|// @supportURL|d' \
-e '\|// @downloadURL|d' \
-e '\|// @version|a\
// @namespace '"${BASE_URL%/*}"'\
// @author '"${REPO_SLUG%/*}"'\
// @license GPL-3.0-or-later\
// @homepageURL '"$BASE_URL"'#readme\
// @supportURL '"$BASE_URL"'/issues' <<< "$header")"
grep -q '// @updateURL' <<< "$header" || header="$(sed '\|// @supportURL|a\
// @updateURL '"$BASE_URL_RAW/$META_DIR/$id.meta.js" <<< "$header")"
DOWNLOAD_URL_DIST="$BASE_URL_RAW/$DIST_DIR/$id.user.js"
DOWNLOAD_URL_DIST_MIN="$BASE_URL_RAW/$DIST_DIR/$id.min.user.js"
header_meta="$(sed '\|// @updateURL|d' <<< "$header")"
header_dist="$(sed "\|// @updateURL|a\// @downloadURL $DOWNLOAD_URL_DIST" <<< "$header")"
header_dist_min="$(sed "\|// @updateURL|a\// @downloadURL $DOWNLOAD_URL_DIST_MIN" <<< "$header")"
middle_content="${readme_comment:+$'\n\n'"$readme_comment"}"$'\n\n\n'
minified_body="$(npx esbuild --minify --loader=js <<< "$body")"
printf '%s' "$header_meta" > "$META_DIR/$id.meta.js"
printf '%s%s%s' "$header_dist" "$middle_content" "$body" > "$DIST_DIR/$id.user.js"
printf '%s%s%s' "$header_dist_min" "$middle_content" "$minified_body" > "$DIST_DIR/$id.min.user.js"
printf ' 3. Generating doc files\n'
version_slug=$(sed 's|_|__|g; s|-|--|g' <<< "$script_version")
loc_count=$(npx cloc --quiet --sum-one --stdin-name="$id.user.js" - <<< "$body" | grep -m 1 'SUM:' | awk '{print $5}')
[[ "$id" != 'zzzzzzzz' ]] && ((loc_count_total += loc_count))
install_badge="[]($DOWNLOAD_URL_DIST)"
install_min_badge="[]($DOWNLOAD_URL_DIST_MIN)"
version_badge="[](../../../../blame/main/$DIST_DIR/$id.user.js)"
loc_count_badge="[](../../$DIST_DIR/$id.user.js)"
( printf '# %s\n%s\n\n' "$script_name" "$script_description"
printf '%s\n%s\n%s\n%s\n\n' "$install_badge" "$install_min_badge" "$version_badge" "$loc_count_badge"
printf '%s' "${readme_comment:+$'## Info\n'"$(sed '1d;$d' <<< "$readme_comment")"$'\n\n'}"
printf '%s' "${screenshots:+$'## Screenshots\n<p align="center">\n'"$(sed -E 's|(.*)| <img src="screenshots/\1" alt="screenshot" align="middle">|' <<< "$screenshots")"$'\n</p>'}"
) > "$DOCS_DIR/$id.md"
( [[ -n "$readme_comment$screenshots" ]] && printf '%s\n\n' "# [README]($BASE_URL/blob/main/$DOCS_DIR/$id.md#StickyHeader)"
printf '%s\n' "Click [HERE]($BASE_URL#readme) for general info, requirements and a full list of all my Trakt.tv userscripts."
) > "$DOCS_DIR/$id-gf.md"
printf '| [%s](%s "%s") | `%s` | `%s` | [Standard](%s) // [Minified](%s) |\n' \
"$(sed 's#|#\\|#g' <<< "$script_name")" "$DOCS_DIR/$id.md#StickyHeader" "$(sed 's#|#\\|#g; s|"|\\"|g' <<< "$script_description")" \
"$(sed $'s|_|_\u200b|' <<< "$script_version")" \
"$loc_count" \
"$DOWNLOAD_URL_DIST" "$DOWNLOAD_URL_DIST_MIN" >> "$TABLE_CONTENT_FILE"
done
printf 'Finalizing README.md\n'
( cat "$BEFORE_TABLE_FILE" | sed -E "s|(/badge/loc-)[0-9]+|\1$loc_count_total|; s|(/badge/userscripts-)[0-9]+|\1$userscript_count|"
head -n 2 "$TABLE_CONTENT_FILE"
sed -n '\|zzzzzzzz|p' "$TABLE_CONTENT_FILE"
sed '1,2d; \|zzzzzzzz|d' "$TABLE_CONTENT_FILE" | sort
cat "$AFTER_TABLE_FILE"
) > README.md
printf 'Removing temp files\n'
rm -f "$BEFORE_TABLE_FILE" "$AFTER_TABLE_FILE" "$TABLE_CONTENT_FILE"
printf 'Build process completed successfully!\n'