/
expander.go
274 lines (238 loc) · 7.46 KB
/
expander.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package project
import (
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/constraints"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/language"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/osutils"
"github.com/ActiveState/cli/internal/rxutils"
"github.com/ActiveState/cli/internal/scriptfile"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/projectfile"
)
type Expansion struct {
Project *Project
Script *Script
BashifyPaths bool
}
func NewExpansion(p *Project) *Expansion {
return &Expansion{Project: p}
}
// ApplyWithMaxDepth limits the depth of an expansion to avoid infinite expansion of a value.
func (ctx *Expansion) ApplyWithMaxDepth(s string, depth int) (string, error) {
if depth > constants.ExpanderMaxDepth {
return "", locale.NewInputError("err_expand_recursion", "Infinite recursion trying to expand variable '{{.V0}}'", s)
}
regex := regexp.MustCompile(`\${?(\w+)\.?([\w-]+)?\.?([\w\.-]+)?(\(\))?}?`)
var err error
expanded := rxutils.ReplaceAllStringSubmatchFunc(regex, s, func(groups []string) string {
if err != nil {
return ""
}
var variable, category, name, meta string
var isFunction bool
variable = groups[0]
if len(groups) == 2 {
category = "toplevel"
name = groups[1]
}
if len(groups) > 2 {
category = groups[1]
name = groups[2]
}
if len(groups) > 3 {
meta = groups[3]
}
lastGroup := groups[len(groups)-1]
if strings.HasPrefix(lastGroup, "(") && strings.HasSuffix(lastGroup, ")") {
isFunction = true
}
var value string
if expanderFn, foundExpander := expanderRegistry[category]; foundExpander {
var err2 error
if value, err2 = expanderFn(variable, name, meta, isFunction, ctx); err2 != nil {
err = errs.Wrap(err2, "Could not expand %s.%s", category, name)
return ""
}
} else {
return variable // we don't control this variable, so leave it as is
}
if value != "" && value != variable {
value, err = ctx.ApplyWithMaxDepth(value, depth+1)
}
return value
})
return expanded, err
}
// ExpandFromProject searches for $category.name-style variables in the given
// string and substitutes them with their contents, derived from the given
// project, and subject to the given constraints (if any).
func ExpandFromProject(s string, p *Project) (string, error) {
return NewExpansion(p).ApplyWithMaxDepth(s, 0)
}
// ExpandFromProjectBashifyPaths is like ExpandFromProject, but bashifies all instances of
// $script.name.path().
func ExpandFromProjectBashifyPaths(s string, p *Project) (string, error) {
expansion := &Expansion{Project: p, BashifyPaths: true}
return expansion.ApplyWithMaxDepth(s, 0)
}
func ExpandFromScript(s string, script *Script) (string, error) {
expansion := &Expansion{
Project: script.project,
Script: script,
BashifyPaths: runtime.GOOS == "windows" && (script.LanguageSafe()[0] == language.Bash || script.LanguageSafe()[0] == language.Sh),
}
return expansion.ApplyWithMaxDepth(s, 0)
}
// ExpanderFunc defines an Expander function which can expand the name for a category. An Expander expects the name
// to be expanded along with the project-file definition. It will return the expanded value of the name
// or a Failure if expansion was unsuccessful.
type ExpanderFunc func(variable, name, meta string, isFunction bool, ctx *Expansion) (string, error)
// EventExpander expands events defined in the project-file.
func EventExpander(_ string, name string, meta string, isFunction bool, ctx *Expansion) (string, error) {
projectFile := ctx.Project.Source()
constrained, err := constraints.FilterUnconstrained(pConditional, projectFile.Events.AsConstrainedEntities())
if err != nil {
return "", err
}
for _, v := range constrained {
if v.ID() == name {
return projectfile.MakeEventsFromConstrainedEntities([]projectfile.ConstrainedEntity{v})[0].Value, nil
}
}
return "", nil
}
// ScriptExpander expands scripts defined in the project-file.
func ScriptExpander(_ string, name string, meta string, isFunction bool, ctx *Expansion) (string, error) {
script := ctx.Project.ScriptByName(name)
if script == nil {
return "", nil
}
if !isFunction {
return script.Raw(), nil
}
if meta == "path" || meta == "path._posix" {
path, err := expandPath(name, script)
if err != nil {
return "", err
}
if ctx.BashifyPaths || meta == "path._posix" {
return osutils.BashifyPath(path)
}
return path, nil
}
return script.Raw(), nil
}
func expandPath(name string, script *Script) (string, error) {
if script.cachedFile() != "" {
return script.cachedFile(), nil
}
languages := script.LanguageSafe()
if len(languages) == 0 {
languages = DefaultScriptLanguage()
}
sf, err := scriptfile.NewEmpty(languages[0], name)
if err != nil {
return "", err
}
script.setCachedFile(sf.Filename())
v, err := script.Value()
if err != nil {
return "", err
}
err = sf.Write(v)
if err != nil {
return "", err
}
return sf.Filename(), nil
}
// userExpander
func userExpander(auth *authentication.Auth, element string) string {
if element == "name" {
return auth.WhoAmI()
}
if element == "email" {
return auth.Email()
}
if element == "jwt" {
return auth.BearerToken()
}
return ""
}
// Mixin provides expansions that are not sourced from a project file
type Mixin struct {
auth *authentication.Auth
}
// NewMixin creates a Mixin object providing extra expansions
func NewMixin(auth *authentication.Auth) *Mixin {
return &Mixin{auth}
}
// Expander expands mixin variables
func (m *Mixin) Expander(_ string, name string, meta string, _ bool, _ *Expansion) (string, error) {
if name == "user" {
return userExpander(m.auth, meta), nil
}
return "", nil
}
// ConstantExpander expands constants defined in the project-file.
func ConstantExpander(_ string, name string, meta string, isFunction bool, ctx *Expansion) (string, error) {
projectFile := ctx.Project.Source()
constrained, err := constraints.FilterUnconstrained(pConditional, projectFile.Constants.AsConstrainedEntities())
if err != nil {
return "", err
}
for _, v := range constrained {
if v.ID() == name {
return projectfile.MakeConstantsFromConstrainedEntities([]projectfile.ConstrainedEntity{v})[0].Value, nil
}
}
return "", nil
}
// ProjectExpander expands constants defined in the project-file.
func ProjectExpander(_ string, name string, _ string, isFunction bool, ctx *Expansion) (string, error) {
if !isFunction {
return "", nil
}
project := ctx.Project
switch name {
case "url":
return project.URL(), nil
case "commit":
commitID := project.LegacyCommitID() // Not using localcommit due to import cycle. See anti-pattern comment in localcommit pkg.
return commitID, nil
case "branch":
return project.BranchName(), nil
case "owner":
return project.Namespace().Owner, nil
case "name":
return project.Namespace().Project, nil
case "namespace":
return project.Namespace().String(), nil
case "path":
path := project.Source().Path()
if path == "" {
return path, nil
}
dir := filepath.Dir(path)
if ctx.BashifyPaths {
return osutils.BashifyPath(dir)
}
return dir, nil
}
return "", nil
}
func TopLevelExpander(variable string, name string, _ string, _ bool, ctx *Expansion) (string, error) {
projectFile := ctx.Project.Source()
switch name {
case "project":
return projectFile.Project, nil
case "lock":
return projectFile.Lock, nil
}
return variable, nil
}