-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
132 lines (113 loc) · 4.63 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
MODULE_NAME := myapp
SRC := $(shell find . -name '*.go')
BUILD_DIR := ./build
BINARY_NAME := $(BUILD_DIR)/$(MODULE_NAME)
SONAR_HOST_URL := https://sonarcloud.io
SONAR_SECRET := $(shell cat .sonar.secret)
BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD)
CHANGE_TARGET := $(shell git rev-parse --abbrev-ref --symbolic-full-name @{u} | sed 's/.*\///')
# CHANGE_ID := $(shell git rev-parse --short=8 HEAD)
CHANGE_ID := $(shell whoami)
ifeq ($(OS), Windows_NT)
SHELL := powershell.exe
.SHELLFLAGS := -NoProfile -Command
SHELL_VERSION = $(shell (Get-Host | Select-Object Version | Format-Table -HideTableHeaders | Out-String).Trim())
OS = $(shell "{0} {1}" -f "windows", (Get-ComputerInfo -Property OsVersion, OsArchitecture | Format-Table -HideTableHeaders | Out-String).Trim())
PACKAGE = $(shell (Get-Content go.mod -head 1).Split(" ")[1])
CHECK_DIR_CMD = if (!(Test-Path $@)) { $$e = [char]27; Write-Error "$$e[31mDirectory $@ doesn't exist$${e}[0m" }
HELP_CMD = Select-String "^[a-zA-Z_-]+:.*?\#\# .*$$" "./Makefile" | Foreach-Object { $$_data = $$_.matches -split ":.*?\#\# "; $$obj = New-Object PSCustomObject; Add-Member -InputObject $$obj -NotePropertyName ('Command') -NotePropertyValue $$_data[0]; Add-Member -InputObject $$obj -NotePropertyName ('Description') -NotePropertyValue $$_data[1]; $$obj } | Format-Table -HideTableHeaders @{Expression={ $$e = [char]27; "$$e[36m$$($$_.Command)$${e}[0m" }}, Description
RM_F_CMD = Remove-Item -erroraction silentlycontinue -Force
RM_RF_CMD = ${RM_F_CMD} -Recurse
SERVER_BIN = ${SERVER_DIR}.exe
CLIENT_BIN = ${CLIENT_DIR}.exe
else
SHELL := bash
SHELL_VERSION = $(shell echo $$BASH_VERSION)
UNAME := $(shell uname -s)
VERSION_AND_ARCH = $(shell uname -rm)
ifeq ($(UNAME),Darwin)
OS = macos ${VERSION_AND_ARCH}
else ifeq ($(UNAME),Linux)
OS = linux ${VERSION_AND_ARCH}
else
$(error OS not supported by this Makefile)
endif
PACKAGE = $(shell head -1 go.mod | awk '{print $$2}')
CHECK_DIR_CMD = test -d $@ || (echo "\033[31mDirectory $@ doesn't exist\033[0m" && false)
HELP_CMD = grep -E '^[a-zA-Z_-]+:.*?\#\# .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?\#\# "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
RM_F_CMD = rm -f
RM_RF_CMD = ${RM_F_CMD} -r
SERVER_BIN = ${SERVER_DIR}
CLIENT_BIN = ${CLIENT_DIR}
endif
.DEFAULT_GOAL := help
.PHONY: all
build: ## Build the binary
go build -v -ldflags="-X 'version.Version=v1.0.0' -X 'version.GitCommit=$(shell git rev-parse --short=8 HEAD)' -X 'build.User=$(shell id -u -n)' -X 'build.Time=$(shell date)'" -o $(BINARY_NAME)
clean: ## Clean build files
go clean
rm -f $(BINARY_NAME)
unit-test: ## Run unit tests
@echo "Running unit tests"
rm -f unit-test-coverage.out && \
go test -v $(shell go list ./... | grep -v /test) \
-count=1 \
-cover \
-coverpkg=./... \
-coverprofile=./unit-test-coverage.out
api-test: ## Run api tests
@echo "Running api tests"
rm -f api-test-coverage.out && \
go test -v ./test/ \
-count=1 \
-cover \
-coverpkg=./... \
-coverprofile=./interface-test-coverage.out
cov-html: ## Generate coverage html
go tool cover -html=api-test-coverage.out -html=unit-test-coverage.out -o merged-coverage.html
sonarqube-pr: ## Run sonarqube analysis for pull request
rm -rf .scannerwork && \
sonar-scanner \
-Dsonar.host.url="$(SONAR_HOST_URL)" \
-Dsonar.working.directory=".scannerwork" \
-Dsonar.pullrequest.key="$(CHANGE_ID)" \
-Dsonar.pullrequest.branch="$(BRANCH_NAME)" \
-Dsonar.pullrequest.base="$(CHANGE_TARGET)" \
-Dsonar.login="$(SONAR_SECRET)"
sonarqube-branch: ## Run sonarqube analysis on branch
rm -rf .scannerwork && \
sonar-scanner \
-Dsonar.host.url="$(SONAR_HOST_URL)" \
-Dsonar.working.directory=".scannerwork" \
-Dsonar.branch.name="$(BRANCH_NAME)" \
-Dsonar.login="$(SONAR_SECRET)"
coverage: ## Run unit and api tests with coverage
make unit-test && make api-test
vet: ## Run go vet
go vet $(SRC)
lint: ## Run golint
go get golang.org/x/lint/golint
$(GOPATH)/bin/golint ./...
pb: ## Generate protobuf files
protoc -I proto/ proto/*.proto \
--go_out=proto \
--go-grpc_out=proto \
--go_opt=paths=source_relative \
--go-grpc_opt=paths=source_relative
pb-with-module: ## Generate protobuf files with module
protoc -I proto proto/*.proto \
--go_out=proto \
--go_opt=module=${PACKAGE} \
--go-grpc_out=proto \
--go-grpc_opt=module=${PACKAGE} \
bump: all ## Update packages version
go get -u ./...
about: ## Display info related to the build
@echo "OS: ${OS}"
@echo "Shell: ${SHELL} ${SHELL_VERSION}"
@echo "Protoc version: $(shell protoc --version)"
@echo "Go version: $(shell go version)"
@echo "Go package: ${PACKAGE}"
@echo "Openssl version: $(shell openssl version)"
help: ## Show this help
@${HELP_CMD}