Skip to content

Commit 4c7f7f6

Browse files
Copilotvgoehler
andauthored
Address code review: fix implicit global state between functions
Agent-Logs-Url: https://github.com/TUBAF-IfI-LiaScript/TUBAF-IfI-LiaScript.github.io/sessions/ba058e20-3bf9-4dba-b44b-66dfa10c52a7 Co-authored-by: vgoehler <1705385+vgoehler@users.noreply.github.com>
1 parent 8bb548e commit 4c7f7f6

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

scripts/detect_changes.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ get_changed_yamls() {
1515

1616
# ---------------------------------------------------------------------------
1717
# check_courses – iterate all course YAMLs, collect which need regeneration.
18+
# $1: space-separated list of changed YAML files
1819
# Sets COURSES_TO_GENERATE and MISSING_HTML.
1920
# ---------------------------------------------------------------------------
2021
check_courses() {
22+
local changed_yamls="$1"
2123
COURSES_TO_GENERATE=""
2224
MISSING_HTML=""
2325

@@ -26,9 +28,9 @@ check_courses() {
2628
course_name=$(basename "$yaml_file" .yml)
2729
html_file="${course_name}.html"
2830

29-
if echo "$CHANGED_YAMLS" | grep -q "$yaml_file" || [ ! -f "$html_file" ]; then
31+
if echo "$changed_yamls" | grep -q "$yaml_file" || [ ! -f "$html_file" ]; then
3032
echo "Course '$course_name' needs regeneration:"
31-
if echo "$CHANGED_YAMLS" | grep -q "$yaml_file"; then
33+
if echo "$changed_yamls" | grep -q "$yaml_file"; then
3234
echo " - YAML file was changed"
3335
fi
3436
if [ ! -f "$html_file" ]; then
@@ -56,7 +58,7 @@ write_outputs() {
5658
main() {
5759
echo "=== Detecting Changes ==="
5860
get_changed_yamls
59-
check_courses
61+
check_courses "$CHANGED_YAMLS"
6062
write_outputs
6163
}
6264

scripts/download_upstream_pdfs.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
2020
# shellcheck source=courses_lib.sh
2121
. "${SCRIPT_DIR}/courses_lib.sh"
2222

23+
# Global state shared between functions
24+
NAMES=() # deduplicated PDF asset names (set by parse_pdf_assets)
25+
URLS=() # corresponding download URLs (set by parse_pdf_assets)
26+
declare -A CACHED_URL # manifest cache: name → url (set by load_manifest)
27+
2328
# ---------------------------------------------------------------------------
2429
# validate_args – check required arguments and resolve course→repo mapping
2530
# ---------------------------------------------------------------------------
@@ -107,7 +112,7 @@ parse_pdf_assets() {
107112
fi
108113

109114
# Deduplicate: keep the first occurrence of each filename (= most-recent release)
110-
declare -gA SEEN
115+
declare -A SEEN
111116
NAMES=()
112117
URLS=()
113118
for i in "${!ALL_NAMES[@]}"; do
@@ -126,7 +131,6 @@ parse_pdf_assets() {
126131
# load_manifest – read the cached URL manifest into CACHED_URL associative array
127132
# ---------------------------------------------------------------------------
128133
load_manifest() {
129-
declare -gA CACHED_URL
130134
if [ -f "$MANIFEST" ]; then
131135
while IFS=$'\t' read -r cached_name cached_url; do
132136
[[ -z "$cached_name" ]] && continue

scripts/generate_courses.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1010

1111
# ---------------------------------------------------------------------------
1212
# determine_pdf_needs – decide if PDF generation is required for a course.
13-
# Sets the global `needs_pdfs` variable (true/false).
13+
# Sets the caller-visible variable `needs_pdfs` to "true" or "false".
14+
# Must be called from a context where `needs_pdfs` is already declared.
1415
# ---------------------------------------------------------------------------
1516
determine_pdf_needs() {
1617
local course="$1"
1718
local yaml_file="${course}.yml"
1819
local manifest=".cache/${course}_upstream_pdfs"
19-
needs_pdfs=false
2020

2121
# Try to download upstream PDFs (creates/updates the manifest)
2222
if bash "$SCRIPT_DIR/download_upstream_pdfs.sh" "$course"; then
@@ -51,9 +51,11 @@ determine_pdf_needs() {
5151

5252
# ---------------------------------------------------------------------------
5353
# run_liaex – run the liaex command for a given course.
54+
# $1: course name $2: "true" if PDF generation is needed, "false" otherwise
5455
# ---------------------------------------------------------------------------
5556
run_liaex() {
5657
local course="$1"
58+
local needs_pdfs="$2"
5759
local yaml_file="${course}.yml"
5860

5961
case "$course" in
@@ -107,13 +109,14 @@ generate_course() {
107109

108110
echo "Generating $html_file from $yaml_file..."
109111

110-
# Determine whether PDF generation is needed (skipped for index)
112+
# Initialize needs_pdfs; determine_pdf_needs() will update it if applicable.
113+
# Not declared local so that determine_pdf_needs can write to the same variable.
111114
needs_pdfs=false
112115
if [ "$course" != "index" ]; then
113116
determine_pdf_needs "$course"
114117
fi
115118

116-
run_liaex "$course"
119+
run_liaex "$course" "$needs_pdfs"
117120

118121
if [ -f "$html_file" ]; then
119122
echo "✅ Successfully generated $html_file"

scripts/prune_pdfs.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ fi
1212
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
1313
cd "$repo_root"
1414

15-
# Associative array of PDF absolute paths to keep
15+
# Associative array of PDF absolute paths to keep (populated by collect_* functions)
1616
declare -A keep
17+
# Array of on-disk PDFs that are not referenced (populated by find_unreferenced)
18+
unreferenced=()
1719

1820
# ---------------------------------------------------------------------------
1921
# collect_html_refs – scan HTML files and add referenced PDFs to `keep`

0 commit comments

Comments
 (0)