1+ # /*
2+ # * Copyright 2024 The CNAI Authors
3+ # *
4+ # * Licensed under the Apache License, Version 2.0 (the "License");
5+ # * you may not use this file except in compliance with the License.
6+ # * You may obtain a copy of the License at
7+ # *
8+ # * http://www.apache.org/licenses/LICENSE-2.0
9+ # *
10+ # * Unless required by applicable law or agreed to in writing, software
11+ # * distributed under the License is distributed on an "AS IS" BASIS,
12+ # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ # * See the License for the specific language governing permissions and
14+ # * limitations under the License.
15+ # */
16+
17+ # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
18+ ifeq (,$(shell go env GOBIN) )
19+ GOBIN =$(shell go env GOPATH) /bin
20+ else
21+ GOBIN =$(shell go env GOBIN)
22+ endif
23+ # Setting SHELL to bash allows bash commands to be executed by recipes.
24+ # Options are set to exit when a recipe line exits non-zero or a piped command fails.
25+ SHELL = /usr/bin/env bash -o pipefail
26+ .SHELLFLAGS = -ec
27+
28+ .PHONY : all
29+ all : build
30+
31+ # #@ General
32+
33+ # The help target prints out all targets with their descriptions organized
34+ # beneath their categories. The categories are represented by '##@' and the
35+ # target descriptions by '##'. The awk command is responsible for reading the
36+ # entire set of makefiles included in this invocation, looking for lines of the
37+ # file as xyz: ## something, and then pretty-format the target and help. Then,
38+ # if there's a line with ##@ something, that gets pretty-printed as a category.
39+ # More info on the usage of ANSI control characters for terminal formatting:
40+ # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
41+ # More info on the awk command:
42+ # http://linuxcommand.org/lc3_adv_awk.php
43+
44+ .PHONY : help
45+ help : # # Display this help.
46+ @awk ' BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST )
47+
48+ .PHONY : fmt
49+ fmt : # # Run go fmt against code.
50+ go fmt ./...
51+
52+ .PHONY : vet
53+ vet : # # Run go vet against code.
54+ go vet ./...
55+
56+ .PHONY : test
57+ test : fmt vet
58+ go tool cover -func cover.out
59+
60+ GOLANGCI_LINT = $(shell pwd) /bin/golangci-lint
61+ GOLANGCI_LINT_VERSION ?= v1.54.2
62+ golangci-lint :
63+ @[ -f $( GOLANGCI_LINT) ] || { \
64+ set -e ; \
65+ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT ) ) $(GOLANGCI_LINT_VERSION ) ; \
66+ }
67+
68+ .PHONY : lint
69+ lint : golangci-lint # # Run golangci-lint linter & yamllint
70+ $(GOLANGCI_LINT ) run
71+
72+ .PHONY : lint-fix
73+ lint-fix : golangci-lint # # Run golangci-lint linter and perform fixes
74+
75+ # 检查goimports是否已安装
76+ GOIMPORTS := $(shell command -v goimports 2> /dev/null)
77+ ifeq ($(GOIMPORTS ) ,)
78+ GOIMPORTS_INSTALL = go install golang.org/x/tools/cmd/goimports@latest
79+ else
80+ GOIMPORTS_INSTALL =
81+ endif
82+
83+ $(GOIMPORTS_INSTALL ) :
84+ $(GOIMPORTS_INSTALL )
85+
86+ lint-fix : $(GOIMPORTS_INSTALL )
87+ $(GOIMPORTS ) -l -w .
88+ $(GOLANGCI_LINT ) run --fix
89+
90+ # #@ Build
91+
92+ .PHONY : build
93+ build : fmt vet
94+ go build -o output/modctl cmd/main.go
95+
96+ # # Location to install dependencies to
97+ LOCALBIN ?= $(shell pwd) /bin
98+ $(LOCALBIN ) :
99+ mkdir -p $(LOCALBIN )
100+
101+ .PHONY : gen
102+ gen : gen-mockery# # Generate all we need!
103+ echo " generating-openapi"
104+ GOBIN=$(LOCALBIN ) go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config ${SERVER_DIR} /api/cfg.yaml ${SERVER_DIR} /api/api.yaml
105+
106+ .PHONY : gen-mockery check-mockery install-mockery
107+ gen-mockery : check-mockery # # Generate mockery code
108+ echo " generating mockery code according to .mockery.yaml"
109+ mockery
110+
111+
112+ check-mockery :
113+ @which mockery > /dev/null || { echo " mockery not found. Trying to install via Homebrew..." ; install-mockery; }
114+
115+ install-mockery :
116+ @if command -v brew > /dev/null; then \
117+ echo " Installing mockery via Homebrew" ; \
118+ brew install mockery; \
119+ else \
120+ echo " Error: Homebrew is not installed. Please install Homebrew first and ensure it's in your PATH." ; \
121+ exit 1; \
122+ fi
0 commit comments