Skip to content

Commit

Permalink
implement semver --increment to remove dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Dec 5, 2017
1 parent 56a7bc3 commit 5e3a62e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
7 changes: 1 addition & 6 deletions package.json
Expand Up @@ -17,10 +17,5 @@
"type": "git",
"url": "git://github.com/davidchambers/xyz.git"
},
"dependencies": {
"semver": "5.3.x"
},
"bundledDependencies": [
"semver"
]
"dependencies": {}
}
97 changes: 70 additions & 27 deletions xyz
Expand Up @@ -31,6 +31,11 @@ Options:
'X.Y.Z' acts as a placeholder for the version number.
'Version X.Y.Z' is assumed if this option is omitted.
--prerelease-label <label>
Specify the label to be used in the version number when publishing
a pre-release version (e.g. 'beta' is the label in '2.0.0-beta.0').
'rc' is assumed if this option is omitted.
--publish-command <command>
Specify the command to be run to publish the package. It may refer
to the VERSION and PREVIOUS_VERSION environment variables. A no-op
Expand Down Expand Up @@ -60,27 +65,70 @@ Options:
Print xyz's version number and exit.
"

# http://stackoverflow.com/a/246128/312785
unset CDPATH
path="${BASH_SOURCE[0]}"
while [ -h "$path" ] ; do
dir="$(cd -P "$(dirname "$path")" && pwd)"
path="$(readlink "$path")"
[[ $path == /* ]] || path="$dir/$path"
done
dir="$(cd -P "$(dirname "$path")" && pwd)"
inc() {
local prerelease_label="$1" increment="$2" version="$3"
local number='0|[1-9][0-9]*'
local part="$number|[-0-9A-Za-z]+"
local pattern="^($number)[.]($number)[.]($number)(-(($part)([.]($part))*))?$"
if ! [[ $version =~ $pattern ]] ; then
printf 'Invalid version: %s\n' "$version" >&2
return 1
fi
local -i x=${BASH_REMATCH[1]}
local -i y=${BASH_REMATCH[2]}
local -i z=${BASH_REMATCH[3]}
local qual=${BASH_REMATCH[5]}

local -a parts=("$prerelease_label" 0)
case $increment in
major) (( y + z == 0 )) && [[ -n $qual ]] || x+=1 ; y=0 ; z=0 ;;
minor) (( z == 0 )) && [[ -n $qual ]] || y+=1 ; z=0 ;;
patch) [[ -n $qual ]] || z+=1 ;;
premajor) x+=1 ; y=0 ; z=0 ;;
preminor) y+=1 ; z=0 ;;
prepatch) z+=1 ;;
prerelease)
case ${BASH_REMATCH[6]} in
'') z+=1 ;;
$prerelease_label)
local idx
local -a xs
IFS=. read -r -a xs <<<"$qual"
for (( idx = ${#xs[@]} - 1 ; idx > 0 ; idx -= 1 )) ; do
pattern="^($number)$"
if [[ ${xs[idx]} =~ $pattern ]] ; then
parts=(${xs[@]})
(( parts[idx] += 1 ))
break
fi
done
esac
;;
*)
echo "Invalid --increment" >&2
return 1
esac

version="$x.$y.$z"
if [[ $increment =~ ^pre ]] ; then
join() { local IFS=. ; echo "$*" ; }
version+="-$(join "${parts[@]}")"
fi
printf %s "$version"
}

branch=master
edit=false
increment=patch
message_template='Version X.Y.Z'
prerelease_label=rc
publish_command='npm publish'
repo=origin
declare -a scripts
tag_template=vX.Y.Z
dry_run=false

while (($# > 0)) ; do
while (( $# > 0 )) ; do
option="$1"
shift

Expand All @@ -90,29 +138,25 @@ while (($# > 0)) ; do
exit
;;
-v|--version)
node -p "require('$dir/package.json').version"
node -p 'require("xyz/package.json").version'
exit
;;
-b|--branch) branch="$1" ; shift ;;
-e|--edit) edit=true ;;
-i|--increment) increment="$1" ; shift ;;
-m|--message) message_template="$1" ; shift ;;
--publish-command) publish_command="$1" ; shift ;;
-r|--repo) repo="$1" ; shift ;;
-s|--script) scripts+=("$1") ; shift ;;
-t|--tag) tag_template="$1" ; shift ;;
--dry-run) dry_run=true ;;
-b|--branch) branch="$1" ; shift ;;
-e|--edit) edit=true ;;
-i|--increment) increment="$1" ; shift ;;
-m|--message) message_template="$1" ; shift ;;
--prerelease-label) prerelease_label="$1" ; shift ;;
--publish-command) publish_command="$1" ; shift ;;
-r|--repo) repo="$1" ; shift ;;
-s|--script) scripts+=("$1") ; shift ;;
-t|--tag) tag_template="$1" ; shift ;;
--dry-run) dry_run=true ;;
*)
echo "Unrecognized option $option" >&2
exit 1
esac
done

case "$increment" in
major|minor|patch|premajor|preminor|prepatch|prerelease) ;;
*) echo "Invalid --increment" >&2 ; exit 1 ;;
esac

[[ $(git rev-parse --abbrev-ref HEAD) == "$branch" ]] ||
(echo "Current branch does not match specified --branch" >&2 ; exit 1)

Expand All @@ -125,8 +169,7 @@ name=$(node -p "require('./package.json').name" 2>/dev/null) ||
version=$(node -p "require('./package.json').version" 2>/dev/null) ||
(echo "Cannot read package version" >&2 ; exit 1)

next_version=$("$dir/node_modules/.bin/semver" -i "$increment" "$version") ||
(echo "Cannot increment version number" >&2 ; exit 1)
next_version=$(inc "$prerelease_label" "$increment" "$version")

message="${message_template//X.Y.Z/$next_version}"
tag="${tag_template//X.Y.Z/$next_version}"
Expand Down

0 comments on commit 5e3a62e

Please sign in to comment.