util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/ioutil"
  22. "log"
  23. "os"
  24. "os/exec"
  25. "path/filepath"
  26. "strings"
  27. "text/template"
  28. )
  29. var (
  30. DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands")
  31. )
  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. path := filepath.SplitList(os.Getenv("GOPATH"))
  51. if len(path) == 0 {
  52. log.Fatal("GOPATH is not set")
  53. }
  54. // Ensure Godeps workspace is present in the path.
  55. godeps, _ := filepath.Abs(filepath.Join("Godeps", "_workspace"))
  56. for _, dir := range path {
  57. if dir == godeps {
  58. return strings.Join(path, string(filepath.ListSeparator))
  59. }
  60. }
  61. newpath := append(path[:1], godeps)
  62. newpath = append(newpath, path[1:]...)
  63. return strings.Join(newpath, string(filepath.ListSeparator))
  64. }
  65. func VERSION() string {
  66. version, err := ioutil.ReadFile("VERSION")
  67. if err != nil {
  68. log.Fatal(err)
  69. }
  70. return string(bytes.TrimSpace(version))
  71. }
  72. func GitCommit() string {
  73. return RunGit("rev-parse", "HEAD")
  74. }
  75. func RunGit(args ...string) string {
  76. cmd := exec.Command("git", args...)
  77. var stdout, stderr bytes.Buffer
  78. cmd.Stdout, cmd.Stderr = &stdout, &stderr
  79. if err := cmd.Run(); err == exec.ErrNotFound {
  80. log.Println("no git in PATH")
  81. return ""
  82. } else if err != nil {
  83. log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String())
  84. }
  85. return strings.TrimSpace(stdout.String())
  86. }
  87. // Render renders the given template file.
  88. func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) {
  89. tpl := template.Must(template.ParseFiles(templateFile))
  90. render(tpl, outputFile, outputPerm, x)
  91. }
  92. func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) {
  93. tpl := template.Must(template.New("").Parse(templateContent))
  94. render(tpl, outputFile, outputPerm, x)
  95. }
  96. func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) {
  97. if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
  98. log.Fatal(err)
  99. }
  100. out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm)
  101. if err != nil {
  102. log.Fatal(err)
  103. }
  104. if err := tpl.Execute(out, x); err != nil {
  105. log.Fatal(err)
  106. }
  107. if err := out.Close(); err != nil {
  108. log.Fatal(err)
  109. }
  110. }