installerbuilder/Makefile

128 lines
3.2 KiB
Makefile
Raw Permalink 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.

# Makefile for Installer Builder
# 基本变量
NAME := installer-builder
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT_HASH := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME := $(shell date -u '+%Y-%m-%d %H:%M:%S')
LDFLAGS := -ldflags "-X git.kingecg.top/kingecg/installerbuilder/internal/version.Version=$(VERSION) \
-X git.kingecg.top/kingecg/installerbuilder/internal/version.CommitHash=$(COMMIT_HASH) \
-X git.kingecg.top/kingecg/installerbuilder/internal/version.BuildTime=$(BUILD_TIME)"
# 目标平台
PLATFORMS := linux windows darwin
ARCHITECTURES := amd64 arm64
# 输出目录
DIST_DIR := dist
# Go命令
GO := go
GOBUILD := $(GO) build
GOTEST := $(GO) test
GOCLEAN := $(GO) clean
GOMOD := $(GO) mod
GOGET := $(GO) get
GOFMT := $(GO) fmt
GOLINT := golangci-lint
# 默认目标
.PHONY: all
all: clean build
# 构建目标
.PHONY: build
build:
@echo "构建 $(NAME)..."
@mkdir -p $(DIST_DIR)
$(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(NAME) ./cmd/cli
# 构建所有平台
.PHONY: build-all
build-all:
@echo "构建所有平台的二进制文件..."
@mkdir -p $(DIST_DIR)
@for platform in $(PLATFORMS); do \
for arch in $(ARCHITECTURES); do \
output_dir=$(DIST_DIR)/$$platform; \
mkdir -p $$output_dir; \
output_name=$$output_dir/$(NAME); \
if [ "$$platform" = "windows" ]; then output_name=$$output_name.exe; fi; \
echo "构建 $$platform/$$arch..."; \
GOOS=$$platform GOARCH=$$arch $(GOBUILD) $(LDFLAGS) -o $$output_name ./cmd/cli || exit 1; \
done; \
done
# 运行测试
.PHONY: test
test:
@echo "运行测试..."
$(GOTEST) -v ./...
# 运行测试覆盖率
.PHONY: test-coverage
test-coverage:
@echo "运行测试覆盖率..."
$(GOTEST) -v -coverprofile=coverage.out ./...
$(GO) tool cover -html=coverage.out -o coverage.html
# 运行代码检查
.PHONY: lint
lint:
@echo "运行代码检查..."
$(GOLINT) run
# 格式化代码
.PHONY: fmt
fmt:
@echo "格式化代码..."
$(GOFMT) ./...
# 清理构建产物
.PHONY: clean
clean:
@echo "清理构建产物..."
$(GOCLEAN)
rm -rf $(DIST_DIR)
rm -f coverage.out coverage.html
# 初始化项目
.PHONY: init
init:
@echo "初始化项目..."
$(GOMOD) tidy
$(GOGET) -u ./...
# 安装二进制文件
.PHONY: install
install: build
@echo "安装 $(NAME)..."
cp $(DIST_DIR)/$(NAME) /usr/local/bin/
# 卸载二进制文件
.PHONY: uninstall
uninstall:
@echo "卸载 $(NAME)..."
rm -f /usr/local/bin/$(NAME)
# 帮助信息
.PHONY: help
help:
@echo "Installer Builder Makefile"
@echo ""
@echo "使用方法:"
@echo " make [target]"
@echo ""
@echo "目标:"
@echo " all 默认目标执行clean和build"
@echo " build 构建项目"
@echo " build-all 构建所有平台的二进制文件"
@echo " test 运行测试"
@echo " test-coverage 运行测试覆盖率"
@echo " lint 运行代码检查"
@echo " fmt 格式化代码"
@echo " clean 清理构建产物"
@echo " init 初始化项目"
@echo " install 安装二进制文件"
@echo " uninstall 卸载二进制文件"
@echo " help 显示帮助信息"