-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
156 lines (109 loc) · 4.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
NPROCS = $(shell grep -c 'processor' /proc/cpuinfo)
MAKEFLAGS += -j$(NPROCS)
# Variables typically overridden
TARGETOS?=linux
TARGETARCH?=amd64
REPO_NAME?=chadweimer/gomp
GO_MODULE_NAME?=github.com/$(REPO_NAME)
DOCKER_ARGS?=--platform $(TARGETOS)/$(TARGETARCH)
BUILD_VERSION?=
ARCHIVE_SUFFIX=
ifdef BUILD_VERSION
ARCHIVE_SUFFIX=-$(BUILD_VERSION)
endif
# Basic metadata
COPYRIGHT:=Copyright © 2016-$(shell date +%Y) Chad Weimer
# Output directories
ROOT_BUILD_DIR:=build
BUILD_DIR=$(ROOT_BUILD_DIR)/$(TARGETOS)/$(TARGETARCH)
CLIENT_INSTALL_DIR:=static/node_modules
CLIENT_BUILD_DIR:=static/www/static
# Codegen-related files and directories
CLIENT_CODEGEN_DIR:=static/src/generated
MODELS_CODEGEN_FILE:=models/models.gen.go
API_CODEGEN_FILE:=api/routes.gen.go
MOCKS_CODEGEN_DIR:=mocks
CODEGEN_FILES=$(API_CODEGEN_FILE) $(MODELS_CODEGEN_FILE) $(MOCKS_CODEGEN_DIR)/db/mocks.gen.go $(MOCKS_CODEGEN_DIR)/upload/mocks.gen.go
# Source files
GO_FILES:=$(shell find . -type f -name "*.go" ! -name "*.gen.go")
DB_MIGRATION_FILES:=$(shell find db/migrations -type f -name "*.*")
CLIENT_FILES:=$(filter-out $(shell test -d $(CLIENT_CODEGEN_DIR) && find $(CLIENT_CODEGEN_DIR) -name "*"), $(shell find static -maxdepth 1 -type f -name "*") $(shell find static/src -type f -name "*"))
# Go command arguments
GO_ENV=GOOS=$(TARGETOS) GOARCH=$(TARGETARCH) CGO_ENABLED=0
GO_LD_FLAGS=-ldflags '-X "$(GO_MODULE_NAME)/metadata.BuildVersion=$(BUILD_VERSION)" -X "$(GO_MODULE_NAME)/metadata.Copyright=$(COPYRIGHT)"'
.DEFAULT_GOAL:=$(ROOT_BUILD_DIR)
# ---- INSTALL ----
.PHONY: install
install: $(CLIENT_INSTALL_DIR)
go get ./...
$(CLIENT_INSTALL_DIR): static/package.json
cd static && npm install --silent
.PHONY: uninstall
uninstall:
cd static && npm run clear
# ---- CODEGEN ----
$(CLIENT_CODEGEN_DIR): $(CLIENT_INSTALL_DIR) openapi.yaml models.yaml
cd static && npm run codegen
$(API_CODEGEN_FILE): $(MODELS_CODEGEN_FILE) openapi.yaml api/cfg.yaml
go generate $(GO_MODULE_NAME)/api
$(MODELS_CODEGEN_FILE): models.yaml models/cfg.yaml
go generate $(GO_MODULE_NAME)/models
$(MOCKS_CODEGEN_DIR)/%/mocks.gen.go: $(GO_FILES) $(MODELS_CODEGEN_FILE)
go generate $(GO_MODULE_NAME)/$*
# ---- LINT ----
.PHONY: lint
lint: lint-client lint-server
.PHONY: lint-client
lint-client: $(CLIENT_INSTALL_DIR) $(CLIENT_CODEGEN_DIR)
cd static && npm run lint
.PHONY: lint-server
lint-server: $(CODEGEN_FILES)
mkdir -p $(ROOT_BUILD_DIR)
go vet ./...
go run github.com/mgechev/revive -config=revive.toml ./... > $(ROOT_BUILD_DIR)/revive.golint
go run github.com/securego/gosec/v2/cmd/gosec -no-fail -fmt=sonarqube -out=$(ROOT_BUILD_DIR)/gosec.json -stdout ./...
# ---- CLEAN ----
.PHONY: clean
clean:
rm -rf $(ROOT_BUILD_DIR)
find . -type f -name "*.gen.go" -delete
rm -rf $(MOCKS_CODEGEN_DIR)
cd static && npm run clean
$(GO_ENV) go clean -i ./...
# ---- BUILD ----
$(ROOT_BUILD_DIR): $(BUILD_DIR)
$(CLIENT_BUILD_DIR): $(CLIENT_INSTALL_DIR) $(CLIENT_CODEGEN_DIR) $(CLIENT_FILES)
rm -rf $@ && cd static && npm run build
$(BUILD_DIR): $(BUILD_DIR)/gomp $(BUILD_DIR)/db/migrations $(BUILD_DIR)/static
$(BUILD_DIR)/db/migrations: $(DB_MIGRATION_FILES)
rm -rf $@ && mkdir -p $@ && cp -R db/migrations/* $@
$(BUILD_DIR)/static: $(CLIENT_BUILD_DIR)
rm -rf $@ && mkdir -p $@ && cp -R $</* $@
$(BUILD_DIR)/gomp: go.mod $(CODEGEN_FILES) $(GO_FILES)
$(GO_ENV) go build -o $@ $(GO_LD_FLAGS)
# ---- TEST ----
.PHONY: test
test: $(ROOT_BUILD_DIR)/coverage/server $(ROOT_BUILD_DIR)/coverage/client
$(ROOT_BUILD_DIR)/coverage/server: go.mod $(CODEGEN_FILES) $(GO_FILES)
rm -rf $@
mkdir -p $@
go test -coverprofile=$@/coverage.out -coverpkg=./... -json > $@/results.json ./...
sed -i '/^.\+\.gen\.go.\+$$/d' $@/coverage.out
go tool cover -html=$@/coverage.out -o $@/coverage.html
$(ROOT_BUILD_DIR)/coverage/client: $(CLIENT_FILES) $(CLIENT_CODEGEN_DIR)
rm -rf $@
mkdir -p $@
cd static && npm run cover
cp -r static/coverage/* $@
# ---- ARCHIVE ----
.PHONY: archive
archive: $(ROOT_BUILD_DIR)/gomp-$(TARGETOS)-$(TARGETARCH)$(ARCHIVE_SUFFIX).tar.gz
$(ROOT_BUILD_DIR)/gomp-$(TARGETOS)-$(TARGETARCH)$(ARCHIVE_SUFFIX).tar.gz: $(BUILD_DIR)
tar -C $< -zcf $@ .
# ---- DOCKER ----
.PHONY: docker
# This make target does not directly require any other targets,
# and assumes that the required archives are already present,
# so that it can be used in an optimized way in the github actions workflow.
docker:
docker buildx build --build-arg ARCHIVE_SUFFIX=$(ARCHIVE_SUFFIX) $(DOCKER_ARGS) .