本文整理汇总了Golang中go/build.Package.ImportPath方法的典型用法代码示例。如果您正苦于以下问题:Golang Package.ImportPath方法的具体用法?Golang Package.ImportPath怎么用?Golang Package.ImportPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/build.Package
的用法示例。
在下文中一共展示了Package.ImportPath方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
//.........这里部分代码省略.........
testFlags.BoolVar(&options.Minify, "m", false, "minify generated code")
testFlags.Parse(cmdArgs)
os.Exit(handleError(func() error {
pkgs := make([]*build.Package, testFlags.NArg())
for i, pkgPath := range testFlags.Args() {
pkgPath = filepath.ToSlash(pkgPath)
var err error
pkgs[i], err = gbuild.Import(pkgPath, 0, "js")
if err != nil {
return err
}
}
if len(pkgs) == 0 {
firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: Not sure if always picking first GOPATH workspace here is the right thing.
srcDir, err := filepath.EvalSymlinks(filepath.Join(firstGopathWorkspace, "src"))
if err != nil {
return err
}
var pkg *build.Package
if strings.HasPrefix(currentDirectory, srcDir) {
pkgPath, err := filepath.Rel(srcDir, currentDirectory)
if err != nil {
return err
}
if pkg, err = gbuild.Import(pkgPath, 0, "js"); err != nil {
return err
}
}
if pkg == nil {
if pkg, err = build.ImportDir(currentDirectory, 0); err != nil {
return err
}
pkg.ImportPath = "_" + currentDirectory
}
pkgs = []*build.Package{pkg}
}
var exitErr error
for _, buildPkg := range pkgs {
if len(buildPkg.TestGoFiles) == 0 && len(buildPkg.XTestGoFiles) == 0 {
fmt.Printf("? \t%s\t[no test files]\n", buildPkg.ImportPath)
continue
}
buildPkg.PkgObj = ""
buildPkg.GoFiles = append(buildPkg.GoFiles, buildPkg.TestGoFiles...)
pkg := &gbuild.PackageData{Package: buildPkg}
s := gbuild.NewSession(options)
if err := s.BuildPackage(pkg); err != nil {
return err
}
tests := &testFuncs{Package: buildPkg}
collectTests := func(pkg *gbuild.PackageData, testPkg string) bool {
for _, name := range pkg.Archive.Tests {
tests.Tests = append(tests.Tests, testFunc{Package: testPkg, Name: name})
}
return len(pkg.Archive.Tests) != 0
}
tests.NeedTest = collectTests(pkg, "_test")
if len(pkg.XTestGoFiles) != 0 {
testPkg := &gbuild.PackageData{Package: &build.Package{
ImportPath: pkg.ImportPath + "_test",
示例2: main
func main() {
flag.Parse()
cmd := flag.Arg(0)
switch cmd {
case "build":
buildFlags := flag.NewFlagSet("build", flag.ContinueOnError)
var pkgObj string
buildFlags.StringVar(&pkgObj, "o", "", "")
verbose := buildFlags.Bool("v", false, "print the names of packages as they are compiled")
watch := buildFlags.Bool("w", false, "watch for changes to the source files")
buildFlags.Parse(flag.Args()[1:])
for {
s := NewSession(*verbose, *watch)
exitCode := handleError(func() error {
if buildFlags.NArg() == 0 {
buildContext := &build.Context{
GOROOT: build.Default.GOROOT,
GOPATH: build.Default.GOPATH,
GOOS: build.Default.GOOS,
GOARCH: "js",
Compiler: "gc",
}
if s.watcher != nil {
s.watcher.Watch(currentDirectory)
}
buildPkg, err := buildContext.ImportDir(currentDirectory, 0)
if err != nil {
return err
}
pkg := &packageData{Package: buildPkg}
pkg.ImportPath = currentDirectory
if err := s.buildPackage(pkg); err != nil {
return err
}
if pkgObj == "" {
pkgObj = filepath.Base(currentDirectory) + ".js"
}
if err := s.writeCommandPackage(pkg, pkgObj); err != nil {
return err
}
return nil
}
if strings.HasSuffix(buildFlags.Arg(0), ".go") {
for _, arg := range buildFlags.Args() {
if !strings.HasSuffix(arg, ".go") {
return fmt.Errorf("named files must be .go files")
}
}
if pkgObj == "" {
basename := filepath.Base(buildFlags.Arg(0))
pkgObj = basename[:len(basename)-3] + ".js"
}
names := make([]string, buildFlags.NArg())
for i, name := range buildFlags.Args() {
name = filepath.ToSlash(name)
names[i] = name
if s.watcher != nil {
s.watcher.Watch(filepath.ToSlash(name))
}
}
if err := s.buildFiles(buildFlags.Args(), pkgObj); err != nil {
return err
}
return nil
}
for _, pkgPath := range buildFlags.Args() {
pkgPath = filepath.ToSlash(pkgPath)
if s.watcher != nil {
s.watcher.Watch(pkgPath)
}
buildPkg, err := buildImport(pkgPath, 0)
if err != nil {
return err
}
pkg := &packageData{Package: buildPkg}
if err := s.buildPackage(pkg); err != nil {
return err
}
if pkgObj == "" {
pkgObj = filepath.Base(buildFlags.Arg(0)) + ".js"
}
if err := s.writeCommandPackage(pkg, pkgObj); err != nil {
return err
}
}
return nil
})
if s.watcher == nil {
os.Exit(exitCode)
}
s.waitForChange()
}
case "install":
//.........这里部分代码省略.........
示例3: main
//.........这里部分代码省略.........
cmdTest.Flags().AddFlag(flagMinify)
cmdTest.Flags().AddFlag(flagColor)
cmdTest.Run = func(cmd *cobra.Command, args []string) {
os.Exit(handleError(func() error {
pkgs := make([]*build.Package, len(args))
for i, pkgPath := range args {
pkgPath = filepath.ToSlash(pkgPath)
var err error
pkgs[i], err = gbuild.Import(pkgPath, 0, "", nil)
if err != nil {
return err
}
}
if len(pkgs) == 0 {
firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0]
srcDir, err := filepath.EvalSymlinks(filepath.Join(firstGopathWorkspace, "src"))
if err != nil {
return err
}
var pkg *build.Package
if strings.HasPrefix(currentDirectory, srcDir) {
pkgPath, err := filepath.Rel(srcDir, currentDirectory)
if err != nil {
return err
}
if pkg, err = gbuild.Import(pkgPath, 0, "", nil); err != nil {
return err
}
}
if pkg == nil {
if pkg, err = build.ImportDir(currentDirectory, 0); err != nil {
return err
}
pkg.ImportPath = "_" + currentDirectory
}
pkgs = []*build.Package{pkg}
}
var exitErr error
for _, pkg := range pkgs {
if len(pkg.TestGoFiles) == 0 && len(pkg.XTestGoFiles) == 0 {
fmt.Printf("? \t%s\t[no test files]\n", pkg.ImportPath)
continue
}
s := gbuild.NewSession(options)
tests := &testFuncs{Package: pkg}
collectTests := func(buildPkg *build.Package, testPkgName string, needVar *bool) error {
testPkg := &gbuild.PackageData{Package: buildPkg, IsTest: true}
if err := s.BuildPackage(testPkg); err != nil {
return err
}
for _, decl := range testPkg.Archive.Declarations {
if strings.HasPrefix(decl.FullName, testPkg.ImportPath+".Test") {
tests.Tests = append(tests.Tests, testFunc{Package: testPkgName, Name: decl.FullName[len(testPkg.ImportPath)+1:]})
*needVar = true
}
if strings.HasPrefix(decl.FullName, testPkg.ImportPath+".Benchmark") {
tests.Benchmarks = append(tests.Benchmarks, testFunc{Package: testPkgName, Name: decl.FullName[len(testPkg.ImportPath)+1:]})
*needVar = true
}
}
return nil
}