github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/internal/build/env.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:38</date>
    10  //</624450091620503552>
    11  
    12  
    13  package build
    14  
    15  import (
    16  	"flag"
    17  	"fmt"
    18  	"os"
    19  	"strings"
    20  )
    21  
    22  var (
    23  //These flags override values in build env.
    24  	GitCommitFlag   = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
    25  	GitBranchFlag   = flag.String("git-branch", "", `Overrides git branch being built`)
    26  	GitTagFlag      = flag.String("git-tag", "", `Overrides git tag being built`)
    27  	BuildnumFlag    = flag.String("buildnum", "", `Overrides CI build number`)
    28  	PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
    29  	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
    30  )
    31  
    32  //环境包含由生成环境提供的元数据。
    33  type Environment struct {
    34  Name                string //环境名称
    35  Repo                string //name of GitHub repo
    36  Commit, Branch, Tag string //GIT信息
    37  	Buildnum            string
    38  	IsPullRequest       bool
    39  	IsCronJob           bool
    40  }
    41  
    42  func (env Environment) String() string {
    43  	return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)",
    44  		env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
    45  }
    46  
    47  //env返回有关当前CI环境的元数据,返回到localenv
    48  //如果不在CI上运行。
    49  func Env() Environment {
    50  	switch {
    51  	case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
    52  		return Environment{
    53  			Name:          "travis",
    54  			Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
    55  			Commit:        os.Getenv("TRAVIS_COMMIT"),
    56  			Branch:        os.Getenv("TRAVIS_BRANCH"),
    57  			Tag:           os.Getenv("TRAVIS_TAG"),
    58  			Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
    59  			IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
    60  			IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
    61  		}
    62  	case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
    63  		return Environment{
    64  			Name:          "appveyor",
    65  			Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
    66  			Commit:        os.Getenv("APPVEYOR_REPO_COMMIT"),
    67  			Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
    68  			Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
    69  			Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
    70  			IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
    71  			IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
    72  		}
    73  	default:
    74  		return LocalEnv()
    75  	}
    76  }
    77  
    78  //localenv返回从git收集的生成环境元数据。
    79  func LocalEnv() Environment {
    80  	env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
    81  
    82  	head := readGitFile("HEAD")
    83  	if splits := strings.Split(head, " "); len(splits) == 2 {
    84  		head = splits[1]
    85  	} else {
    86  		return env
    87  	}
    88  	if env.Commit == "" {
    89  		env.Commit = readGitFile(head)
    90  	}
    91  	if env.Branch == "" {
    92  		if head != "HEAD" {
    93  			env.Branch = strings.TrimPrefix(head, "refs/heads/")
    94  		}
    95  	}
    96  	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
    97  		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
    98  	}
    99  	return env
   100  }
   101  
   102  func firstLine(s string) string {
   103  	return strings.Split(s, "\n")[0]
   104  }
   105  
   106  func applyEnvFlags(env Environment) Environment {
   107  	if !flag.Parsed() {
   108  		panic("you need to call flag.Parse before Env or LocalEnv")
   109  	}
   110  	if *GitCommitFlag != "" {
   111  		env.Commit = *GitCommitFlag
   112  	}
   113  	if *GitBranchFlag != "" {
   114  		env.Branch = *GitBranchFlag
   115  	}
   116  	if *GitTagFlag != "" {
   117  		env.Tag = *GitTagFlag
   118  	}
   119  	if *BuildnumFlag != "" {
   120  		env.Buildnum = *BuildnumFlag
   121  	}
   122  	if *PullRequestFlag {
   123  		env.IsPullRequest = true
   124  	}
   125  	if *CronJobFlag {
   126  		env.IsCronJob = true
   127  	}
   128  	return env
   129  }
   130