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 all commits
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
1 change: 1 addition & 0 deletions .goreleaser/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
- make gen-version
builds:
- env:
- CGO_ENABLED=0
Expand Down
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
53 changes: 53 additions & 0 deletions pkg/util/gitutil/gitutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright The Karpor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


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)
}
80 changes: 80 additions & 0 deletions pkg/version/scripts/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright The Karpor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


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-%s", latestTag, currentCommit), nil
}

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)
}
20 changes: 20 additions & 0 deletions pkg/version/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright The Karpor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package version

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