Files
full/apps/base/fts/Makefile

168 lines
4.7 KiB
Makefile
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# FTS Service Makefile
# 变量定义
APP_NAME := fts
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "v1.0.0")
BUILD_TIME := $(shell date +%Y-%m-%d\ %H:%M:%S)
GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
GO_VERSION := $(shell go version | awk '{print $$3}')
# 构建标志
LDFLAGS := -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT) -w -s"
BUILD_FLAGS := -trimpath $(LDFLAGS)
# 目录
BUILD_DIR := build
# Go环境变量
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
export GOPRIVATE=git.apinb.com/*
export GONOPROXY=git.apinb.com/*
export GOINSECURE=git.apinb.com/*
export GONOSUMDB=git.apinb.com/*
export GOEXPERIMENT=jsonv2
.PHONY: help
help: ## 显示帮助信息
@echo "FTS Service - 可用的命令:"
@echo ""
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
.PHONY: deps
deps: ## 下载依赖
@echo "下载Go依赖..."
go mod download
go mod tidy
.PHONY: build
build: deps ## 构建应用
@echo "构建应用..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(APP_NAME) ./cmd/main/main.go
@echo "构建完成: $(BUILD_DIR)/$(APP_NAME)"
.PHONY: build-linux
build-linux: deps ## 构建Linux版本
@echo "构建Linux版本..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(APP_NAME)-linux-amd64 ./cmd/main/main.go
@echo "构建完成: $(BUILD_DIR)/$(APP_NAME)-linux-amd64"
.PHONY: build-windows
build-windows: deps ## 构建Windows版本
@echo "构建Windows版本..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(APP_NAME)-windows-amd64.exe ./cmd/main/main.go
@echo "构建完成: $(BUILD_DIR)/$(APP_NAME)-windows-amd64.exe"
.PHONY: build-darwin
build-darwin: deps ## 构建macOS版本
@echo "构建macOS版本..."
@mkdir -p $(BUILD_DIR)
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build $(BUILD_FLAGS) -o $(BUILD_DIR)/$(APP_NAME)-darwin-amd64 ./cmd/main/main.go
@echo "构建完成: $(BUILD_DIR)/$(APP_NAME)-darwin-amd64"
.PHONY: build-all
build-all: build-linux build-windows build-darwin ## 构建所有平台版本
.PHONY: run
run: build ## 运行应用
@echo "启动应用..."
./$(BUILD_DIR)/$(APP_NAME)
.PHONY: dev
dev: ## 开发模式运行
@echo "开发模式启动..."
go run ./cmd/main/main.go
.PHONY: test
test: ## 运行测试
@echo "运行测试..."
go test -v ./...
.PHONY: test-coverage
test-coverage: ## 运行测试并生成覆盖率报告
@echo "运行测试覆盖率..."
go test -v -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
@echo "覆盖率报告生成: coverage.html"
.PHONY: lint
lint: ## 运行代码检查
@echo "运行代码检查..."
@if command -v golangci-lint >/dev/null 2>&1; then \
golangci-lint run; \
else \
echo "golangci-lint 未安装使用go vet..."; \
go vet ./...; \
fi
.PHONY: fmt
fmt: ## 格式化代码
@echo "格式化代码..."
go fmt ./...
@if command -v goimports >/dev/null 2>&1; then \
goimports -w .; \
fi
.PHONY: security
security: ## 安全检查
@echo "运行安全检查..."
@if command -v gosec >/dev/null 2>&1; then \
gosec ./...; \
else \
echo "gosec 未安装,请运行: go install github.com/securego/gosec/v2/cmd/gosec@latest"; \
fi
.PHONY: docker-build
docker-build: ## 构建Docker镜像
@echo "构建Docker镜像..."
docker build -t $(APP_NAME):$(VERSION) .
docker tag $(APP_NAME):$(VERSION) $(APP_NAME):latest
.PHONY: docker-run
docker-run: ## 运行Docker容器
@echo "运行Docker容器..."
docker run -d --name $(APP_NAME) -p 8080:8080 $(APP_NAME):latest
.PHONY: docker-stop
docker-stop: ## 停止Docker容器
@echo "停止Docker容器..."
docker stop $(APP_NAME) || true
docker rm $(APP_NAME) || true
.PHONY: clean
clean: ## 清理构建文件
@echo "清理构建文件..."
rm -rf $(BUILD_DIR)
rm -f coverage.out coverage.html
docker system prune -f
.PHONY: install-tools
install-tools: ## 安装开发工具
@echo "安装开发工具..."
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
.PHONY: mod-update
mod-update: ## 更新依赖
@echo "更新依赖..."
go get -u ./...
go mod tidy
.PHONY: version
version: ## 显示版本信息
@echo "应用名称: $(APP_NAME)"
@echo "版本: $(VERSION)"
@echo "构建时间: $(BUILD_TIME)"
@echo "Git提交: $(GIT_COMMIT)"
@echo "Go版本: $(GO_VERSION)"
.PHONY: all
all: clean deps build test lint ## 执行完整的构建流程
# 默认目标
.DEFAULT_GOAL := help