|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Temporary test script for CDS compilation logic from GitHub Actions workflow |
| 4 | +# Usage: ./cds-compilation-for-actions.test.sh |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Base directory to scan (relative to project root) |
| 9 | +BASE_DIR="javascript/frameworks/cap/test/queries" |
| 10 | + |
| 11 | +# Navigate to project root directory (4 levels up from extractors/cds/tools/test/) |
| 12 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 13 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../../" && pwd)" |
| 14 | +cd "$PROJECT_ROOT" |
| 15 | + |
| 16 | +# Verify base directory exists |
| 17 | +if [ ! -d "$BASE_DIR" ]; then |
| 18 | + echo "Error: Base directory '$BASE_DIR' does not exist" |
| 19 | + echo "Current working directory: $(pwd)" |
| 20 | + echo "Expected path: $PROJECT_ROOT/$BASE_DIR" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +echo "Testing CDS compilation logic from GitHub Actions workflow" |
| 25 | +echo "Project root: $PROJECT_ROOT" |
| 26 | +echo "Base directory: $BASE_DIR" |
| 27 | +echo "Working from: $(pwd)" |
| 28 | +echo "" |
| 29 | + |
| 30 | +# Function to resolve CDS-DK version based on package.json |
| 31 | +# Follows the same logic as resolveCdsVersions in command.ts |
| 32 | +resolve_cds_dk_version() { |
| 33 | + local package_json_path="$1" |
| 34 | + local minimum_version=8 |
| 35 | + |
| 36 | + if [ ! -f "$package_json_path" ]; then |
| 37 | + echo "^$minimum_version" |
| 38 | + return |
| 39 | + fi |
| 40 | + |
| 41 | + # Extract @sap/cds and @sap/cds-dk versions from package.json using grep and sed |
| 42 | + local cds_version="" |
| 43 | + local cds_dk_version="" |
| 44 | + |
| 45 | + if grep -q '"@sap/cds"' "$package_json_path"; then |
| 46 | + cds_version=$(grep '"@sap/cds"' "$package_json_path" | sed -E 's/.*"@sap\/cds": "([^"]+)".*/\1/') |
| 47 | + fi |
| 48 | + |
| 49 | + if grep -q '"@sap/cds-dk"' "$package_json_path"; then |
| 50 | + cds_dk_version=$(grep '"@sap/cds-dk"' "$package_json_path" | sed -E 's/.*"@sap\/cds-dk": "([^"]+)".*/\1/') |
| 51 | + fi |
| 52 | + |
| 53 | + local preferred_dk_version="" |
| 54 | + |
| 55 | + if [ -n "$cds_dk_version" ]; then |
| 56 | + # Use explicit @sap/cds-dk version if available, but enforce minimum |
| 57 | + preferred_dk_version=$(enforce_minimum_version "$cds_dk_version" "$minimum_version") |
| 58 | + elif [ -n "$cds_version" ]; then |
| 59 | + # Derive compatible @sap/cds-dk version from @sap/cds version |
| 60 | + preferred_dk_version=$(derive_compatible_version "$cds_version" "$minimum_version") |
| 61 | + else |
| 62 | + # No version information found, use minimum |
| 63 | + preferred_dk_version="^$minimum_version" |
| 64 | + fi |
| 65 | + |
| 66 | + echo "$preferred_dk_version" |
| 67 | +} |
| 68 | + |
| 69 | +# Function to enforce minimum version requirement |
| 70 | +enforce_minimum_version() { |
| 71 | + local version="$1" |
| 72 | + local minimum_version="$2" |
| 73 | + |
| 74 | + # Extract major version number (handle ^, ~, and plain numbers) |
| 75 | + local major_version=$(echo "$version" | sed -E 's/^[\^~]?([0-9]+).*/\1/') |
| 76 | + |
| 77 | + if [[ "$major_version" =~ ^[0-9]+$ ]]; then |
| 78 | + if [ "$major_version" -lt "$minimum_version" ]; then |
| 79 | + echo "^$minimum_version" |
| 80 | + else |
| 81 | + echo "$version" |
| 82 | + fi |
| 83 | + else |
| 84 | + echo "$version" |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +# Function to derive compatible @sap/cds-dk version from @sap/cds version |
| 89 | +derive_compatible_version() { |
| 90 | + local cds_version="$1" |
| 91 | + local minimum_version="$2" |
| 92 | + |
| 93 | + # Extract major version and use same range |
| 94 | + local major_version=$(echo "$cds_version" | sed -E 's/^[\^~]?([0-9]+).*/\1/') |
| 95 | + |
| 96 | + if [[ "$major_version" =~ ^[0-9]+$ ]]; then |
| 97 | + local derived_version="^$major_version" |
| 98 | + # Apply minimum version enforcement |
| 99 | + enforce_minimum_version "$derived_version" "$minimum_version" |
| 100 | + else |
| 101 | + # Fallback if version can't be parsed - use minimum |
| 102 | + echo "^$minimum_version" |
| 103 | + fi |
| 104 | +} |
| 105 | + |
| 106 | +# Function to create relative path (macOS compatible) |
| 107 | +get_relative_path() { |
| 108 | + local target="$1" |
| 109 | + local base="$2" |
| 110 | + |
| 111 | + # Use Python to calculate relative path (works on both macOS and Linux) |
| 112 | + python3 -c "import os.path; print(os.path.relpath('$target', '$base'))" 2>/dev/null || echo "$target" |
| 113 | +} |
| 114 | + |
| 115 | +# Clean up any existing model.cds.json files first |
| 116 | +echo "Cleaning up existing model.cds.json files..." |
| 117 | +find "$BASE_DIR" -name "model.cds.json" -type f -delete |
| 118 | +echo "Cleanup completed." |
| 119 | +echo "" |
| 120 | + |
| 121 | +# Array to collect generated model.cds.json files |
| 122 | +GENERATED_FILES=() |
| 123 | + |
| 124 | +# Find test directories (those containing .expected files) |
| 125 | +echo "Scanning for test directories..." |
| 126 | +for test_dir in $(find "$BASE_DIR" -type f -name '*.expected' -exec dirname {} \;); |
| 127 | +do |
| 128 | + echo "Processing test directory: $test_dir" |
| 129 | + |
| 130 | + # Change to test directory |
| 131 | + pushd "$test_dir" > /dev/null |
| 132 | + |
| 133 | + # Generate a single model.cds.json file per project directory, |
| 134 | + # aligning with the CDS extractor's standardized compilation behavior. |
| 135 | + echo " Compiling CDS project in directory: $(pwd)" |
| 136 | + |
| 137 | + # Resolve the appropriate @sap/cds-dk version for this project |
| 138 | + local_package_json="$(pwd)/package.json" |
| 139 | + preferred_dk_version=$(resolve_cds_dk_version "$local_package_json") |
| 140 | + echo " Resolved @sap/cds-dk version: $preferred_dk_version" |
| 141 | + |
| 142 | + # Determine compilation targets using simplified logic from CDS extractor |
| 143 | + COMPILE_TARGETS="" |
| 144 | + |
| 145 | + # Rule 1. index.cds if the test_dir directly contains an index.cds file (highest priority) |
| 146 | + if [ -f "index.cds" ]; then |
| 147 | + COMPILE_TARGETS="index.cds" |
| 148 | + echo " Using index.cds as compilation target" |
| 149 | + else |
| 150 | + # Rule 2. app/ db/ srv/ if there are no .cds files directly in the test_dir |
| 151 | + ROOT_CDS_FILES=$(find . -maxdepth 1 -type f -name '*.cds' | wc -l) |
| 152 | + if [ "$ROOT_CDS_FILES" -eq 0 ]; then |
| 153 | + # No root CDS files, use CAP directories |
| 154 | + COMPILE_TARGETS="app db srv" |
| 155 | + echo " Using CAP directories as compilation targets: app db srv" |
| 156 | + else |
| 157 | + # Rule 3. app/ db/ srv/ custom-alt.cds if there is some custom-alt.cds file directly in the test_dir |
| 158 | + ROOT_FILES=$(find . -maxdepth 1 -type f -name '*.cds' -printf '%f ' 2>/dev/null || find . -maxdepth 1 -type f -name '*.cds' | sed 's|^\./||' | tr '\n' ' ') |
| 159 | + COMPILE_TARGETS="app db srv $ROOT_FILES" |
| 160 | + echo " Using CAP directories and root CDS files as compilation targets: app db srv $ROOT_FILES" |
| 161 | + fi |
| 162 | + fi |
| 163 | + |
| 164 | + # Use npx with project-specific version to ensure compatibility |
| 165 | + cds_dk_package="@sap/cds-dk@$preferred_dk_version" |
| 166 | + echo " Running: npx --yes --package $cds_dk_package cds compile $COMPILE_TARGETS --locations --to json --dest model.cds.json" |
| 167 | + |
| 168 | + # Disable exit-on-error for this compilation attempt |
| 169 | + set +e |
| 170 | + npx --yes --package "$cds_dk_package" cds compile \ |
| 171 | + $COMPILE_TARGETS \ |
| 172 | + --locations \ |
| 173 | + --to json \ |
| 174 | + --dest "model.cds.json" \ |
| 175 | + --log-level warn |
| 176 | + COMPILE_EXIT_CODE=$? |
| 177 | + set -e |
| 178 | + |
| 179 | + # Log compilation result |
| 180 | + if [ -f "model.cds.json" ]; then |
| 181 | + echo " ✓ Successfully generated model.cds.json in $(pwd)" |
| 182 | + # Add to list of generated files (convert to relative path) |
| 183 | + RELATIVE_PATH=$(get_relative_path "$(pwd)/model.cds.json" "$PROJECT_ROOT") |
| 184 | + GENERATED_FILES+=("$RELATIVE_PATH") |
| 185 | + else |
| 186 | + echo " ✗ Warning: model.cds.json was not generated in $(pwd) (exit code: $COMPILE_EXIT_CODE)" |
| 187 | + if [ -s "compilation.err" ]; then |
| 188 | + echo " Compilation errors:" |
| 189 | + cat "compilation.err" | sed 's/^/ /' |
| 190 | + fi |
| 191 | + fi |
| 192 | + |
| 193 | + popd > /dev/null |
| 194 | + echo "" |
| 195 | +done |
| 196 | + |
| 197 | +echo "=== COMPILATION SUMMARY ===" |
| 198 | +if [ ${#GENERATED_FILES[@]} -eq 0 ]; then |
| 199 | + echo "No model.cds.json files were generated." |
| 200 | +else |
| 201 | + echo "Generated ${#GENERATED_FILES[@]} model.cds.json file(s):" |
| 202 | + for file in "${GENERATED_FILES[@]}"; do |
| 203 | + echo " $file" |
| 204 | + done |
| 205 | +fi |
| 206 | + |
| 207 | +echo "" |
| 208 | +echo "Test script completed." |
0 commit comments