.PHONY: build clean test install run fmt lint help # Variables BINARY_NAME=go-doc BUILD_DIR=bin MAIN_PATH=./cmd/go-doc VERSION?=1.0.0 COMMIT=$(shell git rev-parse --short HEAD 2>/dev/null || echo "dev") BUILD_DATE=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") LDFLAGS=-ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(BUILD_DATE)" help: ## Display this help screen @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' build: ## Build the binary @echo "Building $(BINARY_NAME)..." @mkdir -p $(BUILD_DIR) @go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(MAIN_PATH) @echo "✅ Build complete: $(BUILD_DIR)/$(BINARY_NAME)" clean: ## Remove build artifacts @echo "Cleaning..." @rm -rf $(BUILD_DIR) @rm -rf example/test_output @echo "✅ Clean complete" test: ## Run tests @echo "Running tests..." @go test -v -race -coverprofile=coverage.txt -covermode=atomic ./... @echo "✅ Tests complete" install: ## Install the binary to $GOPATH/bin @echo "Installing $(BINARY_NAME)..." @go install $(LDFLAGS) $(MAIN_PATH) @echo "✅ Install complete" run: build ## Build and run with example API file @echo "Running $(BINARY_NAME)..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example_cn.api -d example/test_output -f example -s openapi3.0 @echo "✅ Generated: example/test_output/gateway.json" fmt: ## Format code @echo "Formatting code..." @gofmt -s -w . @goimports -w . @echo "✅ Format complete" lint: ## Run linters @echo "Running linters..." @golangci-lint run || (echo "golangci-lint not found, skipping..." && exit 0) @echo "✅ Lint complete" deps: ## Download dependencies @echo "Downloading dependencies..." @go mod download @go mod tidy @echo "✅ Dependencies updated" # Build for multiple platforms build-all: ## Build for all platforms @echo "Building for all platforms..." @mkdir -p $(BUILD_DIR) GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(MAIN_PATH) GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(MAIN_PATH) GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(MAIN_PATH) GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(MAIN_PATH) GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(MAIN_PATH) @echo "✅ Multi-platform build complete" example: build ## Generate example swagger and openapi files @echo "Generating examples..." @mkdir -p example/test_output @echo " - Swagger 2.0 (JSON)..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_swagger2 @echo " - Swagger 2.0 (YAML)..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_swagger2 -y @echo " - OpenAPI 3.0 (JSON)..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_openapi3 -s openapi3.0 @echo " - OpenAPI 3.0 (YAML)..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example.api -d example/test_output -f example_openapi3 -s openapi3.0 -y @echo " - Chinese Swagger 2.0..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example_cn.api -d example/test_output -f example_cn_swagger2 @echo " - Chinese OpenAPI 3.0..." @$(BUILD_DIR)/$(BINARY_NAME) -a example/example_cn.api -d example/test_output -f example_cn_openapi3 -s openapi3.0 @echo "✅ Examples generated in example/test_output/" gen-gateway: build ## Generate Gateway API documentation (OpenAPI 3.0) @echo "Generating Gateway API documentation..." @mkdir -p example/test_output @$(BUILD_DIR)/$(BINARY_NAME) -a example/api/gateway.api -d example/test_output -f gateway -s openapi3.0 @echo "✅ Generated: example/test_output/gateway.json" @echo "" @echo "📊 驗證結果:" @echo " - Total endpoints: $$(jq '.paths | length' example/test_output/gateway.json)" @echo " - Total schemas: $$(jq '.components.schemas // {} | length' example/test_output/gateway.json 2>/dev/null || echo '0')"