From f70a2aba439c4b3b333640f757405b14d15bf1ee Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 19:12:10 +0200 Subject: [PATCH 01/10] Start creating ./run subcommands --- .gitlab-ci.yml | 2 +- Makefile | 29 +++++++++-------------------- _run/check-formatting | 2 ++ _run/format | 1 + _run/install-developer-tools | 2 ++ _run/lint | 2 ++ _run/notice-up-to-date | 2 ++ _support/gofmt-all.go | 2 +- _support/release | 2 +- run | 2 ++ 10 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 _run/check-formatting create mode 100644 _run/format create mode 100644 _run/install-developer-tools create mode 100644 _run/lint create mode 100644 _run/notice-up-to-date create mode 100755 run diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1b8f75df221..adb9de464c7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,7 +20,7 @@ test:go1.8: verify: stage: test script: - - make notice-up-to-date + - ./run notice-up-to-date - make verify package: diff --git a/Makefile b/Makefile index 3acb0c2e8a6..6328c5871be 100644 --- a/Makefile +++ b/Makefile @@ -29,12 +29,12 @@ install: build mkdir -p $(DESTDIR)${PREFIX}/bin/ cd ${BIN_BUILD_DIR} && install ${CMDS} ${DESTDIR}${PREFIX}/bin/ -verify: lint check-formatting govendor-status +verify: govendor-status + ./run lint + ./run check-formatting -check-formatting: install-developer-tools - go run _support/gofmt-all.go -n - -govendor-status: ${BUILD_DIR}/_build install-developer-tools +govendor-status: ${BUILD_DIR}/_build + ./run install-developer-tools cd ${PKG_BUILD_DIR} && govendor status ${TEST_REPO}: @@ -43,32 +43,21 @@ ${TEST_REPO}: test: clean-build ${TEST_REPO} ${BUILD_DIR}/_build go test ${PKG}/... -lint: install-developer-tools +lint: + ./run install-developer-tools go run _support/lint.go package: build ./_support/package/package ${CMDS} -notice: ${BUILD_DIR}/_build install-developer-tools +notice: ${BUILD_DIR}/_build + ./run install-developer-tools rm -f ${PKG_BUILD_DIR}/NOTICE # Avoid NOTICE-in-NOTICE cd ${PKG_BUILD_DIR} && govendor license -template _support/notice.template -o ${BUILD_DIR}/NOTICE -notice-up-to-date: notice - git ls-files --error-unmatch NOTICE # NOTICE is a tracked file - git diff --exit-code # there are no changed files - clean: clean-build rm -rf internal/testhelper/testdata rm -f $(foreach cmd,${CMDS},./${cmd}) clean-build: rm -rf ${BUILD_DIR}/_build - -.PHONY: format -format: - @go run _support/gofmt-all.go -f - -.PHONY: install-developer-tools -install-developer-tools: - @go run _support/go-get-if-missing.go govendor github.com/kardianos/govendor - @go run _support/go-get-if-missing.go golint github.com/golang/lint/golint diff --git a/_run/check-formatting b/_run/check-formatting new file mode 100644 index 00000000000..ffb28f1c749 --- /dev/null +++ b/_run/check-formatting @@ -0,0 +1,2 @@ +./run install-developer-tools +go run _support/gofmt-all.go -n diff --git a/_run/format b/_run/format new file mode 100644 index 00000000000..775a53e14a6 --- /dev/null +++ b/_run/format @@ -0,0 +1 @@ +go run _support/gofmt-all.go -f diff --git a/_run/install-developer-tools b/_run/install-developer-tools new file mode 100644 index 00000000000..a9490d2ccf4 --- /dev/null +++ b/_run/install-developer-tools @@ -0,0 +1,2 @@ +go run _support/go-get-if-missing.go govendor github.com/kardianos/govendor +go run _support/go-get-if-missing.go golint github.com/golang/lint/golint diff --git a/_run/lint b/_run/lint new file mode 100644 index 00000000000..44a9cb48263 --- /dev/null +++ b/_run/lint @@ -0,0 +1,2 @@ +./run install-developer-tools +go run _support/lint.go diff --git a/_run/notice-up-to-date b/_run/notice-up-to-date new file mode 100644 index 00000000000..e4de130512c --- /dev/null +++ b/_run/notice-up-to-date @@ -0,0 +1,2 @@ +git ls-files --error-unmatch NOTICE # NOTICE is a tracked file +git diff --exit-code # there are no changed files diff --git a/_support/gofmt-all.go b/_support/gofmt-all.go index 32a1a1753c7..834bc5c2a73 100644 --- a/_support/gofmt-all.go +++ b/_support/gofmt-all.go @@ -52,7 +52,7 @@ func gofmt(write bool) error { fmt.Printf("%s", output) if !write && len(output) > 0 { - return fmt.Errorf("Please run 'make format'") + return fmt.Errorf("Please run './run/format'") } return nil } diff --git a/_support/release b/_support/release index 091004639b7..92913448eee 100755 --- a/_support/release +++ b/_support/release @@ -4,7 +4,7 @@ require 'erb' require_relative 'run.rb' def main(version) - run!(%w[make notice-up-to-date]) + run!(%w[./run notice-up-to-date]) run!(%w[make test]) puts 'Testing for changed files' diff --git a/run b/run new file mode 100755 index 00000000000..545e0c250ea --- /dev/null +++ b/run @@ -0,0 +1,2 @@ +#!/bin/sh +exec sh -e -x -- ./_run/"$1" -- GitLab From 48564b5f61b5a7a23933b53d0426ce6e0c3dfd17 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 19:12:51 +0200 Subject: [PATCH 02/10] typo --- _support/gofmt-all.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_support/gofmt-all.go b/_support/gofmt-all.go index 834bc5c2a73..631bda0412c 100644 --- a/_support/gofmt-all.go +++ b/_support/gofmt-all.go @@ -52,7 +52,7 @@ func gofmt(write bool) error { fmt.Printf("%s", output) if !write && len(output) > 0 { - return fmt.Errorf("Please run './run/format'") + return fmt.Errorf("Please run './run format'") } return nil } -- GitLab From c10217778428f749c67b78acfb5b35b18f83b302 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 19:20:33 +0200 Subject: [PATCH 03/10] no longer need "make lint" --- Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Makefile b/Makefile index 6328c5871be..c5586e84212 100644 --- a/Makefile +++ b/Makefile @@ -43,10 +43,6 @@ ${TEST_REPO}: test: clean-build ${TEST_REPO} ${BUILD_DIR}/_build go test ${PKG}/... -lint: - ./run install-developer-tools - go run _support/lint.go - package: build ./_support/package/package ${CMDS} -- GitLab From 60456a57c9b868799f666b4327ae3db696b18583 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 20:11:59 +0200 Subject: [PATCH 04/10] Prepare _build using ./run --- Makefile | 18 +++++------ _run/prepare-build | 1 + _run/src/build/config.go | 37 +++++++++++++++++++++++ _support/prepare-build.go | 63 +++++++++++++++++++++++++++++++++++++++ build_config.json | 4 +++ 5 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 _run/prepare-build create mode 100644 _run/src/build/config.go create mode 100644 _support/prepare-build.go create mode 100644 build_config.json diff --git a/Makefile b/Makefile index c5586e84212..b36c7f94095 100644 --- a/Makefile +++ b/Makefile @@ -14,13 +14,8 @@ export PATH:=${GOPATH}/bin:$(PATH) .PHONY: all all: build -.PHONY: ${BUILD_DIR}/_build -${BUILD_DIR}/_build: - mkdir -p $@/src/${PKG} - tar -cf - --exclude _build --exclude .git . | (cd $@/src/${PKG} && tar -xf -) - touch $@ - -build: clean-build ${BUILD_DIR}/_build $(shell find . -name '*.go' -not -path './vendor/*' -not -path './_build/*') +build: $(shell find . -name '*.go' -not -path './vendor/*' -not -path './_build/*') + ./run prepare-build rm -f -- "${BIN_BUILD_DIR}/*" go install -ldflags "-X main.version=${VERSION}" ${PKG}/cmd/... cp ${BIN_BUILD_DIR}/* ${BUILD_DIR}/ @@ -33,20 +28,23 @@ verify: govendor-status ./run lint ./run check-formatting -govendor-status: ${BUILD_DIR}/_build +govendor-status: + ./run prepare-build ./run install-developer-tools cd ${PKG_BUILD_DIR} && govendor status ${TEST_REPO}: git clone --bare https://gitlab.com/gitlab-org/gitlab-test.git $@ -test: clean-build ${TEST_REPO} ${BUILD_DIR}/_build +test: ${TEST_REPO} + ./run prepare-build go test ${PKG}/... package: build ./_support/package/package ${CMDS} -notice: ${BUILD_DIR}/_build +notice: + ./run prepare-build ./run install-developer-tools rm -f ${PKG_BUILD_DIR}/NOTICE # Avoid NOTICE-in-NOTICE cd ${PKG_BUILD_DIR} && govendor license -template _support/notice.template -o ${BUILD_DIR}/NOTICE diff --git a/_run/prepare-build b/_run/prepare-build new file mode 100644 index 00000000000..426a285c835 --- /dev/null +++ b/_run/prepare-build @@ -0,0 +1 @@ +GOPATH=$(pwd)/_run go run _support/prepare-build.go diff --git a/_run/src/build/config.go b/_run/src/build/config.go new file mode 100644 index 00000000000..4264da6837f --- /dev/null +++ b/_run/src/build/config.go @@ -0,0 +1,37 @@ +package build + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "path" +) + +type Config struct { + BuildGopath string `json:"build_gopath"` + Package string `json:"package"` +} + +func ReadConfig() (*Config, error) { + data, err := ioutil.ReadFile("build_config.json") + if err != nil { + return nil, err + } + result := &Config{} + err = json.Unmarshal(data, result) + if err != nil { + return nil, err + } + + for _, s := range []string{result.BuildGopath, result.Package} { + if len(s) == 0 { + return nil, fmt.Errorf("invalid build config: %q", data) + } + } + + return result, nil +} + +func (c *Config) PackageBuildDir() string { + return path.Join(c.BuildGopath, "src", c.Package) +} diff --git a/_support/prepare-build.go b/_support/prepare-build.go new file mode 100644 index 00000000000..859ca118f97 --- /dev/null +++ b/_support/prepare-build.go @@ -0,0 +1,63 @@ +package main + +import ( + "build" + "context" + "fmt" + "os" + "os/exec" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintf(os.Stderr, "fatal: %v\n", err) + os.Exit(1) + } +} + +func run() error { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + config, err := build.ReadConfig() + if err != nil { + return err + } + + if err := os.RemoveAll(config.BuildGopath); err != nil { + return err + } + if err := os.MkdirAll(config.PackageBuildDir(), 0755); err != nil { + return err + } + + tarSource := exec.CommandContext(ctx, "tar", "-cf", "-", "--exclude", config.BuildGopath, "--exclude", ".git", ".") + tarSource.Stderr = os.Stderr + sourcePipe, err := tarSource.StdoutPipe() + if err != nil { + return err + } + + tarDest := exec.CommandContext(ctx, "tar", "-xf", "-") + tarDest.Stdin = sourcePipe + tarDest.Stderr = os.Stderr + tarDest.Dir = config.PackageBuildDir() + + for _, err := range []error{tarSource.Start(), tarDest.Start()} { + if err != nil { + return err + } + } + + if err := sourcePipe.Close(); err != nil { + return err + } + + for _, err := range []error{tarSource.Wait(), tarDest.Wait()} { + if err != nil { + return err + } + } + + return nil +} diff --git a/build_config.json b/build_config.json new file mode 100644 index 00000000000..126bc35f754 --- /dev/null +++ b/build_config.json @@ -0,0 +1,4 @@ +{ +"build_gopath": "./_build", +"package":"gitlab.com/gitlab-org/gitaly" +} \ No newline at end of file -- GitLab From b8df334f2df3492bc7c1488c00099dbdac483570 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 20:40:41 +0200 Subject: [PATCH 05/10] Move more stuff to ./run --- Makefile | 8 ++------ _run/govendor-status | 3 +++ _run/src/build/config.go | 12 +++++++++--- _support/govendor-status.go | 32 ++++++++++++++++++++++++++++++++ _support/prepare-build.go | 4 ++-- build_config.json | 2 +- 6 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 _run/govendor-status create mode 100644 _support/govendor-status.go diff --git a/Makefile b/Makefile index b36c7f94095..37caa77afea 100644 --- a/Makefile +++ b/Makefile @@ -24,14 +24,10 @@ install: build mkdir -p $(DESTDIR)${PREFIX}/bin/ cd ${BIN_BUILD_DIR} && install ${CMDS} ${DESTDIR}${PREFIX}/bin/ -verify: govendor-status +verify: ./run lint ./run check-formatting - -govendor-status: - ./run prepare-build - ./run install-developer-tools - cd ${PKG_BUILD_DIR} && govendor status + ./run govendor-status ${TEST_REPO}: git clone --bare https://gitlab.com/gitlab-org/gitlab-test.git $@ diff --git a/_run/govendor-status b/_run/govendor-status new file mode 100644 index 00000000000..d05b4365306 --- /dev/null +++ b/_run/govendor-status @@ -0,0 +1,3 @@ +./run prepare-build +./run install-developer-tools +GOPATH=$(pwd)/_run go run _support/govendor-status.go diff --git a/_run/src/build/config.go b/_run/src/build/config.go index 4264da6837f..f28fbdbd632 100644 --- a/_run/src/build/config.go +++ b/_run/src/build/config.go @@ -4,12 +4,14 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "path" ) type Config struct { - BuildGopath string `json:"build_gopath"` + BuildDir string `json:"build_dir"` Package string `json:"package"` + BuildGopath string } func ReadConfig() (*Config, error) { @@ -23,12 +25,16 @@ func ReadConfig() (*Config, error) { return nil, err } - for _, s := range []string{result.BuildGopath, result.Package} { + for _, s := range []string{result.BuildDir, result.Package} { if len(s) == 0 { return nil, fmt.Errorf("invalid build config: %q", data) } } - + cwd, err := os.Getwd() + if err != nil { + return nil, err + } + result.BuildGopath = path.Join(cwd, result.BuildDir) return result, nil } diff --git a/_support/govendor-status.go b/_support/govendor-status.go new file mode 100644 index 00000000000..58ec59d14b9 --- /dev/null +++ b/_support/govendor-status.go @@ -0,0 +1,32 @@ +package main + +import ( + "build" + "fmt" + "os" + "os/exec" + "syscall" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintf(os.Stderr, "fatal: %v\n", err) + os.Exit(1) + } +} + +func run() error { + config, err := build.ReadConfig() + if err != nil { + return err + } + govendor, err := exec.LookPath("govendor") + if err != nil { + return err + } + os.Setenv("GOPATH", config.BuildGopath) + if err := os.Chdir(config.PackageBuildDir()); err != nil { + return err + } + return syscall.Exec(govendor, []string{"govendor", "status"}, os.Environ()) +} diff --git a/_support/prepare-build.go b/_support/prepare-build.go index 859ca118f97..32a8053a1e9 100644 --- a/_support/prepare-build.go +++ b/_support/prepare-build.go @@ -24,14 +24,14 @@ func run() error { return err } - if err := os.RemoveAll(config.BuildGopath); err != nil { + if err := os.RemoveAll(config.BuildDir); err != nil { return err } if err := os.MkdirAll(config.PackageBuildDir(), 0755); err != nil { return err } - tarSource := exec.CommandContext(ctx, "tar", "-cf", "-", "--exclude", config.BuildGopath, "--exclude", ".git", ".") + tarSource := exec.CommandContext(ctx, "tar", "-cf", "-", "--exclude", config.BuildDir, "--exclude", ".git", ".") tarSource.Stderr = os.Stderr sourcePipe, err := tarSource.StdoutPipe() if err != nil { diff --git a/build_config.json b/build_config.json index 126bc35f754..146eefdeea3 100644 --- a/build_config.json +++ b/build_config.json @@ -1,4 +1,4 @@ { -"build_gopath": "./_build", +"build_dir": "_build", "package":"gitlab.com/gitlab-org/gitaly" } \ No newline at end of file -- GitLab From 9e9fd41452d20aa8b416cc93b361242d688a8507 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 20:43:56 +0200 Subject: [PATCH 06/10] No more make verify --- .gitlab-ci.yml | 2 +- Makefile | 5 ----- _run/verify | 3 +++ 3 files changed, 4 insertions(+), 6 deletions(-) create mode 100644 _run/verify diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index adb9de464c7..860a0932167 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,7 +21,7 @@ verify: stage: test script: - ./run notice-up-to-date - - make verify + - ./run verify package: stage: package diff --git a/Makefile b/Makefile index 37caa77afea..57ee345a216 100644 --- a/Makefile +++ b/Makefile @@ -24,11 +24,6 @@ install: build mkdir -p $(DESTDIR)${PREFIX}/bin/ cd ${BIN_BUILD_DIR} && install ${CMDS} ${DESTDIR}${PREFIX}/bin/ -verify: - ./run lint - ./run check-formatting - ./run govendor-status - ${TEST_REPO}: git clone --bare https://gitlab.com/gitlab-org/gitlab-test.git $@ diff --git a/_run/verify b/_run/verify new file mode 100644 index 00000000000..17c38e3300a --- /dev/null +++ b/_run/verify @@ -0,0 +1,3 @@ +./run lint +./run check-formatting +./run govendor-status -- GitLab From fcb02322bcddeaf4e77ad4dc091f1853bd73149c Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 21:55:18 +0200 Subject: [PATCH 07/10] Add "./run with-gopath" --- _run/govendor-status | 2 +- _run/with-gopath | 1 + _support/{govendor-status.go => with-gopath.go} | 8 ++++++-- run | 4 +++- 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 _run/with-gopath rename _support/{govendor-status.go => with-gopath.go} (69%) diff --git a/_run/govendor-status b/_run/govendor-status index d05b4365306..f156b7bf018 100644 --- a/_run/govendor-status +++ b/_run/govendor-status @@ -1,3 +1,3 @@ ./run prepare-build ./run install-developer-tools -GOPATH=$(pwd)/_run go run _support/govendor-status.go +./run with-gopath govendor status diff --git a/_run/with-gopath b/_run/with-gopath new file mode 100644 index 00000000000..8298efcf0a3 --- /dev/null +++ b/_run/with-gopath @@ -0,0 +1 @@ +GOPATH=$(pwd)/_run go run _support/with-gopath.go "$@" diff --git a/_support/govendor-status.go b/_support/with-gopath.go similarity index 69% rename from _support/govendor-status.go rename to _support/with-gopath.go index 58ec59d14b9..906cdae9d90 100644 --- a/_support/govendor-status.go +++ b/_support/with-gopath.go @@ -16,11 +16,15 @@ func main() { } func run() error { +if len(os.Args) <= 1 { +return fmt.Errorf("need at least one argument") +} config, err := build.ReadConfig() if err != nil { return err } - govendor, err := exec.LookPath("govendor") + + executable, err := exec.LookPath(os.Args[1]) if err != nil { return err } @@ -28,5 +32,5 @@ func run() error { if err := os.Chdir(config.PackageBuildDir()); err != nil { return err } - return syscall.Exec(govendor, []string{"govendor", "status"}, os.Environ()) + return syscall.Exec(executable, os.Args[1:], os.Environ()) } diff --git a/run b/run index 545e0c250ea..63f9919ca65 100755 --- a/run +++ b/run @@ -1,2 +1,4 @@ #!/bin/sh -exec sh -e -x -- ./_run/"$1" +sub_command=$1 +shift +exec sh -e -x -- ./_run/"$sub_command" "$@" -- GitLab From dd7675a2f5124cd4b4e472d9193df962a191c780 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 22:02:43 +0200 Subject: [PATCH 08/10] clean-build --- Makefile | 6 ++---- _run/clean-build | 1 + _support/clean-build.go | 23 +++++++++++++++++++++++ _support/with-gopath.go | 6 +++--- 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 _run/clean-build create mode 100644 _support/clean-build.go diff --git a/Makefile b/Makefile index 57ee345a216..1e703f4ac36 100644 --- a/Makefile +++ b/Makefile @@ -40,9 +40,7 @@ notice: rm -f ${PKG_BUILD_DIR}/NOTICE # Avoid NOTICE-in-NOTICE cd ${PKG_BUILD_DIR} && govendor license -template _support/notice.template -o ${BUILD_DIR}/NOTICE -clean: clean-build +clean: + ./run clean-build rm -rf internal/testhelper/testdata rm -f $(foreach cmd,${CMDS},./${cmd}) - -clean-build: - rm -rf ${BUILD_DIR}/_build diff --git a/_run/clean-build b/_run/clean-build new file mode 100644 index 00000000000..0a09c1ecee4 --- /dev/null +++ b/_run/clean-build @@ -0,0 +1 @@ +GOPATH=$(pwd)/_run go run _support/clean-build.go diff --git a/_support/clean-build.go b/_support/clean-build.go new file mode 100644 index 00000000000..2d5419f10dd --- /dev/null +++ b/_support/clean-build.go @@ -0,0 +1,23 @@ +package main + +import ( + "build" + "fmt" + "os" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintf(os.Stderr, "fatal: %v\n", err) + os.Exit(1) + } +} + +func run() error { + config, err := build.ReadConfig() + if err != nil { + return err + } + + return os.RemoveAll(config.BuildDir) +} diff --git a/_support/with-gopath.go b/_support/with-gopath.go index 906cdae9d90..d85930410b3 100644 --- a/_support/with-gopath.go +++ b/_support/with-gopath.go @@ -16,9 +16,9 @@ func main() { } func run() error { -if len(os.Args) <= 1 { -return fmt.Errorf("need at least one argument") -} + if len(os.Args) <= 1 { + return fmt.Errorf("need at least one argument") + } config, err := build.ReadConfig() if err != nil { return err -- GitLab From 12e6ca4888f440d1418cf9e9e755ebf6a255656a Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 22:22:52 +0200 Subject: [PATCH 09/10] Migrate "make build" to ./run --- Makefile | 7 ++---- _run/build | 2 ++ _run/src/build/config.go | 14 +++++++++++ _support/build.go | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 _run/build create mode 100644 _support/build.go diff --git a/Makefile b/Makefile index 1e703f4ac36..0a35e7db6d0 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,8 @@ export PATH:=${GOPATH}/bin:$(PATH) .PHONY: all all: build -build: $(shell find . -name '*.go' -not -path './vendor/*' -not -path './_build/*') - ./run prepare-build - rm -f -- "${BIN_BUILD_DIR}/*" - go install -ldflags "-X main.version=${VERSION}" ${PKG}/cmd/... - cp ${BIN_BUILD_DIR}/* ${BUILD_DIR}/ +build: + ./run build install: build mkdir -p $(DESTDIR)${PREFIX}/bin/ diff --git a/_run/build b/_run/build new file mode 100644 index 00000000000..d747ab634d1 --- /dev/null +++ b/_run/build @@ -0,0 +1,2 @@ +./run prepare-build +GOPATH=$(pwd)/_run go run _support/build.go diff --git a/_run/src/build/config.go b/_run/src/build/config.go index f28fbdbd632..4593810cb52 100644 --- a/_run/src/build/config.go +++ b/_run/src/build/config.go @@ -1,10 +1,12 @@ package build import ( + "bytes" "encoding/json" "fmt" "io/ioutil" "os" + "os/exec" "path" ) @@ -41,3 +43,15 @@ func ReadConfig() (*Config, error) { func (c *Config) PackageBuildDir() string { return path.Join(c.BuildGopath, "src", c.Package) } + +func Version() (string, error) { + describe, err := exec.Command("git", "describe").Output() + if err != nil { + return "", err + } + date, err := exec.Command("date", "-u", "+%Y%m%d.%H%M%S").Output() // TODO use package 'time' + if err != nil { + return "", err + } + return fmt.Sprintf("%s-%s", bytes.TrimSpace(describe), bytes.TrimSpace(date)), nil +} diff --git a/_support/build.go b/_support/build.go new file mode 100644 index 00000000000..6f1e9a604f5 --- /dev/null +++ b/_support/build.go @@ -0,0 +1,51 @@ +package main + +import ( + "build" + "fmt" + "os" + "os/exec" + "path" + "strings" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintf(os.Stderr, "fatal: %v\n", err) + os.Exit(1) + } +} + +func run() error { + config, err := build.ReadConfig() + if err != nil { + return err + } + + binDir := path.Join(config.BuildDir, "bin") + if err := os.RemoveAll(binDir); err != nil { + return err + } + version, err := build.Version() + if err != nil { + return err + } + goInstall := exec.Command("go", "install", "-ldflags", "-X main.version="+version, config.Package+"/cmd/...") + goInstall.Stdout = os.Stdout + goInstall.Stderr = os.Stderr + env := []string{} + for _, s := range os.Environ() { + if strings.HasPrefix(s, "GOPATH=") { + continue + } + env = append(env, s) + } + goInstall.Env = append(env, "GOPATH="+config.BuildGopath) + if err := goInstall.Run(); err != nil { + return err + } + cp := exec.Command("sh", "-c", fmt.Sprintf("cp %s/* .", binDir)) + cp.Stdout = os.Stdout + cp.Stderr = os.Stderr + return cp.Run() +} -- GitLab From 3d2a8a9acc11b4cdcc76276e077a0affe947fc8d Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Wed, 14 Jun 2017 22:24:46 +0200 Subject: [PATCH 10/10] Remove VERSION definition from Makefile --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index 0a35e7db6d0..0f85c1df231 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ BIN_BUILD_DIR=${BUILD_DIR}/_build/bin PKG_BUILD_DIR:=${BUILD_DIR}/_build/src/${PKG} CMDS:=$(shell cd cmd && ls) TEST_REPO=internal/testhelper/testdata/data/gitlab-test.git -VERSION=$(shell git describe)-$(shell date -u +%Y%m%d.%H%M%S) export GOPATH=${BUILD_DIR}/_build export GO15VENDOREXPERIMENT=1 -- GitLab