5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-05 13:29:56 +08:00
wails/v3/internal/debug/debug.go
2023-08-12 14:16:53 +10:00

55 lines
1.5 KiB
Go

package debug
import (
"path/filepath"
"runtime"
"runtime/debug"
"github.com/samber/lo"
)
// Why go doesn't provide this as a map already is beyond me.
var buildSettings = map[string]string{}
var LocalModulePath = ""
func init() {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return
}
buildSettings = lo.Associate(buildInfo.Settings, func(setting debug.BuildSetting) (string, string) {
return setting.Key, setting.Value
})
if isLocalBuild() || buildInfo.Path == "" {
modulePath := RelativePath("..", "..", "..")
LocalModulePath, _ = filepath.Abs(modulePath)
}
}
func isLocalBuild() bool {
return buildSettings["vcs.modified"] == "true"
}
// RelativePath returns a qualified path created by joining the
// directory of the calling file and the given relative path.
func RelativePath(relativepath string, optionalpaths ...string) string {
_, thisFile, _, _ := runtime.Caller(1)
localDir := filepath.Dir(thisFile)
// If we have optional paths, join them to the relativepath
if len(optionalpaths) > 0 {
paths := []string{relativepath}
paths = append(paths, optionalpaths...)
relativepath = filepath.Join(paths...)
}
result, err := filepath.Abs(filepath.Join(localDir, relativepath))
if err != nil {
// I'm allowing this for 1 reason only: It's fatal if the path
// supplied is wrong as it's only used internally in Wails. If we get
// that path wrong, we should know about it immediately. The other reason is
// that it cuts down a ton of unnecassary error handling.
panic(err)
}
return result
}