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