agravity_client/update_version.sh

123 lines
3.3 KiB
Bash

#!/bin/bash
# Update version across Agravity Client project files.
#
# This script updates the version in:
# - pyproject.toml
# - agravity_client/__init__.py
#
# Usage:
# ./update_version.sh <version>
# ./update_version.sh 0.2.0
#
# The version must follow semantic versioning (major.minor.patch).
set -euo pipefail
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
validate_version() {
local version=$1
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?$ ]]; then
return 1
fi
return 0
}
update_file() {
local file_path=$1
local pattern=$2
local new_version=$3
if [[ ! -f "$file_path" ]]; then
echo -e "${RED}✗ File not found: $file_path${NC}"
return 1
fi
# Check if content would change
local content
content=$(cat "$file_path")
# Use sed to update the file (handles both macOS and Linux)
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS requires -i '' for in-place editing
sed -i '' "$pattern" "$file_path"
else
# Linux
sed -i "$pattern" "$file_path"
fi
local new_content
new_content=$(cat "$file_path")
if [[ "$content" == "$new_content" ]]; then
echo -e "${GREEN}$(basename "$file_path") already up-to-date${NC}"
else
echo -e "${GREEN}✓ Updated $(basename "$file_path")${NC}"
fi
return 0
}
main() {
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <version>"
echo "Example: $0 0.2.0"
exit 1
fi
local new_version=$1
if ! validate_version "$new_version"; then
echo -e "${RED}✗ Invalid version format: $new_version${NC}"
echo -e "${YELLOW}Version must follow semantic versioning (major.minor.patch)${NC}"
echo -e "${YELLOW}Examples: 1.0.0, 0.2.0, 1.0.0-beta${NC}"
exit 1
fi
local project_root
project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local pyproject_path="$project_root/pyproject.toml"
local init_path="$project_root/agravity_client/__init__.py"
echo "======================================================================"
echo "Updating Agravity Client to version $new_version"
echo "======================================================================"
local success=true
# Update pyproject.toml
if [[ -f "$pyproject_path" ]]; then
local pattern_pyproject="s|version = \"[^\"]*\"|version = \"$new_version\"|g"
if ! update_file "$pyproject_path" "$pattern_pyproject"; then
success=false
fi
else
echo -e "${RED}$pyproject_path not found${NC}"
success=false
fi
# Update __init__.py
if [[ -f "$init_path" ]]; then
local pattern_init="s|__version__ = \"[^\"]*\"|__version__ = \"$new_version\"|g"
if ! update_file "$init_path" "$pattern_init"; then
success=false
fi
else
echo -e "${RED}$init_path not found${NC}"
success=false
fi
echo "======================================================================"
if [[ "$success" == true ]]; then
echo -e "${GREEN}✓ Version successfully updated to $new_version${NC}"
exit 0
else
echo -e "${RED}✗ Version update completed with errors${NC}"
exit 1
fi
}
main "$@"