-
Notifications
You must be signed in to change notification settings - Fork 103
/
ext_descriptor.go
89 lines (76 loc) · 2.3 KB
/
ext_descriptor.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
// Buildpack descriptor file (https://github.com/buildpacks/spec/blob/main/buildpack.md#buildpacktoml-toml).
package buildpack
import (
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
type ExtDescriptor struct {
WithAPI string `toml:"api"`
Extension ExtInfo `toml:"extension"`
WithRootDir string `toml:"-"`
Targets []TargetMetadata `toml:"targets"`
}
type ExtInfo struct {
BaseInfo
}
func ReadExtDescriptor(path string) (*ExtDescriptor, error) {
var (
descriptor *ExtDescriptor
err error
)
if _, err = toml.DecodeFile(path, &descriptor); err != nil {
return &ExtDescriptor{}, err
}
if descriptor.WithRootDir, err = filepath.Abs(filepath.Dir(path)); err != nil {
return &ExtDescriptor{}, err
}
err = descriptor.inferTargets()
return descriptor, err
}
func (d *ExtDescriptor) inferTargets() error {
if len(d.Targets) == 0 {
binDir := filepath.Join(d.WithRootDir, "bin")
if stat, _ := os.Stat(binDir); stat != nil {
binFiles, err := os.ReadDir(binDir)
if err != nil {
return err
}
var windowsDetected, linuxDetected bool
for i := 0; i < len(binFiles); i++ { // detect and generate files are optional
bf := binFiles[len(binFiles)-i-1] // we're iterating backwards b/c os.ReadDir sorts "foo.exe" after "foo" but we want to preferentially detect windows first.
fname := bf.Name()
if !windowsDetected && (fname == "detect.exe" || fname == "detect.bat" || fname == "generate.exe" || fname == "generate.bat") {
d.Targets = append(d.Targets, TargetMetadata{OS: "windows"})
windowsDetected = true
}
if !linuxDetected && (fname == "detect" || fname == "generate") {
d.Targets = append(d.Targets, TargetMetadata{OS: "linux"})
linuxDetected = true
}
}
}
}
if len(d.Targets) == 0 {
d.Targets = append(d.Targets, TargetMetadata{}) // matches any
}
return nil
}
func (d *ExtDescriptor) API() string {
return d.WithAPI
}
func (d *ExtDescriptor) ClearEnv() bool {
return d.Extension.ClearEnv
}
func (d *ExtDescriptor) Homepage() string {
return d.Extension.Homepage
}
func (d *ExtDescriptor) RootDir() string {
return d.WithRootDir
}
func (d *ExtDescriptor) String() string {
return d.Extension.Name + " " + d.Extension.Version
}
func (d *ExtDescriptor) TargetsList() []TargetMetadata {
return d.Targets
}