func()

in cmd/kazel/sourcerer.go [40:109]


func (v *Vendorer) walkSource(pkgPath string) ([]string, error) {
	// clean pkgPath since we access v.newRules directly
	pkgPath = filepath.Clean(pkgPath)
	for _, r := range v.skippedPaths {
		if r.MatchString(pkgPath) {
			return nil, nil
		}
	}
	files, err := ioutil.ReadDir(pkgPath)
	if err != nil {
		return nil, err
	}

	// Find any children packages we need to include in an all-srcs rule.
	var children []string
	for _, f := range files {
		if f.IsDir() {
			c, err := v.walkSource(filepath.Join(pkgPath, f.Name()))
			if err != nil {
				return nil, err
			}
			children = append(children, c...)
		}
	}

	// This path is a package either if we've added rules or if a BUILD file already exists.
	_, hasRules := v.newRules[pkgPath]
	isPkg := hasRules
	if !isPkg {
		isPkg, _ = findBuildFile(pkgPath)
	}

	if !isPkg {
		// This directory isn't a package (doesn't contain a BUILD file),
		// but there might be subdirectories that are packages,
		// so pass that up to our parent.
		return children, nil
	}

	// Enforce formatting the BUILD file, even if we're not adding srcs rules
	if !hasRules {
		v.addRules(pkgPath, nil)
	}

	if !v.cfg.AddSourcesRules {
		return nil, nil
	}

	pkgSrcsExpr := &build.LiteralExpr{Token: `glob(["**"])`}
	if pkgPath == "." {
		pkgSrcsExpr = &build.LiteralExpr{Token: `glob(["**"], exclude=["bazel-*/**", ".git/**"])`}
	}

	v.addRules(pkgPath, []*build.Rule{
		newRule("filegroup",
			pkgSrcsTarget,
			map[string]build.Expr{
				"srcs":       pkgSrcsExpr,
				"visibility": asExpr([]string{"//visibility:private"}),
			}),
		newRule("filegroup",
			allSrcsTarget,
			map[string]build.Expr{
				"srcs": asExpr(append(children, fmt.Sprintf(":%s", pkgSrcsTarget))),
				// TODO: should this be more restricted?
				"visibility": asExpr([]string{"//visibility:public"}),
			}),
	})
	return []string{fmt.Sprintf("//%s:%s", pkgPath, allSrcsTarget)}, nil
}