/
tasks.go
148 lines (134 loc) · 3.74 KB
/
tasks.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package pkg
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"fyne.io/tools"
"golang.org/x/mod/semver"
)
type task struct {
name, hint string
test func() (string, error)
}
var tasks = []*task{
{"Go compiler", "Checking Go is installed and up to date", func() (string, error) {
cmd := tools.CommandInShell("go", "version")
ret, err := cmd.Output()
if err != nil {
return "", err
}
ver := parseGoVersion(strings.TrimSpace(string(ret)))
before114 := semver.Compare("v"+ver, "v1.14.0") < 0
if before114 {
return "go" + ver, errors.New("go version is too old, must be 1.14 or newer")
}
return "go" + ver, nil
}},
{"C compiler", "Checking a C compiler is installed", func() (string, error) {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = tools.CommandInShell("where", "gcc")
} else {
cmd = tools.CommandInShell("which", "gcc")
}
_, err := cmd.Output()
if err == nil {
return "gcc found", nil
}
if runtime.GOOS == "windows" {
cmd = tools.CommandInShell("where", "clang")
} else {
cmd = tools.CommandInShell("which", "clang")
}
_, err = cmd.Output()
if err == nil {
return "clang found", nil
}
return "", errors.New("could not find gcc or clang compilers")
}},
{"Go bin PATH", "Verify that the PATH environment is set", func() (string, error) {
home, _ := os.UserHomeDir()
goPath := filepath.Join(home, "go", "bin")
cmd := tools.CommandInShell("go", "env", "GOBIN")
ret, err := cmd.Output()
if err != nil {
return "", err
}
bin := strings.TrimSpace(string(ret))
if bin != "" {
goPath = bin
} else {
cmd = tools.CommandInShell("go", "env", "GOPATH")
ret, err = cmd.Output()
if err != nil {
return "", err
}
path := strings.TrimSpace(string(ret))
if path != "" {
goPath = filepath.Join(path, "bin")
}
}
isMsys := false
if runtime.GOOS == "windows" {
cygpath, err := exec.LookPath("cygpath.exe")
if err == nil {
isMsys = true
goPath = winToUnixPath(goPath, cygpath)
}
if isMsys { // use the shell environment
cmd = exec.Command("env")
} else {
cmd = tools.CommandInShell("set")
}
} else {
cmd = tools.CommandInShell("env")
}
ret, err = cmd.Output()
allPath := string(ret)
if err == nil {
for _, line := range strings.Split(allPath, "\n") {
line = strings.TrimSpace(line)
if len(line) > 5 && (line[:5] == "PATH=" || line[:5] == "Path=") {
allPath = line
break
}
}
} else if runtime.GOOS == "windows" {
cmd = tools.CommandInShell("PowerShell", "-Command", "Get-ChildItem", "Env:Path", "|", "select", "Value", "|", "Format-List")
ret, err = cmd.Output()
if err != nil {
return "", err
}
lines := strings.Split(string(ret), "\n")
allPath = ""
for _, line := range lines {
allPath += strings.TrimSpace(line)
}
}
if !strings.Contains(allPath, goPath) {
return "", errors.New("PATH missing " + goPath)
}
return "confirmed", nil
}},
//{"Dependencies installed", "Various libraries required are present", func() (string, error) {
// return "", errors.New("no dependencies found")
//}},
{"Fyne helper", "Checking Fyne tool is installed for packaging", func() (string, error) {
cmd := tools.CommandInShell("fyne", "--version")
ret, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(ret)), nil
}},
}
// winToUnixPath converts a home path from windows to unix using the given cygpath path.
func winToUnixPath(in, convert string) string {
unix, _ := exec.Command(convert, "-u", "~").Output()
win, _ := exec.Command(convert, "-w", "~").Output()
in = strings.ReplaceAll(in, strings.TrimSpace(string(win)), strings.TrimSpace(string(unix)))
return strings.ReplaceAll(in, "\\", "/")
}