From 80c06a483af08d261a3a9af268df5f3ba3915f3b Mon Sep 17 00:00:00 2001 From: sh0rez Date: Fri, 28 Feb 2020 19:40:37 +0100 Subject: [PATCH 1/3] feat(cli): --version using ldflags The go-modules approach is a bit broken: https://github.com/golang/go/issues/29228 To resolve that, we now compute that version using `git describe` and set it using `ldflags`. While this is not as nice/builtin as the other approach, it at least works all the time and across go versions. Furthermore, I did refactor the Makefile in preparation for an AUR package that builds from source and for building release artifacts in CI. --- Makefile | 52 ++++++++++++++++++++++++++++---------------- cmd/jb/main.go | 5 ++++- cmd/jb/version.go | 34 ----------------------------- cmd/jb/version111.go | 29 ------------------------ 4 files changed, 37 insertions(+), 83 deletions(-) delete mode 100644 cmd/jb/version.go delete mode 100644 cmd/jb/version111.go diff --git a/Makefile b/Makefile index 451331a..dfd46ac 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,35 @@ -all: check-license build generate test +.PHONY: all check-license crossbuild build install test generate embedmd GITHUB_URL=github.com/jsonnet-bundler/jsonnet-bundler -GOOS?=$(shell uname -s | tr A-Z a-z) -GOARCH?=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) +VERSION := $(shell git describe --tags --dirty --always) OUT_DIR=_output BIN?=jb -VERSION?=$(shell cat VERSION) PKGS=$(shell go list ./... | grep -v /vendor/) -check-license: - @echo ">> checking license headers" - @./scripts/check_license.sh +all: check-license build generate test -crossbuild: - @GOOS=linux ARCH=amd64 $(MAKE) -s build + +# Binaries +LDFLAGS := '-s -w -extldflags "-static" -X main.Version=${VERSION}' +cross: clean + CGO_ENABLED=0 gox \ + -output="$(OUT_DIR)/jb-{{.OS}}-{{.Arch}}" \ + -ldflags=$(LDFLAGS) \ + -arch="amd64 arm64 arm" -os="linux" \ + -osarch="darwin/amd64" \ + ./cmd/$(BIN) + +static: + CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o $(OUT_DIR)/$(BIN) ./cmd/$(BIN) build: - @$(eval OUTPUT=$(OUT_DIR)/$(GOOS)/$(GOARCH)/$(BIN)) - @echo ">> building for $(GOOS)/$(GOARCH) to $(OUTPUT)" - @mkdir -p $(OUT_DIR)/$(GOOS)/$(GOARCH) - @CGO_ENABLED=0 go build --installsuffix cgo -o $(OUTPUT) $(GITHUB_URL)/cmd/$(BIN) + CGO_ENABLED=0 go build ./cmd/$(BIN) -install: build - @$(eval OUTPUT=$(OUT_DIR)/$(GOOS)/$(GOARCH)/$(BIN)) +install: static @echo ">> copying $(BIN) into $(GOPATH)/bin/$(BIN)" - @cp $(OUTPUT) $(GOPATH)/bin/$(BIN) + cp $(OUT_DIR)/$(BIN) $(GOPATH)/bin/$(BIN) +# Tests test: @echo ">> running all unit tests" go test -v $(PKGS) @@ -34,12 +38,22 @@ test-integration: @echo ">> running all integration tests" go test -v -tags=integration $(PKGS) +# Documentation generate: embedmd @echo ">> generating docs" @./scripts/generate-help-txt.sh @$(GOPATH)/bin/embedmd -w `find ./ -path ./vendor -prune -o -name "*.md" -print` -embedmd: - @go get github.com/campoy/embedmd +check-license: + @echo ">> checking license headers" + @./scripts/check_license.sh -.PHONY: all check-license crossbuild build install test generate embedmd +embedmd: + pushd /tmp && GO111MODULES=on go get github.com/campoy/embedmd && popd + +# Other +clean: + rm -rf $(OUT_DIR) $(BIN) + +drone: + drone jsonnet --format diff --git a/cmd/jb/main.go b/cmd/jb/main.go index 0a16202..df40a45 100644 --- a/cmd/jb/main.go +++ b/cmd/jb/main.go @@ -21,6 +21,7 @@ import ( "github.com/fatih/color" "github.com/pkg/errors" + "gopkg.in/alecthomas/kingpin.v2" ) const ( @@ -30,6 +31,8 @@ const ( rewriteActionName = "rewrite" ) +var Version = "dev" + func main() { os.Exit(Main()) } @@ -41,7 +44,7 @@ func Main() int { color.Output = color.Error - a := newApp() + a := kingpin.New(filepath.Base(os.Args[0]), "A jsonnet package manager").Version(Version) a.HelpFlag.Short('h') a.Flag("jsonnetpkg-home", "The directory used to cache packages in."). diff --git a/cmd/jb/version.go b/cmd/jb/version.go deleted file mode 100644 index f58bfd1..0000000 --- a/cmd/jb/version.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2018 jsonnet-bundler authors -// -// 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. - -// +build go1.12 - -package main - -import ( - "os" - "path/filepath" - "runtime/debug" - - kingpin "gopkg.in/alecthomas/kingpin.v2" -) - -func newApp() *kingpin.Application { - a := kingpin.New(filepath.Base(os.Args[0]), "A jsonnet package manager") - d, ok := debug.ReadBuildInfo() - if ok { - return a.Version(d.Main.Version) - } - return a -} diff --git a/cmd/jb/version111.go b/cmd/jb/version111.go deleted file mode 100644 index 40ca400..0000000 --- a/cmd/jb/version111.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018 jsonnet-bundler authors -// -// 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. - -// +build !go1.12 - -package main - -import ( - "os" - "path/filepath" - - kingpin "gopkg.in/alecthomas/kingpin.v2" -) - -func newApp() *kingpin.Application { - a := kingpin.New(filepath.Base(os.Args[0]), "A jsonnet package manager") - return a -} From 4f76fe7be6bd4a39df6b953b367b0b653ced6247 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Mon, 2 Mar 2020 07:37:21 +0000 Subject: [PATCH 2/3] fix: makefile embedmd Runs the Makefile on bash, so that pushd/popd works --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index dfd46ac..36fb2e1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .PHONY: all check-license crossbuild build install test generate embedmd +SHELL=/bin/bash + GITHUB_URL=github.com/jsonnet-bundler/jsonnet-bundler VERSION := $(shell git describe --tags --dirty --always) OUT_DIR=_output @@ -8,7 +10,6 @@ PKGS=$(shell go list ./... | grep -v /vendor/) all: check-license build generate test - # Binaries LDFLAGS := '-s -w -extldflags "-static" -X main.Version=${VERSION}' cross: clean @@ -42,7 +43,7 @@ test-integration: generate: embedmd @echo ">> generating docs" @./scripts/generate-help-txt.sh - @$(GOPATH)/bin/embedmd -w `find ./ -path ./vendor -prune -o -name "*.md" -print` + $(GOPATH)/bin/embedmd -w `find ./ -path ./vendor -prune -o -name "*.md" -print` check-license: @echo ">> checking license headers" From 7beb30ed18b4be29e32c4b3d13fc6d2f746f5445 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Mon, 2 Mar 2020 16:43:11 +0100 Subject: [PATCH 3/3] fix: generate-help-script Binary path is different, need to adapt --- Makefile | 2 +- scripts/generate-help-txt.sh | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 36fb2e1..2ccd60b 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ static: CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o $(OUT_DIR)/$(BIN) ./cmd/$(BIN) build: - CGO_ENABLED=0 go build ./cmd/$(BIN) + CGO_ENABLED=0 go build -o $(OUT_DIR)/$(BIN) ./cmd/$(BIN) install: static @echo ">> copying $(BIN) into $(GOPATH)/bin/$(BIN)" diff --git a/scripts/generate-help-txt.sh b/scripts/generate-help-txt.sh index 6f21926..e5f9041 100755 --- a/scripts/generate-help-txt.sh +++ b/scripts/generate-help-txt.sh @@ -1,10 +1,8 @@ #!/usr/bin/env bash BINARY_NAME=jb -GOOS=$(go env GOOS) -GOARCH=$(go env GOARCH) HELP_FILE=$PWD/_output/help.txt echo "$ $BINARY_NAME -h" > $HELP_FILE -$PWD/_output/$GOOS/$GOARCH/$BINARY_NAME 2>> $HELP_FILE +$PWD/_output/$BINARY_NAME 2>> $HELP_FILE exit 0