buildutil

package
v0.0.0-...-076a18f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2023 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package buildutil provides utilities related to the go/build package in the standard library.

All I/O is done via the build.Context file system interface, which must be concurrency-safe.

Index

Constants

View Source
const TagsFlagDoc = "a list of `build tags` to consider satisfied during the build. " +
	"For more information about build tags, see the description of " +
	"build constraints in the documentation for the go/build package"

Variables

This section is empty.

Functions

func AllPackages

func AllPackages(ctxt *build.Context) []string

AllPackages returns the package path of each Go package in any source directory of the specified build context (e.g. $GOROOT or an element of $GOPATH). Errors are ignored. The results are sorted. All package paths are canonical, and thus may contain "/vendor/".

The result may include import paths for directories that contain no *.go files, such as "archive" (in $GOROOT/src).

All I/O is done via the build.Context file system interface, which must be concurrency-safe.

func ContainingPackage

func ContainingPackage(ctxt *build.Context, dir, filename string) (*build.Package, error)

ContainingPackage returns the package containing filename.

If filename is not absolute, it is interpreted relative to working directory dir. All I/O is via the build context's file system interface, if any.

The '...Files []string' fields of the resulting build.Package are not populated (build.FindOnly mode).

func ExpandPatterns

func ExpandPatterns(ctxt *build.Context, patterns []string) map[string]bool

ExpandPatterns returns the set of packages matched by patterns, which may have the following forms:

golang.org/x/tools/cmd/guru     # a single package
golang.org/x/tools/...          # all packages beneath dir
...                             # the entire workspace.

Order is significant: a pattern preceded by '-' removes matching packages from the set. For example, these patterns match all encoding packages except encoding/xml:

encoding/... -encoding/xml

A trailing slash in a pattern is ignored. (Path components of Go package names are separated by slash, not the platform's path separator.)

func FakeContext

func FakeContext(pkgs map[string]map[string]string) *build.Context

FakeContext returns a build.Context for the fake file tree specified by pkgs, which maps package import paths to a mapping from file base names to contents.

The fake Context has a GOROOT of "/go" and no GOPATH, and overrides the necessary file access methods to read from memory instead of the real file system.

Unlike a real file tree, the fake one has only two levels---packages and files---so ReadDir("/go/src/") returns all packages under /go/src/ including, for instance, "math" and "math/big". ReadDir("/go/src/math/big") would return all the files in the "math/big" package.

func FileExists

func FileExists(ctxt *build.Context, path string) bool

FileExists returns true if the specified file exists, using the build context's file system interface.

func ForEachPackage

func ForEachPackage(ctxt *build.Context, found func(importPath string, err error))

ForEachPackage calls the found function with the package path of each Go package it finds in any source directory of the specified build context (e.g. $GOROOT or an element of $GOPATH). All package paths are canonical, and thus may contain "/vendor/".

If the package directory exists but could not be read, the second argument to the found function provides the error.

All I/O is done via the build.Context file system interface, which must be concurrency-safe.

func HasSubdir

func HasSubdir(ctxt *build.Context, root, dir string) (rel string, ok bool)

HasSubdir calls ctxt.HasSubdir (if not nil) or else uses the local file system to answer the question.

func IsAbsPath

func IsAbsPath(ctxt *build.Context, path string) bool

IsAbsPath behaves like filepath.IsAbs, but uses the build context's file system interface, if any.

func IsDir

func IsDir(ctxt *build.Context, path string) bool

IsDir behaves like os.Stat plus IsDir, but uses the build context's file system interface, if any.

func JoinPath

func JoinPath(ctxt *build.Context, path ...string) string

JoinPath behaves like filepath.Join, but uses the build context's file system interface, if any.

func OpenFile

func OpenFile(ctxt *build.Context, path string) (io.ReadCloser, error)

OpenFile behaves like os.Open, but uses the build context's file system interface, if any.

func OverlayContext

func OverlayContext(orig *build.Context, overlay map[string][]byte) *build.Context

OverlayContext overlays a build.Context with additional files from a map. Files in the map take precedence over other files.

In addition to plain string comparison, two file names are considered equal if their base names match and their directory components point at the same directory on the file system. That is, symbolic links are followed for directories, but not files.

A common use case for OverlayContext is to allow editors to pass in a set of unsaved, modified files.

Currently, only the Context.OpenFile function will respect the overlay. This may change in the future.

func ParseFile

func ParseFile(fset *token.FileSet, ctxt *build.Context, displayPath func(string) string, dir string, file string, mode parser.Mode) (*ast.File, error)

ParseFile behaves like parser.ParseFile, but uses the build context's file system interface, if any.

If file is not absolute (as defined by IsAbsPath), the (dir, file) components are joined using JoinPath; dir must be absolute.

The displayPath function, if provided, is used to transform the filename that will be attached to the ASTs.

TODO(adonovan): call this from go/loader.parseFiles when the tree thaws.

func ParseOverlayArchive

func ParseOverlayArchive(archive io.Reader) (map[string][]byte, error)

ParseOverlayArchive parses an archive containing Go files and their contents. The result is intended to be used with OverlayContext.

Archive format

The archive consists of a series of files. Each file consists of a name, a decimal file size and the file contents, separated by newlines. No newline follows after the file contents.

func ReadDir

func ReadDir(ctxt *build.Context, path string) ([]os.FileInfo, error)

ReadDir behaves like ioutil.ReadDir, but uses the build context's file system interface, if any.

func SplitPathList

func SplitPathList(ctxt *build.Context, s string) []string

SplitPathList behaves like filepath.SplitList, but uses the build context's file system interface, if any.

Types

type TagsFlag

type TagsFlag []string

TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses a flag value in the same manner as go build's -tags flag and populates a []string slice.

See $GOROOT/src/go/build/doc.go for description of build tags. See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag.

Example:

flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)

func (*TagsFlag) Get

func (v *TagsFlag) Get() interface{}

func (*TagsFlag) Set

func (v *TagsFlag) Set(s string) error

func (*TagsFlag) String

func (v *TagsFlag) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL