|
| 1 | +# Get the current version from git. |
| 2 | +# `git describe` will use the most recent tag. |
| 3 | +# `--tags` ensures any tag is considered, not just annotated ones. |
| 4 | +# `--always` ensures that if no tags are present, the commit hash is used. |
| 5 | +# `--dirty` will append "-dirty" if the working directory has uncommitted changes. |
| 6 | +VERSION ?= $(shell git describe --tags --always --dirty) |
| 7 | + |
| 8 | +# Release target to create a new semantic version tag |
| 9 | +.PHONY: release |
| 10 | +release: $(RELEASE_DEPS) |
| 11 | + # 1. Check for a clean working directory |
| 12 | + @if ! git diff --quiet; then \ |
| 13 | + echo "Error: Working directory is not clean. Commit or stash changes before releasing."; \ |
| 14 | + exit 1; \ |
| 15 | + fi |
| 16 | + |
| 17 | + # This shell 'if' statement runs at execution time, not parse time. |
| 18 | + @if [ -z "$(BUMP)" ]; then \ |
| 19 | + echo "Error: BUMP is not set. Usage: make release BUMP=patch|minor|major"; \ |
| 20 | + exit 1; \ |
| 21 | + fi |
| 22 | + |
| 23 | + # 2. Get the latest git tag, or start at v0.0.0 |
| 24 | + @CURRENT_TAG=$$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0"); \ |
| 25 | + CURRENT_VERSION=$$(echo $$CURRENT_TAG | sed 's/^v//'); \ |
| 26 | + MAJOR=$$(echo $$CURRENT_VERSION | cut -d. -f1); \ |
| 27 | + MINOR=$$(echo $$CURRENT_VERSION | cut -d. -f2); \ |
| 28 | + PATCH=$$(echo $$CURRENT_VERSION | cut -d. -f3); \ |
| 29 | + \ |
| 30 | + if [ "$(BUMP)" = "patch" ]; then \ |
| 31 | + PATCH=$$((PATCH + 1)); \ |
| 32 | + elif [ "$(BUMP)" = "minor" ]; then \ |
| 33 | + MINOR=$$((MINOR + 1)); \ |
| 34 | + PATCH=0; \ |
| 35 | + elif [ "$(BUMP)" = "major" ]; then \ |
| 36 | + MAJOR=$$((MAJOR + 1)); \ |
| 37 | + MINOR=0; \ |
| 38 | + PATCH=0; \ |
| 39 | + else \ |
| 40 | + echo "Error: Invalid BUMP value. Use 'patch', 'minor', or 'major'."; \ |
| 41 | + exit 1; \ |
| 42 | + fi; \ |
| 43 | + \ |
| 44 | + NEW_TAG="v$${MAJOR}.$${MINOR}.$${PATCH}"; \ |
| 45 | + echo "Current version: $$CURRENT_TAG"; \ |
| 46 | + echo "Creating new version: $$NEW_TAG"; \ |
| 47 | + git tag $$NEW_TAG; \ |
| 48 | + git push origin $$NEW_TAG; |
0 commit comments