@@ -9,106 +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
17+ # Get remote repository name from central config via shared library
18+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
19+ # shellcheck source=courses_lib.sh
20+ . " ${SCRIPT_DIR} /courses_lib.sh"
2521
26- # Calculate YAML hash
27- YAML_HASH=$( sha256sum " $YAML_FILE " 2> /dev/null | cut -d' ' -f1 || echo " missing" )
28-
29- # Get remote repository hash
30- case " $COURSE " in
31- " digitalesysteme" )
32- REPO_NAME=" EingebetteteSysteme"
33- ;;
34- " prozprog" )
35- REPO_NAME=" ProzeduraleProgrammierung"
36- ;;
37- " softwareentwicklung" )
38- REPO_NAME=" Softwareentwicklung"
39- ;;
40- " robotikprojekt" )
41- REPO_NAME=" SoftwareprojektRobotik"
42- ;;
43- " index" )
44- REPO_NAME=" " # No remote monitoring for index
45- ;;
46- * )
47- REPO_NAME=" "
48- ;;
49- esac
50-
51- if [ -n " $REPO_NAME " ]; then
52- echo " 🌐 Checking VL_${REPO_NAME} repository..."
53- API_URL=" https://api.github.com/repos/TUBAF-IfI-LiaScript/VL_${REPO_NAME} /commits/master"
54-
55- # Try jq first (more reliable), fallback to grep
56- # Use -L to follow redirects (in case repository was renamed/moved)
57- API_RESPONSE=$( curl -sL --connect-timeout 10 " $API_URL " 2> /dev/null)
58-
59- if command -v jq > /dev/null 2>&1 ; then
60- REMOTE_HASH=$( echo " $API_RESPONSE " | jq -r ' .sha' 2> /dev/null || echo " unreachable" )
61- else
62- REMOTE_HASH=$( echo " $API_RESPONSE " | sed -n ' s/.*"sha":"\([^"]*\)".*/\1/p' | head -1)
63- if [ -z " $REMOTE_HASH " ]; then
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"
6466 REMOTE_HASH=" unreachable"
6567 fi
68+ else
69+ REMOTE_HASH=" no-remote"
6670 fi
67-
68- if [ " $REMOTE_HASH " = " unreachable" ] || [ -z " $REMOTE_HASH " ]; then
69- echo " ⚠️ Failed to get remote hash"
70- 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"
7184 fi
72- else
73- REMOTE_HASH=" no-remote"
74- fi
85+ }
7586
76- # Read cached values
77- if [ -f " $CACHE_FILE " ]; then
78- CACHED_YAML=$( sed -n ' 1p' " $CACHE_FILE " 2> /dev/null || echo " missing" )
79- CACHED_REMOTE=$( sed -n ' 2p' " $CACHE_FILE " 2> /dev/null || echo " missing" )
80- else
81- CACHED_YAML=" missing"
82- CACHED_REMOTE=" missing"
83- 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+ }
8496
85- # Display status
86- echo " 📄 YAML hash: ${YAML_HASH: 0: 8} ..."
87- echo " 🌐 Remote hash: ${REMOTE_HASH: 0: 8} ..."
88- echo " 💾 Cached YAML: ${CACHED_YAML: 0: 8} ..."
89- echo " 💾 Cached remote: ${CACHED_REMOTE: 0: 8} ..."
90-
91- # Check for changes
92- REBUILD_NEEDED=false
93- REASON=" "
94-
95- if [ " $YAML_HASH " != " $CACHED_YAML " ]; then
96- REBUILD_NEEDED=true
97- REASON=" YAML file changed"
98- elif [ " $REMOTE_HASH " != " $CACHED_REMOTE " ] && [ " $REMOTE_HASH " != " unreachable" ]; then
99- REBUILD_NEEDED=true
100- REASON=" Remote repository changed"
101- elif [ ! -f " $HTML_FILE " ]; then
102- REBUILD_NEEDED=true
103- REASON=" HTML file missing"
104- 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+ }
105137
106- # Output result
107- if [ " $REBUILD_NEEDED " = true ]; then
108- echo " ✅ $REASON - rebuild needed"
109- # Note: Cache will be updated by the build system after successful build
110- exit 0 # Rebuild needed
111- else
112- echo " ⏭️ No changes detected - skipping"
113- exit 1 # No rebuild needed
114- fi
138+ main
0 commit comments