UNPKG

3.12 kBapplication/x-shView Raw
1#!/bin/bash
2
3AVAILABLE_INCREMENTS=(major minor patch)
4PACKAGE_JSON_FILE="./package.json"
5CHANGELOG_MD_FILE="./CHANGELOG.md"
6GH_ORG="craft-ai"
7GH_REPO="craft-ai-client-js"
8
9# Let's check if we have GNU sed or BSD sed
10sed --help >/dev/null 2>&1
11if [ $? -eq 0 ]; then
12 # There is a '--help' option, it is GNU BSD
13 NL="\\n"
14else
15 NL="\\
16"
17fi
18
19set -e
20# Any subsequent(*) commands which fail will cause the shell script to exit immediately
21
22usage ()
23{
24 echo "usage: ./update_version.sh [--skip-git] $(join_by "|" "${AVAILABLE_INCREMENTS[@]}")"
25}
26
27array_contains ()
28{
29 local array="$1[@]"
30 local seeking=$2
31 local in=1
32 for element in "${!array}"; do
33 if [[ $element == $seeking ]]; then
34 in=0
35 break
36 fi
37 done
38 echo $in
39}
40
41function join_by { local IFS="$1"; shift; echo "$*"; }
42
43do_git=1
44increment=""
45
46while [ "$1" != "" ]; do
47 case $1 in
48 --skip-git ) do_git=0
49 ;;
50 * ) if [ $(array_contains AVAILABLE_INCREMENTS $1) == 1 ]; then
51 echo "Invalid increment: $1"
52 usage
53 exit 1
54 elif [ -n "$increment" ]; then
55 echo "Only one increment can be provided"
56 usage
57 exit 1
58 else
59 increment=$1
60 fi
61 ;;
62 esac
63 shift
64done
65
66if [ -z $increment ]; then
67 usage
68 exit 1
69fi
70
71# Make sure we are at the root directory of the repository
72cd ${BASH_SOURCE%/*}/..
73
74current_version=$(sed -n "s/^ \"version\": \"\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\",$/\1/p" $PACKAGE_JSON_FILE)
75IFS='.' read -r -a current_version_array <<< "$current_version"
76unset IFS
77
78current_major=${current_version_array[0]}
79current_minor=${current_version_array[1]}
80current_patch=${current_version_array[2]}
81
82case $increment in
83 major)
84 next_major=$((current_major+1))
85 next_minor=0
86 next_patch=0
87 ;;
88 minor)
89 next_major=$current_major
90 next_minor=$((current_minor+1))
91 next_patch=0
92 ;;
93 patch)
94 next_major=$current_major
95 next_minor=$current_minor
96 next_patch=$((current_patch+1))
97 ;;
98esac
99
100echo "Increment version from v$current_major.$current_minor.$current_patch to v$next_major.$next_minor.$next_patch"
101
102TODAY=`date +%Y-%m-%d`
103
104eval "sed -i.bak 's/\"version\": \"$current_major.$current_minor.$current_patch/\"version\": \"$next_major.$next_minor.$next_patch/g' $PACKAGE_JSON_FILE"
105eval "sed -i.bak 's/.*\[Unreleased\].*/## [Unreleased](https:\/\/github.com\/$GH_ORG\/$GH_REPO\/compare\/v$next_major.$next_minor.$next_patch...HEAD) ##$NL$NL## [$next_major.$next_minor.$next_patch](https:\/\/github.com\/$GH_ORG\/$GH_REPO\/compare\/v$current_major.$current_minor.$current_patch...v$next_major.$next_minor.$next_patch) - $TODAY ##/g' $CHANGELOG_MD_FILE"
106
107if [ $do_git == 1 ]; then
108 eval "git add $PACKAGE_JSON_FILE $CHANGELOG_MD_FILE"
109 eval "git commit --quiet -m 'Bumping from v$current_major.$current_minor.$current_patch to v$next_major.$next_minor.$next_patch'"
110 eval "git tag -a v$next_major.$next_minor.$next_patch -m v$next_major.$next_minor.$next_patch"
111fi