/
deps.go
169 lines (159 loc) · 4.87 KB
/
deps.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"sync"
)
// Arch Linux:
// - Obviously, needs to use pacman instead of apt
// - Some sort of libcrypto: no version information available thing
// - Needs wget
// - Had weird issues with gut precompiled binaries:
// - "cannot run rev-list: No such file or directory"
// - "cannot run unpack-objects: No such file or directory"
// - "fetch-pack: unable to fork off unpack-objects"
// - `gut build --build-deps` worked, though.
func (ctx *SyncContext) tryRun(args ...string) bool {
_, _, retCode, err := ctx.Run(args...)
if err != nil {
return false
}
return retCode == 0
}
func (ctx *SyncContext) listMissingLocalDeps() []string {
missing := []string{}
if !ctx.HasGutInstalled() {
// This is needed to build & install gut, but then aren't needed after that.
if !ctx.tryRun("git", "version") {
missing = append(missing, "git")
}
}
return missing
}
func (ctx *SyncContext) listMissingRemoteDeps() []string {
missing := []string{}
if !ctx.IsLocal() {
if !ctx.tryRun("which", "inotifywait") {
if !ctx.tryRun("fswatch", "--version") {
if ctx.IsDarwin() {
missing = append(missing, "fswatch")
} else {
missing = append(missing, "inotify-tools")
}
}
}
}
if !ctx.HasGutInstalled() {
// These are needed to build & install gut, but then aren't needed after that.
if !ctx.tryRun("autoconf", "--version") {
missing = append(missing, "autoconf")
}
if !ctx.tryRun("make", "--version") || !ctx.tryRun("gcc", "--version") {
missing = append(missing, "build-essential")
}
if ctx.IsLinux() {
// On errors, just skip this check
exists, err := ctx.PathExists("/usr/local/include/zlib.h")
if err == nil {
if !exists {
exists, err = ctx.PathExists("/usr/include/zlib.h")
if err == nil {
if !exists {
missing = append(missing, "zlib1g-dev")
}
}
}
}
}
if ctx.IsLinux() && !ctx.tryRun("msgfmt", "--version") {
missing = append(missing, "gettext")
}
}
return missing
}
func (ctx *SyncContext) CheckLocalDeps() (err error) {
missing := ctx.listMissingLocalDeps()
missing = append(missing, ctx.listMissingRemoteDeps()...)
return ctx.MissingDependency(missing...)
}
func (ctx *SyncContext) CheckRemoteDeps() (err error) {
missing := ctx.listMissingRemoteDeps()
return ctx.MissingDependency(missing...)
}
// FWIW, here's the snippet used in python-gut to install inotify-win:
// gut_build.git_clone_update(config.INOTIFY_WIN_REPO_URL, config.INOTIFY_WIN_PATH, config.INOTIFY_WIN_VERSION)
// status.out('(@dim)Building (@r)inotify-win(@dim)...')
// with context.cwd(context.path(config.INOTIFY_WIN_PATH)):
// context['cmd']['/c', '%WINDIR%\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe /t:exe /out:inotifywait.exe src\\*.cs']()
var mutex sync.Mutex
func (ctx *SyncContext) MissingDependency(names ...string) (err error) {
if len(names) == 0 {
return nil
}
// Only bombard the user with on MissingDependency dialog at a time, even though we'll do the
// dependency checks in parallel (for fast startup).
mutex.Lock()
defer mutex.Unlock()
logger := ctx.Logger()
depsStr := strings.Join(names, " ")
depsStrCommas := strings.Join(names, logger.Colorify("@(dim:,) "))
depsStrLong := JoinWithAndAndCommas(names...)
noun := "dependency"
quantifier := "A"
pronoun := "it"
if len(names) > 1 {
noun = "dependencies"
quantifier = "These"
pronoun = "them"
}
depsError := errors.New(fmt.Sprintf("Missing %s: %s", noun, depsStr))
logger.Printf("@(error:%s %s appear to be missing on) %s@(error::) %s\n", quantifier, noun, ctx.NameAnsi(), depsStrCommas)
installCmd := ""
if ctx.IsLinux() {
installCmd = "sudo apt update && sudo apt install -y"
} else if ctx.IsDarwin() {
installCmd = "brew install"
} else {
return depsError
}
installCmd += " " + depsStr
logger.Printf("I think we might be able to install %s by running this command:\n", pronoun)
ctx.Logger().Printf("@(dim:$) %s\n", installCmd)
attempts := 0
for {
logger.Printf("Would you like gut-sync to try to install %s [Y/n]? ", pronoun)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
logger.Replacef("")
attempts++
if text == "" || text == "y" || text == "Y" {
break
} else if attempts >= 3 || text == "N" || text == "n" || text == "q" || text == "Q" {
Shutdown("", 1)
}
}
logger.Printf("@(dim:Attempting to install) %s on %s@(dim:...)\n", depsStrLong, ctx.NameAnsi())
var retCode int
for _, cmd := range strings.Split(installCmd, " && ") {
ctx.Logger().Printf("@(dim:$) %s\n", cmd)
retCode, err = ctx.ShellInteractive(cmd)
if err != nil {
logger.Bail(err)
}
if retCode != 0 {
break
}
}
if retCode == 0 {
logger.Printf("@(dim:Successfully installed) %s@(dim:.)\n", depsStrLong)
ctx.ResetHasGutInstalled()
} else {
logger.Printf("@(error:Installation failed.)\n")
Shutdown("", 1)
}
return nil
}