Skip to content

Commit 4f00e1d

Browse files
committed
scripts: Refactor scripts to improve clarity and error handling
1 parent 9e03210 commit 4f00e1d

9 files changed

Lines changed: 462 additions & 304 deletions

File tree

scripts/build.sh

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44

55
## --- Base --- ##
6-
# Getting path of this script file:
7-
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6+
_SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-"$0"}")" >/dev/null 2>&1 && pwd -P)"
87
_PROJECT_DIR="$(cd "${_SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd)"
98
cd "${_PROJECT_DIR}" || exit 2
109

1110

12-
if [ -z "$(which python)" ]; then
13-
echo "[ERROR]: 'python' not found or not installed!"
11+
if ! command -v python >/dev/null 2>&1; then
12+
echo "[ERROR]: Not found 'python' command, please install it first!" >&2
1413
exit 1
1514
fi
1615

@@ -30,53 +29,80 @@ _IS_STAGING=true
3029
## --- Variables --- ##
3130

3231

33-
## --- Main --- ##
34-
main()
35-
{
36-
## --- Menu arguments --- ##
37-
if [ -n "${1:-}" ]; then
38-
local _input
39-
for _input in "${@:-}"; do
40-
case ${_input} in
41-
-c | --disable-clean)
42-
_IS_CLEAN=false
43-
shift;;
44-
-t | --test)
45-
_IS_TEST=true
46-
shift;;
47-
-u | --upload)
48-
_IS_UPLOAD=true
49-
shift;;
50-
-p | --production)
51-
_IS_STAGING=false
52-
shift;;
53-
*)
54-
echo "[ERROR]: Failed to parsing input -> ${_input}!"
55-
echo "[INFO]: USAGE: ${0} -c, --disable-clean | -t, --test | -u, --upload | -p, --production"
56-
exit 1;;
57-
esac
58-
done
59-
fi
60-
## --- Menu arguments --- ##
32+
## --- Menu arguments --- ##
33+
_usage_help() {
34+
cat <<EOF
35+
USAGE: ${0} [options]
6136
37+
OPTIONS:
38+
-c, --disable-clean Disable clean process. Default: true
39+
-t, --test Enable test mode. Default: false
40+
-u, --upload Enable upload mode. Default: false
41+
-p, --production Disable staging mode. Default: true
42+
-h, --help Show this help message.
6243
63-
if [ "${_IS_UPLOAD}" == true ]; then
64-
if [ -z "$(which twine)" ]; then
65-
echo "[ERROR]: 'twine' not found or not installed!"
66-
exit 1
67-
fi
44+
EXAMPLES:
45+
${0} --test
46+
${0} -c -u -p
47+
EOF
48+
}
49+
50+
while [ $# -gt 0 ]; do
51+
case "${1}" in
52+
-c | --disable-clean)
53+
_IS_CLEAN=false
54+
shift;;
55+
-t | --test)
56+
_IS_TEST=true
57+
shift;;
58+
-u | --upload)
59+
_IS_UPLOAD=true
60+
shift;;
61+
-p | --production)
62+
_IS_STAGING=false
63+
shift;;
64+
-h | --help)
65+
_usage_help
66+
exit 0;;
67+
*)
68+
echo "[ERROR]: Failed to parse argument -> ${1}!" >&2
69+
_usage_help
70+
exit 1;;
71+
esac
72+
done
73+
## --- Menu arguments --- ##
74+
75+
76+
if [ "${_IS_UPLOAD}" == true ]; then
77+
if ! command -v twine >/dev/null 2>&1; then
78+
echo "[ERROR]: Not found 'twine' command, please install it first!" >&2
79+
exit 1
6880
fi
81+
fi
6982

7083

71-
if [ "${_IS_CLEAN}" == true ]; then
72-
./scripts/clean.sh || exit 2
84+
if [ "${_IS_CLEAN}" == true ]; then
85+
if [ ! -f ./scripts/clean.sh ]; then
86+
echo "[ERROR]: 'clean.sh' script not found!" >&2
87+
exit 1
7388
fi
7489

75-
if [ "${_IS_TEST}" == true ]; then
76-
./scripts/test.sh || exit 2
90+
./scripts/clean.sh || exit 2
91+
fi
92+
93+
if [ "${_IS_TEST}" == true ]; then
94+
if [ ! -f ./scripts/test.sh ]; then
95+
echo "[ERROR]: 'test.sh' script not found!" >&2
96+
exit 1
7797
fi
7898

99+
./scripts/test.sh || exit 2
100+
fi
101+
79102

103+
## --- Main --- ##
104+
main()
105+
{
80106
echo "[INFO]: Building package..."
81107
# python setup.py sdist bdist_wheel || exit 2
82108
python -m build || exit 2
@@ -98,5 +124,5 @@ main()
98124
fi
99125
}
100126

101-
main "${@:-}"
127+
main
102128
## --- Main --- ##

scripts/bump-version.sh

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -euo pipefail
33

44

55
## --- Base --- ##
6-
# Getting path of this script file:
7-
_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6+
_SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-"$0"}")" >/dev/null 2>&1 && pwd -P)"
87
_PROJECT_DIR="$(cd "${_SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd)"
98
cd "${_PROJECT_DIR}" || exit 2
109

1110

12-
# Loading .env file (if exists):
13-
if [ -f ".env" ]; then
14-
# shellcheck disable=SC1091
15-
source .env
11+
# shellcheck disable=SC1091
12+
[ -f .env ] && . .env
13+
14+
15+
if [ ! -f ./scripts/get-version.sh ]; then
16+
echo "[ERROR]: 'get-version.sh' script not found!" >&2
17+
exit 1
1618
fi
1719
## --- Base --- ##
1820

@@ -31,54 +33,75 @@ _IS_PUSH=false
3133
## --- Variables --- ##
3234

3335

34-
## --- Main --- ##
35-
main()
36-
{
37-
## --- Menu arguments --- ##
38-
if [ -n "${1:-}" ]; then
39-
local _input
40-
for _input in "${@:-}"; do
41-
case ${_input} in
42-
-b=* | --bump-type=*)
43-
_BUMP_TYPE="${_input#*=}"
44-
shift;;
45-
-c | --commit)
46-
_IS_COMMIT=true
47-
shift;;
48-
-t | --tag)
49-
_IS_TAG=true
50-
shift;;
51-
-p | --push)
52-
_IS_PUSH=true
53-
shift;;
54-
*)
55-
echo "[ERROR]: Failed to parsing input -> ${_input}!"
56-
echo "[INFO]: USAGE: ${0} -b=*, --bump-type=* [major | minor | patch] | -c, --commit | -t, --tag | -p, --push"
57-
exit 1;;
58-
esac
59-
done
60-
fi
61-
## --- Menu arguments --- ##
36+
## --- Menu arguments --- ##
37+
_usage_help() {
38+
cat <<EOF
39+
USAGE: ${0} [options]
6240
41+
OPTIONS:
42+
-b, --bump-type [TYPE] Specify version bump type. [major | minor | patch]
43+
-c, --commit Create a commit for the bumped version. Default: false
44+
-t, --tag Create a git tag for the bumped version. Default: false
45+
-p, --push Push commits and tags to the remote. Default: false
46+
-h, --help Show this help message.
6347
64-
if [ -z "${_BUMP_TYPE:-}" ]; then
65-
echo "[ERROR]: Bump type is empty, use '-b=' or '--bump-type=' argument!"
66-
exit 1
67-
fi
48+
EXAMPLES:
49+
${0} -b patch -c -t -p
50+
${0} --bump-type=minor
51+
EOF
52+
}
6853

69-
if [ "${_BUMP_TYPE}" != "major" ] && [ "${_BUMP_TYPE}" != "minor" ] && [ "${_BUMP_TYPE}" != "patch" ]; then
70-
echo "[ERROR]: Bump type '${_BUMP_TYPE}' is invalid, should be: 'major', 'minor' or 'patch'!"
71-
exit 1
72-
fi
54+
while [ $# -gt 0 ]; do
55+
case "${1}" in
56+
-b | --bump-type)
57+
[ $# -ge 2 ] || { echo "[ERROR]: ${1} requires a value!" >&2; exit 1; }
58+
_BUMP_TYPE="${2}"
59+
shift 2;;
60+
-b=* | --bump-type=*)
61+
_BUMP_TYPE="${1#*=}"
62+
shift;;
63+
-c | --commit)
64+
_IS_COMMIT=true
65+
shift;;
66+
-t | --tag)
67+
_IS_TAG=true
68+
shift;;
69+
-p | --push)
70+
_IS_PUSH=true
71+
shift;;
72+
-h | --help)
73+
_usage_help
74+
exit 0;;
75+
*)
76+
echo "[ERROR]: Failed to parse argument -> ${1}!" >&2
77+
_usage_help
78+
exit 1;;
79+
esac
80+
done
81+
## --- Menu arguments --- ##
82+
83+
84+
if [ -z "${_BUMP_TYPE:-}" ]; then
85+
echo "[ERROR]: Bump type is empty, use '-b=' or '--bump-type=' argument!"
86+
exit 1
87+
fi
7388

74-
if [ "${_IS_COMMIT}" == true ]; then
75-
if [ -z "$(which git)" ]; then
76-
echo "[ERROR]: 'git' not found or not installed!"
77-
exit 1
78-
fi
89+
if [ "${_BUMP_TYPE}" != "major" ] && [ "${_BUMP_TYPE}" != "minor" ] && [ "${_BUMP_TYPE}" != "patch" ]; then
90+
echo "[ERROR]: Bump type '${_BUMP_TYPE}' is invalid, should be: 'major', 'minor' or 'patch'!"
91+
exit 1
92+
fi
93+
94+
if [ "${_IS_COMMIT}" == true ]; then
95+
if ! command -v git >/dev/null 2>&1; then
96+
echo "[ERROR]: Not found 'git' command, please install it first!" >&2
97+
exit 1
7998
fi
99+
fi
80100

81101

102+
## --- Main --- ##
103+
main()
104+
{
82105
echo "[INFO]: Checking current version..."
83106
local _current_version
84107
_current_version="$(./scripts/get-version.sh)"
@@ -135,5 +158,5 @@ main()
135158
fi
136159
}
137160

138-
main "${@:-}"
161+
main
139162
## --- Main --- ##

0 commit comments

Comments
 (0)