[v2] Auto update wails version in projects in dev mode

This commit is contained in:
Lea Anthony
2021-10-08 19:53:46 +11:00
parent 10ac38c650
commit 3765c8fb57
7 changed files with 233 additions and 8 deletions

View File

@@ -3,6 +3,8 @@ package dev
import (
"context"
"fmt"
"github.com/wailsapp/wails/v2/cmd/wails/internal"
"github.com/wailsapp/wails/v2/internal/gomod"
"io"
"net/http"
"os"
@@ -106,6 +108,12 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
return err
}
// Update go.mod to use current wails version
err = syncGoModVersion(cwd)
if err != nil {
return err
}
// Run go mod tidy to ensure we're up to date
err = runCommand(cwd, false, "go", "mod", "tidy")
if err != nil {
@@ -194,6 +202,27 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
return nil
}
func syncGoModVersion(cwd string) error {
gomodFilename := filepath.Join(cwd, "go.mod")
gomodData, err := os.ReadFile(gomodFilename)
if err != nil {
return err
}
outOfSync, err := gomod.GoModOutOfSync(gomodData, internal.Version)
if err != nil {
return err
}
if !outOfSync {
return nil
}
LogGreen("Updating go.mod to use Wails '%s'", internal.Version)
newGoData, err := gomod.UpdateGoModVersion(gomodData, internal.Version)
if err != nil {
return err
}
return os.WriteFile(gomodFilename, newGoData, 0755)
}
func runCommand(dir string, exitOnError bool, command string, args ...string) error {
LogGreen("Executing: " + command + " " + strings.Join(args, " "))
cmd := exec.Command(command, args...)

View File

@@ -0,0 +1,3 @@
package internal
var Version = "v2.0.0-beta.4"

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"github.com/wailsapp/wails/v2/cmd/wails/internal"
"os"
"github.com/wailsapp/wails/v2/internal/colour"
@@ -24,7 +25,7 @@ func fatal(message string) {
func banner(_ *clir.Cli) string {
return fmt.Sprintf("%s %s\n",
colour.Yellow("Wails CLI"),
colour.DarkRed(version))
colour.DarkRed(internal.Version))
}
func printFooter() {
@@ -35,7 +36,7 @@ func main() {
var err error
app := clir.NewCli("Wails", "Go/HTML Appkit", version)
app := clir.NewCli("Wails", "Go/HTML Appkit", internal.Version)
app.SetBannerFunction(banner)
defer printFooter()
@@ -61,14 +62,14 @@ func main() {
fatal(err.Error())
}
err = update.AddSubcommand(app, os.Stdout, version)
err = update.AddSubcommand(app, os.Stdout, internal.Version)
if err != nil {
fatal(err.Error())
}
command := app.NewSubCommand("version", "The Wails CLI version")
command.Action(func() error {
println(version)
println(internal.Version)
return nil
})

View File

@@ -1,3 +0,0 @@
package main
var version = "v2.0.0-beta.4"

View File

@@ -41,7 +41,7 @@ require (
github.com/wzshiming/ctc v1.2.3
github.com/xyproto/xpm v1.2.1
github.com/ztrue/tracerr v0.3.0
golang.org/x/mod v0.4.1 // indirect
golang.org/x/mod v0.4.1
golang.org/x/net v0.0.0-20210510120150-4163338589ed
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6
golang.org/x/tools v0.1.0

View File

@@ -0,0 +1,56 @@
package gomod
import (
"fmt"
"github.com/Masterminds/semver"
"golang.org/x/mod/modfile"
)
func GetWailsVersionFromModFile(goModText []byte) (*semver.Version, error) {
file, err := modfile.Parse("", goModText, nil)
if err != nil {
return nil, err
}
for _, req := range file.Require {
if req.Syntax == nil {
continue
}
if len(req.Syntax.Token) < 3 {
continue
}
if req.Syntax.Token[1] == "github.com/wailsapp/wails/v2" {
version := req.Syntax.Token[2]
return semver.NewVersion(version)
}
}
return nil, nil
}
func GoModOutOfSync(goModData []byte, currentVersion string) (bool, error) {
gomodversion, err := GetWailsVersionFromModFile(goModData)
if err != nil {
return false, err
}
result, err := semver.NewVersion(currentVersion)
if err != nil {
return false, fmt.Errorf("Unable to parse Wails version: %s", currentVersion)
}
return !gomodversion.Equal(result), nil
}
func UpdateGoModVersion(goModText []byte, currentVersion string) ([]byte, error) {
file, err := modfile.Parse("", goModText, nil)
if err != nil {
return nil, err
}
err = file.AddRequire("github.com/wailsapp/wails/v2", currentVersion)
if err != nil {
return nil, err
}
return file.Format()
}

View File

@@ -0,0 +1,139 @@
package gomod
import (
"github.com/Masterminds/semver"
"reflect"
"testing"
)
const basic string = `module changeme
go 1.17
require github.com/wailsapp/wails/v2 v2.0.0-beta.4
require (
github.com/andybalholm/brotli v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fasthttp/websocket v0.0.0-20200320073529-1554a54587ab // indirect
github.com/gabriel-vasile/mimetype v1.3.1 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/gofiber/fiber/v2 v2.17.0 // indirect
github.com/gofiber/websocket/v2 v2.0.8 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
github.com/klauspost/compress v1.12.2 // indirect
github.com/leaanthony/debme v1.2.1 // indirect
github.com/leaanthony/go-ansi-parser v1.0.1 // indirect
github.com/leaanthony/go-common-file-dialog v1.0.3 // indirect
github.com/leaanthony/go-webview2 v0.0.0-20211007092718-65d2f028ef2d // indirect
github.com/leaanthony/gosod v1.0.3 // indirect
github.com/leaanthony/slicer v1.5.0 // indirect
github.com/leaanthony/typescriptify-golang-structs v0.1.7 // indirect
github.com/leaanthony/webview2runtime v1.1.0 // indirect
github.com/leaanthony/winc v0.0.0-20210921073452-54963136bf18 // indirect
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f // indirect
github.com/tkrajina/go-reflector v0.5.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.28.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)
//replace github.com/wailsapp/wails/v2 v2.0.0-beta.4 => C:\Users\leaan\Documents\wails-v2-beta\wails\v2
`
func TestGetWailsVersion(t *testing.T) {
tests := []struct {
name string
goModText []byte
want *semver.Version
wantErr bool
}{
{"basic", []byte(basic), semver.MustParse("v2.0.0-beta.4"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetWailsVersionFromModFile(tt.goModText)
if (err != nil) != tt.wantErr {
t.Errorf("GetWailsVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetWailsVersion() got = %v, want %v", got, tt.want)
}
})
}
}
const basicUpdated string = `module changeme
go 1.17
require github.com/wailsapp/wails/v2 v2.0.0-beta.5
require (
github.com/andybalholm/brotli v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fasthttp/websocket v0.0.0-20200320073529-1554a54587ab // indirect
github.com/gabriel-vasile/mimetype v1.3.1 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/gofiber/fiber/v2 v2.17.0 // indirect
github.com/gofiber/websocket/v2 v2.0.8 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
github.com/klauspost/compress v1.12.2 // indirect
github.com/leaanthony/debme v1.2.1 // indirect
github.com/leaanthony/go-ansi-parser v1.0.1 // indirect
github.com/leaanthony/go-common-file-dialog v1.0.3 // indirect
github.com/leaanthony/go-webview2 v0.0.0-20211007092718-65d2f028ef2d // indirect
github.com/leaanthony/gosod v1.0.3 // indirect
github.com/leaanthony/slicer v1.5.0 // indirect
github.com/leaanthony/typescriptify-golang-structs v0.1.7 // indirect
github.com/leaanthony/webview2runtime v1.1.0 // indirect
github.com/leaanthony/winc v0.0.0-20210921073452-54963136bf18 // indirect
github.com/pkg/browser v0.0.0-20210706143420-7d21f8c997e2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f // indirect
github.com/tkrajina/go-reflector v0.5.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.28.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
)
//replace github.com/wailsapp/wails/v2 v2.0.0-beta.4 => C:\Users\leaan\Documents\wails-v2-beta\wails\v2
`
func TestUpdateGoModVersion(t *testing.T) {
type args struct {
goModText []byte
currentVersion string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{"basic", args{[]byte(basic), "v2.0.0-beta.5"}, []byte(basicUpdated), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UpdateGoModVersion(tt.args.goModText, tt.args.currentVersion)
if (err != nil) != tt.wantErr {
t.Errorf("UpdateGoModVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UpdateGoModVersion() got = %v, want %v", string(got), string(tt.want))
}
})
}
}