Skip to content

Commit

Permalink
chore: release semver names map
Browse files Browse the repository at this point in the history
Using a map allows more flexibility in the future.
This should also fix more bugs introduced in the previous commit.
  • Loading branch information
DadiBit committed Aug 23, 2022
1 parent c6a80aa commit f737de7
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions .github/workflows/release/semver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,84 @@
# old=<major>.<minor>.<patch>[-<name>[.<id>]]
# new=<major>.<minor>.<patch>[-<name>[.<id>]]

declare -A pre=(
[alpha]=1
[beta]=2
[rc]=3
[0]=""
[1]="alpha"
[2]="beta"
[3]="rc"
)

[[ ! "$old" =~ ^$regex$ ]] && exit 1

IFS='.-' read -r -a old <<< "$old"
[ -v 'old[3]' ] && old[3]="${pre[${old[3]}]}"

valid=0 # 0 means "not valid", other values are for "reason" debugging
declare -a ver
if [[ "$new" =~ ^$regex$ ]]; then

IFS='.-' read -r -a ver <<< "$new"
[ -v 'ver[3]' ] && ver[3]="${pre[${ver[3]}]}"
for i in $(seq 0 $level); do
if [ $i == 3 ]; then
# same pre-release
if [ "${ver[3]}" == "${old[3]}" ]; then
if [[ $i == 3 ]]; then
# same pre-release name
if [[ ${ver[3]} == ${old[3]} ]]; then
continue
# left pre-release stage
elif [ ${#ver[3]} == 0 ]; then
elif [[ ${ver[3]} == 0 ]]; then
valid=1
break
# entered pre-release stage (ko because we know the first part is equal -> check for if!)
elif [ ${#ver[3]} == 0 ]; then
# entered pre-release stage (ko; we know the part before is equal!)
# eg: 1.0.0 --> 1.0.0-alpha is bad
elif [[ ${old[3]} == 0 ]]; then
break
# a "grater" pre-release: "a(alpha)" > "b(eta)" > "r(c)"?
elif [[ "${ver[3]}" > "${old[3]}" ]]; then
# a "grater" pre-release?
elif [[ ${ver[3]} -gt ${old[3]} ]]; then
valid=2
break
fi
elif [[ ${ver[$i]} -gt ${old[$i]} ]]; then
valid=2
break
valid=2
break
elif [[ ${ver[$i]} -lt ${old[$i]} ]]; then
break
break
fi
done

fi

step() {
ver[$1]=$(( ${ver[$1]}+1 ))
}

# if ver should should be generated
if [ $valid == 0 ]; then
ver=(${old[@]})
if [[ $level == 4 ]]; then
ver[$level]=$(( ${ver[$level]}+1 ))
elif [[ $level == 3 ]]; then
# if we increase pre-release name we also reset the id
ver=(${ver[@]::4})
if [ "${ver[3]}" == "alpha" ]; then
ver[3]="beta"
elif [ "${ver[3]}" == "beta" ]; then
ver[3]="rc"
elif [ "${ver[3]}" == "rc" ]; then
ver=(${ver[@]::3}) # leave pre-release stage
ver[2]=$(( ${ver[2]}+1 ))
fi
else
ver=(${ver[@]::3}) # no pre-release
ver[$level]=$(( ${ver[$level]}+1 ))
# if we increase major/minor we want to also reset minor/patch
[[ $level == 0 ]] && ver[1]=0
[[ $level == 0 ]] || [[ $level == 1 ]] && ver[2]=0
ver=(${old[@]})
step "$level"
if [[ $level == 3 ]]; then
# if no name was found increase patch as well
if [[ ! -v "pre[${ver[3]}]" ]]; then
ver[3]=1
step 2
# if we just got in alpha increase patch as well
elif [[ ${ver[3]} == 1 ]]; then
step 2
fi
# if we just got in alpha increase patch as well
elif [[ $level == 4 ]] && [[ ! -v 'ver[3]' ]]; then
step 2
fi
for i in $(seq $(( level+1 )) 4); do
# reset all parts after the one increased
ver[$i]=0
done
fi

echo "version=${ver[0]}.${ver[1]}.${ver[2]}${ver[3]:+-${ver[3]}}${ver[4]:+.${ver[4]}}"
# fix: bash is ***... it thinks ver[3] is missing?
ver=(${ver[@]})
ver[3]="${pre[${ver[3]}]}"

echo "version=${ver[0]}.${ver[1]}.${ver[2]}${ver[3]:+-${ver[3]}.${ver[4]:-0}}"

0 comments on commit f737de7

Please sign in to comment.