-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
127 lines (100 loc) · 3.44 KB
/
Makefile
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
126
127
SHELL := /usr/bin/env bash
PATH := $(PATH):/usr/local/bin
# The name of the executable (default is current directory name)
TARGET := $(shell echo $${PWD\#\#*/})
.DEFAULT_GOAL: $(TARGET)
# These will be provided to the target
BUILD := `git rev-parse HEAD`
# always add one to the commit number to fix an off by one bug
# as the release makes a commit prior to publishing
COMMIT := $(shell git rev-list HEAD | wc -l | sed 's/^ *//g' | awk '{print $$1 + 1}')
VERSION := 1.0.$(COMMIT)
# Use linker flags to provide version/build settings to the target
LDFLAGS=-ldflags "-X=main.Version=$(VERSION) -X=main.Build=$(BUILD) -s -w"
# go source files, ignore vendor directory
SRC = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
RELEASER := $(shell command -v goreleaser 2> /dev/null)
REVIVE := $(shell command -v revive 2> /dev/null)
FPM := $(shell command -v fpm 2> /dev/null)
BRANCH := `git rev-parse --abbrev-ref HEAD`
GOROOT := `go env GOROOT`
.PHONY: all build clean install uninstall fmt simplify check run
all: build install
$(TARGET): $(SRC)
@go build $(LDFLAGS) -o $(TARGET)
build: $(TARGET) deps check test
@go build $(LDFLAGS)
release: build
@sed -i .bak 's/\"1.0.*\"/\"1.0.'$(COMMIT)'\"/' cmd/root.go
@grep $(COMMIT) cmd/root.go 2> /dev/null && rm cmd/root.go.bak
@sed -i .bak 's/VERSION 1.0.*/VERSION 1.0.'$(COMMIT)'/' README.md
@grep $(COMMIT) README.md 2> /dev/null && rm README.md.bak
@git commit -am "new $(BRANCH) build: $(VERSION)"
@git tag -a v$(VERSION) -m "new $(BRANCH) build: $(VERSION)"
@echo pushing to branch $(BRANCH)
@git push origin v$(VERSION)
@git push origin $(BRANCH)
ifndef RELEASER
@echo "cannot build release (missing goreleaser)"
else
@echo "creating a new release"
GITHUB_TOKEN=`cat ~/.config/goreleaser/github_token` goreleaser --clean
endif
@true
clean:
@rm -f $(TARGET)
$(shell find ./bin -type f -perm +111 -delete)
install:
@go install $(LDFLAGS)
uninstall: clean
@rm -f $$(which ${TARGET})
fmt:
@gofmt -l -w $(SRC)
simplify:
@gofmt -s -l -w $(SRC)
check:
@test -z $(shell gofmt -l main.go | tee /dev/stderr) || echo "[WARN] Fix formatting issues with 'make fmt'"
ifndef REVIVE
@echo "running 'staticcheck .'"
@staticcheck .
else
@echo "running 'revive ./...'"
@revive --formatter friendly ./...
endif
run: install
@$(TARGET)
test: $(TARGET)
@go test -v
deps:
GO111MODULE="on" go mod init | true
GO111MODULE="on" go mod tidy
GO111MODULE="on" go mod verify
mac: GOOS = darwin
mac: GOARCH = amd64
mac:
@echo "building for $(GOOS)/$(GOARCH)"
@mkdir -p bin/$(GOARCH)/$(GOOS)/ && GOOS=$(GOOS) GOARCH=$(GOARCH) go build && mv $(TARGET) bin/$(GOARCH)/$(GOOS)/
ubuntu: GOOS = linux
ubuntu: GOARCH = amd64
ubuntu:
@echo "building for $(GOOS)/$(GOARCH)"
@mkdir -p bin/$(GOARCH)/$(GOOS)/ && GOOS=$(GOOS) GOARCH=$(GOARCH) go build && mv $(TARGET) bin/$(GOARCH)/$(GOOS)/
packages: deb pkg
deb: GOOS = linux
deb: GOARCH = amd64
deb: ubuntu
ifndef FPM
@echo "'fpm' is not installed, cannot make packages"
else
@fpm -n $(TARGET) -s dir -t deb -a $(GOARCH) -p $(TARGET)_$(VERSION)_$(GOARCH).deb --deb-no-default-config-files ./bin/$(GOARCH)/$(GOOS)/$(TARGET)=/usr/local/bin/$(TARGET)
@mv $(TARGET)*.deb ./packages
endif
pkg: GOOS = darwin
pkg: GOARCH = amd64
pkg: mac
ifndef FPM
@echo "'fpm' is not installed, cannot make packages"
else
@fpm -n $(TARGET) -s dir -t osxpkg -a $(GOARCH) -p $(TARGET)-$(VERSION)-$(GOARCH).pkg ./bin/$(GOARCH)/$(GOOS)/$(TARGET)=/usr/local/bin/$(TARGET)
@mv $(TARGET)*.pkg ./packages
endif