@@ -9,90 +9,130 @@ if [ -z "$COURSE" ]; then
99 exit 1
1010fi
1111
12- # Create cache directory
13- mkdir -p .cache
14-
1512# File paths
1613YAML_FILE=" ${COURSE} .yml"
1714HTML_FILE=" ${COURSE} .html"
1815CACHE_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
3018SCRIPT_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
0 commit comments