Skip to content

Instantly share code, notes, and snippets.

@envomer
Created May 13, 2023 09:27
Show Gist options
  • Save envomer/e83e9825fed4935882942e6f5cd6896f to your computer and use it in GitHub Desktop.
Save envomer/e83e9825fed4935882942e6f5cd6896f to your computer and use it in GitHub Desktop.
Upgrade the current version according to semver of pubspec.yaml file (build, patch, minor, major)
#!/bin/bash
# Function to extract version number from pubspec.yaml file
get_version() {
local version_line=$(grep -m1 "^version:" pubspec.yaml)
local version=$(echo $version_line | cut -d' ' -f2)
echo $version
}
# Function to update the version based on semver
update_version() {
local current_version=$1
local version_type=$2
# Splitting version into major, minor, patch, and build
local major=$(echo $current_version | cut -d'.' -f1)
local minor=$(echo $current_version | cut -d'.' -f2)
local patch=$(echo $current_version | cut -d'.' -f3 | cut -d'+' -f1)
local build=$(echo $current_version | cut -d'+' -f2)
# Incrementing version based on the specified type
case $version_type in
"build")
build=$((build + 1))
;;
"patch")
patch=$((patch + 1))
;;
"minor")
minor=$((minor + 1))
patch=0
;;
"major")
major=$((major + 1))
minor=0
patch=0
;;
*)
echo "Invalid version type. Usage: ./upgrade.sh [build|patch|minor|major]"
exit 1
esac
local new_version="$major.$minor.$patch+$build"
echo $new_version
}
# Read the version from pubspec.yaml
current_version=$(get_version)
echo "Current version: $current_version"
# Check the argument and update the version accordingly
case $1 in
"build" | "patch" | "minor" | "major")
new_version=$(update_version $current_version $1)
echo "Updating version to: $new_version"
# Replace the version line in pubspec.yaml
awk -v new_version="$new_version" '/^version:/ { print "version: " new_version; next }1' pubspec.yaml > pubspec.yaml.tmp
mv pubspec.yaml.tmp pubspec.yaml
echo "Version updated successfully!"
;;
*)
echo "Invalid argument. Usage: ./upgrade.sh [build|patch|minor|major]"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment