...

Source file src/cuelang.org/go/cue/load/loader.go

Documentation: cuelang.org/go/cue/load

     1  // Copyright 2018 The 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 load
    16  
    17  // Files in this package are to a large extent based on Go files from the following
    18  // Go packages:
    19  //    - cmd/go/internal/load
    20  //    - go/build
    21  
    22  import (
    23  	"path/filepath"
    24  
    25  	"cuelang.org/go/cue/build"
    26  	"cuelang.org/go/cue/errors"
    27  	"cuelang.org/go/cue/token"
    28  	"cuelang.org/go/internal/encoding"
    29  	"cuelang.org/go/internal/mod/modpkgload"
    30  
    31  	// Trigger the unconditional loading of all core builtin packages if load
    32  	// is used. This was deemed the simplest way to avoid having to import
    33  	// this line explicitly, and thus breaking existing code, for the majority
    34  	// of cases, while not introducing an import cycle.
    35  	_ "cuelang.org/go/pkg"
    36  )
    37  
    38  type loader struct {
    39  	cfg      *Config
    40  	tagger   *tagger
    41  	stk      importStack
    42  	loadFunc build.LoadFunc
    43  	pkgs     *modpkgload.Packages
    44  }
    45  
    46  func newLoader(c *Config, tg *tagger, pkgs *modpkgload.Packages) *loader {
    47  	l := &loader{
    48  		cfg:    c,
    49  		tagger: tg,
    50  		pkgs:   pkgs,
    51  	}
    52  	l.loadFunc = l._loadFunc
    53  	return l
    54  }
    55  
    56  func (l *loader) buildLoadFunc() build.LoadFunc {
    57  	return l.loadFunc
    58  }
    59  
    60  func (l *loader) abs(filename string) string {
    61  	if !isLocalImport(filename) {
    62  		return filename
    63  	}
    64  	return filepath.Join(l.cfg.Dir, filename)
    65  }
    66  
    67  func (l *loader) errPkgf(importPos []token.Pos, format string, args ...interface{}) *PackageError {
    68  	err := &PackageError{
    69  		ImportStack: l.stk.Copy(),
    70  		Message:     errors.NewMessagef(format, args...),
    71  	}
    72  	err.fillPos(l.cfg.Dir, importPos)
    73  	return err
    74  }
    75  
    76  // cueFilesPackage creates a package for building a collection of CUE files
    77  // (typically named on the command line).
    78  func (l *loader) cueFilesPackage(files []*build.File) *build.Instance {
    79  	cfg := l.cfg
    80  	cfg.filesMode = true
    81  	// ModInit() // TODO: support modules
    82  	pkg := l.cfg.Context.NewInstance(cfg.Dir, l.loadFunc)
    83  
    84  	for _, bf := range files {
    85  		f := bf.Filename
    86  		if f == "-" {
    87  			continue
    88  		}
    89  		if !filepath.IsAbs(f) {
    90  			f = filepath.Join(cfg.Dir, f)
    91  		}
    92  		fi, err := cfg.fileSystem.stat(f)
    93  		if err != nil {
    94  			return cfg.newErrInstance(errors.Wrapf(err, token.NoPos, "could not find file %v", f))
    95  		}
    96  		if fi.IsDir() {
    97  			return cfg.newErrInstance(errors.Newf(token.NoPos, "file is a directory %v", f))
    98  		}
    99  	}
   100  
   101  	fp := newFileProcessor(cfg, pkg, l.tagger)
   102  	for _, file := range files {
   103  		fp.add(cfg.Dir, file, allowAnonymous)
   104  	}
   105  
   106  	// TODO: ModImportFromFiles(files)
   107  	pkg.Dir = cfg.Dir
   108  	rewriteFiles(pkg, pkg.Dir, true)
   109  	for _, err := range errors.Errors(fp.finalize(pkg)) { // ImportDir(&ctxt, dir, 0)
   110  		var x *NoFilesError
   111  		if len(pkg.OrphanedFiles) == 0 || !errors.As(err, &x) {
   112  			pkg.ReportError(err)
   113  		}
   114  	}
   115  	// TODO: Support module importing.
   116  	// if ModDirImportPath != nil {
   117  	// 	// Use the effective import path of the directory
   118  	// 	// for deciding visibility during pkg.load.
   119  	// 	bp.ImportPath = ModDirImportPath(dir)
   120  	// }
   121  
   122  	l.addFiles(cfg.Dir, pkg)
   123  
   124  	pkg.User = true
   125  	l.stk.Push("user")
   126  	_ = pkg.Complete()
   127  	l.stk.Pop()
   128  	pkg.User = true
   129  	//pkg.LocalPrefix = dirToImportPath(dir)
   130  	pkg.DisplayPath = "command-line-arguments"
   131  
   132  	return pkg
   133  }
   134  
   135  func (l *loader) addFiles(dir string, p *build.Instance) {
   136  	for _, f := range p.BuildFiles {
   137  		d := encoding.NewDecoder(f, &encoding.Config{
   138  			Stdin:     l.cfg.stdin(),
   139  			ParseFile: l.cfg.ParseFile,
   140  		})
   141  		for ; !d.Done(); d.Next() {
   142  			_ = p.AddSyntax(d.File())
   143  		}
   144  		if err := d.Err(); err != nil {
   145  			p.ReportError(errors.Promote(err, "load"))
   146  		}
   147  		d.Close()
   148  	}
   149  }
   150  

View as plain text