1SHELL := /usr/bin/env bash
2GOPATH := $(shell go env GOPATH)
3PATH := $(PATH):$(GOPATH)/bin
4GO_SOURCE_FILES := $(shell find . -name "*.go" | sort)
5LIBNAME := $(shell basename $$(dirname $$(pwd)))
6EXE_BASE_NAME := cucumber-$(LIBNAME)
7GOX_LDFLAGS := "-X main.version=${NEW_VERSION}"
8EXES := $(shell find dist -name '$(EXE_BASE_NAME)-*')
9UPX_EXES = $(patsubst dist/$(EXE_BASE_NAME)-%,dist_compressed/$(EXE_BASE_NAME)-%,$(EXES))
10# Determine if we're on linux or osx (ignoring other OSes as we're not building on them)
11OS := $(shell [[ "$$(uname)" == "Darwin" ]] && echo "darwin" || echo "linux")
12# Determine if we're on 386 or amd64 (ignoring other processors as we're not building on them)
13ARCH := $(shell [[ "$$(uname -m)" == "x86_64" ]] && echo "amd64" || echo "386")
14EXE = dist/$(EXE_BASE_NAME)-$(OS)-$(ARCH)
15REPLACEMENTS := $(shell sed -n "/^\s*github.com\/cucumber/p" go.mod | perl -wpe 's/\s*(github.com\/cucumber\/(.*)-go\/v\d+).*/q{replace } . $$1 . q{ => ..\/..\/} . $$2 . q{\/go}/eg')
16CURRENT_MAJOR := $(shell sed -n "/^module/p" go.mod | awk '{ print $$0 "/v1" }' | cut -d'/' -f4 | cut -d'v' -f2)
17NEW_MAJOR := $(shell echo ${NEW_VERSION} | awk -F'.' '{print $$1}')
18
19default: .linted .tested
20.PHONY: default
21
22# Run the .dist target if there is a main file
23ifneq (,$(wildcard ./cmd/main.go))
24default: dist
25endif
26
27.deps:
28	touch $@
29
30dist: $(EXE)
31ifndef NO_UPX_COMPRESSION
32	make .dist-compressed
33endif
34	touch $@
35
36$(EXE): .deps $(GO_SOURCE_FILES)
37	mkdir -p dist
38ifndef NO_CROSS_COMPILE
39	# Cross-compile executable for many platforms
40	go get github.com/aslakhellesoy/gox
41	gox -buildmode=exe -ldflags $(GOX_LDFLAGS) -output "dist/$(EXE_BASE_NAME)-{{.OS}}-{{.Arch}}" -rebuild ./cmd
42else
43	# Compile executable for the local platform only
44	go build -ldflags $(GOX_LDFLAGS) -o $@ ./cmd
45endif
46
47.dist-compressed: $(UPX_EXES)
48	touch $@
49
50update-dependencies:
51	go get -u && go mod tidy
52.PHONY: update-dependencies
53
54pre-release: remove-replaces update-version update-dependencies clean default
55.PHONY: pre-release
56
57update-version: update-major
58	# no-op
59.PHONY: update-version
60
61ifneq (,$(wildcard ./cmd/main.go))
62publish: dist
63ifdef NEW_VERSION
64	./scripts/github-release $(NEW_VERSION)
65else
66	@echo -e "\033[0;31mNEW_VERSION is not defined. Can't publish :-(\033[0m"
67	exit 1
68endif
69else
70publish:
71	# no-op
72endif
73.PHONY: publish
74
75dist_compressed/$(EXE_BASE_NAME)-%: dist/$(EXE_BASE_NAME)-%
76	mkdir -p dist_compressed
77	# requires upx in PATH to compress supported binaries
78	# may produce an error ARCH not supported
79	-upx $< -o $@
80
81	# Test the integrity
82	if [ -f "$@" ]; then upx -t $@ && cp $@ $< || rm $@; fi
83
84.linted: $(GO_SOURCE_FILES)
85	gofmt -w $^
86	touch $@
87
88.tested: .deps $(GO_SOURCE_FILES)
89	go test ./...
90	touch $@
91
92post-release: add-replaces
93.PHONY: post-release
94
95clean: clean-go
96.PHONY: clean
97
98clean-go:
99	rm -rf .deps .tested* .linted dist/ .dist-compressed dist_compressed/ acceptance/
100.PHONY: clean-go
101
102remove-replaces:
103	sed -i '/^replace/d' go.mod
104	sed -i 'N;/^\n$$/D;P;D;' go.mod
105.PHONY: remove-replaces
106
107add-replaces:
108ifeq ($(shell sed -n "/^\s*github.com\/cucumber/p" go.mod | wc -l), 0)
109	# No replacements here
110else
111	sed -i '/^go .*/i $(REPLACEMENTS)\n' go.mod
112endif
113.PHONY: add-replaces
114
115update-major:
116ifeq ($(CURRENT_MAJOR), $(NEW_MAJOR))
117	# echo "No major version change"
118else
119	echo "Updating major from $(CURRENT_MAJOR) to $(NEW_MAJOR)"
120	sed -Ei "s/$(LIBNAME)-go(\/v$(CURRENT_MAJOR))?/$(LIBNAME)-go\/v$(NEW_MAJOR)/" go.mod
121	sed -Ei "s/$(LIBNAME)-go(\/v$(CURRENT_MAJOR))?/$(LIBNAME)-go\/v$(NEW_MAJOR)/" $(shell find . -name "*.go")
122endif
123.PHONY: update-major
124