|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Colors for output |
| 5 | +RED='\033[0;31m' |
| 6 | +GREEN='\033[0;32m' |
| 7 | +YELLOW='\033[1;33m' |
| 8 | +NC='\033[0m' # No Color |
| 9 | + |
| 10 | +# Function to print colored output |
| 11 | +info() { |
| 12 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 13 | +} |
| 14 | + |
| 15 | +warn() { |
| 16 | + echo -e "${YELLOW}[WARN]${NC} $1" |
| 17 | +} |
| 18 | + |
| 19 | +error() { |
| 20 | + echo -e "${RED}[ERROR]${NC} $1" |
| 21 | + exit 1 |
| 22 | +} |
| 23 | + |
| 24 | +# Function to get the latest git tag |
| 25 | +get_latest_tag() { |
| 26 | + git fetch --tags 2>/dev/null || true |
| 27 | + git tag -l "v*" | sort -V | tail -1 |
| 28 | +} |
| 29 | + |
| 30 | +# Function to bump version |
| 31 | +bump_version() { |
| 32 | + local version=$1 |
| 33 | + local bump_type=$2 |
| 34 | + |
| 35 | + # Remove 'v' prefix if present |
| 36 | + version=${version#v} |
| 37 | + |
| 38 | + # Split version into components |
| 39 | + IFS='.' read -r major minor patch <<< "$version" |
| 40 | + |
| 41 | + case $bump_type in |
| 42 | + major) |
| 43 | + major=$((major + 1)) |
| 44 | + minor=0 |
| 45 | + patch=0 |
| 46 | + ;; |
| 47 | + minor) |
| 48 | + minor=$((minor + 1)) |
| 49 | + patch=0 |
| 50 | + ;; |
| 51 | + patch) |
| 52 | + patch=$((patch + 1)) |
| 53 | + ;; |
| 54 | + *) |
| 55 | + error "Invalid bump type: $bump_type. Use: major, minor, or patch" |
| 56 | + ;; |
| 57 | + esac |
| 58 | + |
| 59 | + echo "v${major}.${minor}.${patch}" |
| 60 | +} |
| 61 | + |
| 62 | +# Function to update SDK to latest version |
| 63 | +update_sdk() { |
| 64 | + info "Updating github.com/tiny-systems/module to latest version..." |
| 65 | + |
| 66 | + # Get the latest version |
| 67 | + go get github.com/tiny-systems/module@latest |
| 68 | + |
| 69 | + # Tidy up |
| 70 | + info "Running go mod tidy..." |
| 71 | + go mod tidy |
| 72 | + |
| 73 | + # Show the updated version |
| 74 | + NEW_VERSION=$(go list -m github.com/tiny-systems/module | awk '{print $2}') |
| 75 | + info "SDK updated to: $NEW_VERSION" |
| 76 | + |
| 77 | + # Check if go.mod was modified |
| 78 | + if [[ -n $(git status -s go.mod go.sum 2>/dev/null) ]]; then |
| 79 | + info "go.mod and/or go.sum have been modified" |
| 80 | + read -p "Commit the changes? (Y/n) " -n 1 -r |
| 81 | + echo |
| 82 | + if [[ ! $REPLY =~ ^[Nn]$ ]]; then |
| 83 | + git add go.mod go.sum |
| 84 | + git commit -m "update SDK to $NEW_VERSION" |
| 85 | + info "Changes committed" |
| 86 | + else |
| 87 | + warn "Don't forget to commit the changes!" |
| 88 | + fi |
| 89 | + else |
| 90 | + info "No changes to commit (SDK already at latest version)" |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +# Main script |
| 95 | +main() { |
| 96 | + # Handle 'update' command |
| 97 | + if [[ "${1:-}" == "update" ]]; then |
| 98 | + update_sdk |
| 99 | + exit 0 |
| 100 | + fi |
| 101 | + # Check if git repo |
| 102 | + if ! git rev-parse --git-dir > /dev/null 2>&1; then |
| 103 | + error "Not a git repository" |
| 104 | + fi |
| 105 | + |
| 106 | + # Check for uncommitted changes |
| 107 | + if [[ -n $(git status -s) ]]; then |
| 108 | + warn "You have uncommitted changes:" |
| 109 | + git status -s |
| 110 | + read -p "Continue anyway? (Y/n) " -n 1 -r |
| 111 | + echo |
| 112 | + if [[ $REPLY =~ ^[Nn]$ ]]; then |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + fi |
| 116 | + |
| 117 | + # Get bump type from argument or prompt |
| 118 | + BUMP_TYPE=${1:-} |
| 119 | + if [[ -z "$BUMP_TYPE" ]]; then |
| 120 | + echo "Select version bump type:" |
| 121 | + echo " 1) patch (0.1.x -> 0.1.x+1)" |
| 122 | + echo " 2) minor (0.x.0 -> 0.x+1.0)" |
| 123 | + echo " 3) major (x.0.0 -> x+1.0.0)" |
| 124 | + read -p "Enter choice [1-3]: " -n 1 -r choice |
| 125 | + echo |
| 126 | + case $choice in |
| 127 | + 1) BUMP_TYPE="patch" ;; |
| 128 | + 2) BUMP_TYPE="minor" ;; |
| 129 | + 3) BUMP_TYPE="major" ;; |
| 130 | + *) error "Invalid choice" ;; |
| 131 | + esac |
| 132 | + fi |
| 133 | + |
| 134 | + # Get current version |
| 135 | + CURRENT_TAG=$(get_latest_tag) |
| 136 | + if [[ -z "$CURRENT_TAG" ]]; then |
| 137 | + warn "No existing tags found, starting from v0.0.0" |
| 138 | + CURRENT_TAG="v0.0.0" |
| 139 | + fi |
| 140 | + |
| 141 | + info "Current version: $CURRENT_TAG" |
| 142 | + |
| 143 | + # Calculate new version |
| 144 | + NEW_TAG=$(bump_version "$CURRENT_TAG" "$BUMP_TYPE") |
| 145 | + info "New version: $NEW_TAG" |
| 146 | + |
| 147 | + # Ask for commit message |
| 148 | + read -p "Commit message (default: 'release $NEW_TAG'): " COMMIT_MSG |
| 149 | + COMMIT_MSG=${COMMIT_MSG:-"release $NEW_TAG"} |
| 150 | + |
| 151 | + # Commit any staged changes |
| 152 | + if [[ -n $(git diff --cached --name-only) ]]; then |
| 153 | + info "Committing changes..." |
| 154 | + git commit -m "$COMMIT_MSG" |
| 155 | + fi |
| 156 | + |
| 157 | + # Create tag locally first |
| 158 | + info "Creating tag $NEW_TAG..." |
| 159 | + git tag -a "$NEW_TAG" -m "$COMMIT_MSG" |
| 160 | + |
| 161 | + # Check if there are unpushed commits |
| 162 | + PUSH_COMMITS=false |
| 163 | + if [[ -n $(git log origin/$(git rev-parse --abbrev-ref HEAD)..HEAD 2>/dev/null) ]]; then |
| 164 | + read -p "Push commits to remote? (Y/n) " -n 1 -r |
| 165 | + echo |
| 166 | + if [[ $REPLY =~ ^[Nn]$ ]]; then |
| 167 | + warn "Commits not pushed. Tag will not be pushed either (would point to non-existent commit)." |
| 168 | + info "✓ Tag $NEW_TAG created locally (not pushed)" |
| 169 | + warn "To push later: git push && git push origin $NEW_TAG" |
| 170 | + return |
| 171 | + else |
| 172 | + info "Pushing commits to remote..." |
| 173 | + git push |
| 174 | + PUSH_COMMITS=true |
| 175 | + fi |
| 176 | + fi |
| 177 | + |
| 178 | + # Now ask about pushing the tag |
| 179 | + read -p "Push tag to remote? (Y/n) " -n 1 -r |
| 180 | + echo |
| 181 | + if [[ $REPLY =~ ^[Nn]$ ]]; then |
| 182 | + info "✓ Tag $NEW_TAG created locally (not pushed)" |
| 183 | + warn "Remember to push with: git push origin $NEW_TAG" |
| 184 | + else |
| 185 | + info "Pushing tag to remote..." |
| 186 | + git push origin "$NEW_TAG" |
| 187 | + |
| 188 | + info "✓ Release $NEW_TAG created and pushed successfully!" |
| 189 | + echo "" |
| 190 | + info "Tag: $NEW_TAG" |
| 191 | + info "You can create a GitHub release at:" |
| 192 | + echo " https://github.com/$(git remote get-url origin | sed 's/.*github.com[:/]\(.*\)\.git/\1/')/releases/new?tag=$NEW_TAG" |
| 193 | + fi |
| 194 | +} |
| 195 | + |
| 196 | +# Run main function |
| 197 | +main "$@" |
0 commit comments