util.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "strings"
  28. "text/template"
  29. )
  30. var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands")
  31. // MustRun executes the given command and exits the host process for
  32. // any error.
  33. func MustRun(cmd *exec.Cmd) {
  34. fmt.Println(">>>", strings.Join(cmd.Args, " "))
  35. if !*DryRunFlag {
  36. cmd.Stderr = os.Stderr
  37. cmd.Stdout = os.Stdout
  38. if err := cmd.Run(); err != nil {
  39. log.Fatal(err)
  40. }
  41. }
  42. }
  43. func MustRunCommand(cmd string, args ...string) {
  44. MustRun(exec.Command(cmd, args...))
  45. }
  46. // GOPATH returns the value that the GOPATH environment
  47. // variable should be set to.
  48. func GOPATH() string {
  49. path := filepath.SplitList(os.Getenv("GOPATH"))
  50. if len(path) == 0 {
  51. log.Fatal("GOPATH is not set")
  52. }
  53. // Ensure that our internal vendor folder is on GOPATH
  54. vendor, _ := filepath.Abs(filepath.Join("build", "_vendor"))
  55. for _, dir := range path {
  56. if dir == vendor {
  57. return strings.Join(path, string(filepath.ListSeparator))
  58. }
  59. }
  60. newpath := append(path[:1], append([]string{vendor}, path[1:]...)...)
  61. return strings.Join(newpath, string(filepath.ListSeparator))
  62. }
  63. // VERSION returns the content of the VERSION file.
  64. func VERSION() string {
  65. version, err := ioutil.ReadFile("VERSION")
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. return string(bytes.TrimSpace(version))
  70. }
  71. var warnedAboutGit bool
  72. // RunGit runs a git subcommand and returns its output.
  73. // The command must complete successfully.
  74. func RunGit(args ...string) string {
  75. cmd := exec.Command("git", args...)
  76. var stdout, stderr bytes.Buffer
  77. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  78. if err := cmd.Run(); err == exec.ErrNotFound {
  79. if !warnedAboutGit {
  80. log.Println("Warning: can't find 'git' in PATH")
  81. warnedAboutGit = true
  82. }
  83. return ""
  84. } else if err != nil {
  85. log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
  86. }
  87. return strings.TrimSpace(stdout.String())
  88. }
  89. // Render renders the given template file into outputFile.
  90. func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) {
  91. tpl := template.Must(template.ParseFiles(templateFile))
  92. render(tpl, outputFile, outputPerm, x)
  93. }
  94. // RenderString renders the given template string into outputFile.
  95. func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) {
  96. tpl := template.Must(template.New("").Parse(templateContent))
  97. render(tpl, outputFile, outputPerm, x)
  98. }
  99. func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) {
  100. if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
  101. log.Fatal(err)
  102. }
  103. out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm)
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. if err := tpl.Execute(out, x); err != nil {
  108. log.Fatal(err)
  109. }
  110. if err := out.Close(); err != nil {
  111. log.Fatal(err)
  112. }
  113. }
  114. // CopyFile copies a file.
  115. func CopyFile(dst, src string, mode os.FileMode) {
  116. if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
  117. log.Fatal(err)
  118. }
  119. destFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
  120. if err != nil {
  121. log.Fatal(err)
  122. }
  123. defer destFile.Close()
  124. srcFile, err := os.Open(src)
  125. if err != nil {
  126. log.Fatal(err)
  127. }
  128. defer srcFile.Close()
  129. if _, err := io.Copy(destFile, srcFile); err != nil {
  130. log.Fatal(err)
  131. }
  132. }