Refactoring of the make file

This commit is contained in:
Thomas Boerger 2015-09-01 16:08:42 +02:00
parent 7df78a2864
commit 17740d0026
2 changed files with 177 additions and 90 deletions

View file

@ -31,16 +31,28 @@ cd $GOPATH/src/github.com/drone/drone
Commands to build from source: Commands to build from source:
```sh ```sh
make deps # download dependencies go run make.go deps # Download required dependencies
make # create binary files in ./bin go run make.go bindata # Generate required bindata
make test # execute unit tests 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: Commands to start drone:
```sh ```sh
bin/drone 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: If you are seeing slow compile times please install the following:

247
make.go
View file

@ -12,8 +12,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/jteeuwen/go-bindata"
) )
var ( var (
@ -24,28 +22,33 @@ var (
// list of all posible steps that can be executed // list of all posible steps that can be executed
// as part of the build process. // as part of the build process.
var steps = map[string]step{ var steps = map[string]step{
"scripts": scripts, "deps": executeDeps,
"styles": styles, "json": executeJson,
"json": json, "embed": executeEmbed,
"embed": embed, "scripts": executeScripts,
"vet": vet, "styles": executeStyles,
"bindata": bindat, "vet": executeVet,
"build": build, "fmt": executeFmt,
"test": test, "test": executeTest,
"image": image, "build": executeBuild,
"clean": clean, "image": executeImage,
"bindata": executeBindata,
"clean": executeClean,
} }
func main() { func main() {
for _, arg := range os.Args[1:] { for _, arg := range os.Args[1:] {
step, ok := steps[arg] step, ok := steps[arg]
if !ok { if !ok {
fmt.Println("error: invalid step", arg) fmt.Println("Error: Invalid step", arg)
os.Exit(1) os.Exit(1)
} }
err := step() err := step()
if err != nil { if err != nil {
fmt.Println("error: failed step", arg) fmt.Println("Error: Failed step", arg)
os.Exit(1) os.Exit(1)
} }
} }
@ -53,15 +56,43 @@ func main() {
type step func() error 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. // embed step embeds static files in .go files.
func embed() error { func executeEmbed() error {
// embed drone.{revision}.css // embed drone.{revision}.css
// embed drone.{revision}.js // embed drone.{revision}.js
return nil return nil
} }
// scripts step concatinates all javascript files. // scripts step concatinates all javascript files.
func scripts() error { func executeScripts() error {
files := []string{ files := []string{
"cmd/drone-server/static/scripts/term.js", "cmd/drone-server/static/scripts/term.js",
"cmd/drone-server/static/scripts/drone.js", "cmd/drone-server/static/scripts/drone.js",
@ -105,7 +136,7 @@ func scripts() error {
} }
// styles step concatinates the stylesheet files. // styles step concatinates the stylesheet files.
func styles() error { func executeStyles() error {
files := []string{ files := []string{
"cmd/drone-server/static/styles/reset.css", "cmd/drone-server/static/styles/reset.css",
"cmd/drone-server/static/styles/fonts.css", "cmd/drone-server/static/styles/fonts.css",
@ -143,101 +174,151 @@ func styles() error {
return nil return nil
} }
// json step generates optimized json marshal and // vet step executes the `go vet` command
// unmarshal functions to override defaults. func executeVet() error {
func json() error { return run(
return nil "go",
"vet",
"github.com/drone/drone/pkg/...",
"github.com/drone/drone/cmd/...")
} }
// bindata step generates go-bindata package. // fmt step executes the `go fmt` command
func bindat() error { func executeFmt() error {
var paths = []struct { return run(
input string "go",
recursive bool "fmt",
}{ "github.com/drone/drone/pkg/...",
{"cmd/drone-server/static", true}, "github.com/drone/drone/cmd/...")
} }
c := bindata.NewConfig() // test step executes unit tests and coverage.
c.Output = "cmd/drone-server/drone_bindata.go" func executeTest() error {
c.Input = make([]bindata.InputConfig, len(paths)) ldf := fmt.Sprintf(
"-X main.revision=%s -X main.version=%s",
sha,
version)
for i, path := range paths { return run(
c.Input[i] = bindata.InputConfig{ "go",
Path: path.input, "test",
Recursive: path.recursive, "-cover",
} "-ldflags",
} ldf,
"github.com/drone/drone/pkg/...",
return bindata.Translate(c) "github.com/drone/drone/cmd/...")
} }
// build step creates the application binaries. // build step creates the application binaries.
func build() error { func executeBuild() error {
var bins = []struct { var bins = []struct {
input string input string
output 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 { for _, bin := range bins {
ldf := fmt.Sprintf("-X main.revision=%s -X main.version=%s", sha, version) ldf := fmt.Sprintf(
cmd := exec.Command("go", "build", "-o", bin.output, "-ldflags", ldf, bin.input) "-X main.revision=%s -X main.version=%s",
cmd.Stdout = os.Stdout sha,
cmd.Stderr = os.Stderr version)
trace(cmd.Args)
err := cmd.Run() err := run(
"go",
"build",
"-o",
bin.output,
"-ldflags",
ldf,
bin.input)
if err != nil { if err != nil {
return err return err
} }
} }
return nil return nil
} }
// vet step executes the `go vet` command // image step builds docker images.
func vet() error { func executeImage() 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 {
var images = []struct { var images = []struct {
dir string dir string
name string name string
}{ }{
{"./bin/drone-server", "drone/drone"}, {
"bin/drone-server",
"drone/drone",
},
} }
for _, image := range images { for _, image := range images {
path := filepath.Join(image.dir, "Dockerfile") path := filepath.Join(
name := image.name + ":" + version image.dir,
cmd := exec.Command("docker", "build", "-rm", path, name) "Dockerfile")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr name := fmt.Sprintf("%s:%s",
trace(cmd.Args) image.name,
err := cmd.Run() version)
err := run(
"docker",
"build",
"-rm",
path,
name)
if err != nil { if err != nil {
return err return err
} }
} }
return nil 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 { err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error {
suffixes := []string{ suffixes := []string{
".out", ".out",
@ -285,13 +366,7 @@ func run(command string, args ...string) error {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
trace(cmd.Args) trace(cmd.Args)
err := cmd.Run() return cmd.Run()
if err != nil {
return err
}
return nil
} }
// helper function to parse the git revision // helper function to parse the git revision