Merge pull request #1229 from harshavardhana/build-ldflags

build: Populate mcVersion through ldflags
This commit is contained in:
Harshavardhana
2015-11-02 12:51:34 -08:00
11 changed files with 122 additions and 106 deletions
+5 -13
View File
@@ -1,4 +1,5 @@
## filter multiple GOPATH
LDFLAGS = $(shell go run buildscripts/gen-ldflags.go)
all: install
checkdeps:
@@ -42,27 +43,18 @@ deadcode:
@echo "Running $@:"
@GO15VENDOREXPERIMENT=1 deadcode
build: getdeps verifiers
build: verifiers
@echo "Installing mc:"
test: getdeps verifiers
test: verifiers
@echo "Running all testing:"
@GO15VENDOREXPERIMENT=1 go test $(GOFLAGS) ./
@GO15VENDOREXPERIMENT=1 go test $(GOFLAGS) github.com/minio/mc/pkg...
gomake-all: build
@GO15VENDOREXPERIMENT=1 go install github.com/minio/mc
@GO15VENDOREXPERIMENT=1 go build -ldflags $(LDFLAGS) -o $(GOPATH)/bin/mc
@mkdir -p $(HOME)/.mc
release: genversion
@echo "Installing mc with new version.go:"
@GO15VENDOREXPERIMENT=1 go install github.com/minio/mc
@mkdir -p $(HOME)/.mc
genversion:
@echo "Generating a new version.go:"
@GO15VENDOREXPERIMENT=1 go run buildscripts/genversion.go
coverage:
@GO15VENDOREXPERIMENT=1 go test -race -coverprofile=cover.out ./
@go tool cover -html=cover.out && echo "Visit your browser"
+3
View File
@@ -26,6 +26,9 @@ build_script:
- go test -race .
- go test github.com/minio/mc/pkg...
- go test -race github.com/minio/mc/pkg...
- go run buildscripts/gen-ldflags.go > temp.txt
- set /p LDFLAGS=<temp.txt
- go build -ldflags=%LDFLAGS% -o %GOPATH%\bin\mc.exe
# to disable automatic tests
test: off
+24
View File
@@ -0,0 +1,24 @@
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
var (
mcVersion = "UNOFFICIAL.GOGET"
mcReleaseTag = "UNOFFICIAL.GOGET"
mcCommitID = "UNOFFICIAL.GOGET"
mcShortCommitID = mcCommitID[:]
)
+70
View File
@@ -0,0 +1,70 @@
// +build ignore
/*
* Mc Client (C) 2014, 2015 Mc, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)
func genLDFlags(version string) string {
var ldflagsStr string
ldflagsStr = "\"-X main.mcVersion=" + version + " "
ldflagsStr = ldflagsStr + "-X main.mcReleaseTag=" + releaseTag(version) + " "
ldflagsStr = ldflagsStr + "-X main.mcCommitID=" + commitID() + " "
ldflagsStr = ldflagsStr + "-X main.mcShortCommitID=" + commitID()[:12] + "\""
return ldflagsStr
}
// genReleaseTag prints release tag to the console for easy git tagging.
func releaseTag(version string) string {
relPrefix := "UNOFFICIAL"
if prefix := os.Getenv("MC_RELEASE"); prefix != "" {
relPrefix = prefix
}
relTag := strings.Replace(version, " ", "-", -1)
relTag = strings.Replace(relTag, ":", "-", -1)
relTag = strings.Replace(relTag, ",", "", -1)
return relPrefix + "." + relTag
}
// commitID returns the abbreviated commit-id hash of the last commit.
func commitID() string {
// git log --format="%h" -n1
var (
commit []byte
e error
)
cmdName := "git"
cmdArgs := []string{"log", "--format=%H", "-n1"}
if commit, e = exec.Command(cmdName, cmdArgs...).Output(); e != nil {
fmt.Fprintln(os.Stderr, "Error generating git commit-id: ", e)
os.Exit(1)
}
return strings.TrimSpace(string(commit))
}
func main() {
fmt.Println(genLDFlags(time.Now().UTC().Format(time.RFC3339)))
}
-77
View File
@@ -1,77 +0,0 @@
// +build ignore
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"net/http"
"os"
"strings"
"text/template"
"time"
)
var versionTemplate = `// -------- DO NOT EDIT --------
// This file is autogenerated by genversion.go during the release process.
package main
const mcVersion = {{if .Version}}"{{.Version}}"{{else}}""{{end}}
const mcReleaseTag = {{if .ReleaseTag}}"{{.ReleaseTag}}"{{else}}""{{end}}
`
// genVersion generates ‘version.go’.
func genVersion(version string) {
t := template.Must(template.New("version").Parse(versionTemplate))
versionFile, err := os.OpenFile("version.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Printf("genversion: Unable to generate ‘version.go’. Error: %s.\n", err)
os.Exit(1)
}
defer versionFile.Close()
err = t.Execute(versionFile, struct {
Version string
ReleaseTag string
}{version, genReleaseTag(version)})
if err != nil {
fmt.Printf("genversion: Unable to generate ‘version.go’. Error: %s.\n", err)
os.Exit(1)
}
}
// genReleaseTag prints release tag to the console for easy git tagging.
func genReleaseTag(version string) string {
relTag := strings.Replace(version, " ", "-", -1)
relTag = strings.Replace(relTag, ":", "-", -1)
relTag = strings.Replace(relTag, ",", "", -1)
return "RELEASE." + relTag
}
func main() {
// Version is in HTTP TimeFormat.
version := time.Now().UTC().Format(http.TimeFormat)
// generate ‘version.go’ file.
genVersion(version)
fmt.Println("Version: \"" + version + "\"")
fmt.Println("Release-Tag: " + genReleaseTag(version))
}
+1 -1
View File
@@ -164,7 +164,7 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error)
return auth.SecretAccessKey
}()
s3Config.AppName = "Minio"
s3Config.AppVersion = globalMCVersion
s3Config.AppVersion = mcVersion
s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH}
s3Config.HostURL = urlStr
s3Config.Debug = globalDebugFlag
-1
View File
@@ -27,7 +27,6 @@ var (
// mc configuration related constants.
const (
globalMCConfigVersion = "5"
globalMCVersion = mcVersion
globalMCConfigDir = ".mc/"
globalMCConfigWindowsDir = "mc\\"
+1
View File
@@ -197,6 +197,7 @@ func setMainPalette(style string) {
func main() {
probe.Init() // Set project's root source path.
probe.SetAppInfo("Release-Tag", mcReleaseTag)
probe.SetAppInfo("Commit", mcShortCommitID)
app := registerApp()
app.Before = registerBefore
+12 -4
View File
@@ -141,11 +141,15 @@ func getExperimentalUpdate() {
data, _, err := clnt.Get(0, 0)
fatalIf(err.Trace(mcExperimentalURL), "Unable to read from experimental URL ‘"+mcExperimentalURL+"’.")
current, e := time.Parse(http.TimeFormat, mcVersion)
if mcVersion == "UNOFFICIAL.GOGET" {
fatalIf(errDummy().Trace(mcVersion), "Update mechanism is not supported for ‘go get’ based binary builds. Please download official releases from https://minio.io/#mc")
}
current, e := time.Parse(time.RFC3339, mcVersion)
fatalIf(probe.NewError(e), "Unable to parse Version string as time.")
if current.IsZero() {
fatalIf(errDummy().Trace(), "Experimental updates not supported for custom build. Version field is empty. Please download official releases from https://dl.minio.io:9000")
fatalIf(errDummy().Trace(), "Experimental updates not supported for custom build. Version field is empty. Please download official releases from https://minio.io/#mc")
}
var experimentals Updates
@@ -183,11 +187,15 @@ func getReleaseUpdate() {
data, _, err := clnt.Get(0, 0)
fatalIf(err.Trace(mcUpdateURL), "Unable to read from update URL ‘"+mcUpdateURL+"’.")
current, e := time.Parse(http.TimeFormat, mcVersion)
if mcVersion == "UNOFFICIAL.GOGET" {
fatalIf(errDummy().Trace(mcVersion), "Update mechanism is not supported for ‘go get’ based binary builds. Please download official releases from https://minio.io/#mc")
}
current, e := time.Parse(time.RFC3339, mcVersion)
fatalIf(probe.NewError(e), "Unable to parse Version string as time.")
if current.IsZero() {
fatalIf(errDummy().Trace(), "Updates not supported for custom build. Version field is empty. Please download official releases from https://dl.minio.io:9000")
fatalIf(errDummy().Trace(), "Updates not supported for custom build. Version field is empty. Please download official releases from https://minio.io/#mc")
}
var updates Updates
+6 -3
View File
@@ -67,17 +67,19 @@ func mainVersion(ctx *cli.Context) {
if globalJSONFlag {
tB, e := json.Marshal(
struct {
Version struct {
CommitID string `json:"commitId"`
Version struct {
Value string `json:"value"`
Format string `json:"format"`
} `json:"version"`
}{
CommitID: mcCommitID,
Version: struct {
Value string `json:"value"`
Format string `json:"format"`
}{
Value: mcVersion,
Format: "RFC2616",
Format: "RFC3339",
},
},
)
@@ -86,6 +88,7 @@ func mainVersion(ctx *cli.Context) {
return
}
msg := console.Colorize("Version", fmt.Sprintf("Version: %s\n", mcVersion))
msg += console.Colorize("Version", fmt.Sprintf("Release-Tag: %s", mcReleaseTag))
msg += console.Colorize("Version", fmt.Sprintf("Release-Tag: %s\n", mcReleaseTag))
msg += console.Colorize("Version", fmt.Sprintf("Commit-ID: %s", mcCommitID))
console.Println(msg)
}
-7
View File
@@ -1,7 +0,0 @@
// -------- DO NOT EDIT --------
// This file is autogenerated by genversion.go during the release process.
package main
const mcVersion = "Mon, 05 Oct 2015 17:52:35 GMT"
const mcReleaseTag = "RELEASE.Mon-05-Oct-2015-17-52-35-GMT"