1 | # Since we rely on paths relative to the makefile location, abort if make isn't being run from there.
|
---|
2 | $(if $(findstring /,$(MAKEFILE_LIST)),$(error Please only invoke this makefile from the directory it resides in))
|
---|
3 |
|
---|
4 | # The files that need updating when incrementing the version number.
|
---|
5 | VERSIONED_FILES := *.js *.json README*
|
---|
6 |
|
---|
7 |
|
---|
8 | # Add the local npm packages' bin folder to the PATH, so that `make` can find them, when invoked directly.
|
---|
9 | # Note that rather than using `$(npm bin)` the 'node_modules/.bin' path component is hard-coded, so that invocation works even from an environment
|
---|
10 | # where npm is (temporarily) unavailable due to having deactivated an nvm instance loaded into the calling shell in order to avoid interference with tests.
|
---|
11 | export PATH := $(shell printf '%s' "$$PWD/node_modules/.bin:$$PATH")
|
---|
12 | UTILS := semver
|
---|
13 | # Make sure that all required utilities can be located.
|
---|
14 | UTIL_CHECK := $(or $(shell PATH="$(PATH)" which $(UTILS) >/dev/null && echo 'ok'),$(error Did you forget to run `npm install` after cloning the repo? At least one of the required supporting utilities not found: $(UTILS)))
|
---|
15 |
|
---|
16 | # Default target (by virtue of being the first non '.'-prefixed in the file).
|
---|
17 | .PHONY: _no-target-specified
|
---|
18 | _no-target-specified:
|
---|
19 | $(error Please specify the target to make - `make list` shows targets. Alternatively, use `npm test` to run the default tests; `npm run` shows all tests)
|
---|
20 |
|
---|
21 | # Lists all targets defined in this makefile.
|
---|
22 | .PHONY: list
|
---|
23 | list:
|
---|
24 | @$(MAKE) -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | command grep -v -e '^[^[:alnum:]]' -e '^$@$$command ' | sort
|
---|
25 |
|
---|
26 | # All-tests target: invokes the specified test suites for ALL shells defined in $(SHELLS).
|
---|
27 | .PHONY: test
|
---|
28 | test:
|
---|
29 | @npm test
|
---|
30 |
|
---|
31 | .PHONY: _ensure-tag
|
---|
32 | _ensure-tag:
|
---|
33 | ifndef TAG
|
---|
34 | $(error Please invoke with `make TAG=<new-version> release`, where <new-version> is either an increment specifier (patch, minor, major, prepatch, preminor, premajor, prerelease), or an explicit major.minor.patch version number)
|
---|
35 | endif
|
---|
36 |
|
---|
37 | CHANGELOG_ERROR = $(error No CHANGELOG specified)
|
---|
38 | .PHONY: _ensure-changelog
|
---|
39 | _ensure-changelog:
|
---|
40 | @ (git status -sb --porcelain | command grep -E '^( M|[MA] ) CHANGELOG.md' > /dev/null) || (echo no CHANGELOG.md specified && exit 2)
|
---|
41 |
|
---|
42 | # Ensures that the git workspace is clean.
|
---|
43 | .PHONY: _ensure-clean
|
---|
44 | _ensure-clean:
|
---|
45 | @[ -z "$$((git status --porcelain --untracked-files=no || echo err) | command grep -v 'CHANGELOG.md')" ] || { echo "Workspace is not clean; please commit changes first." >&2; exit 2; }
|
---|
46 |
|
---|
47 | # Makes a release; invoke with `make TAG=<versionOrIncrementSpec> release`.
|
---|
48 | .PHONY: release
|
---|
49 | release: _ensure-tag _ensure-changelog _ensure-clean
|
---|
50 | @old_ver=`git describe --abbrev=0 --tags --match 'v[0-9]*.[0-9]*.[0-9]*'` || { echo "Failed to determine current version." >&2; exit 1; }; old_ver=$${old_ver#v}; \
|
---|
51 | new_ver=`echo "$(TAG)" | sed 's/^v//'`; new_ver=$${new_ver:-patch}; \
|
---|
52 | if printf "$$new_ver" | command grep -q '^[0-9]'; then \
|
---|
53 | semver "$$new_ver" >/dev/null || { echo 'Invalid version number specified: $(TAG) - must be major.minor.patch' >&2; exit 2; }; \
|
---|
54 | semver -r "> $$old_ver" "$$new_ver" >/dev/null || { echo 'Invalid version number specified: $(TAG) - must be HIGHER than current one.' >&2; exit 2; } \
|
---|
55 | else \
|
---|
56 | new_ver=`semver -i "$$new_ver" "$$old_ver"` || { echo 'Invalid version-increment specifier: $(TAG)' >&2; exit 2; } \
|
---|
57 | fi; \
|
---|
58 | printf "=== Bumping version **$$old_ver** to **$$new_ver** before committing and tagging:\n=== TYPE 'proceed' TO PROCEED, anything else to abort: " && read response && [ "$$response" = 'proceed' ] || { echo 'Aborted.' >&2; exit 2; }; \
|
---|
59 | replace "$$old_ver" "$$new_ver" -- $(VERSIONED_FILES) && \
|
---|
60 | git commit -m "v$$new_ver" $(VERSIONED_FILES) CHANGELOG.md && \
|
---|
61 | git tag -a -m "v$$new_ver" "v$$new_ver"
|
---|