|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to create an upgradeable contract by copying v1 to a new version |
| 4 | +# Usage: ./patch-upgrade.sh <contract_path> <version> |
| 5 | +# Example: ./patch-upgrade.sh contract/r/gnoswap/launchpad v2 |
| 6 | + |
| 7 | +set -e |
| 8 | + |
| 9 | +# Check if required arguments are provided |
| 10 | +if [ $# -ne 2 ]; then |
| 11 | + echo "Usage: $0 <contract_path> <version>" |
| 12 | + echo "Example: $0 contract/r/gnoswap/launchpad v2" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# Get the script directory and project root |
| 17 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 18 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 19 | + |
| 20 | +# Get contract path relative to project root |
| 21 | +CONTRACT_PATH_REL=$1 |
| 22 | +VERSION=$2 |
| 23 | + |
| 24 | +# Convert to absolute path based on project root |
| 25 | +CONTRACT_PATH="${PROJECT_ROOT}/${CONTRACT_PATH_REL}" |
| 26 | + |
| 27 | +# Validate that VERSION starts with 'v' followed by a number |
| 28 | +if [[ ! $VERSION =~ ^v[0-9]+$ ]]; then |
| 29 | + echo "Error: Version must be in format 'v<number>' (e.g., v2, v3)" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Check if source directory exists |
| 34 | +SOURCE_DIR="${CONTRACT_PATH}/v1" |
| 35 | +if [ ! -d "$SOURCE_DIR" ]; then |
| 36 | + echo "Error: Source directory '$SOURCE_DIR' does not exist" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Set target directory |
| 41 | +TARGET_DIR="${CONTRACT_PATH}/${VERSION}" |
| 42 | + |
| 43 | +# Check if target directory already exists |
| 44 | +if [ -d "$TARGET_DIR" ]; then |
| 45 | + echo "Error: Target directory '$TARGET_DIR' already exists" |
| 46 | + echo "Please remove it first or choose a different version" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +echo "Creating upgradeable contract..." |
| 51 | +echo " Project Root: $PROJECT_ROOT" |
| 52 | +echo " Contract Path: $CONTRACT_PATH_REL" |
| 53 | +echo " Source: $SOURCE_DIR" |
| 54 | +echo " Target: $TARGET_DIR" |
| 55 | +echo " Version: $VERSION" |
| 56 | +echo "" |
| 57 | + |
| 58 | +# Step 1: Copy v1 directory to new version directory (excluding *test.gno files) |
| 59 | +echo "[1/3] Copying v1 directory to ${VERSION} (excluding *test.gno files)..." |
| 60 | +rsync -a --exclude='*test.gno' "$SOURCE_DIR/" "$TARGET_DIR/" |
| 61 | +echo " ✓ Directory copied successfully" |
| 62 | + |
| 63 | +# Step 2: Update gnomod.toml module path |
| 64 | +GNOMOD_FILE="${TARGET_DIR}/gnomod.toml" |
| 65 | +if [ -f "$GNOMOD_FILE" ]; then |
| 66 | + echo "[2/3] Updating gnomod.toml module path..." |
| 67 | + # Replace /v1 with /${VERSION} in the module line |
| 68 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 69 | + # macOS sed syntax |
| 70 | + sed -i '' "s|/v1\"|/${VERSION}\"|g" "$GNOMOD_FILE" |
| 71 | + else |
| 72 | + # Linux sed syntax |
| 73 | + sed -i "s|/v1\"|/${VERSION}\"|g" "$GNOMOD_FILE" |
| 74 | + fi |
| 75 | + echo " ✓ gnomod.toml updated successfully" |
| 76 | +else |
| 77 | + echo " ⚠ Warning: gnomod.toml not found in target directory" |
| 78 | +fi |
| 79 | + |
| 80 | +# Step 3: Update package declaration in all .gno files |
| 81 | +echo "[3/3] Updating package declarations in *.gno files..." |
| 82 | +GNO_FILES=$(find "$TARGET_DIR" -name "*.gno" -type f) |
| 83 | +FILE_COUNT=0 |
| 84 | + |
| 85 | +for file in $GNO_FILES; do |
| 86 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 87 | + # macOS sed syntax |
| 88 | + sed -i '' "s/^package v1$/package ${VERSION}/g" "$file" |
| 89 | + else |
| 90 | + # Linux sed syntax |
| 91 | + sed -i "s/^package v1$/package ${VERSION}/g" "$file" |
| 92 | + fi |
| 93 | + FILE_COUNT=$((FILE_COUNT + 1)) |
| 94 | +done |
| 95 | + |
| 96 | +echo " ✓ Updated $FILE_COUNT .gno files" |
| 97 | +echo "" |
| 98 | +echo "✅ Upgradeable contract created successfully!" |
| 99 | +echo "New contract location: $TARGET_DIR" |
0 commit comments