本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.2KB

  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This file enables an external tool to intercept package requests.
  5. // If the tool is present then its results are used in preference to
  6. // the go list command.
  7. package packages
  8. import (
  9. "bytes"
  10. "encoding/json"
  11. "fmt"
  12. "os/exec"
  13. "strings"
  14. )
  15. // The Driver Protocol
  16. //
  17. // The driver, given the inputs to a call to Load, returns metadata about the packages specified.
  18. // This allows for different build systems to support go/packages by telling go/packages how the
  19. // packages' source is organized.
  20. // The driver is a binary, either specified by the GOPACKAGESDRIVER environment variable or in
  21. // the path as gopackagesdriver. It's given the inputs to load in its argv. See the package
  22. // documentation in doc.go for the full description of the patterns that need to be supported.
  23. // A driver receives as a JSON-serialized driverRequest struct in standard input and will
  24. // produce a JSON-serialized driverResponse (see definition in packages.go) in its standard output.
  25. // driverRequest is used to provide the portion of Load's Config that is needed by a driver.
  26. type driverRequest struct {
  27. Mode LoadMode `json:"mode"`
  28. // Env specifies the environment the underlying build system should be run in.
  29. Env []string `json:"env"`
  30. // BuildFlags are flags that should be passed to the underlying build system.
  31. BuildFlags []string `json:"build_flags"`
  32. // Tests specifies whether the patterns should also return test packages.
  33. Tests bool `json:"tests"`
  34. // Overlay maps file paths (relative to the driver's working directory) to the byte contents
  35. // of overlay files.
  36. Overlay map[string][]byte `json:"overlay"`
  37. }
  38. // findExternalDriver returns the file path of a tool that supplies
  39. // the build system package structure, or "" if not found."
  40. // If GOPACKAGESDRIVER is set in the environment findExternalTool returns its
  41. // value, otherwise it searches for a binary named gopackagesdriver on the PATH.
  42. func findExternalDriver(cfg *Config) driver {
  43. const toolPrefix = "GOPACKAGESDRIVER="
  44. tool := ""
  45. for _, env := range cfg.Env {
  46. if val := strings.TrimPrefix(env, toolPrefix); val != env {
  47. tool = val
  48. }
  49. }
  50. if tool != "" && tool == "off" {
  51. return nil
  52. }
  53. if tool == "" {
  54. var err error
  55. tool, err = exec.LookPath("gopackagesdriver")
  56. if err != nil {
  57. return nil
  58. }
  59. }
  60. return func(cfg *Config, words ...string) (*driverResponse, error) {
  61. req, err := json.Marshal(driverRequest{
  62. Mode: cfg.Mode,
  63. Env: cfg.Env,
  64. BuildFlags: cfg.BuildFlags,
  65. Tests: cfg.Tests,
  66. Overlay: cfg.Overlay,
  67. })
  68. if err != nil {
  69. return nil, fmt.Errorf("failed to encode message to driver tool: %v", err)
  70. }
  71. buf := new(bytes.Buffer)
  72. cmd := exec.CommandContext(cfg.Context, tool, words...)
  73. cmd.Dir = cfg.Dir
  74. cmd.Env = cfg.Env
  75. cmd.Stdin = bytes.NewReader(req)
  76. cmd.Stdout = buf
  77. cmd.Stderr = new(bytes.Buffer)
  78. if err := cmd.Run(); err != nil {
  79. return nil, fmt.Errorf("%v: %v: %s", tool, err, cmd.Stderr)
  80. }
  81. var response driverResponse
  82. if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
  83. return nil, err
  84. }
  85. return &response, nil
  86. }
  87. }
上海开阖软件有限公司 沪ICP备12045867号-1