github.com/decred/dcrlnd@v0.7.6/build/version.go (about) 1 // Copyright (c) 2013-2014 The btcsuite developers 2 // Copyright (c) 2015-2018 The Decred developers 3 // Copyright (c) 2015-2017 The Lightning Network Developers 4 // Use of this source code is governed by an ISC 5 // license that can be found in the LICENSE file. 6 7 package build 8 9 import ( 10 "bytes" 11 "fmt" 12 "strings" 13 ) 14 15 const ( 16 // semanticAlphabet defines the allowed characters for the pre-release 17 // portion of a semantic version string. 18 semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" 19 20 // semanticBuildAlphabet defines the allowed characters for the build 21 // portion of a semantic version string. 22 semanticBuildAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-." 23 ) 24 25 // These constants define the application version and follow the semantic 26 // versioning 2.0.0 spec (https://semver.org/). 27 const ( 28 appMajor uint = 0 29 appMinor uint = 7 30 appPatch uint = 6 31 ) 32 33 var ( 34 // PreRelease is defined as a variable so it can be overridden during the 35 // build process with: 36 // '-ldflags "-X github.com/decred/dcrlnd/build.PreRelease=foo"' 37 // if needed. It MUST only contain characters from semanticAlphabet per 38 // the semantic versioning spec. 39 PreRelease = "pre" 40 41 // BuildMetadata is defined as a variable so it can be overridden during the 42 // build process with: 43 // '-ldflags "-X github.com/decred/dcrlnd/build.BuildMetadata=foo"' 44 // if needed. It MUST only contain characters from semanticBuildAlphabet 45 // per the semantic versioning spec. 46 BuildMetadata = "" 47 48 // Commit is defined as a variable so it can be overriden during the 49 // build process with: 50 // '-ldflags "-X github.com/decred/dcrlnd/build.Commit=foo"' 51 // if needed. It is NOT included in the version string returned by this 52 // module and MUST only contain characters from semanticBuildAlphabet. 53 Commit = "" 54 ) 55 56 // Version returns the application version as a properly formed string per the 57 // semantic versioning 2.0.0 spec (https://semver.org/). 58 func Version() string { 59 // Start with the major, minor, and patch versions. 60 version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch) 61 62 // Append pre-release version if there is one. The hyphen called for 63 // by the semantic versioning spec is automatically appended and should 64 // not be contained in the pre-release string. The pre-release version 65 // is not appended if it contains invalid characters. 66 preRelease := normalizePreRelString(PreRelease) 67 if preRelease != "" { 68 version = fmt.Sprintf("%s-%s", version, preRelease) 69 } 70 71 // Append build metadata if there is any, otherwise use the go 1.18+ 72 // VCS build info as metadata. The plus called for by the semantic 73 // versioning spec is automatically appended and should not be 74 // contained in the build metadata string. The build metadata string 75 // is not appended if it contains invalid characters. 76 build := normalizeBuildString(BuildMetadata) 77 if build == "" { 78 build = vcsCommitID() 79 } 80 if build != "" { 81 version = fmt.Sprintf("%s+%s", version, build) 82 } 83 84 return version 85 } 86 87 // MajorMinorPatch returns the compiled-in respective semver version elements. 88 func MajorMinorPatch() (uint, uint, uint) { 89 return appMajor, appMinor, appPatch 90 } 91 92 // SourceCommit returns the normalized version of the Commit variable according 93 // to the rules of semantic versioning guidelines. 94 func SourceCommit() string { 95 return normalizeBuildString(Commit) 96 } 97 98 // normalizeSemString returns the passed string stripped of all characters 99 // which are not valid according to the provided semantic versioning alphabet. 100 func normalizeSemString(str, alphabet string) string { 101 var result bytes.Buffer 102 for _, r := range str { 103 if strings.ContainsRune(alphabet, r) { 104 result.WriteRune(r) 105 } 106 } 107 return result.String() 108 } 109 110 // normalizePreRelString returns the passed string stripped of all characters 111 // which are not valid according to the semantic versioning guidelines for 112 // pre-release strings. In particular they MUST only contain characters in 113 // semanticAlphabet. 114 func normalizePreRelString(str string) string { 115 return normalizeSemString(str, semanticAlphabet) 116 } 117 118 // normalizeBuildString returns the passed string stripped of all characters 119 // which are not valid according to the semantic versioning guidelines for 120 // build metadata strings. In particular they MUST only contain characters in 121 // semanticBuildAlphabet. 122 func normalizeBuildString(str string) string { 123 return normalizeSemString(str, semanticBuildAlphabet) 124 }