From 982280dcb0a289ecd236b893b2ef76c621623371 Mon Sep 17 00:00:00 2001 From: andev0x Date: Fri, 12 Jun 2026 01:08:46 +0700 Subject: [PATCH] refactor(optimization/func): update build configuration and version handling - Refactored Makefile to support cross-platform binary names and installation directories. - Updated version handling to use Git tags or fallback to 'dev' if no tag is found. - Removed redundant BINARY_NAME definition in cmd/root.go. --- Makefile | 133 +++++++++++++++++++++++----------------------------- cmd/root.go | 2 +- 2 files changed, 59 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index 9a2a687..8f9c4d8 100644 --- a/Makefile +++ b/Makefile @@ -1,116 +1,99 @@ -# Gitmit Makefile +BINARY_NAME := gitmit -# Variables -BINARY_NAME=gitmit -VERSION=$(shell git describe --tags --always --dirty) -BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S') -LDFLAGS=-ldflags "-X main.version=${VERSION} -X main.buildTime=${BUILD_TIME}" +ifeq ($(OS),Windows_NT) +EXE_NAME := $(BINARY_NAME).exe +INSTALL_DIR := $(USERPROFILE)\bin +else +EXE_NAME := $(BINARY_NAME) +INSTALL_DIR := /usr/local/bin +endif + +VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo dev) +BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S') +LDFLAGS := -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)" + +BIN_DIR := bin +BIN_PATH := $(BIN_DIR)/$(EXE_NAME) -# Default target .PHONY: all -all: clean test build +all: test build + +.PHONY: prepare +prepare: + mkdir -p $(BIN_DIR) -# Build the binary .PHONY: build -build: - @echo "Building ${BINARY_NAME}..." - go build ${LDFLAGS} -o ${BINARY_NAME} +build: prepare + @echo "Building $(BINARY_NAME)..." + go build $(LDFLAGS) -o $(BIN_PATH) . -# Build for multiple platforms .PHONY: build-all -build-all: clean +build-all: @echo "Building for multiple platforms..." - GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -o dist/${BINARY_NAME}-linux-amd64 - GOOS=darwin GOARCH=amd64 go build ${LDFLAGS} -o dist/${BINARY_NAME}-darwin-amd64 - GOOS=darwin GOARCH=arm64 go build ${LDFLAGS} -o dist/${BINARY_NAME}-darwin-arm64 - GOOS=windows GOARCH=amd64 go build ${LDFLAGS} -o dist/${BINARY_NAME}-windows-amd64.exe + mkdir -p dist + + GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o dist/$(BINARY_NAME)-linux-amd64 . + GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o dist/$(BINARY_NAME)-darwin-amd64 . + GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o dist/$(BINARY_NAME)-darwin-arm64 . + GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o dist/$(BINARY_NAME)-windows-amd64.exe . -# Run tests .PHONY: test test: - @echo "Running tests..." go test -v ./... -# Run tests with coverage .PHONY: test-coverage test-coverage: - @echo "Running tests with coverage..." - go test -v -coverprofile=coverage.out ./... + go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out -o coverage.html -# Run linting -.PHONY: lint -lint: - @echo "Running linter..." - golangci-lint run - -# Format code .PHONY: fmt fmt: - @echo "Formatting code..." - go fmt -s ./... + go fmt -s ./... -# Vet code .PHONY: vet vet: - @echo "Vetting code..." go vet ./... -# Clean build artifacts -.PHONY: clean -clean: - @echo "Cleaning..." - rm -f ${BINARY_NAME} - rm -rf dist/ - rm -f coverage.out coverage.html +.PHONY: lint +lint: + golangci-lint run -# Install dependencies .PHONY: deps deps: - @echo "Installing dependencies..." go mod download go mod tidy -# Install the binary -.PHONY: install -install: build - @echo "Installing ${BINARY_NAME}..." - sudo mv ${BINARY_NAME} /usr/local/bin/ - -# Uninstall the binary -.PHONY: uninstall -uninstall: - @echo "Uninstalling ${BINARY_NAME}..." - sudo rm -f /usr/local/bin/${BINARY_NAME} +.PHONY: clean +clean: + rm -rf $(BIN_DIR) + rm -rf dist + rm -f coverage.out coverage.html -# Run the application .PHONY: run run: build - ./${BINARY_NAME} + ./$(BIN_PATH) + +.PHONY: install +install: + go install . -# Development setup .PHONY: dev-setup dev-setup: - @echo "Setting up development environment..." go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest go mod download -# Help .PHONY: help help: - @echo "Available targets:" - @echo " all - Clean, test, and build" - @echo " build - Build the binary" - @echo " build-all - Build for multiple platforms" - @echo " test - Run tests" - @echo " test-coverage- Run tests with coverage report" - @echo " lint - Run linter" - @echo " fmt - Format code" - @echo " vet - Vet code" - @echo " clean - Clean build artifacts" - @echo " deps - Install dependencies" - @echo " install - Install binary to /usr/local/bin" - @echo " uninstall - Remove binary from /usr/local/bin" - @echo " run - Build and run the application" - @echo " dev-setup - Set up development environment" - @echo " help - Show this help message" \ No newline at end of file + @echo "Targets:" + @echo " build" + @echo " build-all" + @echo " test" + @echo " test-coverage" + @echo " fmt" + @echo " vet" + @echo " lint" + @echo " deps" + @echo " clean" + @echo " run" + @echo " install" + @echo " dev-setup" diff --git a/cmd/root.go b/cmd/root.go index 5ab0388..432e6ac 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,7 +7,7 @@ import ( ) var ( - version = "1.0.1" + version = "1.0.6" // Global flags interactiveFlag bool suggestionsFlag bool