diff options
| author | Russ Cox <rsc@golang.org> | 2012-03-01 12:12:09 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2012-03-01 12:12:09 -0500 |
| commit | ebe1664d2789cd4ea0ded0eccb9067c729378cc5 (patch) | |
| tree | 95f94bacee98abc860d9510c097d8b2a590def91 /src/cmd/api/goapi.go | |
| parent | a72b87efa934957245449975a940763f49026a7c (diff) | |
| download | go-ebe1664d2789cd4ea0ded0eccb9067c729378cc5.tar.xz | |
go/build: replace FindTree, ScanDir, Tree, DirInfo with Import, Package
This is an API change, but one I have been promising would
happen when it was clear what the go command needed.
This is basically a complete replacement of what used to be here.
build.Tree is gone.
build.DirInfo is expanded and now called build.Package.
build.FindTree is now build.Import(package, srcDir, build.FindOnly).
The returned *Package contains information that FindTree returned,
but applicable only to a single package.
build.ScanDir is now build.ImportDir.
build.FindTree+build.ScanDir is now build.Import.
The new Import API allows specifying the source directory,
in order to resolve local imports (import "./foo") and also allows
scanning of packages outside of $GOPATH. They will come back
with less information in the Package, but they will still work.
The old go/build API exposed both too much and too little.
This API is much closer to what the go command needs,
and it works well enough in the other places where it is
used. Path is gone, so it can no longer be misused. (Fixes issue 2749.)
This CL updates clients of go/build other than the go command.
The go command changes are in a separate CL, to be submitted
at the same time.
R=golang-dev, r, alex.brainman, adg
CC=golang-dev
https://golang.org/cl/5713043
Diffstat (limited to 'src/cmd/api/goapi.go')
| -rw-r--r-- | src/cmd/api/goapi.go | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go index ee0f92328e..fe9c862f4f 100644 --- a/src/cmd/api/goapi.go +++ b/src/cmd/api/goapi.go @@ -74,16 +74,10 @@ func main() { pkgs = strings.Fields(string(stds)) } - tree, _, err := build.FindTree("os") // some known package - if err != nil { - log.Fatalf("failed to find tree: %v", err) - } - var featureCtx = make(map[string]map[string]bool) // feature -> context name -> true for _, context := range contexts { w := NewWalker() w.context = context - w.tree = tree for _, pkg := range pkgs { w.wantedPkg[pkg] = true @@ -95,7 +89,7 @@ func main() { strings.HasPrefix(pkg, "old/") { continue } - if !tree.HasSrc(pkg) { + if fi, err := os.Stat(filepath.Join(w.root, pkg)); err != nil || !fi.IsDir() { log.Fatalf("no source in tree for package %q", pkg) } w.WalkPackage(pkg) @@ -165,7 +159,7 @@ type pkgSymbol struct { type Walker struct { context *build.Context - tree *build.Tree + root string fset *token.FileSet scope []string features map[string]bool // set @@ -191,6 +185,7 @@ func NewWalker() *Walker { selectorFullPkg: make(map[string]string), wantedPkg: make(map[string]bool), prevConstType: make(map[pkgSymbol]string), + root: filepath.Join(build.Default.GOROOT, "src/pkg"), } } @@ -252,15 +247,13 @@ func (w *Walker) WalkPackage(name string) { defer func() { w.packageState[name] = loaded }() - dir := filepath.Join(w.tree.SrcDir(), filepath.FromSlash(name)) + dir := filepath.Join(w.root, filepath.FromSlash(name)) - var info *build.DirInfo - var err error - if ctx := w.context; ctx != nil { - info, err = ctx.ScanDir(dir) - } else { - info, err = build.ScanDir(dir) + ctxt := w.context + if ctxt == nil { + ctxt = &build.Default } + info, err := ctxt.ImportDir(dir, 0) if err != nil { if strings.Contains(err.Error(), "no Go source files") { return |
