forked from google/go-licenses
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMakefile
141 lines (116 loc) · 4.7 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
TMP = ./.tmp
RESULTS = $(TMP)/results
ASSETS = assets
DBASSET = $(ASSETS)/licenses.db
BIN = $(abspath $(TMP)/bin)
COVER_REPORT = $(RESULTS)/cover.report
COVER_TOTAL = $(RESULTS)/cover.total
LINTCMD = $(BIN)/golangci-lint run --tests=false --config .golangci.yaml
# Color definitions with checks for OS
ifeq ($(OS),Windows_NT)
BOLD := ""
PURPLE := ""
GREEN := ""
CYAN := ""
RED := ""
RESET := ""
else
BOLD := $(shell tput -T linux bold)
PURPLE := $(shell tput -T linux setaf 5)
GREEN := $(shell tput -T linux setaf 2)
CYAN := $(shell tput -T linux setaf 6)
RED := $(shell tput -T linux setaf 1)
RESET := $(shell tput -T linux sgr0)
endif
TITLE := $(BOLD)$(PURPLE)
SUCCESS := $(BOLD)$(GREEN)
COVERAGE_THRESHOLD := 55
RELEASE_CMD=$(BIN)/goreleaser --rm-dist
ifndef TMP
$(error TMP is not set)
endif
ifndef BIN
$(error BIN is not set)
endif
define title
@printf '$(TITLE)$(1)$(RESET)\n'
endef
.PHONY: all bootstrap lint lint-fix unit coverage help test clean
all: lint test ## Run all checks (linting, unit tests, and integration tests)
@printf '$(SUCCESS)All checks pass!$(RESET)\n'
test: unit ## Run all tests (currently only unit)
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "$(BOLD)$(CYAN)%-25s$(RESET)%s\n", $$1, $$2}'
bootstrap: ## Download and install all project dependencies (+ prep tooling in the ./.tmp dir)
$(call title,Downloading dependencies)
mkdir -p $(TMP) $(RESULTS) $(BIN) # Create necessary directories
go mod download
cat tools.go | grep _ | awk -F'"' '{print $$2}' | xargs -tI % env GOBIN=$(BIN) go install %
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BIN) v1.47.2
cd $(TMP) && curl -sLO https://github.com/markbates/pkger/releases/download/v0.17.0/pkger_0.17.0_$(shell uname)_x86_64.tar.gz && \
tar -xzvf pkger_0.17.0_$(shell uname)_x86_64.tar.gz pkger && \
mv pkger $(BIN)
GOBIN=$(BIN) go install github.com/goreleaser/goreleaser@v1.3.1
$(DBASSET):
$(call title,Building assets)
mkdir -p $(ASSETS)
$(BIN)/license_serializer -output $(ASSETS)
pkged.go: $(DBASSET)
$(BIN)/pkger
lint: ## Run gofmt + golangci lint checks
$(call title,Running linters)
@printf "files with gofmt issues: [$(shell gofmt -l -s .)]\n"
@test -z "$(shell gofmt -l -s .)"
$(LINTCMD)
lint-fix: ## Auto-format all source code + run golangci lint fixers
$(call title,Running lint fixers)
gofmt -w -s .
$(LINTCMD) --fix
unit: ## Run unit tests (with coverage)
$(call title,Running unit tests)
go test -coverprofile $(COVER_REPORT) ./...
@go tool cover -func $(COVER_REPORT) | grep total | awk '{print substr($$3, 1, length($$3)-1)}' > $(COVER_TOTAL)
@echo "Coverage: $$(cat $(COVER_TOTAL))"
@if [ $$(echo "$$(cat $(COVER_TOTAL)) >= $(COVERAGE_THRESHOLD)" | bc -l) -ne 1 ]; then \
echo "$(RED)$(BOLD)Failed coverage quality gate (> $(COVERAGE_THRESHOLD)%)$(RESET)" && false; \
fi
ci-build-snapshot-packages: pkged.go
$(RELEASE_CMD) \
--snapshot \
--skip-publish
ci-plugs-out-test:
docker run \
-v //var/run/docker.sock://var/run/docker.sock \
-v ${PWD}://src \
-w //src \
golang:latest \
/bin/bash -x -c "\
./dist/linux-build_linux_amd64/golicenses version && \
./dist/linux-build_linux_amd64/golicenses list github.com/khulnasoft/go-licenses && \
./dist/linux-build_linux_amd64/golicenses check github.com/khulnasoft/go-licenses \
"
ci-test-linux-run:
chmod 755 ./dist/linux-build_linux_amd64/golicenses && \
./dist/linux-build_linux_amd64/golicenses version && \
./dist/linux-build_linux_amd64/golicenses list github.com/khulnasoft/go-licenses
ci-test-linux-arm-run:
chmod 755 ./dist/linux-build_linux_arm64/golicenses && \
./dist/linux-build_linux_arm64/golicenses version && \
./dist/linux-build_linux_arm64/golicenses list github.com/khulnasoft/go-licenses
ci-test-mac-run:
chmod 755 ./dist/darwin-build_darwin_amd64/golicenses && \
./dist/darwin-build_darwin_amd64/golicenses version && \
./dist/darwin-build_darwin_amd64/golicenses list github.com/khulnasoft/go-licenses
ci-test-mac-arm-run:
chmod 755 ./dist/darwin-build_darwin_arm64/golicenses && \
./dist/darwin-build_darwin_arm64/golicenses version && \
./dist/darwin-build_darwin_arm64/golicenses list github.com/khulnasoft/go-licenses
ci-test-windows-run:
@echo "Running Windows tests..."
@powershell -Command "Start-Process -NoNewWindow -File ./dist/windows-build_windows_amd64/golicenses.exe -ArgumentList 'version' -Wait"
@powershell -Command "Start-Process -NoNewWindow -File ./dist/windows-build_windows_amd64/golicenses.exe -ArgumentList 'list github.com/khulnasoft/go-licenses' -Wait"
ci-release: pkged.go
$(BIN)/goreleaser --rm-dist
clean:
rm -rf dist
rm -rf .tmp