Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Automatically calculate the current version number through Git… #544

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ endif

# If you encounter an error like "panic: permission denied" on MacOS,
# please visit https://github.com/eisenxp/macos-golink-wrapper to find the solution.

.PHONY: gen-version
gen-version: ## Generate version file
# Update version
-cd pkg/version/scripts && go run gen/gen.go

.PHONY: test
test: ## Run the tests
@PKG_LIST=$${TARGET_PKG:-$(GOSOURCE_PATHS)}; \
Expand Down Expand Up @@ -84,6 +90,7 @@ build-all: build-darwin build-linux build-windows ## Build for all platforms
# make build-darwin GOARCH=arm64 SKIP_UI_BUILD=true
.PHONY: build-darwin
build-darwin: $(BUILD_UI) ## Build for MacOS (Darwin)
$(MAKE) gen-version
-rm -rf ./_build/darwin
GOOS=darwin GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
go build -o ./_build/darwin/$(APPROOT) \
Expand All @@ -99,6 +106,7 @@ build-darwin: $(BUILD_UI) ## Build for MacOS (Darwin)
# make build-linux GOARCH=arm64 SKIP_UI_BUILD=true
.PHONY: build-linux
build-linux: $(BUILD_UI) ## Build for Linux
$(MAKE) gen-version
-rm -rf ./_build/linux
GOOS=linux GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
go build -o ./_build/linux/$(APPROOT) \
Expand All @@ -114,6 +122,7 @@ build-linux: $(BUILD_UI) ## Build for Linux
# make build-windows GOARCH=arm64 SKIP_UI_BUILD=true
.PHONY: build-windows
build-windows: $(BUILD_UI) ## Build for Windows
$(MAKE) gen-version
-rm -rf ./_build/windows
GOOS=windows GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
go build -o ./_build/windows/$(APPROOT).exe \
Expand All @@ -124,6 +133,7 @@ build-windows: $(BUILD_UI) ## Build for Windows
# Usage: make build-ui
.PHONY: build-ui
build-ui: ## Build UI for the dashboard
$(MAKE) gen-version
@echo "Building UI for the dashboard ..."
cd ui && npm install && npm run build && touch build/.gitkeep

Expand Down
38 changes: 38 additions & 0 deletions pkg/util/gitutil/gitutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gitutil

import (
"bytes"
"os/exec"
"strings"
)

// RunGitCommand runs a git command and returns the output as a string
func runGitCommand(args ...string) (string, error) {
cmd := exec.Command("git", args...)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", err
}
return strings.TrimSpace(out.String()), nil
}

// GetLatestTag returns the latest tag from Git
func GetLatestTag() (string, error) {
return runGitCommand("describe", "--tags", "--abbrev=0")
}

// GetCurrentCommit returns the current commit SHA
func GetCurrentCommit() (string, error) {
return runGitCommand("rev-parse", "--short", "HEAD")
}

// GetCurrentHead returns the current HEAD SHA
func GetCurrentHead() (string, error) {
return runGitCommand("rev-parse", "HEAD")
}

// GetTagCommit returns the commit SHA for a given tag
func GetTagCommit(tag string) (string, error) {
return runGitCommand("rev-list", "-n", "1", tag)
}
68 changes: 68 additions & 0 deletions pkg/version/scripts/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build ignore
// +build ignore

package main

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/KusionStack/karpor/pkg/util/gitutil"
"github.com/KusionStack/karpor/pkg/version"
)

func calculateVersion() (string, error) {
latestTag, err := gitutil.GetLatestTag()
if err != nil {
fmt.Println("Error getting latest tag:", err)
return "v0.1.0", nil
}
currentCommit, err := gitutil.GetCurrentCommit()
if err != nil {
fmt.Println("Error getting current commit:", err)
return "", err
}
currentHead, err := gitutil.GetCurrentHead()
if err != nil {
fmt.Println("Error getting current head:", err)
return "", err
}
tagCommit, err := gitutil.GetTagCommit(latestTag)
if err != nil {
fmt.Println("Error getting tag commit:", err)
return "", err
}
if currentHead == tagCommit {
return latestTag, nil
}
return fmt.Sprintf("%s-g%s", latestTag, currentCommit), nil
rajeshkio marked this conversation as resolved.
Show resolved Hide resolved
}

func main() {
currentDir, err := os.Getwd()
versionStr, err := calculateVersion()
if err != nil {
fmt.Println("Error calculating version:", err)
os.Exit(1)
}
version := version.Version{Version: versionStr}

rootDir := strings.Replace(currentDir, "/pkg/version/scripts", "", 1)
versionFilePath := filepath.Join(rootDir, "pkg", "version", "VERSION")

// Ensure the pkg/version directory exists
err = os.MkdirAll(filepath.Dir(versionFilePath), 0755)
if err != nil {
fmt.Println("Error creating version directory:", err)
os.Exit(1)
}
// Write the version to the VERSION file, replacing any existing content
err = os.WriteFile(versionFilePath, []byte(version.Version+"\n"), 0644)
if err != nil {
fmt.Println("Error writing version file:", err)
os.Exit(1)
}
fmt.Println("Version file updated to:", version.Version)
}
5 changes: 5 additions & 0 deletions pkg/version/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package version

type Version struct {
Version string `json:"version"`
}
Loading