github.com/FusionFoundation/efsn/v4@v4.2.0/internal/build/util.go (about) 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 17 package build 18 19 import ( 20 "bytes" 21 "flag" 22 "fmt" 23 "io" 24 "io/ioutil" 25 "log" 26 "os" 27 "os/exec" 28 "path" 29 "path/filepath" 30 "runtime" 31 "strings" 32 "text/template" 33 ) 34 35 var DryRunFlag = flag.Bool("n", false, "dry run, don't execute commands") 36 37 // MustRun executes the given command and exits the host process for 38 // any error. 39 func MustRun(cmd *exec.Cmd) { 40 fmt.Println(">>>", strings.Join(cmd.Args, " ")) 41 if !*DryRunFlag { 42 cmd.Stderr = os.Stderr 43 cmd.Stdout = os.Stdout 44 if err := cmd.Run(); err != nil { 45 log.Fatal(err) 46 } 47 } 48 } 49 50 func MustRunCommand(cmd string, args ...string) { 51 MustRun(exec.Command(cmd, args...)) 52 } 53 54 var warnedAboutGit bool 55 56 // RunGit runs a git subcommand and returns its output. 57 // The command must complete successfully. 58 func RunGit(args ...string) string { 59 cmd := exec.Command("git", args...) 60 var stdout, stderr bytes.Buffer 61 cmd.Stdout, cmd.Stderr = &stdout, &stderr 62 if err := cmd.Run(); err == exec.ErrNotFound { 63 if !warnedAboutGit { 64 log.Println("Warning: can't find 'git' in PATH") 65 warnedAboutGit = true 66 } 67 return "" 68 } else if err != nil { 69 log.Fatal(strings.Join(cmd.Args, " "), ": ", err, "\n", stderr.String()) 70 } 71 return strings.TrimSpace(stdout.String()) 72 } 73 74 // readGitFile returns content of file in .git directory. 75 func readGitFile(file string) string { 76 content, err := ioutil.ReadFile(path.Join(".git", file)) 77 if err != nil { 78 return "" 79 } 80 return strings.TrimSpace(string(content)) 81 } 82 83 // Render renders the given template file into outputFile. 84 func Render(templateFile, outputFile string, outputPerm os.FileMode, x interface{}) { 85 tpl := template.Must(template.ParseFiles(templateFile)) 86 render(tpl, outputFile, outputPerm, x) 87 } 88 89 // RenderString renders the given template string into outputFile. 90 func RenderString(templateContent, outputFile string, outputPerm os.FileMode, x interface{}) { 91 tpl := template.Must(template.New("").Parse(templateContent)) 92 render(tpl, outputFile, outputPerm, x) 93 } 94 95 func render(tpl *template.Template, outputFile string, outputPerm os.FileMode, x interface{}) { 96 if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil { 97 log.Fatal(err) 98 } 99 out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY|os.O_EXCL, outputPerm) 100 if err != nil { 101 log.Fatal(err) 102 } 103 if err := tpl.Execute(out, x); err != nil { 104 log.Fatal(err) 105 } 106 if err := out.Close(); err != nil { 107 log.Fatal(err) 108 } 109 } 110 111 // CopyFile copies a file. 112 func CopyFile(dst, src string, mode os.FileMode) { 113 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { 114 log.Fatal(err) 115 } 116 destFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode) 117 if err != nil { 118 log.Fatal(err) 119 } 120 defer destFile.Close() 121 122 srcFile, err := os.Open(src) 123 if err != nil { 124 log.Fatal(err) 125 } 126 defer srcFile.Close() 127 128 if _, err := io.Copy(destFile, srcFile); err != nil { 129 log.Fatal(err) 130 } 131 } 132 133 // GoTool returns the command that runs a go tool. This uses go from GOROOT instead of PATH 134 // so that go commands executed by build use the same version of Go as the 'host' that runs 135 // build code. e.g. 136 // 137 // /usr/lib/go-1.11/bin/go run build/ci.go ... 138 // 139 // runs using go 1.11 and invokes go 1.11 tools from the same GOROOT. This is also important 140 // because runtime.Version checks on the host should match the tools that are run. 141 func GoTool(tool string, args ...string) *exec.Cmd { 142 args = append([]string{tool}, args...) 143 return exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...) 144 } 145 146 // ExpandPackagesNoVendor expands a cmd/go import path pattern, skipping 147 // vendored packages. 148 func ExpandPackagesNoVendor(patterns []string) []string { 149 expand := false 150 for _, pkg := range patterns { 151 if strings.Contains(pkg, "...") { 152 expand = true 153 } 154 } 155 if expand { 156 cmd := GoTool("list", patterns...) 157 out, err := cmd.CombinedOutput() 158 if err != nil { 159 log.Fatalf("package listing failed: %v\n%s", err, string(out)) 160 } 161 var packages []string 162 for _, line := range strings.Split(string(out), "\n") { 163 if !strings.Contains(line, "/vendor/") { 164 packages = append(packages, strings.TrimSpace(line)) 165 } 166 } 167 return packages 168 } 169 return patterns 170 }