22
33# Set some basic variables
44SCRIPTS_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " > /dev/null 2>&1 && pwd ) "
5- BASE_DIR=" $( dirname $SCRIPTS_DIR ) "
5+ BASE_DIR=" $( dirname " $SCRIPTS_DIR " ) "
66NEW_VERSION=
7- CAN_COMMIT=false
8- CAN_PUSH=false
9- CAN_DELETE=false
10- PRINT_NEW_VERSION=false
11- GENERATE_CHANGELOG=false
127VERSIONED_FILES=( " $BASE_DIR /backend/pyproject.toml" " $BASE_DIR /backend/ibutsu_server/openapi/openapi.yaml" " $BASE_DIR /frontend/package.json" )
138
149function print_usage() {
15- echo " Usage: make-release.sh [-h|--help] [-c|--commit] [-p|--push] [-d|--delete] [-g|--changelog] [VERSION]"
10+ echo " Usage: make-release.sh [-h|--help] [VERSION]"
11+ echo " "
12+ echo " This script updates version numbers in frontend and backend files."
1613 echo " "
1714 echo " optional arguments:"
1815 echo " -h, --help show this help message"
19- echo " -v, --version show the prospective new version number"
20- echo " -c, --commit create a new branch and commit all the changes"
21- echo " -p, --push push the branch up to origin after commit"
22- echo " -d, --delete delete the branch after pushing"
23- echo " -g, --changelog generate a changelog entry (needs 'git changelog')"
24- echo " VERSION the new version, autogenerated if ommitted"
25- echo " "
26- echo " to generate a changelog, add the following line to the [alias] section"
27- echo " in your .gitconfig file:"
28- echo " changelog = \" !_() { t=\$ (git describe --abbrev=0 --tags); git log \$ {t}..HEAD --no-merges --pretty=format:'* %s'; }; _\" "
16+ echo " VERSION the new version (if omitted, you will be prompted)"
2917 echo " "
3018}
3119
20+ # Function to extract current version from pyproject.toml
21+ function get_current_version() {
22+ grep -m1 ' ^version = ' " $BASE_DIR /backend/pyproject.toml" | sed ' s/version = "\(.*\)"/\1/'
23+ }
24+
25+ # Function to increment semantic version
26+ function increment_version() {
27+ local version=$1
28+ local major minor patch
29+
30+ # Split version into major.minor.patch
31+ IFS=' .' read -ra VERSION_PARTS <<< " $version"
32+ major=" ${VERSION_PARTS[0]} "
33+ minor=" ${VERSION_PARTS[1]} "
34+ patch=" ${VERSION_PARTS[2]} "
35+
36+ # Increment patch version
37+ patch=$(( patch + 1 ))
38+
39+ echo " ${major} .${minor} .${patch} "
40+ }
41+
3242# Parse the arguments
3343while (( "$# " )) ; do
3444 case " $1 " in
3545 -h|--help)
3646 print_usage
3747 exit 0
3848 ;;
39- -c|--commit)
40- CAN_COMMIT=true
41- shift
42- ;;
43- -p|--push)
44- CAN_PUSH=true
45- shift
46- ;;
47- -d|--delete)
48- CAN_DELETE=true
49- shift
50- ;;
51- -v|--version)
52- PRINT_NEW_VERSION=true
53- shift
54- ;;
55- -g|--changelog)
56- GENERATE_CHANGELOG=true
57- shift
58- ;;
59- -* |--* )
49+ -* )
6050 echo " Error: unsupported option $1 " >&2
6151 exit 1
6252 ;;
@@ -67,54 +57,51 @@ while (( "$#" )); do
6757 esac
6858done
6959
70- # Fetch the latest tags from upstream and set the current version
71- git fetch -q upstream
72- CURRENT_VERSION=` git tag --list | tail -n1 | cut -dv -f2`
60+ # Get current version
61+ CURRENT_VERSION=$( get_current_version)
7362
74- if [[ " $NEW_VERSION " == " " ]]; then
75- NEW_VERSION=" ${CURRENT_VERSION% .* } .$(( ${CURRENT_VERSION##* .} + 1 )) "
63+ if [[ -z " $CURRENT_VERSION " ]]; then
64+ echo " Error: Could not determine current version from pyproject.toml" >&2
65+ exit 1
7666fi
7767
78- if [[ $PRINT_NEW_VERSION = true ]]; then
79- echo " Prospective version number: $NEW_VERSION "
80- exit 0
68+ echo " Current version: $CURRENT_VERSION "
69+
70+ # If no version provided, prompt the user
71+ if [[ -z " $NEW_VERSION " ]]; then
72+ DEFAULT_VERSION=$( increment_version " $CURRENT_VERSION " )
73+ echo " "
74+ read -p " Enter new version [$DEFAULT_VERSION ]: " NEW_VERSION
75+ NEW_VERSION=" ${NEW_VERSION:- $DEFAULT_VERSION } "
8176fi
8277
83- echo " Updating files to $NEW_VERSION "
84- SED_VERSION=$( echo " $CURRENT_VERSION " | sed ' s/-beta//' | sed -r ' s/[\.]+/\\\./g' )
85- for FNAME in ${VERSIONED_FILES[@]} ; do
86- echo " - ${FNAME/ $BASE_DIR \/ / } "
87- sed -i " s/$SED_VERSION /$NEW_VERSION /g" $FNAME
88- done
78+ echo " "
79+ echo " Updating files from $CURRENT_VERSION to $NEW_VERSION "
80+ echo " "
8981
90- if [[ $GENERATE_CHANGELOG = true ]]; then
91- echo " Generating changelog entry"
92- VERSION_TITLE=" Version $NEW_VERSION "
93- TITLE_LEN=$(( ${# VERSION_TITLE} + 1 ))
94- UNDERLINE=` seq -s " =" $TITLE_LEN | sed ' s/[0-9]//g' `
95- CHANGELOG=` git changelog`
96- echo -e " $VERSION_TITLE \n$UNDERLINE \n\n$CHANGELOG \n\n$( cat $BASE_DIR /CHANGELOG.md) " > $BASE_DIR /CHANGELOG.md
97- fi
82+ # Escape all relevant regex special characters in current version for sed
83+ SED_VERSION=$( echo " $CURRENT_VERSION " | sed -e ' s/[.[\*^$()+?{|\\]/\\&/g' )
9884
99- # Commit everything
100- if [[ " $CAN_COMMIT " = true ]]; then
101- echo -n " Committing code..."
102- BRANCH_NAME=" release-$NEW_VERSION "
103- git checkout -b $BRANCH_NAME > /dev/null 2>&1
104- git add . > /dev/null 2>&1
105- COMMIT_MSG=" Release $NEW_VERSION "
106- if [[ $GENERATE_CHANGELOG = true ]]; then
107- COMMIT_MSG=" $COMMIT_MSG " $' \n\n ' " $CHANGELOG "
108- fi
109- git commit -q -m " $COMMIT_MSG "
110- echo " done, new branch created: $BRANCH_NAME "
111- if [[ " $CAN_PUSH " = true ]]; then
112- echo -n " Pushing up to origin/$BRANCH_NAME ..."
113- git push -q origin $BRANCH_NAME
114- git checkout main
115- if [[ " $CAN_DELETE " = true ]]; then
116- git branch -D $BRANCH_NAME
117- echo " Deleted branch $BRANCH_NAME "
85+ # Update each file
86+ for FNAME in " ${VERSIONED_FILES[@]} " ; do
87+ if [[ -f " $FNAME " ]]; then
88+ echo " Updating: ${FNAME/ $BASE_DIR \/ / } "
89+ sed -i " s/$SED_VERSION /$NEW_VERSION /g" " $FNAME "
90+ if [[ $? -ne 0 ]]; then
91+ echo " Error: Failed to update ${FNAME/ $BASE_DIR \/ / } " >&2
92+ exit 1
11893 fi
94+ else
95+ echo " Warning: File not found: ${FNAME/ $BASE_DIR \/ / } "
11996 fi
120- fi
97+ done
98+
99+ echo " "
100+ echo " Version update complete!"
101+ echo " "
102+ echo " Files updated:"
103+ for FNAME in " ${VERSIONED_FILES[@]} " ; do
104+ if [[ -f " $FNAME " ]]; then
105+ echo " - ${FNAME/ $BASE_DIR \/ / } "
106+ fi
107+ done
0 commit comments