Skip to content

Commit ef014a2

Browse files
committed
feat: Add 'version' subcommand
1 parent 04d6edd commit ef014a2

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

pkg/lib/cmd/basalt.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ main.basalt() {
3939
install) shift; do-install "$@" ;;
4040
list) shift; do-list "$@" ;;
4141
run) shift; do-run "$@" ;;
42+
version) shift; do-version "$@" ;;
4243
complete) shift; do-complete "$@" ;;
4344
global) shift
4445
case "$1" in

pkg/lib/commands/do-version.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# shellcheck shell=bash
2+
3+
do-version() {
4+
util.init_local
5+
6+
local flag_yes='no'
7+
local -a args=()
8+
for arg; do case "$arg" in
9+
-y|--yes)
10+
flag_yes='yes'
11+
;;
12+
-*)
13+
bprint.die "Flag '$arg' not recognized"
14+
;;
15+
*)
16+
args+=("$arg")
17+
;;
18+
esac done
19+
20+
if ((${#args[@]} > 1)); then
21+
bprint.die "The only argument must be the new version string"
22+
fi
23+
24+
# if [ -n "$(git status --porcelain)" ]; then
25+
# bprint.die "The working tree must be empty (including untracked files)"
26+
# fi
27+
28+
local previous_version_string=
29+
local version_string="${args[0]}"
30+
31+
if util.get_toml_string "$BASALT_LOCAL_PROJECT_DIR/basalt.toml" 'version'; then
32+
previous_version_string="$REPLY"
33+
else
34+
bprint.die "To use 'basalt version', a you must have a 'version' field set to at least an empty string"
35+
fi
36+
37+
if [ -z "$version_string" ]; then
38+
printf '%s\n' "Old version: $previous_version_string"
39+
read -rep "New version: " -i "$previous_version_string"
40+
version_string="$REPLY"
41+
else
42+
if [ "$flag_yes" != yes ]; then
43+
printf '%s\n' "Changing: $previous_version_string -> $version_string"
44+
45+
read -rep "Are you sure (y/n): "
46+
if [[ $REPLY != @(y|yes) ]]; then
47+
bprint.info "Cancelling version change"
48+
return
49+
fi
50+
fi
51+
fi
52+
53+
if [ "$previous_version_string" = "$version_string" ]; then
54+
bprint.info "Previous version same as new version. Exiting without making any changes"
55+
return
56+
fi
57+
58+
if [ -z "$version_string" ]; then
59+
bprint.info "Version string blank. Exiting without making any changes"
60+
return
61+
fi
62+
63+
# TODO: after self-bootstrap, add additional validation checks
64+
if [[ $version_string =~ [,\'\"\\] ]]; then
65+
bprint.die "Version string cannot have commas, backslashes, single quotes, or double quotes"
66+
fi
67+
68+
local toml_file="$BASALT_LOCAL_PROJECT_DIR/basalt.toml"
69+
mv "$toml_file" "$toml_file.bak"
70+
sed -e "s,\([ \t]*version[ \t]*=[ \t]*['\"]\)\(.*\)\(['\"].*\),\1${version_string}\3," "$toml_file.bak" > "$toml_file"
71+
rm "$toml_file.bak"
72+
73+
if util.get_toml_string "$BASALT_LOCAL_PROJECT_DIR/basalt.toml" 'version'; then
74+
if [ "$REPLY" != "$version_string" ]; then
75+
bprint.die "Failed to properly substitute version with sed"
76+
fi
77+
else
78+
bprint.die "Expected a value for field 'version'"
79+
fi
80+
81+
bprint.info "running: git add \"\$BASALT_LOCAL_PROJECT_DIR/basalt.toml\""
82+
if ! git add "$toml_file"; then
83+
bprint.die "Failed to 'git add'"
84+
fi
85+
86+
bprint.info "running: git commit -m v$version_string"
87+
if ! git commit -qm "v$version_string"; then
88+
bprint.die "Failed to 'git commit'"
89+
fi
90+
91+
bprint.info "running: git tag -a -m v$version_string v$version_string"
92+
if ! git tag -a -m "v$version_string" "v$version_string"; then
93+
bprint.die "Failed to 'git tag'"
94+
fi
95+
}

0 commit comments

Comments
 (0)