github.com/kick-project/maker@v1.1.1-0.20211031110251-7b74922fa493/Makefile (about) 1 SHELL = /bin/bash 2 3 MAKEFLAGS += --no-print-directory 4 5 $(shell test -e "$$(go env GOPATH)/bin/maker" || go install ./cmd/maker) 6 7 # Project 8 NAME := maker 9 GOPATH := $(shell go env GOPATH) 10 VERSION ?= $(shell cat VERSION) 11 COMMIT := $(shell test -d .git && git rev-parse --short HEAD) 12 BUILD_INFO := $(COMMIT)-$(shell date -u +"%Y%m%d-%H%M%SZ") 13 HASCMD := $(shell test -d cmd && echo "true") 14 GOOS ?= $(shell uname | tr '[:upper:]' '[:lower:]') 15 GOARCH ?= $(shell uname -m | sed 's/x86_64/amd64/; s/i386/386/') 16 ARCH = $(shell uname -m) 17 18 ISRELEASED := $(shell git show-ref v$(cat VERSION) 2>&1 > /dev/null && echo "true") 19 20 # Utilities 21 # Default environment variables. 22 # Any variables already set will override the values in this file(s). 23 DOTENV := godotenv -f $(HOME)/.env,.env 24 25 # Python 26 PYTHON ?= $(shell command -v python3 python|head -n1) 27 28 # Variables 29 ROOT = $(shell pwd) 30 31 # Go 32 GOMODOPTS = GO111MODULE=on 33 GOGETOPTS = GO111MODULE=off 34 GOFILES := $(shell find cmd pkg internal src -name '*.go' 2> /dev/null) 35 GODIRS = $(shell find . -maxdepth 1 -mindepth 1 -type d | egrep 'cmd|internal|pkg|api') 36 37 # 38 # End user targets 39 # 40 41 ### HELP 42 43 .PHONY: help 44 help: ## Print Help 45 @maker --menu Makefile 46 47 ### DEVELOPMENT 48 49 .PHONY: _build 50 _build: ## Build binary 51 @test -d .cache || go fmt ./... 52 ifeq ($(XCOMPILE),true) 53 GOOS=linux GOARCH=amd64 $(MAKE) dist/$(NAME)_linux_amd64/$(NAME) 54 GOOS=darwin GOARCH=amd64 $(MAKE) dist/$(NAME)_darwin_amd64/$(NAME) 55 GOOS=windows GOARCH=amd64 $(MAKE) dist/$(NAME)_windows_amd64/$(NAME).exe 56 endif 57 ifeq ($(HASCMD),true) 58 @$(MAKE) $(NAME) 59 endif 60 61 .PHONY: _install 62 _install: $(GOPATH)/bin/$(NAME) ## Install to $(GOPATH)/bin 63 64 .PHONY: clean 65 clean: ## Reset project to original state 66 rm -rf .cache maker dist reports tmp vendor nfpm.yaml 67 68 .PHONY: test 69 test: ## Test 70 $(MAKE) test_setup 71 $(MAKE) goversion 72 $(MAKE) lint 73 $(MAKE) unit 74 $(MAKE) cx 75 $(MAKE) cc 76 @# Combined the return codes of all the tests 77 @echo "Exit codes, unit tests: $(cat reports/exitcode-unit.txt), golangci-lint: $(cat reports/exitcode-golangci-lint.txt), golint: $(cat reports/exitcode-golint.txt)" 78 @exit $(( $(cat reports/exitcode-unit.txt) + $(cat reports/exitcode-golangci-lint.txt) + $(cat reports/exitcode-golint.txt) )) 79 80 .PHONY: goversion 81 goversion: 82 @go version | grep go1.16 83 84 .PHONY: _unit 85 _unit: 86 ### Unit Tests 87 gotestsum --jsonfile reports/unit.json --junitfile reports/junit.xml -- -timeout 5s -covermode atomic -coverprofile=./reports/coverage.out -v ./...; echo $$? > reports/exitcode-unit.txt 88 @go-test-report -t "maker unit tests" -o reports/html/unit.html < reports/unit.json > /dev/null 89 90 .PHONY: _cc 91 _cc: 92 ### Code Coverage 93 @go-acc -o ./reports/coverage.out ./... > /dev/null 94 @go tool cover -func=./reports/coverage.out | tee reports/coverage.txt 95 @go tool cover -html=reports/coverage.out -o reports/html/coverage.html 96 97 .PHONY: _cx 98 _cx: 99 ### Cyclomatix Complexity Report 100 @gocyclo -avg $(GODIRS) | grep -v _test.go | tee reports/cyclomaticcomplexity.txt 101 @contents=$$(cat reports/cyclomaticcomplexity.txt); echo "<html><title>cyclomatic complexity</title><body><pre>$${contents}</pre></body><html>" > reports/html/cyclomaticcomplexity.html 102 103 .PHONY: _package 104 _package: ## Create an RPM, Deb, Homebrew package 105 @XCOMPILE=true make build 106 @VERSION=$(VERSION) envsubst < nfpm.yaml.in > nfpm.yaml 107 $(MAKE) dist/maker.rb 108 $(MAKE) dist/maker-$(VERSION).rb 109 $(MAKE) tmp/maker.rb 110 $(MAKE) dist/$(NAME)-$(VERSION).$(ARCH).rpm 111 $(MAKE) dist/$(NAME)_$(VERSION)_$(GOARCH).deb 112 113 .PHONY: _test_setup 114 _test_setup: 115 @mkdir -p tmp 116 @mkdir -p reports/html 117 118 .PHONY: _release 119 _release: ## Trigger a release by creating a tag and pushing to the upstream repository 120 @echo "### Releasing v$(VERSION)" 121 @$(MAKE) _isreleased 2> /dev/null 122 git tag v$(VERSION) 123 git push --tags 124 125 # To be run inside a github workflow 126 .PHONY: _release_github 127 _release_github: _package 128 gh release create v$(VERSION) 129 gh release upload v$(VERSION) dist/maker-$(VERSION).tar.gz 130 gh release upload v$(VERSION) dist/maker.rb 131 gh release upload v$(VERSION) dist/maker-$(VERSION).rb 132 gh release upload v$(VERSION) dist/maker-$(VERSION).x86_64.rpm 133 gh release upload v$(VERSION) dist/maker_$(VERSION)_amd64.deb 134 135 .PHONY: lint 136 lint: internal/version.go 137 golangci-lint run --enable=gocyclo; echo $$? > reports/exitcode-golangci-lint.txt 138 golint -set_exit_status ./..; echo $$? > reports/exitcode-golint.txt 139 140 .PHONY: tag 141 tag: 142 git fetch --tags 143 git tag v$(VERSION) 144 git push --tags 145 146 .PHONY: deps 147 deps: go.mod ## Install build dependencies 148 $(GOMODOPTS) go mod tidy 149 $(GOMODOPTS) go mod download 150 151 .PHONY: depsdev 152 depsdev: ## Install development dependencies 153 ifeq ($(USEGITLAB),true) 154 @mkdir -p $(ROOT)/.cache/{go,gomod} 155 endif 156 cat .installs.txt | egrep -v '^#' | xargs -t -n1 go install 157 158 .PHONY: report 159 report: ## Open reports generated by "make test" in a browser 160 @$(MAKE) $(REPORTS) 161 162 ### VERSION INCREMENT 163 164 .PHONY: bumpmajor 165 bumpmajor: ## Increment VERSION file ${major}.0.0 - major bump 166 git fetch --tags 167 versionbump --checktags major VERSION 168 $(MAKE) internal/version.go 169 $(MAKE) nfpm.yaml 170 171 .PHONY: bumpminor 172 bumpminor: ## Increment VERSION file 0.${minor}.0 - minor bump 173 git fetch --tags 174 versionbump --checktags minor VERSION 175 $(MAKE) internal/version.go 176 $(MAKE) nfpm.yaml 177 178 .PHONY: bumppatch 179 bumppatch: ## Increment VERSION file 0.0.${patch} - patch bump 180 git fetch --tags 181 versionbump --checktags patch VERSION 182 $(MAKE) internal/version.go 183 $(MAKE) nfpm.yaml 184 185 .PHONY: getversion 186 getversion: 187 VERSION=$(VERSION) bash -c 'echo $$VERSION' 188 189 # 190 # Helper targets 191 # 192 $(GOPATH)/bin/$(NAME): $(NAME) 193 install -m 755 $(NAME) $(GOPATH)/bin/$(NAME) 194 195 # Open html reports 196 REPORTS = reports/html/unit.html reports/html/coverage.html reports/html/cyclomaticcomplexity.html 197 .PHONY: $(REPORTS) 198 $(REPORTS): 199 ifeq ($(GOOS),darwin) 200 @test -f $@ && open $@ 201 else ifeq ($(GOOS),linux) 202 @test -f $@ && xdg-open $@ 203 endif 204 205 # Check versionbump 206 .PHONY: _isreleased 207 _isreleased: 208 ifeq ($(ISRELEASED),true) 209 @echo "Version $(VERSION) has been released." 210 @echo "Please bump with 'make bump(minor|patch|major)' depending on breaking changes." 211 @exit 1 212 endif 213 214 # 215 # File targets 216 # 217 $(NAME): dist/$(NAME)_$(GOOS)_$(GOARCH)/$(NAME) 218 install -m 755 $< $@ 219 220 dist/$(NAME)_$(GOOS)_$(GOARCH)/$(NAME) dist/$(NAME)_$(GOOS)_$(GOARCH)/$(NAME).exe: $(GOFILES) internal/version.go 221 @mkdir -p $$(dirname $@) 222 go build -o $@ ./cmd/maker 223 224 dist/$(NAME)-$(VERSION).$(ARCH).rpm: dist/$(NAME)_$(GOOS)_$(GOARCH)/$(NAME) 225 @mkdir -p $$(dirname $@) 226 @$(MAKE) nfpm.yaml 227 nfpm pkg --packager rpm --target dist/ 228 229 dist/$(NAME)_$(VERSION)_$(GOARCH).deb: dist/$(NAME)_$(GOOS)_$(GOARCH)/$(NAME) 230 @mkdir -p $$(dirname $@) 231 @$(MAKE) nfpm.yaml 232 nfpm pkg --packager deb --target dist/ 233 234 internal/version.go: internal/version.go.in VERSION 235 @VERSION=$(VERSION) $(DOTENV) envsubst < $< > $@ 236 237 dist/maker-$(VERSION).rb: dist/maker.rb 238 @cp $< $@ 239 240 dist/maker.rb: maker.rb.in dist/maker-$(VERSION).tar.gz 241 @BASEURL=https://github.com/kick-project/maker/releases/download/v$(VERSION) VERSION=$(VERSION) SHA256=$$(sha256sum dist/maker-$(VERSION).tar.gz | awk '{print $$1}') $(DOTENV) envsubst < $< > $@ 242 243 tmp/maker.rb: maker.rb.in dist/maker-$(VERSION).tar.gz 244 @mkdir -p tmp 245 @BASEURL="file://$(PWD)/dist" VERSION=$(VERSION) SHA256=$$(sha256sum dist/maker-$(VERSION).tar.gz | awk '{print $$1}') $(DOTENV) envsubst < $< > $@ 246 247 nfpm.yaml: nfpm.yaml.in VERSION 248 @VERSION=$(VERSION) $(DOTENV) envsubst < $< > $@ 249 250 dist/maker-$(VERSION).tar.gz: $(GOFILES) 251 tar -zcf dist/maker-$(VERSION).tar.gz $$(find . \( -path ./test -prune -o -path ./tmp \) -prune -false -o \( -name go.mod -o -name go.sum -o -name \*.go \)) 252 253 go.mod: 254 @$(DOTENV) $(MAKE) _go.mod 255 256 _go.mod: 257 ifndef GOSERVER 258 @$(MAKE) _go.mod_err 259 else ifndef GOGROUP 260 @$(MAKE) _go.mod_err 261 endif 262 go mod init $(GOSERVER)/$(GOGROUP)/$(NAME) 263 @$(MAKE) deps 264 265 _go.mod_err: 266 @echo 'Please run "go mod init server.com/group/project"' 267 @echo 'Alternatively set "GOSERVER=$YOURSERVER" and "GOGROUP=$YOURGROUP" in ~/.env or $(ROOT)/.env file' 268 @exit 1 269 270 # 271 # make wrapper - Execute any target target prefixed with a underscore. 272 # EG 'make vmcreate' will result in the execution of 'make _vmcreate' 273 # 274 %: 275 @maker $@