Distribution-generating makefile targets.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 18 May 2014 12:14:21 +0000 (14:14 +0200)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 18 May 2014 12:14:21 +0000 (14:14 +0200)
Makefile

index 862091d8deaa887165aba65076d70b425467c28f..9d74d814373014d9620887c3aabea43fdab0f306 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,63 +1,94 @@
 #!/usr/bin/make -f
 
-JSTEST ?= node_modules/.bin/jstest
-JSHINT ?= node_modules/.bin/jshint
-UGLIFY ?= node_modules/.bin/uglifyjs
-SOURCES = string-lerp.js
+PKGNAME := string-lerp
+
+fallback = $(firstword $(shell command -v $(1)) $(2))
+
+git = $(shell GIT_DIR=$(firstword $(2) $(srcdir) .)/.git $(GIT) $(1))
+git_ls-tree = $(call git,ls-tree -r --name-only $(1),$(2))
+git_tag = $(call git,name-rev --tags --name-only $(call git,rev-parse $(1),$(2)),$(2))
+
+GIT ?= git
+NPM ?= npm
+NPMBINDIR := ./node_modules/.bin
+JSTEST ?= $(NPMBINDIR)/jstest
+JSHINT ?= $(call fallback,jshint,$(NPMBINDIR)/jshint)
+UGLIFY ?= $(call fallback,uglifyjs,$(NPMBINDIR)/uglifyjs)
+SOURCES := string-lerp.js
 UGLIFIED = $(SOURCES:.js=.min.js)
 
-TESTS = $(wildcard tests/*.js);
-LINT_TARGETS = $(addprefix build/stamp/,$(addsuffix .lint,$(SOURCES)))
-TEST_TARGETS = $(addprefix build/stamp/,$(addsuffix .test,$(SOURCES)))
+TESTS = $(wildcard tests/*.js)
+LINT_TARGETS = $(patsubst %.js,build/stamp/%.lint,$(SOURCES))
+TEST_TARGETS = $(patsubst %.js,build/stamp/%.test,$(SOURCES)) \
+               $(patsubst tests/%.js,build/stamp/%.test,$(TESTS))
 
-.PHONY: all check lint test min clean distclean
+.PHONY: all check lint test ugly dist clean distclean
 
 all:
        @echo "Interesting targets:"
        @echo "    lint - validate source (with jshint)"
        @echo "    test - run tests (with jstest)"
        @echo "    ugly - generate minified/mangled source files"
+       @echo "    dist - generate redistributables (if git tagged)"
        @echo
-       @echo "(Running these will download packages from NPM.)"
+       @echo "(Running these may download packages from NPM.)"
        @echo
 
-%.min.js: %.js $(UGLIFY)
+%.min.js: %.js $(UGLIFY)
        $(UGLIFY) --comments -m < $< > $@
 
-build:
-       mkdir -p build/stamp build/dist
-
-build/stamp/%.lint: % build
+build/stamp/%.lint: %.js | $(JSHINT)
        @mkdir -p $(@D)
        $(JSHINT) $<
        @touch $@
 
-build/stamp/%.test: tests/% % build
+build/stamp/%.test: tests/%.js %.js | $(JSTEST)
        @mkdir -p $(@D)
        @echo $(JSTEST) $<
        @$(JSTEST) $< > $@ || (cat $@ && rm -f $@ && exit 1)
        @touch $@
 
-ugly: $(UGLIFIED)
+build/stamp/%.test: tests/%.js $(SOURCES) | $(JSTEST)
+       @mkdir -p $(@D)
+       @echo $(JSTEST) $<
+       @$(JSTEST) $< > $@ || (cat $@ && rm -f $@ && exit 1)
+       @touch $@
 
 check: lint test
 
-lint: $(JSHINT) $(LINT_TARGETS)
+lint: $(LINT_TARGETS)
+
+test: $(TEST_TARGETS)
+
+ugly: $(UGLIFIED)
+
+build/dist/$(PKGNAME)-undefined.zip build/dist/$(PKGNAME)-undefined.tar.gz:
+       $(error Distributables must be tagged or explicitly versioned)
+
+.SECONDEXPANSION:
+build/dist/$(PKGNAME)-%.zip: $$(call git_ls-tree,$$*)
+       @mkdir -p $(@D)
+       git archive --prefix=$(PKGNAME)-$*/ --output=$@ $*
+
+.SECONDEXPANSION:
+build/dist/$(PKGNAME)-%.tar.gz: $$(call git_ls-tree,$$*)
+       @mkdir -p $(@D)
+       git archive --prefix=$(PKGNAME)-$*/ --output=$@ $*
 
-test: $(JSTEST) $(TEST_TARGETS)
+dist: check $(addprefix build/dist/$(PKGNAME)-$(call git_tag,HEAD),.zip .tar.gz)
 
 clean:
-       rm -rf build
-       rm -f $(UGLIFIED)
+       $(RM) -r build
+       $(RM) $(UGLIFIED)
 
 distclean: clean
-       rm -rf node_modules
+       $(RM) -r node_modules
 
 $(JSTEST):
-       npm install jstest
+       $(NPM) install jstest
 
 $(JSHINT):
-       npm install jshint
+       $(NPM) install jshint
 
 $(UGLIFY):
-       npm install uglify-js
+       $(NPM) install uglify-js