# Custom configuration | 独立配置
# Service name | 项目名称
SERVICE=minichat
# Service name in specific style | 项目经过style格式化的名称
SERVICE_STYLE=minichat
# Service name in lowercase | 项目名称全小写格式
SERVICE_LOWER=minichat
# Service name in snake format | 项目名称下划线格式
SERVICE_SNAKE=minichat
# Service name in snake format | 项目名称短杠格式
SERVICE_DASH=minichat

# The project version, if you don't use git, you should set it manually | 项目版本，如果不使用git请手动设置
VERSION=$(shell git describe --tags --always)

# The project file name style | 项目文件命名风格
PROJECT_STYLE=go_zero

# Whether to use i18n | 是否启用 i18n
PROJECT_I18N=true

# The suffix after build or compile | 构建后缀
PROJECT_BUILD_SUFFIX=api

# Swagger type, support yml,json | Swagger 文件类型，支持yml,json
SWAGGER_TYPE=json


# ---- You may not need to modify the codes below | 下面的代码大概率不需要更改 ----
GO ?= go
GOFMT ?= gofmt "-s"
GOFILES := $(shell find . -name "*.go")
LDFLAGS := -s -w

.PHONY: test
test: # Run test for the project | 运行项目测试
	go test -v --cover ./internal/..

.PHONY: fmt
fmt: # Format the codes | 格式化代码
	$(GOFMT) -w $(GOFILES)

.PHONY: lint
lint: # Run go linter | 运行代码错误分析
	golangci-lint run -D staticcheck

.PHONY: tools
tools: # Install the necessary tools | 安装必要的工具
	$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest;
	$(GO) install github.com/go-swagger/go-swagger/cmd/swagger@latest

.PHONY: docker
docker: # Build the docker image | 构建 docker 镜像
	docker build -f Dockerfile -t ${API_IMAGE_NAME}:${VERSION} .
	@echo "Build docker successfully"

.PHONY: publish-docker
publish-docker: # Publish docker image | 发布 docker 镜像
	echo "${DOCKER_PASSWORD}" | docker login --username ${DOCKER_USERNAME} --password-stdin ${REPO}
	docker tag ${API_IMAGE_NAME}:${VERSION} ${API_IMAGE_NAME}:latest
	docker push ${API_IMAGE_NAME}:latest
	@echo "Publish docker successfully"

.PHONY: docker-run
docker-run: # Publish docker image | 发布 docker 镜像
	docker rm -f ${API_CONTAINER_NAME}
	docker compose -p ${DOCKER_PROJECT_NAME} -f ${DOCKER_COMPOSE_FILE} up -d
	@echo "docker run successfully"

.PHONY: swagger-docker
swagger-docker: # Run the swagger Docker | 运行接口文档服务
	@echo "run swagger-ui container..."
	docker rm -f ${PROJECT_NAME}-${SERVICE_NAME}-apidoc
	docker compose -p ${DOC_DOCKER_PROJECT_NAME} -f ${APIDOC_DOCKER_COMPOSE_FILE} up -d
	@echo "run swagger-ui container successfully"

.PHONY: gen-swagger
gen-swagger: # Generate swagger file | 生成 swagger 文件
	swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models
	@echo "Generate swagger successfully"

.PHONY: serve-swagger
serve-swagger: # Run the swagger server | 运行 swagger 服务
	lsof -i:36666 | awk 'NR!=1 {print $2}' | xargs killall -9 || true
	swagger serve -F=swagger --port 36666 $(SERVICE_STYLE).$(SWAGGER_TYPE)
	@echo "Serve swagger-ui successfully"

.PHONY: gen-api
gen-api: # Generate API files | 生成 API 的代码
	goctls api go --api ./desc/all.api --dir ./ --trans_err=true --style=$(PROJECT_STYLE)
	swagger generate spec --output=./$(SERVICE_STYLE).$(SWAGGER_TYPE) --scan-models
	@echo "Generate API codes successfully"

.PHONY: build-win
build-win: # Build project for Windows | 构建Windows下的可执行文件
	env CGO_ENABLED=0 GOOS=windows go build -ldflags "$(LDFLAGS)" -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX).exe main.go
	@echo "Build project for Windows successfully"

.PHONY: build-mac
build-mac: # Build project for MacOS | 构建MacOS下的可执行文件
	env CGO_ENABLED=0 GOOS=darwin go build -ldflags "$(LDFLAGS)" -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) main.go
	@echo "Build project for MacOS successfully"

.PHONY: build-linux
build-linux: # Build project for Linux | 构建Linux下的可执行文件
	env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o $(SERVICE_STYLE)_$(PROJECT_BUILD_SUFFIX) main.go
	@echo "Build project for Linux successfully"

.PHONY: proto2api
proto2api:
	goctls api proto --proto=/Users/mac/Desktop/backend/simple-admin/promotion/promotion-rpc/promotion.proto  --style=go_zero --api_service_name=$(SERVICE) --rpc_service_name=$(SERVICE) --output=./ --model=$(model) --rpc_name=$(SERVICE) --grpc_package=promotion-rpc/types/$(SERVICE_LOWER)
	@echo "generate api describe files successfully"

.PHONY: help
help: # Show help | 显示帮助
	@grep -E '^[a-zA-Z0-9 -]+:.*#'  Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done