github.com/tiagovtristao/plz@v13.4.0+incompatible/src/parse/init.go (about)

     1  package parse
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"path"
     7  	"sort"
     8  	"strings"
     9  
    10  	"github.com/thought-machine/please/src/core"
    11  	"github.com/thought-machine/please/src/parse/asp"
    12  	"github.com/thought-machine/please/src/parse/rules"
    13  )
    14  
    15  // InitParser initialises the parser engine. This is guaranteed to be called exactly once before any calls to Parse().
    16  func InitParser(state *core.BuildState) {
    17  	state.Parser = &aspParser{asp: newAspParser(state)}
    18  }
    19  
    20  // An aspParser implements the core.Parser interface around our asp package.
    21  type aspParser struct {
    22  	asp *asp.Parser
    23  }
    24  
    25  // newAspParser returns a asp.Parser object with all the builtins loaded
    26  func newAspParser(state *core.BuildState) *asp.Parser {
    27  	p := asp.NewParser(state)
    28  	log.Debug("Loading built-in build rules...")
    29  	dir, _ := rules.AssetDir("")
    30  	sort.Strings(dir)
    31  	for _, filename := range dir {
    32  		if strings.HasSuffix(filename, ".gob") {
    33  			srcFile := strings.TrimSuffix(filename, ".gob")
    34  			src, _ := rules.Asset(srcFile)
    35  			p.MustLoadBuiltins("src/parse/rules/"+srcFile, src, rules.MustAsset(filename))
    36  		}
    37  	}
    38  
    39  	for _, preload := range state.Config.Parse.PreloadBuildDefs {
    40  		log.Debug("Preloading build defs from %s...", preload)
    41  		p.MustLoadBuiltins(path.Join(core.RepoRoot, preload), nil, nil)
    42  	}
    43  
    44  	log.Debug("Parser initialised")
    45  	return p
    46  }
    47  
    48  func (p *aspParser) ParseFile(state *core.BuildState, pkg *core.Package, filename string) error {
    49  	return p.asp.ParseFile(pkg, filename)
    50  }
    51  
    52  func (p *aspParser) ParseReader(state *core.BuildState, pkg *core.Package, reader io.ReadSeeker) error {
    53  	_, err := p.asp.ParseReader(pkg, reader)
    54  	return err
    55  }
    56  
    57  func (p *aspParser) RunPreBuildFunction(threadID int, state *core.BuildState, target *core.BuildTarget) error {
    58  	return p.runBuildFunction(threadID, state, target, "pre", func() error {
    59  		return target.PreBuildFunction.Call(target)
    60  	})
    61  }
    62  
    63  func (p *aspParser) RunPostBuildFunction(threadID int, state *core.BuildState, target *core.BuildTarget, output string) error {
    64  	return p.runBuildFunction(threadID, state, target, "post", func() error {
    65  		log.Debug("Running post-build function for %s. Build output:\n%s", target.Label, output)
    66  		return target.PostBuildFunction.Call(target, output)
    67  	})
    68  }
    69  
    70  // runBuildFunction runs either the pre- or post-build function.
    71  func (p *aspParser) runBuildFunction(tid int, state *core.BuildState, target *core.BuildTarget, callbackType string, f func() error) error {
    72  	state.LogBuildResult(tid, target.Label, core.PackageParsing, fmt.Sprintf("Running %s-build function for %s", callbackType, target.Label))
    73  	pkg := state.Graph.PackageByLabel(target.Label)
    74  	changed, err := pkg.EnterBuildCallback(f)
    75  	if err != nil {
    76  		state.LogBuildError(tid, target.Label, core.ParseFailed, err, "Failed %s-build function for %s", callbackType, target.Label)
    77  	} else {
    78  		rescanDeps(state, changed)
    79  		state.LogBuildResult(tid, target.Label, core.TargetBuilding, fmt.Sprintf("Finished %s-build function for %s", callbackType, target.Label))
    80  	}
    81  	return err
    82  }