util.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package build
  17. import (
  18. "bytes"
  19. "flag"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "log"
  24. "os"
  25. "os/exec"
  26. "path/filepath"
  27. "runtime"
  28. "strings"
  29. "text/template"
  30. )
  31. var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands")
  32. // MustRun executes the given command and exits the host process for
  33. // any error.
  34. func MustRun(cmd *exec.Cmd) {
  35. fmt.Println(">>>", strings.Join(cmd.Args, " "))
  36. if !*DryRunFlag {
  37. cmd.Stderr = os.Stderr
  38. cmd.Stdout = os.Stdout
  39. if err := cmd.Run(); err != nil {
  40. log.Fatal(err)
  41. }
  42. }
  43. }
  44. func MustRunCommand(cmd string, args ...string) {
  45. MustRun(exec.Command(cmd, args...))
  46. }
  47. // GOPATH returns the value that the GOPATH environment
  48. // variable should be set to.
  49. func GOPATH() string {
  50. if os.Getenv("GOPATH") == "" {
  51. log.Fatal("GOPATH is not set")
  52. }
  53. return os.Getenv("GOPATH")
  54. }
  55. // VERSION returns the content of the VERSION file.
  56. func VERSION() string {
  57. version, err := ioutil.ReadFile("VERSION")
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. return string(bytes.TrimSpace(version))
  62. }
  63. var warnedAboutGit bool
  64. // RunGit runs a git subcommand and returns its output.
  65. // The command must complete successfully.
  66. func RunGit(args ...string) string {
  67. cmd := exec.Command("git", args...)
  68. var stdout, stderr bytes.Buffer
  69. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  70. if err := cmd.Run(); err == exec.ErrNotFound {
  71. if !warnedAboutGit {
  72. log.Println("Warning: can't find 'git' in PATH")
  73. warnedAboutGit = true
  74. }
  75. return ""
  76. } else if err != nil {
  77. log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
  78. }
  79. return strings.TrimSpace(stdout.String())
  80. }
  81. // Render renders the given template file into outputFile.
  82. func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) {
  83. tpl := template.Must(template.ParseFiles(templateFile))
  84. render(tpl, outputFile, outputPerm, x)
  85. }
  86. // RenderString renders the given template string into outputFile.
  87. func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) {
  88. tpl := template.Must(template.New("").Parse(templateContent))
  89. render(tpl, outputFile, outputPerm, x)
  90. }
  91. func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) {
  92. if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
  93. log.Fatal(err)
  94. }
  95. out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm)
  96. if err != nil {
  97. log.Fatal(err)
  98. }
  99. if err := tpl.Execute(out, x); err != nil {
  100. log.Fatal(err)
  101. }
  102. if err := out.Close(); err != nil {
  103. log.Fatal(err)
  104. }
  105. }
  106. // CopyFile copies a file.
  107. func CopyFile(dst, src string, mode os.FileMode) {
  108. if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
  109. log.Fatal(err)
  110. }
  111. destFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. defer destFile.Close()
  116. srcFile, err := os.Open(src)
  117. if err != nil {
  118. log.Fatal(err)
  119. }
  120. defer srcFile.Close()
  121. if _, err := io.Copy(destFile, srcFile); err != nil {
  122. log.Fatal(err)
  123. }
  124. }
  125. // ExpandPackagesNoVendor expands a cmd/go import path pattern, skipping
  126. // vendored packages.
  127. func ExpandPackagesNoVendor(patterns []string) []string {
  128. expand := false
  129. for _, pkg := range patterns {
  130. if strings.Contains(pkg, "...") {
  131. expand = true
  132. }
  133. }
  134. if expand {
  135. args := append([]string{"list"}, patterns...)
  136. cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)
  137. out, err := cmd.CombinedOutput()
  138. if err != nil {
  139. log.Fatalf("package listing failed: %v\n%s", err, string(out))
  140. }
  141. var packages []string
  142. for _, line := range strings.Split(string(out), "\n") {
  143. if !strings.Contains(line, "/vendor/") {
  144. packages = append(packages, strings.TrimSpace(line))
  145. }
  146. }
  147. return packages
  148. }
  149. return patterns
  150. }