forked from golang/vgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x/vgo: add deplist command to get build information about (test) deps
This CL is a work in progress for golang/go#24661. Following the pattern laid down by the vet command, we want to pass the build details of the transitive (test) deps of a list of packages onto vetters/linters etc that need to load imports of said packages for go/types (and similar) analysis. Fixes golang/go#24661 Change-Id: If1496dd6a3ed501ad6f124226a05f5d57284c57d
- Loading branch information
Showing
5 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2011 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package deplist implements the ``go deplist'' command. | ||
package deplist | ||
|
||
import ( | ||
"cmd/go/internal/base" | ||
"cmd/go/internal/load" | ||
"cmd/go/internal/work" | ||
) | ||
|
||
var CmdDeplist = &base.Command{ | ||
Run: runDeplist, | ||
CustomFlags: true, | ||
UsageLine: "deplist [-n] [-x] [build flags] [deplist flags] [packages]", | ||
Short: "provide JSON package build information for dependencies of packages", | ||
Long: ` | ||
Deplist provide JSON package build information for dependencies of packages | ||
`, | ||
} | ||
|
||
func runDeplist(cmd *base.Command, args []string) { | ||
deplistFlags, pkgArgs := deplistFlags(args) | ||
|
||
work.BuildInit() | ||
|
||
test := false | ||
build := false | ||
|
||
for _, v := range deplistFlags { | ||
switch v { | ||
case "-test": | ||
test = true | ||
case "-build": | ||
build = true | ||
} | ||
} | ||
|
||
if !build { | ||
base.Fatalf("don't yet know what to do without -build flag") | ||
} | ||
|
||
deps := make(map[string]bool) | ||
var testDeps []string | ||
|
||
pkgs := load.PackagesAndErrors(pkgArgs) | ||
if len(pkgs) == 0 { | ||
base.Fatalf("no packages to deplist") | ||
} | ||
|
||
for _, p := range pkgs { | ||
for _, d := range p.Deps { | ||
deps[d] = true | ||
} | ||
|
||
if test { | ||
for _, d := range p.TestImports { | ||
testDeps = append(testDeps, d) | ||
} | ||
for _, d := range p.XTestImports { | ||
testDeps = append(testDeps, d) | ||
} | ||
} | ||
} | ||
|
||
var testPkgArgs []string | ||
|
||
for _, d := range testDeps { | ||
if !deps[d] { | ||
testPkgArgs = append(testPkgArgs, d) | ||
} | ||
} | ||
|
||
if len(testPkgArgs) > 0 { | ||
for _, p := range load.PackagesAndErrors(testPkgArgs) { | ||
deps[p.ImportPath] = true | ||
for _, d := range p.Deps { | ||
deps[d] = true | ||
} | ||
} | ||
} | ||
|
||
var uniqDeps []string | ||
for p := range deps { | ||
uniqDeps = append(uniqDeps, p) | ||
} | ||
|
||
depPkgs := load.PackagesForBuild(uniqDeps) | ||
|
||
var b work.Builder | ||
b.Init() | ||
|
||
root := &work.Action{Mode: "go deplist"} | ||
for _, p := range depPkgs { | ||
root.Deps = append(root.Deps, b.DeplistAction(work.ModeBuild, work.ModeBuild, p)) | ||
} | ||
b.Do(root) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2017 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package deplist | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"cmd/go/internal/base" | ||
"cmd/go/internal/cmdflag" | ||
"cmd/go/internal/work" | ||
) | ||
|
||
const cmd = "deplist" | ||
|
||
// deplistFlagDefn is the set of flags we process. | ||
var deplistFlagDefn = []*cmdflag.Defn{ | ||
{Name: "test", BoolVar: new(bool)}, | ||
{Name: "build", BoolVar: new(bool)}, | ||
} | ||
|
||
var deplistTool string | ||
|
||
// add build flags to deplistFlagDefn. | ||
func init() { | ||
var cmd base.Command | ||
work.AddBuildFlags(&cmd) | ||
cmd.Flag.VisitAll(func(f *flag.Flag) { | ||
deplistFlagDefn = append(deplistFlagDefn, &cmdflag.Defn{ | ||
Name: f.Name, | ||
Value: f.Value, | ||
}) | ||
}) | ||
} | ||
|
||
// deplistFlags processes the command line, splitting it at the first non-flag | ||
// into the list of flags and list of packages. | ||
func deplistFlags(args []string) (passToDeplist, packageNames []string) { | ||
for i := 0; i < len(args); i++ { | ||
if !strings.HasPrefix(args[i], "-") { | ||
return args[:i], args[i:] | ||
} | ||
|
||
f, value, extraWord := cmdflag.Parse(cmd, deplistFlagDefn, args, i) | ||
if f == nil { | ||
fmt.Fprintf(os.Stderr, "deplist: flag %q not defined\n", args[i]) | ||
fmt.Fprintf(os.Stderr, "Run \"go help deplist\" for more information\n") | ||
os.Exit(2) | ||
} | ||
if f.Value != nil { | ||
if err := f.Value.Set(value); err != nil { | ||
base.Fatalf("invalid flag argument for -%s: %v", f.Name, err) | ||
} | ||
} | ||
if extraWord { | ||
i++ | ||
} | ||
} | ||
return args, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters