From 209d71c1f6d26691ab990e2dfac8e6e086d20a89 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Tue, 1 Sep 2015 15:25:29 +0200 Subject: [PATCH 1/5] Updated header comment --- make.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/make.go b/make.go index 7114b7019..746f938d1 100644 --- a/make.go +++ b/make.go @@ -1,10 +1,8 @@ // +build ignore // This program builds Drone. -// $ go run make.go build test -// -// The output binaries go into the ./bin/ directory (under the -// project root, where make.go is) +// $ go run make.go deps bindata build test + package main import ( From 7df78a286464fe88dcd53e7a9d2f079ed596eaf3 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Tue, 1 Sep 2015 15:31:12 +0200 Subject: [PATCH 2/5] Added run helper function --- make.go | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/make.go b/make.go index 746f938d1..ad5a9acca 100644 --- a/make.go +++ b/make.go @@ -276,19 +276,44 @@ func clean() error { return nil } -// trace is a helper fucntion that writes a command +// run is a helper function that executes commands +// and assigns stdout and stderr targets +func run(command string, args ...string) error { + cmd := exec.Command(command, args...) + + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + trace(cmd.Args) + err := cmd.Run() + + if err != nil { + return err + } + + return nil +} + +// helper function to parse the git revision +func rev() string { + cmd := exec.Command( + "git", + "rev-parse", + "--short", + "HEAD") + + raw, err := cmd.CombinedOutput() + + if err != nil { + return "HEAD" + } + + return strings.Trim(string(raw), "\n") +} + +// trace is a helper function that writes a command // to stdout similar to bash +x func trace(args []string) { print("+ ") println(strings.Join(args, " ")) } - -// helper function to parse the git revision -func rev() string { - cmd := exec.Command("git", "rev-parse", "--short", "HEAD") - raw, err := cmd.CombinedOutput() - if err != nil { - return "HEAD" - } - return strings.Trim(string(raw), "\n") -} From 17740d0026f535d15e8552282887e42adc58e6ea Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Tue, 1 Sep 2015 16:08:42 +0200 Subject: [PATCH 3/5] Refactoring of the make file --- README.md | 20 ++++- make.go | 247 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 177 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index fe75b3864..832b24ce2 100644 --- a/README.md +++ b/README.md @@ -31,16 +31,28 @@ cd $GOPATH/src/github.com/drone/drone Commands to build from source: ```sh -make deps # download dependencies -make # create binary files in ./bin -make test # execute unit tests +go run make.go deps # Download required dependencies +go run make.go bindata # Generate required bindata +go run make.go build # Build the binaries +go run make.go image # Build docker images +go run make.go test # Run the test suite +go run make.go clean # Clean up environment +``` + +Commands for development: + +```sh +go run make.go scripts # Concat all javascripts +go run make.go styles # Concat all stylesheets +go run make.go vet # Execute vet command +go run make.go fmt # Execute fmt command ``` Commands to start drone: ```sh bin/drone -bin/drone --debug # debug mode loads static content from filesystem +bin/drone --debug # Debug mode loads static content from filesystem ``` If you are seeing slow compile times please install the following: diff --git a/make.go b/make.go index ad5a9acca..770bcdbd2 100644 --- a/make.go +++ b/make.go @@ -12,8 +12,6 @@ import ( "os/exec" "path/filepath" "strings" - - "github.com/jteeuwen/go-bindata" ) var ( @@ -24,28 +22,33 @@ var ( // list of all posible steps that can be executed // as part of the build process. var steps = map[string]step{ - "scripts": scripts, - "styles": styles, - "json": json, - "embed": embed, - "vet": vet, - "bindata": bindat, - "build": build, - "test": test, - "image": image, - "clean": clean, + "deps": executeDeps, + "json": executeJson, + "embed": executeEmbed, + "scripts": executeScripts, + "styles": executeStyles, + "vet": executeVet, + "fmt": executeFmt, + "test": executeTest, + "build": executeBuild, + "image": executeImage, + "bindata": executeBindata, + "clean": executeClean, } func main() { for _, arg := range os.Args[1:] { step, ok := steps[arg] + if !ok { - fmt.Println("error: invalid step", arg) + fmt.Println("Error: Invalid step", arg) os.Exit(1) } + err := step() + if err != nil { - fmt.Println("error: failed step", arg) + fmt.Println("Error: Failed step", arg) os.Exit(1) } } @@ -53,15 +56,43 @@ func main() { type step func() error +func executeDeps() error { + deps := []string{ + "github.com/jteeuwen/go-bindata/...", + "golang.org/x/tools/cmd/cover", + } + + for _, dep := range deps { + err := run( + "go", + "get", + "-u", + dep) + + if err != nil { + return err + } + } + + return nil +} + +// json step generates optimized json marshal and +// unmarshal functions to override defaults. +func executeJson() error { + return nil +} + // embed step embeds static files in .go files. -func embed() error { +func executeEmbed() error { // embed drone.{revision}.css // embed drone.{revision}.js + return nil } // scripts step concatinates all javascript files. -func scripts() error { +func executeScripts() error { files := []string{ "cmd/drone-server/static/scripts/term.js", "cmd/drone-server/static/scripts/drone.js", @@ -105,7 +136,7 @@ func scripts() error { } // styles step concatinates the stylesheet files. -func styles() error { +func executeStyles() error { files := []string{ "cmd/drone-server/static/styles/reset.css", "cmd/drone-server/static/styles/fonts.css", @@ -143,101 +174,151 @@ func styles() error { return nil } -// json step generates optimized json marshal and -// unmarshal functions to override defaults. -func json() error { - return nil +// vet step executes the `go vet` command +func executeVet() error { + return run( + "go", + "vet", + "github.com/drone/drone/pkg/...", + "github.com/drone/drone/cmd/...") } -// bindata step generates go-bindata package. -func bindat() error { - var paths = []struct { - input string - recursive bool - }{ - {"cmd/drone-server/static", true}, - } +// fmt step executes the `go fmt` command +func executeFmt() error { + return run( + "go", + "fmt", + "github.com/drone/drone/pkg/...", + "github.com/drone/drone/cmd/...") +} - c := bindata.NewConfig() - c.Output = "cmd/drone-server/drone_bindata.go" - c.Input = make([]bindata.InputConfig, len(paths)) +// test step executes unit tests and coverage. +func executeTest() error { + ldf := fmt.Sprintf( + "-X main.revision=%s -X main.version=%s", + sha, + version) - for i, path := range paths { - c.Input[i] = bindata.InputConfig{ - Path: path.input, - Recursive: path.recursive, - } - } - - return bindata.Translate(c) + return run( + "go", + "test", + "-cover", + "-ldflags", + ldf, + "github.com/drone/drone/pkg/...", + "github.com/drone/drone/cmd/...") } // build step creates the application binaries. -func build() error { +func executeBuild() error { var bins = []struct { input string output string }{ - {"github.com/drone/drone/cmd/drone-server", "bin/drone"}, + { + "github.com/drone/drone/cmd/drone-server", + "bin/drone", + }, } + for _, bin := range bins { - ldf := fmt.Sprintf("-X main.revision=%s -X main.version=%s", sha, version) - cmd := exec.Command("go", "build", "-o", bin.output, "-ldflags", ldf, bin.input) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - trace(cmd.Args) - err := cmd.Run() + ldf := fmt.Sprintf( + "-X main.revision=%s -X main.version=%s", + sha, + version) + + err := run( + "go", + "build", + "-o", + bin.output, + "-ldflags", + ldf, + bin.input) + if err != nil { return err } } + return nil } -// vet step executes the `go vet` command -func vet() error { - cmd := exec.Command("go", "vet", - "github.com/drone/drone/pkg/...", - "github.com/drone/drone/cmd/...") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - trace(cmd.Args) - return cmd.Run() -} - -// test step executes unit tests and coverage. -func test() error { - cmd := exec.Command("go", "test", "-cover", "./pkg/...") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - trace(cmd.Args) - return cmd.Run() -} - -// image step builds Docker images. -func image() error { +// image step builds docker images. +func executeImage() error { var images = []struct { dir string name string }{ - {"./bin/drone-server", "drone/drone"}, + { + "bin/drone-server", + "drone/drone", + }, } for _, image := range images { - path := filepath.Join(image.dir, "Dockerfile") - name := image.name + ":" + version - cmd := exec.Command("docker", "build", "-rm", path, name) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - trace(cmd.Args) - err := cmd.Run() + path := filepath.Join( + image.dir, + "Dockerfile") + + name := fmt.Sprintf("%s:%s", + image.name, + version) + + err := run( + "docker", + "build", + "-rm", + path, + name) + if err != nil { return err } } + return nil } -func clean() error { +// bindata step generates go-bindata package. +func executeBindata() error { + var paths = []struct { + input string + output string + pkg string + }{ + { + "cmd/drone-server/static/...", + "cmd/drone-server/drone_bindata.go", + "main", + }, + } + + for _, path := range paths { + binErr := run( + "go-bindata", + fmt.Sprintf("-o=%s", path.output), + fmt.Sprintf("-pkg=%s", path.pkg), + path.input) + + if binErr != nil { + return binErr + } + + fmtErr := run( + "go", + "fmt", + path.output) + + if fmtErr != nil { + return fmtErr + } + } + + return nil +} + +// clean step removes all generated files. +func executeClean() error { err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error { suffixes := []string{ ".out", @@ -285,13 +366,7 @@ func run(command string, args ...string) error { cmd.Stderr = os.Stderr trace(cmd.Args) - err := cmd.Run() - - if err != nil { - return err - } - - return nil + return cmd.Run() } // helper function to parse the git revision From 4977375d330e393b8165c3fb3c17ee0b00a4319a Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Tue, 1 Sep 2015 16:09:07 +0200 Subject: [PATCH 4/5] Use the new make tasks for drone runs --- .drone.yml | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/.drone.yml b/.drone.yml index b1bd7bd56..50ac7fa5a 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,18 +1,18 @@ image: bradrydzewski/go:1.5 + git: path: github.com/drone/drone + env: - GOROOT=/usr/local/go - PATH=$PATH:$GOROOT/bin:$GOPATH/bin script: - - go get golang.org/x/tools/cmd/cover - - go get golang.org/x/tools/cmd/vet - - go get -u github.com/jteeuwen/go-bindata/... - + - go run make.go deps - go run make.go bindata - - go run make.go build - go run make.go vet + - go run make.go fmt + - go run make.go build - go run make.go test - make dist @@ -34,7 +34,7 @@ publish: when: owner: drone -# new .drone.yml syntax + clone: path: github.com/drone/drone @@ -44,10 +44,14 @@ build: commands: - export GOPATH=/drone - export PATH=$PATH:$GOPATH/bin - - go get -u github.com/jteeuwen/go-bindata/... - - make deps - - make - - make test + + - go run make.go deps + - go run make.go bindata + - go run make.go vet + - go run make.go fmt + - go run make.go build + - go run make.go test + - make dist compose: From dafa042e764687da285cfedbbfa261fcd0f79bcc Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Wed, 16 Sep 2015 11:25:58 +0200 Subject: [PATCH 5/5] Added install task to make.go --- make.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/make.go b/make.go index 770bcdbd2..04b932d36 100644 --- a/make.go +++ b/make.go @@ -31,6 +31,7 @@ var steps = map[string]step{ "fmt": executeFmt, "test": executeTest, "build": executeBuild, + "install": executeInstall, "image": executeImage, "bindata": executeBindata, "clean": executeClean, @@ -209,6 +210,37 @@ func executeTest() error { "github.com/drone/drone/cmd/...") } +// install step installs the application binaries. +func executeInstall() error { + var bins = []struct { + input string + }{ + { + "github.com/drone/drone/cmd/drone-server", + }, + } + + for _, bin := range bins { + ldf := fmt.Sprintf( + "-X main.revision=%s -X main.version=%s", + sha, + version) + + err := run( + "go", + "install", + "-ldflags", + ldf, + bin.input) + + if err != nil { + return err + } + } + + return nil +} + // build step creates the application binaries. func executeBuild() error { var bins = []struct {