-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
127 lines (103 loc) · 4.53 KB
/
Copy pathMakefile
File metadata and controls
127 lines (103 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
.PHONY: build run test clean lint fmt deps web-build embed release
# 变量
BINARY_NAME=modelgate
IMPORTER_NAME=import_users
VERSION=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
LDFLAGS=-ldflags "-s -w -X modelgate/internal/version.Version=$(VERSION) -X modelgate/internal/version.BuildTime=$(BUILD_TIME) -X modelgate/internal/version.Commit=$(COMMIT)"
# 默认构建(包含前端嵌入)
build: web-build embed
go build $(LDFLAGS) -o $(BINARY_NAME) ./cmd/server
go build $(LDFLAGS) -o $(IMPORTER_NAME) ./cmd/import_users
@echo "构建完成: $(BINARY_NAME), $(IMPORTER_NAME)"
# 仅构建 Go(不构建前端,用于开发)
build-go:
go build $(LDFLAGS) -o $(BINARY_NAME) ./cmd/server
go build $(LDFLAGS) -o $(IMPORTER_NAME) ./cmd/import_users
# 构建前端
web-build:
@echo "构建前端..."
cd web && npm install && npm run build
# 复制前端文件到嵌入目录
embed: web-build
@echo "准备嵌入文件..."
@rm -rf internal/infra/static/dist
@mkdir -p internal/infra/static/dist
@cp -r web/dist/* internal/infra/static/dist/
@echo "已复制 $$(ls internal/infra/static/dist | wc -l) 个文件"
# 开发模式运行(使用 vite 代理)
dev:
@echo "启动开发服务器..."
@echo "前端: http://localhost:5173"
@echo "后端: http://localhost:8080"
@make -j2 dev-web dev-server
dev-web:
cd web && npm run dev
dev-server:
go run ./cmd/server
# 运行(生产模式,需要先 build)
run: build
./$(BINARY_NAME)
# 运行测试
test:
go test -v ./...
# 清理
clean:
rm -f $(BINARY_NAME) $(IMPORTER_NAME)
rm -rf internal/infra/static/dist
go clean
# 代码检查
lint:
golangci-lint run
# 格式化代码
fmt:
go fmt ./...
cd web && npm run lint -- --fix 2>/dev/null || true
# 安装依赖
deps:
go mod download
go mod tidy
cd web && npm install
# 跨平台构建
release: clean web-build embed
@echo "构建多平台发布版本..."
@mkdir -p releases
# Linux AMD64
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o releases/$(BINARY_NAME)-linux-amd64 ./cmd/server
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o releases/$(IMPORTER_NAME)-linux-amd64 ./cmd/import_users
cp config.yaml.example releases/config.yaml.example
cp docs/import_users_template.csv releases/
cp README.md releases/
cd releases && tar -czf $(BINARY_NAME)-$(VERSION)-linux-amd64.tar.gz $(BINARY_NAME)-linux-amd64 $(IMPORTER_NAME)-linux-amd64 config.yaml.example import_users_template.csv README.md
# Linux ARM64
GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o releases/$(BINARY_NAME)-linux-arm64 ./cmd/server
GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o releases/$(IMPORTER_NAME)-linux-arm64 ./cmd/import_users
cd releases && tar -czf $(BINARY_NAME)-$(VERSION)-linux-arm64.tar.gz $(BINARY_NAME)-linux-arm64 $(IMPORTER_NAME)-linux-arm64 config.yaml.example import_users_template.csv README.md
# Darwin AMD64
GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o releases/$(BINARY_NAME)-darwin-amd64 ./cmd/server
GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o releases/$(IMPORTER_NAME)-darwin-amd64 ./cmd/import_users
cd releases && tar -czf $(BINARY_NAME)-$(VERSION)-darwin-amd64.tar.gz $(BINARY_NAME)-darwin-amd64 $(IMPORTER_NAME)-darwin-amd64 config.yaml.example import_users_template.csv README.md
# Darwin ARM64 (M1/M2)
GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o releases/$(BINARY_NAME)-darwin-arm64 ./cmd/server
GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o releases/$(IMPORTER_NAME)-darwin-arm64 ./cmd/import_users
cd releases && tar -czf $(BINARY_NAME)-$(VERSION)-darwin-arm64.tar.gz $(BINARY_NAME)-darwin-arm64 $(IMPORTER_NAME)-darwin-arm64 config.yaml.example import_users_template.csv README.md
# Windows AMD64
GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o releases/$(BINARY_NAME)-windows-amd64.exe ./cmd/server
GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o releases/$(IMPORTER_NAME)-windows-amd64.exe ./cmd/import_users
cd releases && zip -r $(BINARY_NAME)-$(VERSION)-windows-amd64.zip $(BINARY_NAME)-windows-amd64.exe $(IMPORTER_NAME)-windows-amd64.exe config.yaml.example import_users_template.csv README.md
@echo ""
@echo "发布包已创建:"
@ls -lh releases/*.tar.gz releases/*.zip 2>/dev/null
# 快速启动(使用现有构建)
start:
@if [ ! -f $(BINARY_NAME) ]; then \
echo "未找到 $(BINARY_NAME),先执行构建..."; \
make build; \
fi
./$(BINARY_NAME)
# 查看版本信息
version:
@echo "Version: $(VERSION)"
@echo "Build Time: $(BUILD_TIME)"
@echo "Commit: $(COMMIT)"