fyne-io / fyne

Cross platform GUI toolkit in Go inspired by Material Design
https://fyne.io/
Other
25.31k stars 1.41k forks source link

Can't parse varable at build time #4355

Open PhanSon95 opened 1 year ago

PhanSon95 commented 1 year ago

Checklist

Describe the bug

Hello guys I build my golang app with env at build time. Everything run ok But when I use fyne , I can't build as my previous code This is my golang command build my code. I parse build version and commit hash and time at build time

 go build -ldflags "-s -w" -ldflags="-X 'myapp/build.Environment=staging' -X 'myapp/build.Commit=$CI_COMMIT_SHORT_SHA' -X 'myapp/build.Time=$(date)'" .

But I can' t see any similar command in fyne This is my fyne version

 fyne cli version: v2.4.1

I want to build 2 env staging and production at build time Please help me fix it

How to reproduce

Run command to build go env

 go build -ldflags "-s -w" -ldflags="-X 'myapp/build.Environment=staging' -X 'myapp/build.Commit=$CI_COMMIT_SHORT_SHA' -X 'myapp/build.Time=$(date)'" .

Screenshots

No response

Example code

File build.go (in package build)

package build

var Environment string = "staging"
var Time string
var Commit string

file main.go

package main

import(
        "fmt"
    "myapp/build"
)

func main(){
    fmt.Println"[Binary info] environment           :\t", build.Environment)
        fmt.Println("[Binary info] build at Time     :\t", build.Time)
    fmt.Println("[Binary info] build commit hash :\t", build.Commit, "\n")

}

Fyne version

v2.4.1

Go compiler version

go version go1.21.3 linux/amd64

Operating system and version

Ubuntu20

Additional Information

No response

andydotxyz commented 1 year ago

Please use the build metadata to determine if it is a development or production release. "Fyne package -release" sets release mode to true.

App.Metadata().Release

Or you can compile in environment assets through FyneApp.toml.

https://developer.fyne.io/api/v2.4/appmetadata.html

Passing parameters as you describe does not work on all platforms so we have the approach above which works everywhere.

Bluebugs commented 1 year ago

For embedding build info, I would recommend to look at the standard library which you have access to with go 1.21.3: https://pkg.go.dev/debug/buildinfo .