chore: Documents versioning script usage

Adds a new section to the README to provide instructions on how to use the `version_and_changelog.sh` script. This helps users understand the process for incrementing the project version and generating the changelog.
This commit is contained in:
mandreshope 2025-07-30 17:03:53 +03:00
parent 2c003ba335
commit 7b98eb6227
5 changed files with 402 additions and 1 deletions

View File

@ -15,7 +15,6 @@ For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials, [online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference. samples, guidance on mobile development, and a full API reference.
## flutter_launcher_icons ## flutter_launcher_icons
Icon launcher generator Icon launcher generator
@ -40,3 +39,11 @@ dart run build_runner build --delete-conflicting-outputs
```batch ```batch
dart pub global activate dependency_validator && dart pub run dependency_validator dart pub global activate dependency_validator && dart pub run dependency_validator
``` ```
## Increment version and generate changelog
Run this script in project root to create apk
```batch
Usage: ./scripts/version_and_changelog.sh
```

102
scripts/build_app.sh Executable file
View File

@ -0,0 +1,102 @@
#!/bin/bash
# Get the project name from pubspec.yaml
PROJECT_NAME=$(grep 'name:' pubspec.yaml | awk -F ': ' '{print $2}')
# Set the path to your Flutter project
PROJECT_PATH="."
# Navigate to the project directory
cd "$PROJECT_PATH"
# Clean the project
flutter clean
# Get the latest dependencies
flutter pub get
# Get the current version from pubspec.yaml
VERSION=$(grep 'version:' pubspec.yaml | awk -F ': ' '{print $2}')
VERSION_NUMBER=$(echo $VERSION | awk -F '+' '{print $1}')
BUILD_NUMBER=$(echo $VERSION | awk -F '+' '{print $2}')
# Update the version in pubspec.yaml
NEW_VERSION="$VERSION_NUMBER+$BUILD_NUMBER"
sed -i '' "s/version: $VERSION/version: $NEW_VERSION/" pubspec.yaml
# Build and rename function for APK
build_and_rename_apk() {
local flavor="$1"
flutter build apk --no-tree-shake-icons
# Determine the output directory based on the flavor
OUTPUT_DIR="$PROJECT_PATH/build/app/outputs/flutter-apk"
# Rename the output APK file with the version
APK_FILE="$OUTPUT_DIR/app-release.apk"
NEW_APK_NAME="$OUTPUT_DIR/${flavor}_v$NEW_VERSION.apk"
mv "$APK_FILE" "$NEW_APK_NAME"
echo "Build completed and APK for $flavor renamed to $NEW_APK_NAME"
}
# Build and rename function for IPA
build_and_rename_ipa() {
local flavor="$1"
flutter build ipa
# Determine the output directory for the IPA file
OUTPUT_DIR="$PROJECT_PATH/build/ios/ipa"
# Rename the output IPA file with the version
IPA_FILE="$OUTPUT_DIR/app.ipa"
NEW_IPA_NAME="$OUTPUT_DIR/${flavor}_v$NEW_VERSION.ipa"
mv "$IPA_FILE" "$NEW_IPA_NAME"
echo "Build completed and IPA for $flavor renamed to $NEW_IPA_NAME"
}
# Function to display usage instructions
show_usage() {
echo "Usage: $0 [android|ios|both]"
echo " android - Build only the Android APK"
echo " ios - Build only the iOS IPA (macOS only)"
echo " both - Build both Android APK and iOS IPA (macOS only)"
}
# Check for user input
if [ -z "$1" ]; then
show_usage
exit 1
fi
# Process the user's choice
case "$1" in
android)
echo "Building APK for Android..."
build_and_rename_apk $PROJECT_NAME
;;
ios)
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "Building IPA for iOS..."
build_and_rename_ipa $PROJECT_NAME
else
echo "Skipping IPA build; not running on macOS."
fi
;;
both)
echo "Building APK for Android..."
build_and_rename_apk $PROJECT_NAME
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "Building IPA for iOS..."
build_and_rename_ipa $PROJECT_NAME
else
echo "Skipping IPA build; not running on macOS."
fi
;;
*)
echo "Invalid option: $1"
show_usage
exit 1
;;
esac

View File

@ -0,0 +1,68 @@
#!/bin/bash
# Set the output file for the changelog
OUTPUT_FILE="CHANGELOG.md"
TEMP_FILE="CHANGELOG_TEMP.md"
VERSION=""
# Check if Git and pubspec.yaml are available
if ! command -v git &> /dev/null; then
echo "Git is not installed. Please install Git and try again."
exit 1
fi
if [ ! -f "pubspec.yaml" ]; then
echo "pubspec.yaml not found. Please ensure this script is run in the root of a Flutter project."
exit 1
fi
# Get the current version from pubspec.yaml
VERSION=$(grep 'version:' pubspec.yaml | awk -F ': ' '{print $2}' | awk -F '+' '{print $1}')
if [ -z "$VERSION" ]; then
echo "Failed to extract version from pubspec.yaml."
exit 1
fi
# Check if OUTPUT_FILE exists, if not create it
if [ ! -f "$OUTPUT_FILE" ]; then
echo "# Changelog" > "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "All notable changes to this project are documented below." >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
fi
# Write the new version at the top
echo "## $VERSION" > "$TEMP_FILE"
# Generate sections based on commit types
echo "Generating changelog for version $VERSION..."
# Features
echo "### Features" >> "$TEMP_FILE"
git log --no-merges --pretty=format:"- %s" | grep "feat:" >> "$TEMP_FILE" || echo "No features found." >> "$TEMP_FILE"
echo "" >> "$TEMP_FILE"
# Bug Fixes
echo "### Bug Fixes" >> "$TEMP_FILE"
git log --no-merges --pretty=format:"- %s" | grep "fix:" >> "$TEMP_FILE" || echo "No bug fixes found." >> "$TEMP_FILE"
echo "" >> "$TEMP_FILE"
# Chores
echo "### Chores" >> "$TEMP_FILE"
git log --no-merges --pretty=format:"- %s" | grep "chore:" >> "$TEMP_FILE" || echo "No chores found." >> "$TEMP_FILE"
echo "" >> "$TEMP_FILE"
# Miscellaneous
echo "### Miscellaneous" >> "$TEMP_FILE"
git log --no-merges --pretty=format:"- %s" | grep -v -E "feat:|fix:|chore:" >> "$TEMP_FILE" || echo "No miscellaneous changes found." >> "$TEMP_FILE"
echo "" >> "$TEMP_FILE"
# Append the existing changelog to the temporary file
cat "$OUTPUT_FILE" >> "$TEMP_FILE"
# Replace the old changelog with the new one
mv "$TEMP_FILE" "$OUTPUT_FILE"
# Notify user
echo "Changelog updated successfully: $OUTPUT_FILE"

View File

@ -0,0 +1,83 @@
#!/bin/bash
# File path
PUBSPEC_FILE="pubspec.yaml"
# Function to increment version
increment_version() {
local version=$1
local index=$2
IFS='.' read -r -a parts <<< "$version"
# Incremente la version en fonction de l'index et remet les autres à 0
parts[$index]=$((parts[$index] + 1))
# Remet les parties suivantes à 0
for ((i = index + 1; i < ${#parts[@]}; i++)); do
parts[$i]=0
done
echo "${parts[*]}" | tr ' ' '.'
}
# Check if pubspec.yaml exists
if [ ! -f "$PUBSPEC_FILE" ]; then
echo "Error: $PUBSPEC_FILE not found in the current directory."
exit 1
fi
# Extract the current version and build number
CURRENT_VERSION=$(grep 'version:' $PUBSPEC_FILE | awk -F ': ' '{print $2}')
VERSION=$(echo "$CURRENT_VERSION" | cut -d'+' -f1)
BUILD=$(echo "$CURRENT_VERSION" | cut -d'+' -f2)
if [ -z "$VERSION" ] || [ -z "$BUILD" ]; then
echo "Error: Could not extract version and build number from pubspec.yaml."
exit 1
fi
# Get commit types from the latest changes
LATEST_COMMITS=$(git log --oneline -10)
echo "Analyzing latest commits..."
echo "$LATEST_COMMITS"
if echo "$LATEST_COMMITS" | grep -iq "breaking"; then
INCREMENT_TYPE="major"
elif echo "$LATEST_COMMITS" | grep -iq "feat"; then
INCREMENT_TYPE="minor"
elif echo "$LATEST_COMMITS" | grep -iq "fix"; then
INCREMENT_TYPE="patch"
else
INCREMENT_TYPE="build"
fi
echo "Detected increment type: $INCREMENT_TYPE"
# Determine new version and build number
case $INCREMENT_TYPE in
major)
NEW_VERSION=$(increment_version "$VERSION" 0)
NEW_BUILD=1
;;
minor)
NEW_VERSION=$(increment_version "$VERSION" 1)
NEW_BUILD=1
;;
patch)
NEW_VERSION=$(increment_version "$VERSION" 2)
NEW_BUILD=1
;;
build)
NEW_VERSION=$VERSION
NEW_BUILD=$((BUILD + 1))
;;
*)
echo "Error: Unknown increment type."
exit 1
;;
esac
# Update the version in pubspec.yaml
sed -i.bak "s/^version: .*/version: $NEW_VERSION+$NEW_BUILD/" $PUBSPEC_FILE
echo "Version updated to $NEW_VERSION+$NEW_BUILD in $PUBSPEC_FILE"

141
scripts/version_and_changelog.sh Executable file
View File

@ -0,0 +1,141 @@
#!/bin/bash
# File paths
PUBSPEC_FILE="pubspec.yaml"
CHANGELOG_FILE="CHANGELOG.md"
# Check if pubspec.yaml exists
if [ ! -f "$PUBSPEC_FILE" ]; then
echo "Error: $PUBSPEC_FILE not found in the current directory."
exit 1
fi
# Extract the current version and build number
CURRENT_VERSION=$(grep 'version:' $PUBSPEC_FILE | awk -F ': ' '{print $2}')
VERSION=$(echo "$CURRENT_VERSION" | cut -d'+' -f1)
BUILD=$(echo "$CURRENT_VERSION" | cut -d'+' -f2)
if [ -z "$VERSION" ] || [ -z "$BUILD" ]; then
echo "Error: Could not extract version and build number from pubspec.yaml."
exit 1
fi
echo "Current version: $VERSION+$BUILD"
# Function to increment version
increment_version() {
local version=$1
local index=$2
IFS='.' read -r -a parts <<< "$version"
parts[$index]=$((parts[$index] + 1))
for ((i = index + 1; i < ${#parts[@]}; i++)); do
parts[$i]=0
done
echo "${parts[*]}" | tr ' ' '.'
}
# Get the hash of the last commit containing "chore: increment version"
LAST_BOT_COMMIT_HASH=$(git log --oneline --grep="chore: increment version" -n 1 | cut -d ' ' -f1 || echo "NONE")
# Get commits after the bot commit to analyze changes
if [ "$LAST_BOT_COMMIT_HASH" == "NONE" ]; then
LATEST_COMMITS=$(git log --oneline --no-merges)
else
LATEST_COMMITS=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD)
fi
echo "Analyzing latest commits..."
echo "$LATEST_COMMITS"
# Determine increment type based on commit messages
if echo "$LATEST_COMMITS" | grep -iq "breaking:"; then
INCREMENT_TYPE="major"
elif echo "$LATEST_COMMITS" | grep -iq "feat:"; then
INCREMENT_TYPE="minor"
elif echo "$LATEST_COMMITS" | grep -iq "fix:"; then
INCREMENT_TYPE="patch"
elif echo "$LATEST_COMMITS" | grep -iq "chore:"; then
INCREMENT_TYPE="patch"
elif echo "$LATEST_COMMITS" | grep -iq "build:"; then
INCREMENT_TYPE="build"
else
INCREMENT_TYPE="patch"
fi
echo "Detected increment type: $INCREMENT_TYPE"
# Determine new version and build number based on the increment type
NEW_BUILD=$(($BUILD + 1))
case $INCREMENT_TYPE in
major)
NEW_VERSION=$(increment_version "$VERSION" 0)
;;
minor)
NEW_VERSION=$(increment_version "$VERSION" 1)
;;
patch)
NEW_VERSION=$(increment_version "$VERSION" 2)
;;
*)
echo "Error: Unknown increment type."
exit 1
;;
esac
# Update the version in pubspec.yaml
sed -i.bak "s/^version: .*/version: $NEW_VERSION+$NEW_BUILD/" $PUBSPEC_FILE
echo "Version updated to $NEW_VERSION+$NEW_BUILD in $PUBSPEC_FILE"
# Ensure the changelog file exists
if [ ! -f "$CHANGELOG_FILE" ]; then
echo "# Changelog" > "$CHANGELOG_FILE"
echo "" >> "$CHANGELOG_FILE"
fi
# Start building the changelog for the current version
TEMP_CHANGELOG="CHANGELOG_TEMP.md"
DATE=$(date +"%Y-%m-%d")
{
echo "## $NEW_VERSION+$NEW_BUILD ($DATE)"
echo ""
echo "### Breaking Changes"
BREAKING=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep "breaking:" | sed -E 's/^([a-f0-9]+) breaking: /- (#\1) /g')
echo "${BREAKING:-No breaking changes found.}"
echo ""
echo "### Features"
FEATURES=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep "feat:" | sed -E 's/^([a-f0-9]+) feat: /- (#\1) /g')
echo "${FEATURES:-No features found.}"
echo ""
echo "### Bug Fixes"
BUG_FIXES=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep "fix:" | sed -E 's/^([a-f0-9]+) fix: /- (#\1) /g')
echo "${BUG_FIXES:-No bug fixes found.}"
echo ""
echo "### Chores"
CHORES=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep "chore:" | sed -E 's/^([a-f0-9]+) chore: /- (#\1) /g')
echo "${CHORES:-No chores found.}"
echo ""
echo "### Refactors"
CHORES=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep "refactor:" | sed -E 's/^([a-f0-9]+) refactor: /- (#\1) /g')
echo "${CHORES:-No refactors found.}"
echo ""
echo "### Style Changes"
CHORES=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep "style:" | sed -E 's/^([a-f0-9]+) style: /- (#\1) /g')
echo "${CHORES:-No style Changes found.}"
echo ""
echo "### Miscellaneous"
MISC=$(git log --oneline --no-merges "$LAST_BOT_COMMIT_HASH"..HEAD | grep -v -E "breaking:|feat:|fix:|chore:" | sed -E 's/^([a-f0-9]+) /- (#\1) /g')
echo "${MISC:-No miscellaneous changes found.}"
echo ""
cat "$CHANGELOG_FILE"
} > "$TEMP_CHANGELOG"
mv "$TEMP_CHANGELOG" "$CHANGELOG_FILE"
echo "Changelog updated successfully."
# # Commit version and changelog update
git config --global user.email "your-email@example.com"
git config --global user.name "your-name"
git add pubspec.yaml CHANGELOG.md
git commit -m "chore: increment version $NEW_VERSION+$NEW_BUILD and update changelog"
# git push