Skip to content

Commit 8bb548e

Browse files
Copilotvgoehler
andauthored
Refactor scripts to use functions for logical separation
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 3c5a572 commit 8bb548e

5 files changed

Lines changed: 525 additions & 345 deletions

File tree

scripts/check_changes.sh

Lines changed: 111 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,130 @@ if [ -z "$COURSE" ]; then
99
exit 1
1010
fi
1111

12-
# Create cache directory
13-
mkdir -p .cache
14-
1512
# File paths
1613
YAML_FILE="${COURSE}.yml"
1714
HTML_FILE="${COURSE}.html"
1815
CACHE_FILE=".cache/${COURSE}"
1916

20-
# Check if YAML file exists
21-
if [ ! -f "$YAML_FILE" ]; then
22-
echo "❌ YAML file $YAML_FILE not found"
23-
exit 1
24-
fi
25-
26-
# Calculate YAML hash
27-
YAML_HASH=$(sha256sum "$YAML_FILE" 2>/dev/null | cut -d' ' -f1 || echo "missing")
28-
2917
# Get remote repository name from central config via shared library
3018
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3119
# shellcheck source=courses_lib.sh
3220
. "${SCRIPT_DIR}/courses_lib.sh"
33-
REPO_NAME=$(lookup_repo "$COURSE")
34-
35-
if [ -n "$REPO_NAME" ]; then
36-
echo "🌐 Checking VL_${REPO_NAME} repository..."
37-
API_URL="https://api.github.com/repos/TUBAF-IfI-LiaScript/VL_${REPO_NAME}/commits/master"
38-
39-
# Try jq first (more reliable), fallback to grep
40-
# Use -L to follow redirects (in case repository was renamed/moved)
41-
API_RESPONSE=$(curl -sL --connect-timeout 10 "$API_URL" 2>/dev/null)
42-
43-
if command -v jq >/dev/null 2>&1; then
44-
REMOTE_HASH=$(echo "$API_RESPONSE" | jq -r '.sha' 2>/dev/null || echo "unreachable")
45-
else
46-
REMOTE_HASH=$(echo "$API_RESPONSE" | sed -n 's/.*"sha":"\([^"]*\)".*/\1/p' | head -1)
47-
if [ -z "$REMOTE_HASH" ]; then
21+
22+
# ---------------------------------------------------------------------------
23+
# init – create required directories
24+
# ---------------------------------------------------------------------------
25+
init() {
26+
mkdir -p .cache
27+
if [ ! -f "$YAML_FILE" ]; then
28+
echo "❌ YAML file $YAML_FILE not found"
29+
exit 1
30+
fi
31+
}
32+
33+
# ---------------------------------------------------------------------------
34+
# get_yaml_hash – compute SHA-256 hash of the course YAML file.
35+
# Sets YAML_HASH.
36+
# ---------------------------------------------------------------------------
37+
get_yaml_hash() {
38+
YAML_HASH=$(sha256sum "$YAML_FILE" 2>/dev/null | cut -d' ' -f1 || echo "missing")
39+
}
40+
41+
# ---------------------------------------------------------------------------
42+
# get_remote_hash – fetch the latest commit SHA from the upstream repository.
43+
# Sets REMOTE_HASH.
44+
# ---------------------------------------------------------------------------
45+
get_remote_hash() {
46+
REPO_NAME=$(lookup_repo "$COURSE")
47+
48+
if [ -n "$REPO_NAME" ]; then
49+
echo "🌐 Checking VL_${REPO_NAME} repository..."
50+
API_URL="https://api.github.com/repos/TUBAF-IfI-LiaScript/VL_${REPO_NAME}/commits/master"
51+
52+
# Use -L to follow redirects (in case repository was renamed/moved)
53+
API_RESPONSE=$(curl -sL --connect-timeout 10 "$API_URL" 2>/dev/null)
54+
55+
if command -v jq >/dev/null 2>&1; then
56+
REMOTE_HASH=$(echo "$API_RESPONSE" | jq -r '.sha' 2>/dev/null || echo "unreachable")
57+
else
58+
REMOTE_HASH=$(echo "$API_RESPONSE" | sed -n 's/.*"sha":"\([^"]*\)".*/\1/p' | head -1)
59+
if [ -z "$REMOTE_HASH" ]; then
60+
REMOTE_HASH="unreachable"
61+
fi
62+
fi
63+
64+
if [ "$REMOTE_HASH" = "unreachable" ] || [ -z "$REMOTE_HASH" ]; then
65+
echo "⚠️ Failed to get remote hash"
4866
REMOTE_HASH="unreachable"
4967
fi
68+
else
69+
REMOTE_HASH="no-remote"
5070
fi
51-
52-
if [ "$REMOTE_HASH" = "unreachable" ] || [ -z "$REMOTE_HASH" ]; then
53-
echo "⚠️ Failed to get remote hash"
54-
REMOTE_HASH="unreachable"
71+
}
72+
73+
# ---------------------------------------------------------------------------
74+
# read_cache – load previously stored hashes from cache file.
75+
# Sets CACHED_YAML and CACHED_REMOTE.
76+
# ---------------------------------------------------------------------------
77+
read_cache() {
78+
if [ -f "$CACHE_FILE" ]; then
79+
CACHED_YAML=$(sed -n '1p' "$CACHE_FILE" 2>/dev/null || echo "missing")
80+
CACHED_REMOTE=$(sed -n '2p' "$CACHE_FILE" 2>/dev/null || echo "missing")
81+
else
82+
CACHED_YAML="missing"
83+
CACHED_REMOTE="missing"
5584
fi
56-
else
57-
REMOTE_HASH="no-remote"
58-
fi
85+
}
5986

60-
# Read cached values
61-
if [ -f "$CACHE_FILE" ]; then
62-
CACHED_YAML=$(sed -n '1p' "$CACHE_FILE" 2>/dev/null || echo "missing")
63-
CACHED_REMOTE=$(sed -n '2p' "$CACHE_FILE" 2>/dev/null || echo "missing")
64-
else
65-
CACHED_YAML="missing"
66-
CACHED_REMOTE="missing"
67-
fi
87+
# ---------------------------------------------------------------------------
88+
# print_status – display current vs. cached hash values.
89+
# ---------------------------------------------------------------------------
90+
print_status() {
91+
echo "📄 YAML hash: ${YAML_HASH:0:8}..."
92+
echo "🌐 Remote hash: ${REMOTE_HASH:0:8}..."
93+
echo "💾 Cached YAML: ${CACHED_YAML:0:8}..."
94+
echo "💾 Cached remote: ${CACHED_REMOTE:0:8}..."
95+
}
6896

69-
# Display status
70-
echo "📄 YAML hash: ${YAML_HASH:0:8}..."
71-
echo "🌐 Remote hash: ${REMOTE_HASH:0:8}..."
72-
echo "💾 Cached YAML: ${CACHED_YAML:0:8}..."
73-
echo "💾 Cached remote: ${CACHED_REMOTE:0:8}..."
74-
75-
# Check for changes
76-
REBUILD_NEEDED=false
77-
REASON=""
78-
79-
if [ "$YAML_HASH" != "$CACHED_YAML" ]; then
80-
REBUILD_NEEDED=true
81-
REASON="YAML file changed"
82-
elif [ "$REMOTE_HASH" != "$CACHED_REMOTE" ] && [ "$REMOTE_HASH" != "unreachable" ]; then
83-
REBUILD_NEEDED=true
84-
REASON="Remote repository changed"
85-
elif [ ! -f "$HTML_FILE" ]; then
86-
REBUILD_NEEDED=true
87-
REASON="HTML file missing"
88-
fi
97+
# ---------------------------------------------------------------------------
98+
# check_rebuild_needed – determine if a rebuild is required.
99+
# Exits 0 if rebuild needed, exits 1 if no rebuild needed.
100+
# ---------------------------------------------------------------------------
101+
check_rebuild_needed() {
102+
local rebuild_needed=false
103+
local reason=""
104+
105+
if [ "$YAML_HASH" != "$CACHED_YAML" ]; then
106+
rebuild_needed=true
107+
reason="YAML file changed"
108+
elif [ "$REMOTE_HASH" != "$CACHED_REMOTE" ] && [ "$REMOTE_HASH" != "unreachable" ]; then
109+
rebuild_needed=true
110+
reason="Remote repository changed"
111+
elif [ ! -f "$HTML_FILE" ]; then
112+
rebuild_needed=true
113+
reason="HTML file missing"
114+
fi
115+
116+
if [ "$rebuild_needed" = true ]; then
117+
echo "$reason - rebuild needed"
118+
# Note: Cache will be updated by the build system after successful build
119+
exit 0 # Rebuild needed
120+
else
121+
echo "⏭️ No changes detected - skipping"
122+
exit 1 # No rebuild needed
123+
fi
124+
}
125+
126+
# ---------------------------------------------------------------------------
127+
# main
128+
# ---------------------------------------------------------------------------
129+
main() {
130+
init
131+
get_yaml_hash
132+
get_remote_hash
133+
read_cache
134+
print_status
135+
check_rebuild_needed
136+
}
89137

90-
# Output result
91-
if [ "$REBUILD_NEEDED" = true ]; then
92-
echo "$REASON - rebuild needed"
93-
# Note: Cache will be updated by the build system after successful build
94-
exit 0 # Rebuild needed
95-
else
96-
echo "⏭️ No changes detected - skipping"
97-
exit 1 # No rebuild needed
98-
fi
138+
main

scripts/detect_changes.sh

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,60 @@
44

55
set -euo pipefail
66

7-
echo "=== Detecting Changes ==="
8-
9-
# Get changed YAML files from last commit
10-
changed_yamls=$(git diff --name-only HEAD~1 HEAD | grep '\.yml$' | grep -v '^\.github/' || true)
11-
echo "Changed YAML files: $changed_yamls"
12-
13-
# Initialize lists for processing
14-
courses_to_generate=""
15-
missing_html=""
16-
17-
# Check each course YAML file
18-
for yaml_file in *.yml; do
19-
course_name=$(basename "$yaml_file" .yml)
20-
html_file="${course_name}.html"
21-
22-
# Check if YAML was changed or HTML is missing
23-
if echo "$changed_yamls" | grep -q "$yaml_file" || [ ! -f "$html_file" ]; then
24-
echo "Course '$course_name' needs regeneration:"
25-
if echo "$changed_yamls" | grep -q "$yaml_file"; then
26-
echo " - YAML file was changed"
27-
fi
28-
if [ ! -f "$html_file" ]; then
29-
echo " - HTML file is missing"
30-
missing_html="$missing_html $course_name"
7+
# ---------------------------------------------------------------------------
8+
# get_changed_yamls – list YAML files modified in the last commit.
9+
# Sets CHANGED_YAMLS.
10+
# ---------------------------------------------------------------------------
11+
get_changed_yamls() {
12+
CHANGED_YAMLS=$(git diff --name-only HEAD~1 HEAD | grep '\.yml$' | grep -v '^\.github/' || true)
13+
echo "Changed YAML files: $CHANGED_YAMLS"
14+
}
15+
16+
# ---------------------------------------------------------------------------
17+
# check_courses – iterate all course YAMLs, collect which need regeneration.
18+
# Sets COURSES_TO_GENERATE and MISSING_HTML.
19+
# ---------------------------------------------------------------------------
20+
check_courses() {
21+
COURSES_TO_GENERATE=""
22+
MISSING_HTML=""
23+
24+
for yaml_file in *.yml; do
25+
local course_name html_file
26+
course_name=$(basename "$yaml_file" .yml)
27+
html_file="${course_name}.html"
28+
29+
if echo "$CHANGED_YAMLS" | grep -q "$yaml_file" || [ ! -f "$html_file" ]; then
30+
echo "Course '$course_name' needs regeneration:"
31+
if echo "$CHANGED_YAMLS" | grep -q "$yaml_file"; then
32+
echo " - YAML file was changed"
33+
fi
34+
if [ ! -f "$html_file" ]; then
35+
echo " - HTML file is missing"
36+
MISSING_HTML="$MISSING_HTML $course_name"
37+
fi
38+
COURSES_TO_GENERATE="$COURSES_TO_GENERATE $course_name"
39+
else
40+
echo "Course '$course_name' is up to date"
3141
fi
32-
courses_to_generate="$courses_to_generate $course_name"
33-
else
34-
echo "Course '$course_name' is up to date"
35-
fi
36-
done
37-
38-
echo "courses_to_generate=$courses_to_generate" >> "$GITHUB_OUTPUT"
39-
echo "missing_html=$missing_html" >> "$GITHUB_OUTPUT"
42+
done
43+
}
44+
45+
# ---------------------------------------------------------------------------
46+
# write_outputs – write results to $GITHUB_OUTPUT
47+
# ---------------------------------------------------------------------------
48+
write_outputs() {
49+
echo "courses_to_generate=$COURSES_TO_GENERATE" >> "$GITHUB_OUTPUT"
50+
echo "missing_html=$MISSING_HTML" >> "$GITHUB_OUTPUT"
51+
}
52+
53+
# ---------------------------------------------------------------------------
54+
# main
55+
# ---------------------------------------------------------------------------
56+
main() {
57+
echo "=== Detecting Changes ==="
58+
get_changed_yamls
59+
check_courses
60+
write_outputs
61+
}
62+
63+
main

0 commit comments

Comments
 (0)