Skip to content

Commit fbb42a5

Browse files
authored
Update build.sh
1 parent 713d9e7 commit fbb42a5

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

build.sh

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
# --- CONFIGURATION ---
55
REPO_SLUG=$1
66
if [[ -z "$REPO_SLUG" ]]; then
7-
printf "Error: Repository slug not provided. Exiting.\n" >&2; exit 1
7+
printf 'Error: Repository slug not provided. Exiting.\n' >&2; exit 1
88
fi
99
BASE_URL="https://github.com/$REPO_SLUG"
1010
BASE_URL_RAW="https://raw.githubusercontent.com/$REPO_SLUG/main"
@@ -16,15 +16,15 @@ DOCS_DIR='userscripts/docs'
1616
SCREENSHOTS_DIR='userscripts/docs/screenshots'
1717

1818
# --- SETUP ---
19-
printf "Initializing build environment...\n"
19+
printf 'Initializing build environment...\n'
2020
npm install --save-dev esbuild cloc > /dev/null
2121

22-
printf "Removing old build artifacts...\n"
22+
printf 'Removing old build artifacts...\n'
2323
mkdir -p "$META_DIR" "$DIST_DIR" "$DOCS_DIR" "$SCREENSHOTS_DIR"
2424
find "$META_DIR" "$DIST_DIR" "$DOCS_DIR" -maxdepth 1 -type f -delete
2525

2626
# --- README PREPARATION ---
27-
printf "Preparing README.md for update...\n"
27+
printf 'Preparing README.md for update...\n'
2828
BEFORE_TABLE_FILE=$(mktemp)
2929
AFTER_TABLE_FILE=$(mktemp)
3030
TABLE_CONTENT_FILE=$(mktemp)
@@ -33,19 +33,17 @@ start_line=$(grep -n -m 1 '## Userscripts' README.md | cut -d: -f1)
3333
head -n "$start_line" README.md > "$BEFORE_TABLE_FILE"
3434

3535
end_line=$(tail -n +$((start_line + 1)) README.md | grep -n -m 1 -v '^|' | cut -d: -f1)
36-
if [[ -n "$end_line" ]]; then
37-
tail -n +$((start_line + end_line)) README.md > "$AFTER_TABLE_FILE"
38-
fi
36+
[[ -n "$end_line" ]] && tail -n +$((start_line + end_line)) README.md > "$AFTER_TABLE_FILE"
3937

40-
printf "| *NAME* | *VERSION* | *LOC* | *INSTALL* |\n" > "$TABLE_CONTENT_FILE"
41-
printf "|:---|:---|:---|:---|\n" >> "$TABLE_CONTENT_FILE"
38+
printf '| *NAME* | *VERSION* | *LOC* | *INSTALL* |\n' > "$TABLE_CONTENT_FILE"
39+
printf '|:---|:---|:---|:---|\n' >> "$TABLE_CONTENT_FILE"
4240

4341
# --- MAIN PROCESSING LOOP ---
44-
printf "Starting to process userscripts in '%s'...\n" "$SRC_DIR"
42+
printf 'Starting to process userscripts in %s...\n' "$SRC_DIR"
4543
loc_count_total=0
4644
for file in "$SRC_DIR"/*.user.js; do
4745
id="$(basename "$file" .user.js)"
48-
printf ">> Processing ID: %s\n" "$id"
46+
printf '>> Processing ID: %s\n' "$id"
4947

5048
# --- 1. PARSE SOURCE FILE ---
5149
clean_content="$(tr -d '\r' < "$file")"
@@ -59,72 +57,68 @@ for file in "$SRC_DIR"/*.user.js; do
5957

6058
# --- 2. CHECK FOR ID MISMATCH ---
6159
if [ "$id" != "${script_namespace##*/}" ]; then
62-
printf "Error: Userscript ID from filename does not match ID from namespace. Exiting.\n" >&2; exit 1
60+
printf 'Error: Userscript ID from filename does not match ID from namespace. Exiting.\n' >&2; exit 1
6361
fi
6462

6563
# --- 3. GENERATE DIST AND META VERSIONS ---
6664
header="$(sed \
6765
${readme_comment:+-e '\|// @description| s|$| See README for details.|'} \
6866
-e '\|// @namespace|d' \
6967
-e '\|// @author|d' \
68+
-e '\|// @license|d' \
7069
-e '\|// @homepageURL|d' \
7170
-e '\|// @supportURL|d' \
7271
-e '\|// @updateURL|d' \
7372
-e '\|// @downloadURL|d' \
7473
-e '\|// @version|a\
7574
// @namespace '"${BASE_URL%/*}"'\
7675
// @author '"${REPO_SLUG%/*}"'\
76+
// @license GPL-3.0-or-later\
7777
// @homepageURL '"$BASE_URL"'#readme\
7878
// @supportURL '"$BASE_URL"'/issues\
7979
// @updateURL '"$BASE_URL_RAW/$META_DIR/$id.meta.js" <<< "$header")"
8080

8181
middle_content="${readme_comment:+$'\n\n'"$readme_comment"}"$'\n\n\n'
8282

83-
printf "%s" "$header" > "$META_DIR/$id.meta.js"
83+
printf '%s' "$header" > "$META_DIR/$id.meta.js"
8484

8585
DOWNLOAD_URL_DIST="$BASE_URL_RAW/$DIST_DIR/$id.user.js"
86-
header_dist="$(sed "\|// @updateURL|a\// @downloadURL ${DOWNLOAD_URL_DIST}" <<< "$header")"
87-
printf "%s%s%s" "${header_dist}" "${middle_content}" "${body}" > "$DIST_DIR/$id.user.js"
86+
header_dist="$(sed "\|// @updateURL|a\// @downloadURL $DOWNLOAD_URL_DIST" <<< "$header")"
87+
printf '%s%s%s' "$header_dist" "$middle_content" "$body" > "$DIST_DIR/$id.user.js"
8888

8989
minified_body="$(npx esbuild --minify --loader=js <<< "$body")"
9090
DOWNLOAD_URL_DIST_MIN="$BASE_URL_RAW/$DIST_DIR/$id.min.user.js"
91-
header_dist_min="$(sed "\|// @updateURL|a\// @downloadURL ${DOWNLOAD_URL_DIST_MIN}" <<< "$header")"
92-
printf "%s%s%s" "${header_dist_min}" "${middle_content}" "${minified_body}" > "$DIST_DIR/$id.min.user.js"
91+
header_dist_min="$(sed "\|// @updateURL|a\// @downloadURL $DOWNLOAD_URL_DIST_MIN" <<< "$header")"
92+
printf '%s%s%s' "$header_dist_min" "$middle_content" "$minified_body" > "$DIST_DIR/$id.min.user.js"
9393

9494
# --- 4. GENERATE DOCUMENTATION FILE ---
9595
doc_file="$DOCS_DIR/$id.md"
96-
printf "# %s\n%s\n\n" "$script_name" "$script_desc" > "$doc_file"
97-
98-
loc_count=$(npx cloc --quiet --sum-one --stdin-name="$id.user.js" - <<< "$body" | grep -m 1 'SUM:' | awk '{print $5}')
99-
((loc_count_total += loc_count))
10096

97+
printf '# %s\n%s\n\n' "$script_name" "$script_desc" > "$doc_file"
98+
10199
install_badge="[![install standard](https://img.shields.io/badge/install-standard-006400)]($DOWNLOAD_URL_DIST)"
102100
install_min_badge="[![install minified](https://img.shields.io/badge/install-minified-64962a)]($DOWNLOAD_URL_DIST_MIN)"
103101
version_badge="[![version](https://img.shields.io/badge/version-$script_version-blue)](../../../../commits/main/$DIST_DIR/$id.user.js)"
102+
printf '%s %s %s' "$install_badge" "$install_min_badge" "$version_badge" >> "$doc_file"
103+
104+
loc_count=$(npx cloc --quiet --sum-one --stdin-name="$id.user.js" - <<< "$body" | grep -m 1 'SUM:' | awk '{print $5}')
104105
loc_count_badge="[![lines of code](https://img.shields.io/badge/loc-$loc_count-orange)](../../$DIST_DIR/$id.user.js)"
105-
printf "%s %s %s %s\n\n" "$install_badge" "$install_min_badge" "$version_badge" "$loc_count_badge" >> "$doc_file"
106+
printf ' %s\n\n' "$loc_count_badge" >> "$doc_file"
107+
((loc_count_total += loc_count))
106108

107-
if [[ -n "$readme_comment" ]]; then
108-
printf "## Info\n" >> "$doc_file"
109-
printf "%s" "$readme_comment" | sed '1d; $d; $s|$|\n\n|' >> "$doc_file"
110-
fi
109+
"${readme_comment:+$'## Info\n'"$(sed '1d;$d' <<< "$readme_comment")"$'\n'}" >> "$doc_file"
111110

112-
if [ -n "$(find "$SCREENSHOTS_DIR" -type f -iname "$id-*.*" 2>/dev/null)" ]; then
113-
printf "## Screenshots\n" >> "$doc_file"
114-
while IFS= read -r screenshot_path; do
115-
screenshot_filename="$(basename "$screenshot_path")"
116-
printf "![Screenshot](screenshots/%s)\n" "$screenshot_filename" >> "$doc_file"
117-
done < <(find "$SCREENSHOTS_DIR" -type f -iname "$id-*.*" | sort)
118-
fi
111+
screenshots="$(find "$SCREENSHOTS_DIR" -type f -iname "$id-*.*" -printf '%f\n' | sort)"
112+
"${screenshots:+$'## Screenshots\n'"$(sed -E 's|(.*)|![screenshot](screenshots/\1)|' <<< "$screenshots")"}" >> "$doc_file"
119113

120114
# --- 5. ADD ROW TO README TABLE ---
121115
escaped_script_name="$(sed 's#|#\\|#g' <<< "$script_name")"
122116
install_links="[Standard]($DOWNLOAD_URL_DIST) / [Minified]($DOWNLOAD_URL_DIST_MIN)"
123-
printf "| [%s](%s) | \`%s\` | \`%s\` | %s |\n" "$escaped_script_name" "$DOCS_DIR/$id.md" "$script_version" "$loc_count" "$install_links" >> "$TABLE_CONTENT_FILE"
117+
printf '| [%s](%s) | `%s` | `%s` | %s |\n' "$escaped_script_name" "$DOCS_DIR/$id.md" "$script_version" "$loc_count" "$install_links" >> "$TABLE_CONTENT_FILE"
124118
done
125119

126120
# --- FINALIZE README ---
127-
printf "Finalizing README.md...\n"
121+
printf 'Finalizing README.md...\n'
128122
userscript_count=$(find "$SRC_DIR" -name "*.user.js" | wc -l)
129123
(
130124
cat "$BEFORE_TABLE_FILE" | sed -E "s|(/badge/loc-)[0-9]+|\1$loc_count_total|; s|(/badge/userscripts-)[0-9]+|\1$userscript_count|"
@@ -134,7 +128,7 @@ userscript_count=$(find "$SRC_DIR" -name "*.user.js" | wc -l)
134128
) > README.md
135129

136130
# --- CLEANUP ---
137-
printf "Cleaning up temporary files...\n"
131+
printf 'Cleaning up temporary files...\n'
138132
rm -f "$BEFORE_TABLE_FILE" "$AFTER_TABLE_FILE" "$TABLE_CONTENT_FILE"
139133

140-
printf "Build process completed successfully!\n"
134+
printf 'Build process completed successfully!\n'

0 commit comments

Comments
 (0)