-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
89 lines (70 loc) · 2.61 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
OPENAPI_FILE = ./doc/openapi.yaml
GENERATED_API_SERVER = api/server.gen.go
TOPDIR := $(strip $(dir $(realpath $(lastword $(MAKEFILE_LIST)))))
CGO_ENABLED ?= 0
ifneq (,$(wildcard $(TOPDIR)/.env))
include $(TOPDIR)/.env
export
endif
comma:= ,
empty:=
space:= $(empty) $(empty)
bold := $(shell tput bold)
green := $(shell tput setaf 2)
sgr0 := $(shell tput sgr0)
MODULE_NAME := $(shell go list -m)
PLATFORM ?= $(platform)
ifneq ($(PLATFORM),)
GOOS := $(or $(word 1, $(subst /, ,$(PLATFORM))),$(shell go env GOOS))
GOARCH := $(or $(word 2, $(subst /, ,$(PLATFORM))),$(shell go env GOARCH))
endif
BIN_SUFFIX :=
ifneq ($(or $(GOOS),$(GOARCH)),)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
BIN_SUFFIX := $(BIN_SUFFIX)-$(GOOS)-$(GOARCH)
endif
ifeq ($(GOOS),windows)
BIN_SUFFIX := $(BIN_SUFFIX).exe
endif
APPS := $(patsubst app/%/,%,$(sort $(dir $(wildcard app/*/))))
GOFILES := $(shell find . -type f -name '*.go' -not -path '*/\.*' -not -path './app/*')
$(foreach app,$(APPS),\
$(eval GOFILES_$(app) := $(shell find ./app/$(app) -type f -name '*.go' -not -path '*/\.*')))
DOCFILES := $(shell find doc -type f -name '*.yaml' -not -name '*.gen.yaml')
SQLCFILES := $(shell find ./dbaccess/dbsqlc -type f -name '*.sql')
MIGRATIONFILES := $(shell find ./migrate/migration -type f -name '*.sql')
GENERATED_SQLCFILES := $(SQLCFILES:.sql=.sql.go)
.DEFAULT: all
.PHONY: all
all: $(APPS)
.PHONY: $(APPS)
$(APPS): %: bin/%$(BIN_SUFFIX)
.SECONDEXPANSION:
bin/%: $$(GOFILES) $$(GOFILES_$$(@F)) $$(ASSET_FILES) $(GENERATED_API_SERVER) $(GENERATED_SQLCFILES)
@printf "Building $(bold)$@$(sgr0) ... "
@go build -o ./bin/$(@F) ./app/$(@F:$(BIN_SUFFIX)=)
@printf "$(green)done$(sgr0)\n"
.PHONY: gosec
gosec: ## Run the golang security checker
@gosec -exclude-dir test/mock ./...
.PHONY: test
test: ## Run unit test
@go clean -testcache
@go test ./...
.PHONY: coverage
coverage:
@go clean -testcache
@go test -p 1 -v -cover -covermode=count -coverprofile=coverage.out ./...
@go tool cover -html coverage.out -o coverage.html
@go tool cover -func coverage.out | tail -n 1
.PHONY: generate
generate: $(GENERATED_API_SERVER) $(GENERATED_SQLCFILES)
$(GENERATED_API_SERVER): $(DOCFILES)
@echo "Generating server code from $(OPENAPI_FILE)..."
@mkdir -p $(@D)
@npx -y @redocly/openapi-cli@latest bundle ./doc/openapi.yaml -o ./doc/bundled.gen.yaml
@go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest --config=./api/server.cfg.yaml ./doc/bundled.gen.yaml
$(GENERATED_SQLCFILES): $(SQLCFILES) $(MIGRATIONFILES)
@echo "Generating SQLC code..."
@cd ./dbaccess/dbsqlc && go run github.com/sqlc-dev/sqlc/cmd/sqlc@v1.27.0 generate