1 | #!/bin/bash
|
2 |
|
3 | AVAILABLE_INCREMENTS=(major minor patch)
|
4 | PACKAGE_JSON_FILE="./package.json"
|
5 | CHANGELOG_MD_FILE="./CHANGELOG.md"
|
6 | GH_ORG="craft-ai"
|
7 | GH_REPO="craft-ai-client-js"
|
8 |
|
9 |
|
10 | sed --help >/dev/null 2>&1
|
11 | if [ $? -eq 0 ]; then
|
12 |
|
13 | NL="\\n"
|
14 | else
|
15 | NL="\\
|
16 | "
|
17 | fi
|
18 |
|
19 | set -e
|
20 |
|
21 |
|
22 | usage ()
|
23 | {
|
24 | echo "usage: ./update_version.sh [--skip-git] $(join_by "|" "${AVAILABLE_INCREMENTS[@]}")"
|
25 | }
|
26 |
|
27 | array_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 |
|
41 | function join_by { local IFS="$1"; shift; echo "$*"; }
|
42 |
|
43 | do_git=1
|
44 | increment=""
|
45 |
|
46 | while [ "$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
|
64 | done
|
65 |
|
66 | if [ -z $increment ]; then
|
67 | usage
|
68 | exit 1
|
69 | fi
|
70 |
|
71 |
|
72 | cd ${BASH_SOURCE%/*}/..
|
73 |
|
74 | current_version=$(sed -n "s/^ \"version\": \"\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)\",$/\1/p" $PACKAGE_JSON_FILE)
|
75 | IFS='.' read -r -a current_version_array <<< "$current_version"
|
76 | unset IFS
|
77 |
|
78 | current_major=${current_version_array[0]}
|
79 | current_minor=${current_version_array[1]}
|
80 | current_patch=${current_version_array[2]}
|
81 |
|
82 | case $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 | ;;
|
98 | esac
|
99 |
|
100 | echo "Increment version from v$current_major.$current_minor.$current_patch to v$next_major.$next_minor.$next_patch"
|
101 |
|
102 | TODAY=`date +%Y-%m-%d`
|
103 |
|
104 | eval "sed -i.bak 's/\"version\": \"$current_major.$current_minor.$current_patch/\"version\": \"$next_major.$next_minor.$next_patch/g' $PACKAGE_JSON_FILE"
|
105 | eval "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 |
|
107 | if [ $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"
|
111 | fi
|