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