forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomod.go
84 lines (71 loc) · 1.81 KB
/
gomod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
}
tokenPosition := 0
if !req.Syntax.InBlock {
tokenPosition = 1
}
if req.Syntax.Token[tokenPosition] == "github.com/frankfang/wails/v2" {
version := req.Syntax.Token[tokenPosition+1]
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
}
if gomodversion == nil {
return false, fmt.Errorf("Unable to find Wails in go.mod")
}
result, err := semver.NewVersion(currentVersion)
if err != nil || result == 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/frankfang/wails/v2", currentVersion)
if err != nil {
return nil, err
}
// Replace
if len(file.Replace) == 0 {
return file.Format()
}
for _, req := range file.Replace {
if req.Syntax == nil {
continue
}
tokenPosition := 0
if !req.Syntax.InBlock {
tokenPosition = 1
}
if req.Syntax.Token[tokenPosition] == "github.com/frankfang/wails/v2" {
version := req.Syntax.Token[tokenPosition+1]
_, err := semver.NewVersion(version)
if err == nil {
req.Syntax.Token[tokenPosition+1] = currentVersion
}
}
}
return file.Format()
}