93 lines
4.1 KiB
Makefile
93 lines
4.1 KiB
Makefile
# Release helpers.
|
|
#
|
|
# `make patch|minor|major` bumps the version in Cargo.toml and Cargo.lock, folds
|
|
# that bump into the last commit, and tags it. Everything is one action so a
|
|
# release cannot end up half-done: no version bump left untagged, no tag without
|
|
# a bump.
|
|
|
|
.PHONY: help patch minor major version release install
|
|
|
|
help:
|
|
@echo "make patch bump $(CURRENT) -> next patch, amend into HEAD, tag"
|
|
@echo "make minor bump $(CURRENT) -> next minor, amend into HEAD, tag"
|
|
@echo "make major bump $(CURRENT) -> next major, amend into HEAD, tag"
|
|
@echo "make version print the current version"
|
|
@echo "make install cargo install --path . && worklane install-skill"
|
|
|
|
# Read from the [package] table of Cargo.toml, which is the version the binary
|
|
# reports through `--version`, so it is the single source of truth. The name is
|
|
# what finds this crate's own entry in Cargo.lock.
|
|
PACKAGE = $(shell awk -F'"' '/^\[package\]/{p=1;next} /^\[/{p=0} p && /^name *= *"/{print $$2; exit}' Cargo.toml)
|
|
CURRENT = $(shell awk -F'"' '/^\[package\]/{p=1;next} /^\[/{p=0} p && /^version *= *"/{print $$2; exit}' Cargo.toml)
|
|
|
|
# 1 appends ", and bump to <version>" to the amended commit's subject; empty
|
|
# leaves the message alone.
|
|
AMEND_SUBJECT = 1
|
|
|
|
version:
|
|
@echo $(CURRENT)
|
|
|
|
patch: BUMP = patch
|
|
minor: BUMP = minor
|
|
major: BUMP = major
|
|
patch minor major: release
|
|
|
|
# A release rewrites the last commit, so refuse anything that would make that
|
|
# rewrite lose work or orphan a tag.
|
|
release:
|
|
@set -e; \
|
|
if ! git rev-parse -q --verify HEAD >/dev/null 2>&1; then \
|
|
echo "error: no commits yet, so there is no commit to fold a version bump into. Commit first." >&2; \
|
|
exit 1; \
|
|
fi; \
|
|
if [ -n "$$(git status --porcelain)" ]; then \
|
|
echo "error: working tree is dirty. Commit or stash first, and decide whether that work belongs in this version:" >&2; \
|
|
git status --short >&2; \
|
|
exit 1; \
|
|
fi; \
|
|
if [ -n "$$(git tag --points-at HEAD)" ]; then \
|
|
echo "error: HEAD is already tagged $$(git tag --points-at HEAD | paste -sd' '). Amending it would leave that tag on an orphaned commit. Commit something first." >&2; \
|
|
exit 1; \
|
|
fi; \
|
|
if upstream=$$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) && \
|
|
git merge-base --is-ancestor HEAD "$$upstream" 2>/dev/null; then \
|
|
echo "error: HEAD is already pushed to $$upstream. Amending it would need a force push. Commit something first." >&2; \
|
|
exit 1; \
|
|
fi; \
|
|
current="$(CURRENT)"; \
|
|
if [ -z "$$current" ]; then echo "error: no version found in the [package] table of Cargo.toml" >&2; exit 1; fi; \
|
|
new=$$(echo "$$current" | awk -F. -v kind=$(BUMP) '{ \
|
|
if (NF != 3) exit 1; \
|
|
if (kind == "patch") printf "%d.%d.%d\n", $$1, $$2, $$3 + 1; \
|
|
else if (kind == "minor") printf "%d.%d.0\n", $$1, $$2 + 1; \
|
|
else printf "%d.0.0\n", $$1 + 1 }'); \
|
|
if [ -z "$$new" ]; then echo "error: cannot bump version '$$current': expected major.minor.patch" >&2; exit 1; fi; \
|
|
if git rev-parse -q --verify "refs/tags/v$$new" >/dev/null; then \
|
|
echo "error: tag v$$new already exists" >&2; exit 1; \
|
|
fi; \
|
|
awk -v v="$$new" '/^\[package\]/{p=1} /^\[/ && !/^\[package\]/{p=0} p && /^version *= *"/{sub(/"[^"]*"/, "\"" v "\"")} {print}' Cargo.toml > Cargo.toml.bump; \
|
|
mv Cargo.toml.bump Cargo.toml; \
|
|
git add Cargo.toml; \
|
|
if [ -f Cargo.lock ] && git ls-files --error-unmatch Cargo.lock >/dev/null 2>&1; then \
|
|
awk -v v="$$new" -v pkg="$(PACKAGE)" '$$0 == "name = \"" pkg "\""{f=1} f && /^version = "/{sub(/"[^"]*"/, "\"" v "\""); f=0} {print}' Cargo.lock > Cargo.lock.bump; \
|
|
mv Cargo.lock.bump Cargo.lock; \
|
|
git add Cargo.lock; \
|
|
fi; \
|
|
if [ -n "$(AMEND_SUBJECT)" ]; then \
|
|
subject=$$(git log -1 --format=%s | sed -E 's/, and bump to [0-9]+\.[0-9]+\.[0-9]+$$//'); \
|
|
body=$$(git log -1 --format=%b); \
|
|
if [ -n "$$body" ]; then \
|
|
git commit --quiet --amend -m "$$subject, and bump to $$new" -m "$$body"; \
|
|
else \
|
|
git commit --quiet --amend -m "$$subject, and bump to $$new"; \
|
|
fi; \
|
|
else \
|
|
git commit --quiet --amend --no-edit; \
|
|
fi; \
|
|
git tag -a "v$$new" -m "v$$new"; \
|
|
echo "$$current -> $$new, tagged v$$new on $$(git rev-parse --short HEAD) ($$(git log -1 --format=%s))"
|
|
|
|
install:
|
|
cargo install --path .
|
|
worklane install-skill
|