Skip to content

Commit

Permalink
improve package gbuild; version updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Feb 23, 2022
1 parent 3db5358 commit 8eb9fdf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
8 changes: 4 additions & 4 deletions cmd/gf/internal/cmd/cmd_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type cVersionOutput struct{}

func (c cVersion) Index(ctx context.Context, in cVersionInput) (*cVersionOutput, error) {
info := gbuild.Info()
if info["git"] == "" {
info["git"] = "none"
if info.Git == "" {
info.Git = "none"
}
mlog.Printf(`GoFrame CLI Tool %s, https://goframe.org`, gf.VERSION)
gfVersion, err := c.getGFVersionOfCurrentProject()
Expand All @@ -41,7 +41,7 @@ func (c cVersion) Index(ctx context.Context, in cVersionInput) (*cVersionOutput,
}
mlog.Printf(`GoFrame Version: %s`, gfVersion)
mlog.Printf(`CLI Installed At: %s`, gfile.SelfPath())
if info["gf"] == "" {
if info.GoFrame == "" {
mlog.Print(`Current is a custom installed version, no installation information.`)
return nil, nil
}
Expand All @@ -52,7 +52,7 @@ CLI Built Detail:
GF Version: %s
Git Commit: %s
Build Time: %s
`, info["go"], info["gf"], info["git"], info["time"])))
`, info.Golang, info.GoFrame, info.Git, info.Time)))
return nil, nil
}

Expand Down
37 changes: 27 additions & 10 deletions os/gbuild/gbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ import (
"github.com/gogf/gf/v2/internal/json"
)

// BuildInfo maintains the built info of current binary.
type BuildInfo struct {
GoFrame string // Built used GoFrame version.
Golang string // Built used Golang version.
Git string // Built used git repo. commit id and datetime.
Time string // Built datetime.
Data map[string]interface{} // All custom built data key-value pairs.
}

const (
gfVersion = `gfVersion`
goVersion = `goVersion`
builtGit = `builtGit`
builtTime = `builtTime`
)

var (
builtInVarStr = "" // Raw variable base64 string, which is injected by go build flags.
builtInVarMap = map[string]interface{}{} // Binary custom variable map decoded.
Expand All @@ -30,8 +46,8 @@ func init() {
if err != nil {
intlog.Errorf(context.TODO(), `%+v`, err)
}
builtInVarMap["gfVersion"] = gf.VERSION
builtInVarMap["goVersion"] = runtime.Version()
builtInVarMap[gfVersion] = gf.VERSION
builtInVarMap[goVersion] = runtime.Version()
intlog.Printf(context.TODO(), "build variables: %+v", builtInVarMap)
} else {
intlog.Print(context.TODO(), "no build variables")
Expand All @@ -41,12 +57,13 @@ func init() {
// Info returns the basic built information of the binary as map.
// Note that it should be used with gf-cli tool "gf build",
// which automatically injects necessary information into the binary.
func Info() map[string]string {
return map[string]string{
"gf": Get("gfVersion").String(),
"go": Get("goVersion").String(),
"git": Get("builtGit").String(),
"time": Get("builtTime").String(),
func Info() BuildInfo {
return BuildInfo{
GoFrame: Get(gfVersion).String(),
Golang: Get(goVersion).String(),
Git: Get(builtGit).String(),
Time: Get(builtTime).String(),
Data: Data(),
}
}

Expand All @@ -61,7 +78,7 @@ func Get(name string, def ...interface{}) *gvar.Var {
return nil
}

// Map returns the custom build-in variable map.
func Map() map[string]interface{} {
// Data returns the custom build-in variables as map.
func Data() map[string]interface{} {
return builtInVarMap
}
18 changes: 11 additions & 7 deletions os/gbuild/gbuild_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
package gbuild_test

import (
"testing"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gbuild"
"github.com/gogf/gf/v2/test/gtest"
"testing"
"github.com/gogf/gf/v2/util/gconv"
)

func Test_Info(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gbuild.Info(), map[string]string{
"gf": "",
"go": "",
"git": "",
"time": "",
t.Assert(gconv.Map(gbuild.Info()), g.Map{
"GoFrame": "",
"Golang": "",
"Git": "",
"Time": "",
"Data": g.Map{},
})
})
}
Expand All @@ -34,6 +38,6 @@ func Test_Get(t *testing.T) {

func Test_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gbuild.Map(), map[string]interface{}{})
t.Assert(gbuild.Data(), map[string]interface{}{})
})
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gf

const VERSION = "v2.0.0-rc2"
const VERSION = "v2.0.0-rc3"
const AUTHORS = "john<john@goframe.org>"

0 comments on commit 8eb9fdf

Please sign in to comment.