From c88f02c5a52471682be04b588ed3a705b773adbe Mon Sep 17 00:00:00 2001 From: Lea O'Anthony Date: Tue, 30 Jul 2024 22:03:01 +1000 Subject: [PATCH] Add build info tool --- mkdocs-website/docs/en/changelog.md | 2 + v3/Taskfile.yaml | 6 ++ v3/cmd/wails3/main.go | 1 + v3/internal/buildinfo/buildinfo.go | 36 +++++++ v3/internal/buildinfo/buildinfo_test.go | 13 +++ v3/internal/commands/tool_buildinfo.go | 18 ++++ v3/internal/doctor/doctor.go | 7 ++ v3/internal/templates/_common/go.mod.tmpl | 2 +- v3/internal/templates/templates.go | 32 ++++-- v3/internal/version/version.txt | 2 +- v3/tasks/release/release.go | 123 ++++++++++++++++++++++ 11 files changed, 231 insertions(+), 11 deletions(-) create mode 100644 v3/internal/buildinfo/buildinfo.go create mode 100644 v3/internal/buildinfo/buildinfo_test.go create mode 100644 v3/internal/commands/tool_buildinfo.go create mode 100644 v3/tasks/release/release.go diff --git a/mkdocs-website/docs/en/changelog.md b/mkdocs-website/docs/en/changelog.md index c152d61ca..dc8de6593 100644 --- a/mkdocs-website/docs/en/changelog.md +++ b/mkdocs-website/docs/en/changelog.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## v3.0.0-alpha.5 - 2024-07-30 + ### Added - [linux] WindowDidMove / WindowDidResize events in [#3580](https://github.com/wailsapp/wails/pull/3580) diff --git a/v3/Taskfile.yaml b/v3/Taskfile.yaml index 14e35a504..b2ec73bab 100644 --- a/v3/Taskfile.yaml +++ b/v3/Taskfile.yaml @@ -56,6 +56,12 @@ tasks: - cmd: go run ../../../tasks/sed/sed.go replace -dir frontend -old wailsjs.dev -new wails.io -ext .ts,.js,.html -ignore vite.config.js,vite.config.ts,vite-env.d.ts - cmd: go run ../../../tasks/sed/sed.go replace -dir frontend -old "framework powered by Wails" -new "framework powered by Vite" -ext .ts,.js,.html,.svelte -ignore vite.config.js,vite.config.ts,vite-env.d.ts + release: + summary: Release a new version of Wails. Call with `task v3:release -- ` + dir: tasks/release + cmds: + - go run release.go {{.CLI_ARGS}} + taskfile:upgrade: cmds: - go get -u github.com/go-task/task/v3 diff --git a/v3/cmd/wails3/main.go b/v3/cmd/wails3/main.go index 642a8c2b0..c49ed56eb 100644 --- a/v3/cmd/wails3/main.go +++ b/v3/cmd/wails3/main.go @@ -65,6 +65,7 @@ func main() { tool.NewSubCommandFunction("checkport", "Checks if a port is open. Useful for testing if vite is running.", commands.ToolCheckPort) tool.NewSubCommandFunction("watcher", "Watches files and runs a command when they change", commands.Watcher) tool.NewSubCommandFunction("cp", "Copy files", commands.Cp) + tool.NewSubCommandFunction("buildinfo", "Show Build Info", commands.BuildInfo) app.NewSubCommandFunction("version", "Print the version", commands.Version) app.NewSubCommand("sponsor", "Sponsor the project").Action(openSponsor) diff --git a/v3/internal/buildinfo/buildinfo.go b/v3/internal/buildinfo/buildinfo.go new file mode 100644 index 000000000..690fcf75e --- /dev/null +++ b/v3/internal/buildinfo/buildinfo.go @@ -0,0 +1,36 @@ +package buildinfo + +import ( + "fmt" + "github.com/samber/lo" + "runtime/debug" +) + +type Info struct { + Development bool + Version string + BuildSettings map[string]string + wailsPackage *debug.Module +} + +func Get() (*Info, error) { + + var result Info + + // BuildInfo contains the build info for the application + var BuildInfo *debug.BuildInfo + + var ok bool + BuildInfo, ok = debug.ReadBuildInfo() + if !ok { + return nil, fmt.Errorf("could not read build info from binary") + } + result.BuildSettings = lo.Associate(BuildInfo.Settings, func(setting debug.BuildSetting) (string, string) { + return setting.Key, setting.Value + }) + result.Version = BuildInfo.Main.Version + result.Development = BuildInfo.Main.Version == "(devel)" + + return &result, nil + +} diff --git a/v3/internal/buildinfo/buildinfo_test.go b/v3/internal/buildinfo/buildinfo_test.go new file mode 100644 index 000000000..ab79f63f8 --- /dev/null +++ b/v3/internal/buildinfo/buildinfo_test.go @@ -0,0 +1,13 @@ +package buildinfo + +import ( + "testing" +) + +func TestGet(t *testing.T) { + result, err := Get() + if err != nil { + t.Error(err) + } + _ = result +} diff --git a/v3/internal/commands/tool_buildinfo.go b/v3/internal/commands/tool_buildinfo.go new file mode 100644 index 000000000..d8547d8ce --- /dev/null +++ b/v3/internal/commands/tool_buildinfo.go @@ -0,0 +1,18 @@ +package commands + +import ( + "fmt" + "github.com/wailsapp/wails/v3/internal/buildinfo" +) + +type BuildInfoOptions struct{} + +func BuildInfo(_ *BuildInfoOptions) error { + + info, err := buildinfo.Get() + if err != nil { + return err + } + fmt.Printf("%+v\n", info) + return nil +} diff --git a/v3/internal/doctor/doctor.go b/v3/internal/doctor/doctor.go index 4bbae931e..c6d3c0ca7 100644 --- a/v3/internal/doctor/doctor.go +++ b/v3/internal/doctor/doctor.go @@ -2,6 +2,7 @@ package doctor import ( "fmt" + "github.com/wailsapp/wails/v3/internal/buildinfo" "path/filepath" "runtime" "runtime/debug" @@ -19,6 +20,12 @@ import ( func Run() (err error) { + get, err := buildinfo.Get() + if err != nil { + return err + } + _ = get + pterm.DefaultSection = *pterm.DefaultSection. WithBottomPadding(0). WithStyle(pterm.NewStyle(pterm.FgBlue, pterm.Bold)) diff --git a/v3/internal/templates/_common/go.mod.tmpl b/v3/internal/templates/_common/go.mod.tmpl index 3dad9b6fa..2cb53bbd7 100644 --- a/v3/internal/templates/_common/go.mod.tmpl +++ b/v3/internal/templates/_common/go.mod.tmpl @@ -2,7 +2,7 @@ module changeme go 1.21 -require github.com/wailsapp/wails/v3 v3.0.0-alpha.0 +require github.com/wailsapp/wails/v3 {{.WailsVersion}} require ( github.com/json-iterator/go v1.1.12 // indirect diff --git a/v3/internal/templates/templates.go b/v3/internal/templates/templates.go index 70b46ca52..0ad8c3910 100644 --- a/v3/internal/templates/templates.go +++ b/v3/internal/templates/templates.go @@ -4,6 +4,8 @@ import ( "embed" "encoding/json" "fmt" + "github.com/wailsapp/wails/v3/internal/buildinfo" + "github.com/wailsapp/wails/v3/internal/version" "io/fs" "os" "os/exec" @@ -72,6 +74,7 @@ type TemplateOptions struct { *flags.Init LocalModulePath string UseTypescript bool + WailsVersion string } func getInternalTemplate(templateName string) (*Template, error) { @@ -214,24 +217,35 @@ func Install(options *flags.Init) error { return err } - // Calculate relative path from project directory to LocalModulePath - var relativePath string - // Check if the project directory and LocalModulePath are in the same drive - if filepath.VolumeName(wd) != filepath.VolumeName(debug.LocalModulePath) { - relativePath = debug.LocalModulePath - } else { - relativePath, err = filepath.Rel(projectDir, debug.LocalModulePath) - } + buildInfo, err := buildinfo.Get() if err != nil { return err } + // Calculate relative path from project directory to LocalModulePath + var localModulePath string + + // Use module path if it is set + if buildInfo.Development { + var relativePath string + // Check if the project directory and LocalModulePath are in the same drive + if filepath.VolumeName(wd) != filepath.VolumeName(debug.LocalModulePath) { + relativePath = debug.LocalModulePath + } else { + relativePath, err = filepath.Rel(projectDir, debug.LocalModulePath) + } + if err != nil { + return err + } + localModulePath = filepath.ToSlash(relativePath + "/") + } UseTypescript := strings.HasSuffix(options.TemplateName, "-ts") templateData := TemplateOptions{ Init: options, - LocalModulePath: filepath.ToSlash(relativePath + "/"), + LocalModulePath: localModulePath, UseTypescript: UseTypescript, + WailsVersion: version.VersionString, } defer func() { diff --git a/v3/internal/version/version.txt b/v3/internal/version/version.txt index 5781d3b04..fce0cd489 100644 --- a/v3/internal/version/version.txt +++ b/v3/internal/version/version.txt @@ -1 +1 @@ -v3.0.0-alpha.4 \ No newline at end of file +v3.0.0-alpha.5 \ No newline at end of file diff --git a/v3/tasks/release/release.go b/v3/tasks/release/release.go new file mode 100644 index 000000000..d24d23e12 --- /dev/null +++ b/v3/tasks/release/release.go @@ -0,0 +1,123 @@ +package main + +import ( + "os" + "strconv" + "strings" + "time" + + "github.com/wailsapp/wails/v3/internal/s" +) + +const versionFile = "../../internal/version/version.txt" + +func checkError(err error) { + if err != nil { + println(err.Error()) + os.Exit(1) + } +} + +// TODO:This can be replaced with "https://github.com/coreos/go-semver/blob/main/semver/semver.go" +func updateVersion() string { + currentVersionData, err := os.ReadFile(versionFile) + checkError(err) + currentVersion := string(currentVersionData) + vsplit := strings.Split(currentVersion, ".") + minorVersion, err := strconv.Atoi(vsplit[len(vsplit)-1]) + checkError(err) + minorVersion++ + vsplit[len(vsplit)-1] = strconv.Itoa(minorVersion) + newVersion := strings.Join(vsplit, ".") + err = os.WriteFile(versionFile, []byte(newVersion), 0o755) + checkError(err) + return newVersion +} + +//func runCommand(name string, arg ...string) { +// cmd := exec.Command(name, arg...) +// cmd.Stdout = os.Stdout +// cmd.Stderr = os.Stderr +// err := cmd.Run() +// checkError(err) +//} + +//func IsPointRelease(currentVersion string, newVersion string) bool { +// // The first n-1 parts of the version should be the same +// if currentVersion[:len(currentVersion)-2] != newVersion[:len(newVersion)-2] { +// return false +// } +// // split on the last dot in the string +// currentVersionSplit := strings.Split(currentVersion, ".") +// newVersionSplit := strings.Split(newVersion, ".") +// // if the last part of the version is the same, it's a point release +// currentMinor := lo.Must(strconv.Atoi(currentVersionSplit[len(currentVersionSplit)-1])) +// newMinor := lo.Must(strconv.Atoi(newVersionSplit[len(newVersionSplit)-1])) +// return newMinor == currentMinor+1 +//} + +func main() { + var newVersion string + if len(os.Args) > 1 { + newVersion = os.Args[1] + //currentVersion, err := os.ReadFile(versionFile) + //checkError(err) + err := os.WriteFile(versionFile, []byte(newVersion), 0o755) + checkError(err) + //isPointRelease = IsPointRelease(string(currentVersion), newVersion) + } else { + newVersion = updateVersion() + } + + // Update ChangeLog + s.CD("../../../mkdocs-website") + + // Read in `src/pages/changelog.mdx` + changelogData, err := os.ReadFile("docs/en/changelog.md") + checkError(err) + changelog := string(changelogData) + // Split on the line that has `## [Unreleased]` + changelogSplit := strings.Split(changelog, "## [Unreleased]") + // Get today's date in YYYY-MM-DD format + today := time.Now().Format("2006-01-02") + // Add the new version to the top of the changelog + newChangelog := changelogSplit[0] + "## [Unreleased]\n\n## " + newVersion + " - " + today + changelogSplit[1] + // Write the changelog back + err = os.WriteFile("docs/en/changelog.md", []byte(newChangelog), 0o755) + checkError(err) + + // TODO: Documentation Versioning and Translations + + //if !isPointRelease { + // runCommand("npx", "-y", "pnpm", "install") + // + // s.ECHO("Generating new Docs for version: " + newVersion) + // + // runCommand("npx", "pnpm", "run", "docusaurus", "docs:version", newVersion) + // + // runCommand("npx", "pnpm", "run", "write-translations") + // + // // Load the version list/* + // versionsData, err := os.ReadFile("versions.json") + // checkError(err) + // var versions []string + // err = json.Unmarshal(versionsData, &versions) + // checkError(err) + // oldestVersion := versions[len(versions)-1] + // s.ECHO(oldestVersion) + // versions = versions[0 : len(versions)-1] + // newVersions, err := json.Marshal(&versions) + // checkError(err) + // err = os.WriteFile("versions.json", newVersions, 0o755) + // checkError(err) + // + // s.ECHO("Removing old version: " + oldestVersion) + // s.CD("versioned_docs") + // s.RMDIR("version-" + oldestVersion) + // s.CD("../versioned_sidebars") + // s.RM("version-" + oldestVersion + "-sidebars.json") + // s.CD("..") + // + // runCommand("npx", "pnpm", "run", "build") + //} +}