|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2016-2025 Berry Cloud Ltd. All rights reserved. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# Script to update version numbers in documentation examples |
| 8 | +# This script is called during the release process to ensure documentation |
| 9 | +# examples always reference the current release version |
| 10 | + |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | +REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 13 | + |
| 14 | +# Function to extract version from pom.xml |
| 15 | +get_version_from_pom() { |
| 16 | + local pom_file="$1" |
| 17 | + |
| 18 | + # Extract version from the project section (not parent) |
| 19 | + # Use xmllint if available, otherwise use grep/sed |
| 20 | + if command -v xmllint &> /dev/null; then |
| 21 | + xmllint --xpath '//*[local-name()="project"]/*[local-name()="version"]/text()' "$pom_file" 2>/dev/null | head -1 |
| 22 | + else |
| 23 | + # Fallback to grep/sed approach |
| 24 | + grep -m 1 "^ <version>" "$pom_file" | sed 's/.*<version>\(.*\)<\/version>.*/\1/' |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +# Main script |
| 29 | +main() { |
| 30 | + cd "$REPO_ROOT" |
| 31 | + |
| 32 | + echo "Updating example version numbers in documentation..." |
| 33 | + |
| 34 | + # Get the current version from the root pom.xml |
| 35 | + CURRENT_VERSION=$(get_version_from_pom "pom.xml") |
| 36 | + |
| 37 | + if [ -z "$CURRENT_VERSION" ]; then |
| 38 | + echo "ERROR: Could not extract version from pom.xml" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + |
| 42 | + echo "Current version: $CURRENT_VERSION" |
| 43 | + |
| 44 | + # Strip -SNAPSHOT suffix if present (we want the release version) |
| 45 | + RELEASE_VERSION="${CURRENT_VERSION%-SNAPSHOT}" |
| 46 | + |
| 47 | + echo "Updating documentation to use version: $RELEASE_VERSION" |
| 48 | + |
| 49 | + # Files to update |
| 50 | + FILES_TO_UPDATE=( |
| 51 | + "README.md" |
| 52 | + ) |
| 53 | + |
| 54 | + # Update version numbers in all specified files |
| 55 | + for file in "${FILES_TO_UPDATE[@]}"; do |
| 56 | + if [ ! -f "$file" ]; then |
| 57 | + echo "WARNING: File not found: $file" |
| 58 | + continue |
| 59 | + fi |
| 60 | + |
| 61 | + echo "Updating $file..." |
| 62 | + |
| 63 | + # Create a backup |
| 64 | + cp "$file" "$file.bak" |
| 65 | + |
| 66 | + # Replace version numbers in Maven dependency examples |
| 67 | + # Pattern: <version>X.Y.Z</version> where X.Y.Z is a version number |
| 68 | + # Only replace in dependency blocks (between <dependency> and </dependency>) |
| 69 | + sed -i.tmp '/<dependency>/,/<\/dependency>/ s/<version>[0-9]\+\.[0-9]\+\.[0-9]\+<\/version>/<version>'"$RELEASE_VERSION"'<\/version>/g' "$file" |
| 70 | + |
| 71 | + # Clean up temporary file |
| 72 | + rm -f "$file.tmp" |
| 73 | + |
| 74 | + # Check if file was modified |
| 75 | + if ! diff -q "$file" "$file.bak" > /dev/null 2>&1; then |
| 76 | + echo "✓ Updated $file" |
| 77 | + rm "$file.bak" |
| 78 | + else |
| 79 | + echo "- No changes needed in $file" |
| 80 | + mv "$file.bak" "$file" |
| 81 | + fi |
| 82 | + done |
| 83 | + |
| 84 | + echo "" |
| 85 | + echo "Example version update complete!" |
| 86 | + echo "Version updated to: $RELEASE_VERSION" |
| 87 | + echo "" |
| 88 | + echo "Modified files:" |
| 89 | + git --no-pager diff --name-only |
| 90 | + echo "" |
| 91 | + echo "Summary of changes:" |
| 92 | + git --no-pager diff --stat |
| 93 | +} |
| 94 | + |
| 95 | +# Run main function |
| 96 | +main "$@" |
0 commit comments