示例#1
0
func (b *Builder) getTarget(url parser.TargetURL) (n *Node) {

	if gnode, ok := b.Nodes[url.String()]; ok {
		return gnode
	} else {

		doc, err := parser.ReadBuildFile(url, b.Wd)

		if err != nil {
			log.Fatalf("getting target %s failed :%s", url.String(), err.Error())
		}

		if err := preprocessor.Process(doc); err != nil {
			log.Fatalf("error processing document: %s", err.Error())
		}

		var p processor.Processor

		for name, t := range p.Process(doc) {
			if t.GetName() != url.Target {
				continue
			}
			xu := parser.TargetURL{
				Package: url.Package,
				Target:  name,
			}

			node := Node{
				Target:   t,
				Children: make(map[string]*Node),
				Parents:  make(map[string]*Node),
				once:     sync.Once{},
				wg:       sync.WaitGroup{},
				Status:   Pending,
				Url:      xu,
			}

			post := postprocessor.New(url.Package)

			err := post.ProcessDependencies(node.Target)
			if err != nil {
				log.Fatal(err)
			}

			var deps []build.Target

			for _, d := range node.Target.GetDependencies() {
				c := b.Add(d)
				node.wg.Add(1)

				deps = append(deps, c.Target)

				node.Children[d] = c
				c.Parents[xu.String()] = &node

			}

			if err := post.ProcessPaths(t, deps); err != nil {
				log.Fatalf("path processing: %s", err.Error())
			}

			b.Nodes[xu.String()] = &node
			if t.GetName() == url.Target {
				n = &node
			}

		}

		if n == nil {
			log.Fatalf("we couldn't find %s, %s", url.String())
		}
		return n
	}

}
示例#2
0
func (p *Processor) runFunc(f *ast.Func) {
	switch f.Name {
	case "include_defs":
		// takes only one parameter
		packagePath := ""
		switch f.AnonParams[0].(type) {
		case string:
			packagePath = f.AnonParams[0].(string)
			break
		default:
			log.Fatal("include_defs takes a string as an argument")
		}

		document, err := parser.ReadBuildFile(parser.TargetURL{Package: packagePath}, p.wd)
		if err != nil {
			log.Fatal(err)
		}
		if p.document.Vars == nil {
			p.document.Vars = make(map[string]interface{})
		}
		for k, v := range document.Vars {
			p.document.Vars[k] = v
		}
	case "load":

		filePath := ""
		var varsToImport []string
		// Check paramter types
		for i, param := range f.AnonParams {
			switch param.(type) {
			case string:
				if i == 0 {
					filePath = param.(string)
				} else {
					varsToImport = append(varsToImport, param.(string))
				}
				break
			default:
				log.Fatal("should be used like so; load(file, var...)")
			}
		}

		document, err := parser.ReadFile(p.absPath(filePath))

		if err != nil {
			log.Fatal(err)
		}
		if p.document.Vars == nil {
			p.document.Vars = make(map[string]interface{})
		}

		for _, v := range varsToImport {

			if val, ok := document.Vars[v]; ok {
				p.document.Vars[v] = val
			} else {
				log.Fatalf("%s is not present at %s. Please check the file and try again.", v, filePath)
			}
		}
	case "select":
	default:
		targ, err := p.makeTarget(f)
		if err != nil {
			log.Fatal(err)
			return
		}
		p.targs[targ.GetName()] = targ
	}
}