Skip to content

Commit

Permalink
#9: auto set the version information from git hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kcmvp committed Dec 28, 2023
1 parent 02b641f commit bcd29c4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
22 changes: 14 additions & 8 deletions cmd/action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,32 @@ var buildCommand = func(_ *cobra.Command, args ...string) error {
}
bm := map[string]string{}
for _, dir := range dirs {
mf, err := findMain(dir)
mainFile, err := findMain(dir)
if err != nil {
return err
}
if len(mf) > 0 {
if len(mainFile) > 0 {
// action
binary := strings.TrimSuffix(filepath.Base(mf), ".go")
binary := strings.TrimSuffix(filepath.Base(mainFile), ".go")
if f, exists := bm[binary]; exists {
return fmt.Errorf("file %s has already built as %s, please rename %s", f, binary, mf)
return fmt.Errorf("file %s has already built as %s, please rename %s", f, binary, mainFile)
}
output := filepath.Join(internal.CurProject().Root(), internal.CurProject().Target(), binary)
if _, err := exec.Command("go", "build", "-o", output, mf).CombinedOutput(); err != nil { //nolint
return err
output := filepath.Join(internal.CurProject().Target(), binary)
versionFlag := fmt.Sprintf("-X 'main.buildVersion=%s'", internal.Version())
// try to build the binary with version first
if _, err := exec.Command("go", "build", "-ldflags", versionFlag, "-o", output, mainFile).CombinedOutput(); err != nil { //nolint
color.Yellow("no version variable 'buildVersion' defined in the main package")
if _, err := exec.Command("go", "build", "-o", output, mainFile).CombinedOutput(); err != nil {
return err
}
}
fmt.Printf("Build %s to %s successfully\n", mf, output)
fmt.Printf("Build %s to %s successfully\n", mainFile, output)
bm[binary] = output
} else {
color.Yellow("Can not find main function in package %s", dir)
}
}
//
return nil
}

Expand Down
6 changes: 5 additions & 1 deletion main.go → gob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ Copyright © 2023 [email protected]
*/
package main

import "github.com/kcmvp/gob/cmd"
import (
"github.com/kcmvp/gob/cmd"
)

var buildVersion string //nolint

func main() {
cmd.Execute()
Expand Down
3 changes: 1 addition & 2 deletions internal/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
"os"
"os/exec"
"path/filepath"
"strings"
)
Expand Down Expand Up @@ -78,7 +77,7 @@ func (project *Project) Setup(init bool) {
project.viper.WriteConfigAs(project.Configuration())
}
// always generate hook script
if _, err := exec.Command("git", "status").CombinedOutput(); err != nil {
if !InGit() {
if init {
color.Yellow("Project is not in the source control")
}
Expand Down
21 changes: 21 additions & 0 deletions internal/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,27 @@ func (project *Project) InstallPlugin(url string, aliasAndCommand ...string) err
}
}

func InGit() bool {
_, err := exec.Command("git", "status").CombinedOutput()
return err == nil
}

func Version() string {
ver := "unknown"
if InGit() {
if output, err := exec.Command("git", "rev-parse", "HEAD").CombinedOutput(); err == nil {
hash := strings.ReplaceAll(string(output), "\n", "")
tag, err := exec.Command("git", "describe", "--tag", hash).CombinedOutput()
if err == nil {
if date, err := exec.Command("git", "show", "--format=%cd", "--date=format:%Y/%m/%d", hash).CombinedOutput(); err == nil {
ver = fmt.Sprintf("%s@%s", strings.ReplaceAll(string(tag), "\n", ""), strings.ReplaceAll(string(date), "\n", ""))
}
}
}
}
return ver
}

// Windows return true when current os is Windows
func Windows() bool {
return runtime.GOOS == "windows"
Expand Down
6 changes: 6 additions & 0 deletions internal/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -47,3 +48,8 @@ func TestInstallPlugin(t *testing.T) {
assert.Equal(t, plugin.B, "callvisv7")
assert.Equal(t, plugin.C, "run")
}

func TestVersion(t *testing.T) {
assert.True(t, strings.HasPrefix(Version(), "v0.0.2"))
assert.True(t, strings.Contains(Version(), "@"))
}

0 comments on commit bcd29c4

Please sign in to comment.