/
mac_launch_target.go
73 lines (61 loc) 路 1.83 KB
/
mac_launch_target.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
package runner
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
type MacLaunchTarget struct {
Path string
IsAppBundle bool
}
func (t *MacLaunchTarget) String() string {
kind := "naked executable"
if t.IsAppBundle {
kind = "app bundle"
}
return fmt.Sprintf("(%s) [%s]", t.Path, kind)
}
// PrepareMacLaunchTarget looks at a path and tries to figure out if
// it's a mac app bundle, an executable inside of a mac app bundle,
// or just as naked executable.
func PrepareMacLaunchTarget(params RunnerParams) (*MacLaunchTarget, error) {
consumer := params.Consumer
target := &MacLaunchTarget{
Path: params.FullTargetPath,
}
stats, err := os.Stat(params.FullTargetPath)
if err != nil {
return nil, errors.WithMessage(err, "while preparing mac launch target")
}
if stats.IsDir() {
if PathLooksLikeAppBundle(target.Path) {
consumer.Infof("(%s) is a directory and ends with .app - looks like an app bundle alright.", target.Path)
target.IsAppBundle = true
return target, nil
}
return nil, errors.New("(%s) is a directory but does not in .app - doesn't look like an app bundle")
}
{
currentPath := target.Path
for currentPath != params.InstallFolder {
nextPath := filepath.Dir(currentPath)
if nextPath == currentPath {
break
}
if PathLooksLikeAppBundle(nextPath) {
target.Path = nextPath
target.IsAppBundle = true
consumer.Infof("(%s) looks like the real bundle, using that.", target.Path)
return target, nil
}
currentPath = nextPath
}
}
consumer.Infof("(%s) assumed naked executable (not an app bundle and not contained in an app bundle)", target.Path)
return target, nil
}
func PathLooksLikeAppBundle(dir string) bool {
return strings.HasSuffix(strings.ToLower(dir), ".app")
}