Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement semver --increment to remove dependency #45

Merged
merged 1 commit into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,5 @@
"type": "git",
"url": "git://github.com/davidchambers/xyz.git"
},
"dependencies": {
"semver": "5.3.x"
},
"bundledDependencies": [
"semver"
]
"dependencies": {}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

}
97 changes: 70 additions & 27 deletions xyz
Original file line number Diff line number Diff line change
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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition shows that we have never fully supported the pre{major,minor,patch,release} increments.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Avaq, do you agree with rc as the default value?

--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)"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to see this code go!

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))*))?$"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should match the grammar.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've missed the build part. Not sure if that's really relevant though.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've missed the build part.

That's true. I've never used it. Have you, Aldwin?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never used it either.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's omit build. I don't understand how it is used, and I'm not aware of xyz users actually using it. If in the future there is demand for build, we're free to change our minds. :)

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'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change means ./xyz --version no longer works (from this project's root directory), but I don't consider this a problem. Not having to determine our location on the file system is a win.

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --increment value is validated in inc, so this paragraph is no longer necessary.

[[ $(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