-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeneric_manager.go
More file actions
170 lines (145 loc) · 4.06 KB
/
Copy pathgeneric_manager.go
File metadata and controls
170 lines (145 loc) · 4.06 KB
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
170
package managers
import (
"context"
"github.com/git-pkgs/managers/definitions"
)
const argPackage = "package"
type GenericManager struct {
def *definitions.Definition
dir string
translator *Translator
runner Runner
warnings []string
}
func (m *GenericManager) Name() string {
return m.def.Name
}
func (m *GenericManager) Ecosystem() string {
return m.def.Ecosystem
}
func (m *GenericManager) Dir() string {
return m.dir
}
func (m *GenericManager) Warnings() []string {
return m.warnings
}
// run builds the command chain for an operation (base command plus any
// then: entries) and executes them in order. It returns the first
// command's result with subsequent results attached in Then. Execution
// stops after the first non-zero exit; Success() on the returned result
// reflects the whole chain.
func (m *GenericManager) run(ctx context.Context, operation string, input CommandInput) (*Result, error) {
cmds, err := m.translator.BuildCommands(m.def.Name, operation, input)
if err != nil {
return nil, err
}
var first *Result
for i, cmd := range cmds {
res, err := m.runner.Run(ctx, m.dir, cmd...)
if i == 0 {
first = res
} else if first != nil && res != nil {
first.Then = append(first.Then, res)
}
if err != nil {
return first, err
}
if res == nil || res.ExitCode != 0 {
return first, nil
}
}
return first, nil
}
func (m *GenericManager) Init(ctx context.Context) (*Result, error) {
return m.run(ctx, "init", CommandInput{})
}
func (m *GenericManager) Install(ctx context.Context, opts InstallOptions) (*Result, error) {
return m.run(ctx, "install", CommandInput{
Flags: map[string]any{
"frozen": opts.Frozen,
"clean": opts.Clean,
"production": opts.Production,
},
})
}
func (m *GenericManager) Add(ctx context.Context, pkg string, opts AddOptions) (*Result, error) {
input := CommandInput{
Args: map[string]string{argPackage: pkg},
Flags: map[string]any{
"dev": opts.Dev,
"optional": opts.Optional,
"exact": opts.Exact,
"workspace": opts.Workspace,
},
}
if opts.Version != "" {
input.Args["version"] = opts.Version
}
return m.run(ctx, "add", input)
}
func (m *GenericManager) Remove(ctx context.Context, pkg string) (*Result, error) {
return m.run(ctx, "remove", CommandInput{Args: map[string]string{argPackage: pkg}})
}
func (m *GenericManager) List(ctx context.Context) (*Result, error) {
return m.run(ctx, "list", CommandInput{})
}
func (m *GenericManager) Outdated(ctx context.Context) (*Result, error) {
return m.run(ctx, "outdated", CommandInput{})
}
func (m *GenericManager) Update(ctx context.Context, pkg string) (*Result, error) {
input := CommandInput{}
if pkg != "" {
input.Args = map[string]string{argPackage: pkg}
}
return m.run(ctx, "update", input)
}
func (m *GenericManager) Supports(cap Capability) bool {
capName := cap.String()
for _, c := range m.def.Capabilities {
if c == capName {
return true
}
}
return false
}
func (m *GenericManager) Capabilities() []Capability {
var caps []Capability
for _, name := range m.def.Capabilities {
if cap, ok := CapabilityFromString(name); ok {
caps = append(caps, cap)
}
}
return caps
}
func (m *GenericManager) Vendor(ctx context.Context) (*Result, error) {
return m.run(ctx, "vendor", CommandInput{})
}
func (m *GenericManager) Resolve(ctx context.Context) (*Result, error) {
return m.run(ctx, "resolve", CommandInput{})
}
func (m *GenericManager) Path(ctx context.Context, pkg string) (*PathResult, error) {
input := CommandInput{
Args: map[string]string{
argPackage: pkg,
},
Flags: map[string]any{},
}
cmd, err := m.translator.BuildCommand(m.def.Name, "path", input)
if err != nil {
return nil, err
}
result, err := m.runner.Run(ctx, m.dir, cmd...)
if err != nil {
return nil, err
}
var extract *definitions.Extract
if pathCmd, ok := m.def.Commands["path"]; ok {
extract = pathCmd.Extract
}
pathResult, err := extractPathResult(result.Stdout, extract, pkg)
if err != nil {
return &PathResult{Result: result}, err
}
pathResult.Result = result
return pathResult, nil
}