github.com/searKing/golang/go@v1.2.117/version/version.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package version records versioning information about this module.
     6  
     7  // borrowed from https://github.com/protocolbuffers/protobuf-go/blob/v1.25.0/internal/version/version.go
     8  
     9  package version
    10  
    11  import (
    12  	"fmt"
    13  	"io"
    14  	"runtime"
    15  	"strings"
    16  )
    17  
    18  // These constants determine the current version of this module.
    19  //
    20  //
    21  // For our release process, we enforce the following rules:
    22  //	* Tagged releases use a tag that is identical to String.
    23  //	* Tagged releases never reference a commit where the String
    24  //	contains "devel".
    25  //	* The set of all commits in this repository where String
    26  //	does not contain "devel" must have a unique String.
    27  //
    28  //
    29  // Steps for tagging a new release:
    30  //	1. Create a new CL.
    31  //
    32  //	2. Update Minor, Patch, and/or PreRelease as necessary.
    33  //	PreRelease must not contain the string "devel".
    34  //
    35  //	3. Since the last released minor version, have there been any changes to
    36  //	generator that relies on new functionality in the runtime?
    37  //	If yes, then increment RequiredGenerated.
    38  //
    39  //	4. Since the last released minor version, have there been any changes to
    40  //	the runtime that removes support for old .pb.go source code?
    41  //	If yes, then increment SupportMinimum.
    42  //
    43  //	5. Send out the CL for review and submit it.
    44  //	Note that the next CL in step 8 must be submitted after this CL
    45  //	without any other CLs in-between.
    46  //
    47  //	6. Tag a new version, where the tag is is the current String.
    48  //
    49  //	7. Write release notes for all notable changes
    50  //	between this release and the last release.
    51  //
    52  //	8. Create a new CL.
    53  //
    54  //	9. Update PreRelease to include the string "devel".
    55  //	For example: "" -> "devel" or "rc.1" -> "rc.1.devel"
    56  //
    57  //	10. Send out the CL for review and submit it.
    58  
    59  type Version struct {
    60  	Major      int
    61  	Minor      int
    62  	Patch      int
    63  	PreRelease string
    64  
    65  	// NOTE: The $Format strings are replaced during 'git archive' thanks to the
    66  	// companion .gitattributes file containing 'export-subst' in this same
    67  	// directory.  See also https://git-scm.com/docs/gitattributes
    68  	// git describe --long --tags --dirty --tags --always
    69  	// "v0.0.0-master+$Format:%h$"
    70  	RawVersion string
    71  
    72  	// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
    73  	// "1970-01-01T00:00:00Z"
    74  	BuildTime string
    75  	// sha1 from git, output of $(git rev-parse HEAD)
    76  	// "$Format:%H$"
    77  	GitHash string
    78  
    79  	MetaData []string // take effect when PreRelease contains devel
    80  }
    81  
    82  // String formats the version string for this module in semver format.
    83  //
    84  // Examples:
    85  //
    86  //	v1.20.1
    87  //	v1.21.0-rc.1
    88  func (ver Version) String() string {
    89  	if ver.RawVersion != "" {
    90  		return ver.RawVersion
    91  	}
    92  	v := fmt.Sprintf("v%d.%d.%d", ver.Major, ver.Minor, ver.Patch)
    93  	if ver.PreRelease != "" {
    94  		v += "-" + ver.PreRelease
    95  	}
    96  	if ver.GitHash != "" {
    97  		v += "(" + ver.GitHash + ")"
    98  	}
    99  	// TODO: Add metadata about the commit or build hash.
   100  	// See https://golang.org/issue/29814
   101  	// See https://golang.org/issue/33533
   102  	var metadata = strings.Join(ver.MetaData, ".")
   103  	if strings.Contains(ver.PreRelease, "devel") && metadata != "" {
   104  		v += "+" + metadata
   105  	}
   106  	return v
   107  }
   108  
   109  func (ver Version) BuildInfo() string {
   110  	//	GoVersion = runtime.Version()
   111  	//	Compiler  = runtime.Compiler
   112  	//	Platform  = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
   113  	return fmt.Sprintf("%s-%s-%s",
   114  		runtime.Compiler, runtime.Version(),
   115  		fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
   116  }
   117  
   118  // Format Examples:
   119  // v1.2.3-fix, Build #gc-go1.15.6-darwin/amd64, built on NOW
   120  func (ver Version) Format(s fmt.State, verb rune) {
   121  	switch verb {
   122  	case 'v':
   123  		if s.Flag('+') {
   124  			_, _ = io.WriteString(s, ver.String())
   125  			if buildInfo := ver.BuildInfo(); buildInfo != "" {
   126  				_, _ = io.WriteString(s, ", Build #"+buildInfo)
   127  			}
   128  
   129  			if ver.BuildTime != "" {
   130  				_, _ = io.WriteString(s, ", built on "+ver.BuildTime)
   131  			}
   132  			return
   133  		}
   134  		fallthrough
   135  	case 's', 'q':
   136  		_, _ = io.WriteString(s, ver.String())
   137  	}
   138  }