forked from cue-lang/cue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instances.go
142 lines (125 loc) · 3.77 KB
/
instances.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
// Copyright 2018 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package load
// Files in package are to a large extent based on Go files from the following
// Go packages:
// - cmd/go/internal/load
// - go/build
import (
"fmt"
"os"
"github.com/wylswz/cue-se/cue/ast"
"github.com/wylswz/cue-se/cue/build"
"github.com/wylswz/cue-se/internal/filetypes"
// Trigger the unconditional loading of all core builtin packages if load
// is used. This was deemed the simplest way to avoid having to import
// this line explicitly, and thus breaking existing code, for the majority
// of cases, while not introducing an import cycle.
_ "github.com/wylswz/cue-se/pkg"
)
// Instances returns the instances named by the command line arguments 'args'.
// If errors occur trying to load an instance it is returned with Incomplete
// set. Errors directly related to loading the instance are recorded in this
// instance, but errors that occur loading dependencies are recorded in these
// dependencies.
func Instances(args []string, c *Config) []*build.Instance {
if c == nil {
c = &Config{}
}
newC, err := c.complete()
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}
c = newC
// TODO use predictable location
var deps *dependencies
var regClient *registryClient
if c.Registry != nil {
// TODO use configured cache directory.
tmpDir, err := os.MkdirTemp("", "cue-load-")
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}
regClient, err = newRegistryClient(c.Registry, tmpDir)
if err != nil {
return []*build.Instance{c.newErrInstance(fmt.Errorf("cannot make registry client: %v", err))}
}
deps1, err := resolveDependencies(c.modFile, regClient)
if err != nil {
return []*build.Instance{c.newErrInstance(fmt.Errorf("cannot resolve dependencies: %v", err))}
}
deps = deps1
}
tg := newTagger(c)
l := newLoader(c, tg, deps, regClient)
if c.Context == nil {
c.Context = build.NewContext(
build.Loader(l.buildLoadFunc()),
build.ParseFile(c.ParseFile),
)
}
// TODO: require packages to be placed before files. At some point this
// could be relaxed.
i := 0
for ; i < len(args) && filetypes.IsPackage(args[i]); i++ {
}
a := []*build.Instance{}
if len(args) == 0 || i > 0 {
for _, m := range l.importPaths(args[:i]) {
if m.Err != nil {
inst := c.newErrInstance(m.Err)
a = append(a, inst)
continue
}
a = append(a, m.Pkgs...)
}
}
if args = args[i:]; len(args) > 0 {
files, err := filetypes.ParseArgs(args)
if err != nil {
return []*build.Instance{c.newErrInstance(err)}
}
a = append(a, l.cueFilesPackage(files))
}
for _, p := range a {
tags, err := findTags(p)
if err != nil {
p.ReportError(err)
}
tg.tags = append(tg.tags, tags...)
}
// TODO(api): have API call that returns an error which is the aggregate
// of all build errors. Certain errors, like these, hold across builds.
if err := tg.injectTags(c.Tags); err != nil {
for _, p := range a {
p.ReportError(err)
}
return a
}
if tg.replacements == nil {
return a
}
for _, p := range a {
for _, f := range p.Files {
ast.Walk(f, nil, func(n ast.Node) {
if ident, ok := n.(*ast.Ident); ok {
if v, ok := tg.replacements[ident.Node]; ok {
ident.Node = v
}
}
})
}
}
return a
}