github.com/openimsdk/tools@v0.0.49/Makefile (about) 1 # Makefile 2 3 # Copyright 2023 OpenIM. All rights reserved. 4 # Use of this source code is governed by a MIT style 5 # license that can be found in the LICENSE file. 6 7 ###################################=> common commands <=############################################# 8 # ========================== Capture Environment =============================== 9 # get the repo root and output path 10 ROOT_PACKAGE=github.com/openimsdk/tools 11 OUT_DIR=$(REPO_ROOT)/_output 12 VERSION_PACKAGE=github.com/openimsdk/tools/version 13 # ============================================================================== 14 15 16 # define the default goal 17 # 18 19 SHELL := /bin/bash 20 DIRS=$(shell ls) 21 GO=go 22 23 .DEFAULT_GOAL := help 24 25 # include the common makefile 26 COMMON_SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST))) 27 # ROOT_DIR: root directory of the code base 28 ifeq ($(origin ROOT_DIR),undefined) 29 ROOT_DIR := $(abspath $(shell cd $(COMMON_SELF_DIR)/. && pwd -P)) 30 endif 31 # OUTPUT_DIR: The directory where the build output is stored. 32 ifeq ($(origin OUTPUT_DIR),undefined) 33 OUTPUT_DIR := $(ROOT_DIR)/_output 34 $(shell mkdir -p $(OUTPUT_DIR)) 35 endif 36 37 # BIN_DIR: The directory where the build output is stored. 38 ifeq ($(origin BIN_DIR),undefined) 39 BIN_DIR := $(OUTPUT_DIR)/bin 40 $(shell mkdir -p $(BIN_DIR)) 41 endif 42 43 ifeq ($(origin TOOLS_DIR),undefined) 44 TOOLS_DIR := $(OUTPUT_DIR)/tools 45 $(shell mkdir -p $(TOOLS_DIR)) 46 endif 47 48 ifeq ($(origin TMP_DIR),undefined) 49 TMP_DIR := $(OUTPUT_DIR)/tmp 50 $(shell mkdir -p $(TMP_DIR)) 51 endif 52 53 ifeq ($(origin VERSION), undefined) 54 VERSION := $(shell git describe --tags --always --match="v*" --dirty | sed 's/-/./g') #v2.3.3.631.g00abdc9b.dirty 55 endif 56 57 # Check if the tree is dirty. default to dirty(maybe u should commit?) 58 GIT_TREE_STATE:="dirty" 59 ifeq (, $(shell git status --porcelain 2>/dev/null)) 60 GIT_TREE_STATE="clean" 61 endif 62 GIT_COMMIT:=$(shell git rev-parse HEAD) 63 64 # Define the directory you want to copyright 65 CODE_DIRS := $(ROOT_DIR)/ #$(ROOT_DIR)/pkg $(ROOT_DIR)/core $(ROOT_DIR)/integrationtest $(ROOT_DIR)/lib $(ROOT_DIR)/mock $(ROOT_DIR)/db $(ROOT_DIR)/openapi 66 FINDS := find $(CODE_DIRS) 67 68 ifndef V 69 MAKEFLAGS += --no-print-directory 70 endif 71 72 # Linux command settings 73 FIND := find . ! -path './image/*' ! -path './vendor/*' ! -path './bin/*' 74 XARGS := xargs -r 75 76 # ============================================================================== 77 LICENSE_TEMPLATE ?= $(ROOT_DIR)/LICENSE_TEMPLATES # Apache License 78 79 # COMMA: Concatenate multiple strings to form a list of strings 80 COMMA := , 81 # SPACE: Used to separate strings 82 SPACE := 83 # SPACE: Replace multiple consecutive Spaces with a single space 84 SPACE += 85 86 ## all: Build all the necessary targets. 87 .PHONY: all 88 all: copyright-verify tidy test 89 90 # Targets 91 .PHONY: release 92 release: release.verify release.ensure-tag 93 @scripts/release.sh 94 95 .PHONY: install.gsemver 96 release.verify: install.git-chglog install.github-release install.coscmd 97 98 .PHONY: release.tag 99 release.tag: install.gsemver release.ensure-tag 100 @git push origin `git describe --tags --abbrev=0` 101 102 .PHONY: release.ensure-tag 103 release.ensure-tag: install.gsemver 104 @scripts/ensure_tag.sh 105 106 ## tidy: tidy go.mod 107 .PHONY: tidy 108 tidy: 109 @$(GO) mod tidy 110 111 ## style: Code style -> fmt,vet,lint 112 .PHONY: style 113 style: fmt vet lint 114 115 ## fmt: Run go fmt against code. 116 .PHONY: fmt 117 fmt: 118 @$(GO) fmt ./... 119 120 ## vet: Run go vet against code. 121 .PHONY: vet 122 vet: 123 @$(GO) vet ./... 124 125 ## generate: Run go generate against code. 126 .PHONY: generate 127 generate: 128 @$(GO) generate ./... 129 130 ## lint: Run go lint against code. 131 .PHONY: lint 132 lint: tools.verify.golangci-lint 133 @echo "===========> Run golangci to lint source codes" 134 @$(TOOLS_DIR)/golangci-lint run -c $(ROOT_DIR)/.golangci.yml $(ROOT_DIR)/... 135 136 ## test: Run unit test 137 .PHONY: test 138 test: 139 @$(GO) test ./... 140 141 ## cover: Run unit tests with coverage and enforce a minimum coverage requirement. 142 .PHONY: cover 143 cover: 144 @echo "Running tests with coverage..." 145 @$(GO) test -coverprofile=coverage.out ./... 146 @echo "Checking coverage..." 147 @$(GO) tool cover -func=coverage.out | grep total: | awk '{print $$3}' | sed 's/%//g' | { \ 148 read coverage; \ 149 echo "Total coverage: $$coverage%"; \ 150 minCoverage=75; \ 151 if [ `echo "$$coverage < $$minCoverage" | bc` -eq 1 ]; then \ 152 echo "Coverage ($$coverage%) is below the minimum required ($$minCoverage%). Failing."; \ 153 exit 1; \ 154 else \ 155 echo "Coverage meets minimum requirement ($$minCoverage%)."; \ 156 fi; \ 157 } 158 159 ## copyright-verify: Validate boilerplate headers for assign files. 160 .PHONY: copyright-verify 161 copyright-verify: tools.verify.addlicense copyright-add 162 @echo "===========> Validate boilerplate headers for assign files starting in the $(ROOT_DIR) directory" 163 @$(TOOLS_DIR)/addlicense -v -check -ignore **/test/** -f $(LICENSE_TEMPLATE) $(CODE_DIRS) 164 @echo "===========> End of boilerplate headers check..." 165 166 ## copyright-add: Add the boilerplate headers for all files. 167 .PHONY: copyright-add 168 copyright-add: tools.verify.addlicense 169 @echo "===========> Adding $(LICENSE_TEMPLATE) the boilerplate headers for all files" 170 @$(TOOLS_DIR)/addlicense -y $(shell date +"%Y") -v -c "OpenIM open source community." -f $(LICENSE_TEMPLATE) $(CODE_DIRS) 171 @echo "===========> End the copyright is added..." 172 173 174 ## help: Show this help info. 175 .PHONY: help 176 help: Makefile 177 @printf "\n\033[1mUsage: make <TARGETS> ...\033[0m\n\n\\033[1mTargets:\\033[0m\n\n" 178 @sed -n 's/^##//p' $< | awk -F':' '{printf "\033[36m%-28s\033[0m %s\n", $$1, $$2}' | sed -e 's/^/ /' 179 180 ######################################=> common tools<= ############################################ 181 # tools 182 183 BUILD_TOOLS ?= go-gitlint golangci-lint goimports addlicense deepcopy-gen conversion-gen ginkgo go-junit-report 184 185 ## tools.verify.%: Check if a tool is installed and install it 186 .PHONY: tools.verify.% 187 tools.verify.%: 188 @echo "===========> Verifying $* is installed" 189 @if [ ! -f $(TOOLS_DIR)/$* ]; then GOBIN=$(TOOLS_DIR) $(MAKE) tools.install.$*; fi 190 @echo "===========> $* is install in $(TOOLS_DIR)/$*" 191 192 # tools: Install a must tools 193 .PHONY: tools 194 tools: $(addprefix tools.verify., $(BUILD_TOOLS)) 195 196 # tools.install.%: Install a single tool in $GOBIN/ 197 .PHONY: tools.install.% 198 tools.install.%: 199 @echo "===========> Installing $,The default installation path is $(GOBIN)/$*" 200 @$(MAKE) install.$* 201 202 .PHONY: install.golangci-lint 203 install.golangci-lint: 204 @$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest 205 206 .PHONY: install.goimports 207 install.goimports: 208 @$(GO) install golang.org/x/tools/cmd/goimports@latest 209 210 .PHONY: install.addlicense 211 install.addlicense: 212 @$(GO) install github.com/google/addlicense@latest 213 214 .PHONY: install.ginkgo 215 install.ginkgo: 216 @$(GO) install github.com/onsi/ginkgo/ginkgo@v1.16.2 217 218 .PHONY: install.go-gitlint 219 # wget -P _output/tools/ https://openim-1306374445.cos.ap-guangzhou.myqcloud.com/openim/tools/go-gitlint 220 # go install github.com/antham/go-gitlint/cmd/gitlint@latest 221 install.go-gitlint: 222 @wget -q https://openim-1306374445.cos.ap-guangzhou.myqcloud.com/openim/tools/go-gitlint -O ${TOOLS_DIR}/go-gitlint 223 @chmod +x ${TOOLS_DIR}/go-gitlint 224 225 .PHONY: install.go-junit-report 226 install.go-junit-report: 227 @$(GO) install github.com/jstemmer/go-junit-report@latest 228 229 # ============================================================================== 230 # Tools that might be used include go gvm, cos 231 # 232 233 ## install.gsemver: Install gsemver, used to generate semver 234 .PHONY: install.gsemver 235 install.gsemver: 236 @$(GO) install github.com/arnaud-deprez/gsemver@latest 237 238 ## install.git-chglog: Install git-chglog, used to generate changelog 239 .PHONY: install.git-chglog 240 install.git-chglog: 241 @$(GO) install github.com/git-chglog/git-chglog/cmd/git-chglog@latest 242 243 ## install.github-release: Install github-release, used to create github release 244 .PHONY: install.github-release 245 install.github-release: 246 @$(GO) install github.com/github-release/github-release@latest 247 248 ## install.coscli: Install coscli, used to upload files to cos 249 # example: ./coscli cp/sync -r /root/workspaces/kubecub/tools/ cos://kubecub-1306374445/code/ -e cos.ap-hongkong.myqcloud.com 250 # https://cloud.tencent.com/document/product/436/71763 251 # kubecub/* 252 # - code/ 253 # - docs/ 254 # - images/ 255 # - scripts/ 256 .PHONY: install.coscli 257 install.coscli: 258 @wget -q https://github.com/tencentyun/coscli/releases/download/v0.13.0-beta/coscli-linux -O ${TOOLS_DIR}/coscli 259 @chmod +x ${TOOLS_DIR}/coscli 260 261 ## install.coscmd: Install coscmd, used to upload files to cos 262 .PHONY: install.coscmd 263 install.coscmd: 264 @if which pip &>/dev/null; then pip install coscmd; else pip3 install coscmd; fi 265 266 ## install.delve: Install delve, used to debug go program 267 .PHONY: install.delve 268 install.delve: 269 @$(GO) install github.com/go-delve/delve/cmd/dlv@latest 270 271 ## install.air: Install air, used to hot reload go program 272 .PHONY: install.air 273 install.air: 274 @$(GO) install github.com/cosmtrek/air@latest 275 276 ## install.gvm: Install gvm, gvm is a Go version manager, built on top of the official go tool. 277 .PHONY: install.gvm 278 install.gvm: 279 @echo "===========> Installing gvm,The default installation path is ~/.gvm/scripts/gvm" 280 @bash < <(curl -s -S -L https://raw.gitee.com/moovweb/gvm/master/binscripts/gvm-installer) 281 @$(shell source /root/.gvm/scripts/gvm) 282 283 ## install.go-mod-outdated: Install go-mod-outdated, used to check outdated dependencies 284 .PHONY: install.go-mod-outdated 285 install.go-mod-outdated: 286 @$(GO) install github.com/psampaz/go-mod-outdated@latest 287 288 ## install.mockgen: Install mockgen, used to generate mock functions 289 .PHONY: install.mockgen 290 install.mockgen: 291 @$(GO) install github.com/golang/mock/mockgen@latest 292 293 ## install.gotests: Install gotests, used to generate test functions 294 .PHONY: install.gotests 295 install.gotests: 296 @$(GO) install github.com/cweill/gotests/gotests@latest 297 298 ## install.cfssl: Install cfssl, used to generate certificates 299 .PHONY: install.cfssl 300 install.cfssl: 301 @$(ROOT_DIR)/scripts/install/install.sh OpenIM::install::install_cfssl 302 303 ## install.depth: Install depth, used to check dependency tree 304 .PHONY: install.depth 305 install.depth: 306 @$(GO) install github.com/KyleBanks/depth/cmd/depth@latest 307 308 ## install.go-callvis: Install go-callvis, used to visualize call graph 309 .PHONY: install.go-callvis 310 install.go-callvis: 311 @$(GO) install github.com/ofabry/go-callvis@latest