|
6 | 6 | "errors"
|
7 | 7 | "flag"
|
8 | 8 | "fmt"
|
| 9 | + "go/build" |
9 | 10 | "io"
|
10 | 11 | "io/ioutil"
|
11 | 12 | "log"
|
@@ -468,35 +469,53 @@ type mainfileTemplateData struct {
|
468 | 469 | BinaryName string
|
469 | 470 | }
|
470 | 471 |
|
471 |
| -func listGoFiles(magePath, goCmd, tags string, env []string) ([]string, error) { |
472 |
| - args := []string{"list"} |
473 |
| - if tags != "" { |
474 |
| - args = append(args, fmt.Sprintf("-tags=%s", tags)) |
475 |
| - } |
476 |
| - args = append(args, "-e", "-f", `{{join .GoFiles "||"}}`) |
477 |
| - cmd := exec.Command(goCmd, args...) |
478 |
| - cmd.Env = env |
479 |
| - buf := &bytes.Buffer{} |
480 |
| - cmd.Stderr = buf |
481 |
| - cmd.Dir = magePath |
482 |
| - b, err := cmd.Output() |
| 472 | +// listGoFiles returns a list of all .go files in a given directory, |
| 473 | +// matching the provided tag |
| 474 | +func listGoFiles(magePath, goCmd, tag string, envStr []string) ([]string, error) { |
| 475 | + origMagePath := magePath |
| 476 | + if !filepath.IsAbs(magePath) { |
| 477 | + cwd, err := os.Getwd() |
| 478 | + if err != nil { |
| 479 | + return nil, fmt.Errorf("can't get current working directory: %v", err) |
| 480 | + } |
| 481 | + magePath = filepath.Join(cwd, magePath) |
| 482 | + } |
| 483 | + |
| 484 | + env, err := internal.SplitEnv(envStr) |
483 | 485 | if err != nil {
|
484 |
| - stderr := buf.String() |
485 |
| - // if the error is "cannot find module", that can mean that there's no |
486 |
| - // non-mage files, which is fine, so ignore it. |
487 |
| - if !strings.Contains(stderr, "cannot find module for path") { |
488 |
| - if tags == "" { |
489 |
| - return nil, fmt.Errorf("failed to list un-tagged gofiles: %v: %s", err, stderr) |
490 |
| - } |
491 |
| - return nil, fmt.Errorf("failed to list gofiles tagged with %q: %v: %s", tags, err, stderr) |
| 486 | + return nil, fmt.Errorf("error parsing environment variables: %v", err) |
| 487 | + } |
| 488 | + |
| 489 | + bctx := build.Default |
| 490 | + bctx.BuildTags = []string{tag} |
| 491 | + |
| 492 | + if _, ok := env["GOOS"]; ok { |
| 493 | + bctx.GOOS = env["GOOS"] |
| 494 | + } |
| 495 | + |
| 496 | + if _, ok := env["GOARCH"]; ok { |
| 497 | + bctx.GOARCH = env["GOARCH"] |
| 498 | + } |
| 499 | + |
| 500 | + pkg, err := bctx.Import(".", magePath, 0) |
| 501 | + if err != nil { |
| 502 | + if _, ok := err.(*build.NoGoError); ok { |
| 503 | + return []string{}, nil |
| 504 | + } |
| 505 | + |
| 506 | + // Allow multiple packages in the same directory |
| 507 | + if _, ok := err.(*build.MultiplePackageError); !ok { |
| 508 | + return nil, fmt.Errorf("failed to parse go source files: %v", err) |
492 | 509 | }
|
493 | 510 | }
|
494 |
| - out := strings.TrimSpace(string(b)) |
495 |
| - list := strings.Split(out, "||") |
496 |
| - for i := range list { |
497 |
| - list[i] = filepath.Join(magePath, list[i]) |
| 511 | + |
| 512 | + goFiles := make([]string, len(pkg.GoFiles)) |
| 513 | + for i := range pkg.GoFiles { |
| 514 | + goFiles[i] = filepath.Join(origMagePath, pkg.GoFiles[i]) |
498 | 515 | }
|
499 |
| - return list, nil |
| 516 | + |
| 517 | + debug.Printf("found %d go files with build tag %s (files: %v)", len(goFiles), tag, goFiles) |
| 518 | + return goFiles, nil |
500 | 519 | }
|
501 | 520 |
|
502 | 521 | // Magefiles returns the list of magefiles in dir.
|
@@ -549,6 +568,7 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isMagefil
|
549 | 568 | files = append(files, f)
|
550 | 569 | }
|
551 | 570 | }
|
| 571 | + |
552 | 572 | return files, nil
|
553 | 573 | }
|
554 | 574 |
|
|
0 commit comments