cuelang.org/go@v0.13.0/internal/core/runtime/build.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "strings" 19 20 "cuelang.org/go/cue/ast" 21 "cuelang.org/go/cue/ast/astutil" 22 "cuelang.org/go/cue/build" 23 "cuelang.org/go/cue/errors" 24 "cuelang.org/go/cue/stats" 25 "cuelang.org/go/internal/core/adt" 26 "cuelang.org/go/internal/core/compile" 27 ) 28 29 type Config struct { 30 Runtime *Runtime 31 Filename string 32 ImportPath string 33 34 Counts *stats.Counts 35 36 compile.Config 37 } 38 39 // Build builds b and all its transitive dependencies, insofar they have not 40 // been build yet. 41 func (x *Runtime) Build(cfg *Config, b *build.Instance) (v *adt.Vertex, errs errors.Error) { 42 if err := b.Complete(); err != nil { 43 return nil, b.Err 44 } 45 if v := x.getNodeFromInstance(b); v != nil { 46 return v, b.Err 47 } 48 // TODO: clear cache of old implementation. 49 // if s := b.ImportPath; s != "" { 50 // // Use cached result, if available. 51 // if v, err := x.LoadImport(s); v != nil || err != nil { 52 // return v, err 53 // } 54 // } 55 56 errs = b.Err 57 58 // Build transitive dependencies. 59 for _, file := range b.Files { 60 file.VisitImports(func(d *ast.ImportDecl) { 61 for _, s := range d.Specs { 62 errs = errors.Append(errs, x.buildSpec(cfg, b, s)) 63 } 64 }) 65 } 66 67 err := x.ResolveFiles(b) 68 errs = errors.Append(errs, err) 69 70 var cc *compile.Config 71 if cfg != nil { 72 cc = &cfg.Config 73 } 74 if cfg != nil && cfg.ImportPath != "" { 75 b.ImportPath = cfg.ImportPath 76 b.PkgName = astutil.ImportPathName(b.ImportPath) 77 } 78 v, err = compile.Files(cc, x, b.ID(), b.Files...) 79 errs = errors.Append(errs, err) 80 81 errs = errors.Append(errs, x.InjectImplementations(b, v)) 82 83 if errs != nil { 84 v = adt.ToVertex(&adt.Bottom{Err: errs}) 85 b.Err = errs 86 } 87 88 x.AddInst(b.ImportPath, v, b) 89 90 return v, errs 91 } 92 93 func (r *Runtime) Compile(cfg *Config, source interface{}) (*adt.Vertex, *build.Instance) { 94 ctx := build.NewContext() 95 var filename string 96 if cfg != nil && cfg.Filename != "" { 97 filename = cfg.Filename 98 } 99 p := ctx.NewInstance(filename, nil) 100 if err := p.AddFile(filename, source); err != nil { 101 return nil, p 102 } 103 v, _ := r.Build(cfg, p) 104 return v, p 105 } 106 107 func (r *Runtime) CompileFile(cfg *Config, file *ast.File) (*adt.Vertex, *build.Instance) { 108 ctx := build.NewContext() 109 filename := file.Filename 110 if cfg != nil && cfg.Filename != "" { 111 filename = cfg.Filename 112 } 113 p := ctx.NewInstance(filename, nil) 114 err := p.AddSyntax(file) 115 if err != nil { 116 return nil, p 117 } 118 p.PkgName = file.PackageName() 119 v, _ := r.Build(cfg, p) 120 return v, p 121 } 122 123 func (x *Runtime) buildSpec(cfg *Config, b *build.Instance, spec *ast.ImportSpec) (errs errors.Error) { 124 info, err := astutil.ParseImportSpec(spec) 125 if err != nil { 126 return errors.Promote(err, "invalid import path") 127 } 128 129 pkg := b.LookupImport(info.ID) 130 if pkg == nil { 131 if strings.Contains(info.ID, ".") { 132 return errors.Newf(spec.Pos(), 133 "package %q imported but not defined in %s", 134 info.ID, b.ImportPath) 135 } else if x.index.builtinPaths[info.ID] == nil { 136 return errors.Newf(spec.Pos(), 137 "builtin package %q undefined", info.ID) 138 } 139 return nil 140 } 141 142 if v := x.getNodeFromInstance(pkg); v != nil { 143 return pkg.Err 144 } 145 146 if _, err := x.Build(cfg, pkg); err != nil { 147 return err 148 } 149 150 return nil 151 }