66set -e
77
88# Base directory to scan (relative to project root)
9- BASE_DIR=" javascript/frameworks/cap/test/queries "
9+ BASE_DIR=" javascript/frameworks/cap/test"
1010
1111# Navigate to project root directory (4 levels up from extractors/cds/tools/test/)
1212SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
@@ -32,26 +32,26 @@ echo ""
3232resolve_cds_dk_version () {
3333 local package_json_path=" $1 "
3434 local minimum_version=8
35-
35+
3636 if [ ! -f " $package_json_path " ]; then
3737 echo " ^$minimum_version "
3838 return
3939 fi
40-
40+
4141 # Extract @sap/cds and @sap/cds-dk versions from package.json using grep and sed
4242 local cds_version=" "
4343 local cds_dk_version=" "
44-
44+
4545 if grep -q ' "@sap/cds"' " $package_json_path " ; then
4646 cds_version=$( grep ' "@sap/cds"' " $package_json_path " | sed -E ' s/.*"@sap\/cds": "([^"]+)".*/\1/' )
4747 fi
48-
48+
4949 if grep -q ' "@sap/cds-dk"' " $package_json_path " ; then
5050 cds_dk_version=$( grep ' "@sap/cds-dk"' " $package_json_path " | sed -E ' s/.*"@sap\/cds-dk": "([^"]+)".*/\1/' )
5151 fi
52-
52+
5353 local preferred_dk_version=" "
54-
54+
5555 if [ -n " $cds_dk_version " ]; then
5656 # Use explicit @sap/cds-dk version if available, but enforce minimum
5757 preferred_dk_version=$( enforce_minimum_version " $cds_dk_version " " $minimum_version " )
@@ -62,18 +62,18 @@ resolve_cds_dk_version() {
6262 # No version information found, use minimum
6363 preferred_dk_version=" ^$minimum_version "
6464 fi
65-
65+
6666 echo " $preferred_dk_version "
6767}
6868
6969# Function to enforce minimum version requirement
7070enforce_minimum_version () {
7171 local version=" $1 "
7272 local minimum_version=" $2 "
73-
73+
7474 # Extract major version number (handle ^, ~, and plain numbers)
7575 local major_version=$( echo " $version " | sed -E ' s/^[\^~]?([0-9]+).*/\1/' )
76-
76+
7777 if [[ " $major_version " =~ ^[0-9]+$ ]]; then
7878 if [ " $major_version " -lt " $minimum_version " ]; then
7979 echo " ^$minimum_version "
@@ -89,10 +89,10 @@ enforce_minimum_version() {
8989derive_compatible_version () {
9090 local cds_version=" $1 "
9191 local minimum_version=" $2 "
92-
92+
9393 # Extract major version and use same range
9494 local major_version=$( echo " $cds_version " | sed -E ' s/^[\^~]?([0-9]+).*/\1/' )
95-
95+
9696 if [[ " $major_version " =~ ^[0-9]+$ ]]; then
9797 local derived_version=" ^$major_version "
9898 # Apply minimum version enforcement
@@ -107,7 +107,7 @@ derive_compatible_version() {
107107get_relative_path () {
108108 local target=" $1 "
109109 local base=" $2 "
110-
110+
111111 # Use Python to calculate relative path (works on both macOS and Linux)
112112 python3 -c " import os.path; print(os.path.relpath('$target ', '$base '))" 2> /dev/null || echo " $target "
113113}
@@ -121,27 +121,64 @@ echo ""
121121# Array to collect generated model.cds.json files
122122GENERATED_FILES=()
123123
124- # Find test directories (those containing .expected files)
124+ # Array to track processed directories to avoid duplicates
125+ PROCESSED_DIRS=()
126+
127+ # Find test directories (those containing .expected files) and deduplicate
125128echo " Scanning for test directories..."
126- for test_dir in $( find " $BASE_DIR " -type f -name ' *.expected' -exec dirname {} \; ) ;
129+ TEST_DIRS=($( find " $BASE_DIR " -type f -name ' *.expected' -exec dirname {} \; | sort -u) )
130+
131+ for test_dir in " ${TEST_DIRS[@]} " ;
127132do
133+ # Skip if this directory has already been processed
134+ if [[ " ${PROCESSED_DIRS[@]} " =~ " ${test_dir} " ]]; then
135+ echo " Skipping already processed directory: $test_dir "
136+ continue
137+ fi
138+
128139 echo " Processing test directory: $test_dir "
129-
140+
130141 # Change to test directory
131142 pushd " $test_dir " > /dev/null
132-
143+
144+ # Check if this directory contains any .cds files in supported locations
145+ echo " Checking for CDS files in project directory: $( pwd) "
146+ CDS_FILES_FOUND=false
147+
148+ # Check for .cds files in the base directory
149+ if find . -maxdepth 1 -type f -name ' *.cds' | grep -q . ; then
150+ CDS_FILES_FOUND=true
151+ fi
152+
153+ # Check for .cds files in app/, db/, or srv/ subdirectories (including nested)
154+ if [ " $CDS_FILES_FOUND " = false ]; then
155+ for subdir in app db srv; do
156+ if [ -d " $subdir " ] && find " $subdir " -type f -name ' *.cds' | grep -q . ; then
157+ CDS_FILES_FOUND=true
158+ break
159+ fi
160+ done
161+ fi
162+
163+ if [ " $CDS_FILES_FOUND " = false ]; then
164+ echo " ⚠️ No .cds files found in base directory or app/db/srv subdirectories - skipping compilation"
165+ popd > /dev/null
166+ echo " "
167+ continue
168+ fi
169+
133170 # Generate a single model.cds.json file per project directory,
134171 # aligning with the CDS extractor's standardized compilation behavior.
135172 echo " Compiling CDS project in directory: $( pwd) "
136-
173+
137174 # Resolve the appropriate @sap/cds-dk version for this project
138175 local_package_json=" $( pwd) /package.json"
139176 preferred_dk_version=$( resolve_cds_dk_version " $local_package_json " )
140177 echo " Resolved @sap/cds-dk version: $preferred_dk_version "
141-
178+
142179 # Determine compilation targets using simplified logic from CDS extractor
143180 COMPILE_TARGETS=" "
144-
181+
145182 # Rule 1. index.cds if the test_dir directly contains an index.cds file (highest priority)
146183 if [ -f " index.cds" ]; then
147184 COMPILE_TARGETS=" index.cds"
160197 echo " Using CAP directories and root CDS files as compilation targets: app db srv $ROOT_FILES "
161198 fi
162199 fi
163-
200+
164201 # Use npx with project-specific version to ensure compatibility
165202 cds_dk_package=" @sap/cds-dk@$preferred_dk_version "
166203 echo " Running: npx --yes --package $cds_dk_package cds compile $COMPILE_TARGETS --locations --to json --dest model.cds.json"
167-
204+
168205 # Disable exit-on-error for this compilation attempt
169206 set +e
170207 npx --yes --package " $cds_dk_package " cds compile \
175212 --log-level warn
176213 COMPILE_EXIT_CODE=$?
177214 set -e
178-
215+
179216 # Log compilation result
180217 if [ -f " model.cds.json" ]; then
181218 echo " ✓ Successfully generated model.cds.json in $( pwd) "
182219 # Add to list of generated files (convert to relative path)
183220 RELATIVE_PATH=$( get_relative_path " $( pwd) /model.cds.json" " $PROJECT_ROOT " )
184221 GENERATED_FILES+=(" $RELATIVE_PATH " )
222+ # Mark this directory as processed
223+ PROCESSED_DIRS+=(" $test_dir " )
185224 else
186225 echo " ✗ Warning: model.cds.json was not generated in $( pwd) (exit code: $COMPILE_EXIT_CODE )"
187226 if [ -s " compilation.err" ]; then
188227 echo " Compilation errors:"
189228 cat " compilation.err" | sed ' s/^/ /'
190229 fi
191230 fi
192-
231+
193232 popd > /dev/null
194233 echo " "
195234done
0 commit comments