From ed5ccf9bdb1f5ccd5afd7cc8dbdbb57e62459f28 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 27 Jun 2017 19:20:08 -0300 Subject: [PATCH 01/28] wip multiple builds --- config/config.go | 4 ++- goreleaser.yml | 25 +++++++-------- internal/client/github.go | 2 +- pipeline/brew/brew.go | 2 +- pipeline/build/build.go | 34 ++++++++++++--------- pipeline/build/target.go | 20 ++++++------ pipeline/defaults/defaults.go | 57 ++++++++++++++++++++++++----------- 7 files changed, 88 insertions(+), 56 deletions(-) diff --git a/config/config.go b/config/config.go index 1db76a004..15f43ea7a 100644 --- a/config/config.go +++ b/config/config.go @@ -99,13 +99,15 @@ type Snapshot struct { // Project includes all project configuration type Project struct { + Name string `yaml:",omitempty"` Release Release `yaml:",omitempty"` Brew Homebrew `yaml:",omitempty"` - Build Build `yaml:",omitempty"` + Builds []Build `yaml:",omitempty"` Archive Archive `yaml:",omitempty"` FPM FPM `yaml:",omitempty"` Snapshot Snapshot `yaml:",omitempty"` + // Build Build `yaml:",omitempty"` // deprecated, remove // test only property indicating the path to the dist folder Dist string `yaml:"-"` } diff --git a/goreleaser.yml b/goreleaser.yml index b7d8ea538..8754200c3 100644 --- a/goreleaser.yml +++ b/goreleaser.yml @@ -1,17 +1,18 @@ homepage: &homepage http://goreleaser.github.io description: &description Deliver Go binaries as fast and easily as possible -build: - env: - - CGO_ENABLED=0 - goos: - - linux - - darwin - - windows - goarch: - - 386 - - amd64 - - arm - - arm64 +builds: + - + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + - windows + goarch: + - 386 + - amd64 + - arm + - arm64 archive: name_template: '{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' replacements: diff --git a/internal/client/github.go b/internal/client/github.go index 738904a1a..26cfab78a 100644 --- a/internal/client/github.go +++ b/internal/client/github.go @@ -36,7 +36,7 @@ func (c *githubClient) CreateFile( }, Content: content.Bytes(), Message: github.String( - ctx.Config.Build.Binary + " version " + ctx.Git.CurrentTag, + ctx.Config.Name + " version " + ctx.Git.CurrentTag, ), } diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index fb385793e..5a6c4e509 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -106,7 +106,7 @@ func doRun(ctx *context.Context, client client.Client) error { log.Warn("skipped because release is marked as draft") return nil } - path := filepath.Join(ctx.Config.Brew.Folder, ctx.Config.Build.Binary+".rb") + path := filepath.Join(ctx.Config.Brew.Folder, ctx.Config.Name+".rb") log.WithField("formula", path). WithField("repo", ctx.Config.Brew.GitHub.String()). Info("pushing") diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 944e7181c..81cea0ece 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/apex/log" + "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/ext" "golang.org/x/sync/errgroup" @@ -25,12 +26,21 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { - if err := runHook(ctx.Config.Build.Env, ctx.Config.Build.Hooks.Pre); err != nil { + for _, build := range ctx.Config.Builds { + if err := runPipeOnBuild(ctx, build); err != nil { + return err + } + } + return nil +} + +func runPipeOnBuild(ctx *context.Context, build config.Build) error { + if err := runHook(build.Env, build.Hooks.Pre); err != nil { return err } sem := make(chan bool, 4) var g errgroup.Group - for _, target := range buildTargets(ctx) { + for _, target := range buildTargets(build) { name, err := nameFor(ctx, target) if err != nil { return err @@ -43,13 +53,13 @@ func (Pipe) Run(ctx *context.Context) error { defer func() { <-sem }() - return build(ctx, name, target) + return doBuild(ctx, build, name, target) }) } if err := g.Wait(); err != nil { return err } - return runHook(ctx.Config.Build.Env, ctx.Config.Build.Hooks.Post) + return runHook(build.Env, build.Hooks.Post) } func runHook(env []string, hook string) error { @@ -61,23 +71,19 @@ func runHook(env []string, hook string) error { return run(runtimeTarget, cmd, env) } -func build(ctx *context.Context, name string, target buildTarget) error { - output := filepath.Join( - ctx.Config.Dist, - name, - ctx.Config.Build.Binary+ext.For(target.goos), - ) +func doBuild(ctx *context.Context, build config.Build, name string, target buildTarget) error { + output := filepath.Join(ctx.Config.Dist, name, build.Binary+ext.For(target.goos)) log.WithField("binary", output).Info("building") cmd := []string{"go", "build"} - if ctx.Config.Build.Flags != "" { - cmd = append(cmd, strings.Fields(ctx.Config.Build.Flags)...) + if build.Flags != "" { + cmd = append(cmd, strings.Fields(build.Flags)...) } flags, err := ldflags(ctx) if err != nil { return err } - cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main) - return run(target, cmd, ctx.Config.Build.Env) + cmd = append(cmd, "-ldflags="+flags, "-o", output, build.Main) + return run(target, cmd, build.Env) } func run(target buildTarget, command, env []string) error { diff --git a/pipeline/build/target.go b/pipeline/build/target.go index 1bdce6894..826c39d88 100644 --- a/pipeline/build/target.go +++ b/pipeline/build/target.go @@ -5,7 +5,7 @@ import ( "runtime" "github.com/apex/log" - "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/config" ) var runtimeTarget = buildTarget{runtime.GOOS, runtime.GOARCH, ""} @@ -23,14 +23,14 @@ func (t buildTarget) PrettyString() string { return fmt.Sprintf("%v/%v%v", t.goos, t.goarch, t.goarm) } -func buildTargets(ctx *context.Context) (targets []buildTarget) { - for _, target := range allBuildTargets(ctx) { +func buildTargets(build config.Build) (targets []buildTarget) { + for _, target := range allBuildTargets(build) { if !valid(target) { log.WithField("target", target.PrettyString()). Warn("skipped invalid build") continue } - if ignored(ctx, target) { + if ignored(build, target) { log.WithField("target", target.PrettyString()). Warn("skipped ignored build") continue @@ -40,11 +40,11 @@ func buildTargets(ctx *context.Context) (targets []buildTarget) { return } -func allBuildTargets(ctx *context.Context) (targets []buildTarget) { - for _, goos := range ctx.Config.Build.Goos { - for _, goarch := range ctx.Config.Build.Goarch { +func allBuildTargets(build config.Build) (targets []buildTarget) { + for _, goos := range build.Goos { + for _, goarch := range build.Goarch { if goarch == "arm" { - for _, goarm := range ctx.Config.Build.Goarm { + for _, goarm := range build.Goarm { targets = append(targets, buildTarget{goos, goarch, goarm}) } continue @@ -55,8 +55,8 @@ func allBuildTargets(ctx *context.Context) (targets []buildTarget) { return } -func ignored(ctx *context.Context, target buildTarget) bool { - for _, ig := range ctx.Config.Build.Ignore { +func ignored(build config.Build, target buildTarget) bool { + for _, ig := range build.Ignore { var ignored = buildTarget{ig.Goos, ig.Goarch, ig.Goarm} if ignored == target { return true diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index d38f5b2e9..375b3603f 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -4,7 +4,10 @@ package defaults import ( "fmt" + "strings" + "github.com/apex/log" + "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" ) @@ -31,14 +34,23 @@ func (Pipe) Run(ctx *context.Context) error { if err := setReleaseDefaults(ctx); err != nil { return err } + if ctx.Config.Name == "" { + ctx.Config.Name = ctx.Config.Release.GitHub.Name + } setBuildDefaults(ctx) if ctx.Config.Brew.Install == "" { - ctx.Config.Brew.Install = fmt.Sprintf( - `bin.install "%s"`, - ctx.Config.Build.Binary, - ) + var installs []string + for _, build := range ctx.Config.Builds { + installs = append( + installs, + fmt.Sprintf(`bin.install "%s"`, build.Binary), + ) + } + ctx.Config.Brew.Install = strings.Join(installs, "\n") } - return setArchiveDefaults(ctx) + err := setArchiveDefaults(ctx) + log.WithField("config", ctx.Config).Debug("defaults set") + return err } func setReleaseDefaults(ctx *context.Context) error { @@ -54,24 +66,35 @@ func setReleaseDefaults(ctx *context.Context) error { } func setBuildDefaults(ctx *context.Context) { - if ctx.Config.Build.Binary == "" { - ctx.Config.Build.Binary = ctx.Config.Release.GitHub.Name + if len(ctx.Config.Builds) == 0 { + ctx.Config.Builds = append(ctx.Config.Builds, ctx.Config.Build) } - if ctx.Config.Build.Main == "" { - ctx.Config.Build.Main = "." + ctx.Config.Build = config.Build{} + for i, build := range ctx.Config.Builds { + ctx.Config.Builds[i] = buildWithDefaults(ctx, build) } - if len(ctx.Config.Build.Goos) == 0 { - ctx.Config.Build.Goos = []string{"linux", "darwin"} +} + +func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { + if build.Binary == "" { + build.Binary = ctx.Config.Release.GitHub.Name } - if len(ctx.Config.Build.Goarch) == 0 { - ctx.Config.Build.Goarch = []string{"amd64", "386"} + if build.Main == "" { + build.Main = "." } - if len(ctx.Config.Build.Goarm) == 0 { - ctx.Config.Build.Goarm = []string{"6"} + if len(build.Goos) == 0 { + build.Goos = []string{"linux", "darwin"} } - if ctx.Config.Build.Ldflags == "" { - ctx.Config.Build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" + if len(build.Goarch) == 0 { + build.Goarch = []string{"amd64", "386"} } + if len(build.Goarm) == 0 { + build.Goarm = []string{"6"} + } + if build.Ldflags == "" { + build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" + } + return build } func setArchiveDefaults(ctx *context.Context) error { From 4504cd4527825f7654530195eee586b9c8d528a9 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 27 Jun 2017 19:36:36 -0300 Subject: [PATCH 02/28] fixing more errors --- pipeline/archive/archive.go | 8 ++++--- pipeline/brew/brew.go | 6 ++---- pipeline/build/build.go | 7 ++++--- pipeline/build/ldflags.go | 5 +++-- pipeline/build/name.go | 5 +++-- pipeline/checksums/checksums.go | 2 +- pipeline/defaults/defaults.go | 4 ---- pipeline/fpm/fpm.go | 37 ++++++++++++++++++++------------- 8 files changed, 41 insertions(+), 33 deletions(-) diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index b6b4791c5..147411ea9 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -58,9 +58,11 @@ func create(ctx *context.Context, platform, name string) error { return err } } - var binary = ctx.Config.Build.Binary + ext.For(platform) - if err := archive.Add(binary, filepath.Join(folder, binary)); err != nil { - return err + for _, build := range ctx.Config.Builds { + var binary = build.Binary + ext.For(platform) + if err := archive.Add(binary, filepath.Join(folder, binary)); err != nil { + return err + } } ctx.AddArtifact(file.Name()) return nil diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 5a6c4e509..47fb09e71 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -69,7 +69,6 @@ type templateData struct { Repo config.Repo // FIXME: will not work for anything but github right now. Tag string Version string - Binary string Caveats string File string Format string @@ -127,7 +126,7 @@ func buildFormula(ctx *context.Context, client client.Client) (bytes.Buffer, err func doBuildFormula(data templateData) (bytes.Buffer, error) { var out bytes.Buffer - tmpl, err := template.New(data.Binary).Parse(formula) + tmpl, err := template.New(data.Name).Parse(formula) if err != nil { return out, err } @@ -150,13 +149,12 @@ func dataFor(ctx *context.Context, client client.Client) (result templateData, e return } return templateData{ - Name: formulaNameFor(ctx.Config.Build.Binary), + Name: formulaNameFor(ctx.Config.Name), Desc: ctx.Config.Brew.Description, Homepage: ctx.Config.Brew.Homepage, Repo: ctx.Config.Release.GitHub, Tag: ctx.Git.CurrentTag, Version: ctx.Version, - Binary: ctx.Config.Build.Binary, Caveats: ctx.Config.Brew.Caveats, File: file, Format: ctx.Config.Archive.Format, // TODO this can be broken by format_overrides diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 81cea0ece..003b3982f 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -27,6 +27,7 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { for _, build := range ctx.Config.Builds { + log.WithField("build", build).Debug("building") if err := runPipeOnBuild(ctx, build); err != nil { return err } @@ -41,11 +42,11 @@ func runPipeOnBuild(ctx *context.Context, build config.Build) error { sem := make(chan bool, 4) var g errgroup.Group for _, target := range buildTargets(build) { - name, err := nameFor(ctx, target) + name, err := nameFor(ctx, build, target) if err != nil { return err } - ctx.Archives[target.String()] = name + ctx.Archives[build.Binary+target.String()] = name sem <- true target := target @@ -78,7 +79,7 @@ func doBuild(ctx *context.Context, build config.Build, name string, target build if build.Flags != "" { cmd = append(cmd, strings.Fields(build.Flags)...) } - flags, err := ldflags(ctx) + flags, err := ldflags(ctx, build) if err != nil { return err } diff --git a/pipeline/build/ldflags.go b/pipeline/build/ldflags.go index 2dacfb3ed..cf6633616 100644 --- a/pipeline/build/ldflags.go +++ b/pipeline/build/ldflags.go @@ -5,6 +5,7 @@ import ( "text/template" "time" + "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" ) @@ -15,7 +16,7 @@ type ldflagsData struct { Version string } -func ldflags(ctx *context.Context) (string, error) { +func ldflags(ctx *context.Context, build config.Build) (string, error) { var data = ldflagsData{ Commit: ctx.Git.Commit, Tag: ctx.Git.CurrentTag, @@ -23,7 +24,7 @@ func ldflags(ctx *context.Context) (string, error) { Date: time.Now().UTC().Format(time.RFC3339), } var out bytes.Buffer - t, err := template.New("ldflags").Parse(ctx.Config.Build.Ldflags) + t, err := template.New("ldflags").Parse(build.Ldflags) if err != nil { return "", err } diff --git a/pipeline/build/name.go b/pipeline/build/name.go index 89c582b17..2df4adf67 100644 --- a/pipeline/build/name.go +++ b/pipeline/build/name.go @@ -4,6 +4,7 @@ import ( "bytes" "text/template" + "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" ) @@ -16,14 +17,14 @@ type nameData struct { Binary string } -func nameFor(ctx *context.Context, target buildTarget) (string, error) { +func nameFor(ctx *context.Context, build config.Build, target buildTarget) (string, error) { var data = nameData{ Os: replace(ctx.Config.Archive.Replacements, target.goos), Arch: replace(ctx.Config.Archive.Replacements, target.goarch), Arm: replace(ctx.Config.Archive.Replacements, target.goarm), Version: ctx.Version, Tag: ctx.Git.CurrentTag, - Binary: ctx.Config.Build.Binary, + Binary: build.Binary, } var out bytes.Buffer diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index abf33b39a..4c2060948 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -26,7 +26,7 @@ func (Pipe) Run(ctx *context.Context) (err error) { file, err := os.OpenFile( filepath.Join( ctx.Config.Dist, - fmt.Sprintf("%v_checksums.txt", ctx.Config.Build.Binary), + fmt.Sprintf("%v_checksums.txt", ctx.Config.Name), ), os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644, diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index 375b3603f..e010b39cc 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -66,10 +66,6 @@ func setReleaseDefaults(ctx *context.Context) error { } func setBuildDefaults(ctx *context.Context) { - if len(ctx.Config.Builds) == 0 { - ctx.Config.Builds = append(ctx.Config.Builds, ctx.Config.Build) - } - ctx.Config.Build = config.Build{} for i, build := range ctx.Config.Builds { ctx.Config.Builds[i] = buildWithDefaults(ctx, build) } diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index b859826d0..4fafc846d 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -3,6 +3,7 @@ package fpm import ( "errors" + "fmt" "os/exec" "path/filepath" @@ -40,16 +41,19 @@ func (Pipe) Run(ctx *context.Context) error { var g errgroup.Group for _, format := range ctx.Config.FPM.Formats { - format := format - for _, goarch := range ctx.Config.Build.Goarch { - if ctx.Archives["linux"+goarch] == "" { - continue + for _, build := range ctx.Config.Builds { + for _, goarch := range build.Goarch { + var key = build.Binary + "linux" + goarch + if ctx.Archives[key] == "" { + continue + } + format := format + archive := ctx.Archives[key] + arch := goarchToUnix[goarch] + g.Go(func() error { + return create(ctx, format, archive, arch) + }) } - archive := ctx.Archives["linux"+goarch] - arch := goarchToUnix[goarch] - g.Go(func() error { - return create(ctx, format, archive, arch) - }) } } return g.Wait() @@ -58,13 +62,12 @@ func (Pipe) Run(ctx *context.Context) error { func create(ctx *context.Context, format, archive, arch string) error { var path = filepath.Join(ctx.Config.Dist, archive) var file = path + "." + format - var name = ctx.Config.Build.Binary log.WithField("file", file).Info("Creating") var options = []string{ "--input-type", "dir", "--output-type", format, - "--name", name, + "--name", ctx.Config.Name, "--version", ctx.Version, "--architecture", arch, "--chdir", path, @@ -94,9 +97,15 @@ func create(ctx *context.Context, format, archive, arch string) error { options = append(options, "--conflicts", conflict) } - // This basically tells fpm to put the binary in the /usr/local/bin - // binary=/usr/local/bin/binary - options = append(options, name+"="+filepath.Join("/usr/local/bin", name)) + for _, build := range ctx.Config.Builds { + // This basically tells fpm to put the binary in the /usr/local/bin + // binary=/usr/local/bin/binary + options = append(options, fmt.Sprintf( + "%s=%s", + build.Binary, + filepath.Join("/usr/local/bin", build.Binary), + )) + } if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil { return errors.New(string(out)) From a8f1645ea904d3461e90aecbd21924bf9317ffc2 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 27 Jun 2017 20:06:45 -0300 Subject: [PATCH 03/28] fixing archiving and brew --- config/config.go | 2 ++ pipeline/archive/archive.go | 5 ++-- pipeline/brew/brew.go | 20 +++++++++------ pipeline/defaults/defaults.go | 48 +++++++++++++++++------------------ 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/config/config.go b/config/config.go index 15f43ea7a..4323aa9a2 100644 --- a/config/config.go +++ b/config/config.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" + "github.com/apex/log" yaml "gopkg.in/yaml.v1" ) @@ -128,5 +129,6 @@ func LoadReader(fd io.Reader) (config Project, err error) { return config, err } err = yaml.Unmarshal(data, &config) + log.WithField("config", config).Debug("loaded") return } diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index 147411ea9..49fda5440 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -38,7 +38,7 @@ func (Pipe) Run(ctx *context.Context) error { } func create(ctx *context.Context, platform, name string) error { - var folder = filepath.Join(ctx.Config.Dist, name) + var folder = filepath.Join(ctx.Config.Dist, ctx.Config.Name) var format = formatFor(ctx, platform) file, err := os.Create(folder + "." + format) if err != nil { @@ -60,7 +60,8 @@ func create(ctx *context.Context, platform, name string) error { } for _, build := range ctx.Config.Builds { var binary = build.Binary + ext.For(platform) - if err := archive.Add(binary, filepath.Join(folder, binary)); err != nil { + var bin = filepath.Join(ctx.Config.Dist, name) + if err := archive.Add(binary, filepath.Join(bin, binary)); err != nil { return err } } diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 47fb09e71..663bf7960 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -134,17 +134,21 @@ func doBuildFormula(data templateData) (bytes.Buffer, error) { return out, err } +func hasDarwinBuilds(ctx *context.Context) bool { + for key := range ctx.Archives { + if strings.HasSuffix(key, "darwinamd64") { + return true + } + } + return false +} + func dataFor(ctx *context.Context, client client.Client) (result templateData, err error) { - file := ctx.Archives["darwinamd64"] - if file == "" { + if !hasDarwinBuilds(ctx) { return result, ErrNoDarwin64Build } - sum, err := checksum.SHA256( - filepath.Join( - ctx.Config.Dist, - file+"."+ctx.Config.Archive.Format, - ), - ) + var file = ctx.Config.Name + "." + ctx.Config.Archive.Format + sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file)) if err != nil { return } diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index e010b39cc..089c318d9 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -66,31 +66,31 @@ func setReleaseDefaults(ctx *context.Context) error { } func setBuildDefaults(ctx *context.Context) { - for i, build := range ctx.Config.Builds { - ctx.Config.Builds[i] = buildWithDefaults(ctx, build) + var builds []config.Build + log.WithField("builds", ctx.Config.Builds).Debug("builds cleaned") + for _, build := range ctx.Config.Builds { + if build.Binary == "" { + build.Binary = ctx.Config.Release.GitHub.Name + } + if build.Main == "" { + build.Main = "." + } + if len(build.Goos) == 0 { + build.Goos = []string{"linux", "darwin"} + } + if len(build.Goarch) == 0 { + build.Goarch = []string{"amd64", "386"} + } + if len(build.Goarm) == 0 { + build.Goarm = []string{"6"} + } + if build.Ldflags == "" { + build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" + } + builds = append(builds, build) } -} - -func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { - if build.Binary == "" { - build.Binary = ctx.Config.Release.GitHub.Name - } - if build.Main == "" { - build.Main = "." - } - if len(build.Goos) == 0 { - build.Goos = []string{"linux", "darwin"} - } - if len(build.Goarch) == 0 { - build.Goarch = []string{"amd64", "386"} - } - if len(build.Goarm) == 0 { - build.Goarm = []string{"6"} - } - if build.Ldflags == "" { - build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" - } - return build + ctx.Config.Builds = builds + log.WithField("builds", ctx.Config.Builds).Debug("set") } func setArchiveDefaults(ctx *context.Context) error { From 05744bb0c754387b4fbcc292967a495223d91b37 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 27 Jun 2017 22:45:59 -0300 Subject: [PATCH 04/28] wip more changes --- goreleaser.yml | 2 +- pipeline/build/build.go | 2 +- pipeline/build/name.go | 5 ++--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/goreleaser.yml b/goreleaser.yml index 8754200c3..01c8b5844 100644 --- a/goreleaser.yml +++ b/goreleaser.yml @@ -3,7 +3,7 @@ description: &description Deliver Go binaries as fast and easily as possible builds: - env: - - CGO_ENABLED=0 + - CGO_ENABLED=0 goos: - linux - darwin diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 003b3982f..2cc61a61d 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -42,7 +42,7 @@ func runPipeOnBuild(ctx *context.Context, build config.Build) error { sem := make(chan bool, 4) var g errgroup.Group for _, target := range buildTargets(build) { - name, err := nameFor(ctx, build, target) + name, err := nameFor(ctx, target) if err != nil { return err } diff --git a/pipeline/build/name.go b/pipeline/build/name.go index 2df4adf67..b75aa3d51 100644 --- a/pipeline/build/name.go +++ b/pipeline/build/name.go @@ -4,7 +4,6 @@ import ( "bytes" "text/template" - "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" ) @@ -17,14 +16,14 @@ type nameData struct { Binary string } -func nameFor(ctx *context.Context, build config.Build, target buildTarget) (string, error) { +func nameFor(ctx *context.Context, target buildTarget) (string, error) { var data = nameData{ Os: replace(ctx.Config.Archive.Replacements, target.goos), Arch: replace(ctx.Config.Archive.Replacements, target.goarch), Arm: replace(ctx.Config.Archive.Replacements, target.goarm), Version: ctx.Version, Tag: ctx.Git.CurrentTag, - Binary: build.Binary, + Binary: ctx.Config.Name, } var out bytes.Buffer From 059942124a25811beb6ff63ba41b79c6172c886b Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:14:43 -0300 Subject: [PATCH 05/28] moved naming logic to another pkg --- {pipeline/build => internal/name}/name.go | 10 ++--- .../build => internal/name}/name_test.go | 37 ++++++++----------- 2 files changed, 21 insertions(+), 26 deletions(-) rename {pipeline/build => internal/name}/name.go (69%) rename {pipeline/build => internal/name}/name_test.go (74%) diff --git a/pipeline/build/name.go b/internal/name/name.go similarity index 69% rename from pipeline/build/name.go rename to internal/name/name.go index b75aa3d51..1f5a19616 100644 --- a/pipeline/build/name.go +++ b/internal/name/name.go @@ -1,4 +1,4 @@ -package build +package name import ( "bytes" @@ -16,11 +16,11 @@ type nameData struct { Binary string } -func nameFor(ctx *context.Context, target buildTarget) (string, error) { +func For(ctx *context.Context, goos, goarch, goarm string) (string, error) { var data = nameData{ - Os: replace(ctx.Config.Archive.Replacements, target.goos), - Arch: replace(ctx.Config.Archive.Replacements, target.goarch), - Arm: replace(ctx.Config.Archive.Replacements, target.goarm), + Os: replace(ctx.Config.Archive.Replacements, goos), + Arch: replace(ctx.Config.Archive.Replacements, goarch), + Arm: replace(ctx.Config.Archive.Replacements, goarm), Version: ctx.Version, Tag: ctx.Git.CurrentTag, Binary: ctx.Config.Name, diff --git a/pipeline/build/name_test.go b/internal/name/name_test.go similarity index 74% rename from pipeline/build/name_test.go rename to internal/name/name_test.go index cc3dee858..a4327adf1 100644 --- a/pipeline/build/name_test.go +++ b/internal/name/name_test.go @@ -1,4 +1,4 @@ -package build +package name import ( "testing" @@ -20,9 +20,7 @@ func TestNameFor(t *testing.T) { "amd64": "x86_64", }, }, - Build: config.Build{ - Binary: "test", - }, + Name: "test", } var ctx = &context.Context{ Config: config, @@ -32,30 +30,26 @@ func TestNameFor(t *testing.T) { }, } - name, err := nameFor(ctx, buildTarget{"darwin", "amd64", ""}) + name, err := For(ctx, "darwin", "amd64", "") assert.NoError(err) assert.Equal("test_Darwin_x86_64_v1.2.3_1.2.3", name) } func TestInvalidNameTemplate(t *testing.T) { - assert := assert.New(t) - - var config = config.Project{ - Archive: config.Archive{ - NameTemplate: "{{.Binary}_{{.Os}}_{{.Arch}}_{{.Version}}", - }, - Build: config.Build{ - Binary: "test", - }, - } + var assert = assert.New(t) var ctx = &context.Context{ - Config: config, + Config: config.Project{ + Archive: config.Archive{ + NameTemplate: "{{.Binary}_{{.Os}}_{{.Arch}}_{{.Version}}", + }, + Name: "test", + }, Git: context.GitInfo{ CurrentTag: "v1.2.3", }, } - _, err := nameFor(ctx, buildTarget{"darwin", "amd64", ""}) + _, err := For(ctx, "darwin", "amd64", "") assert.Error(err) } @@ -66,19 +60,20 @@ func TestNameDefaltTemplate(t *testing.T) { Archive: config.Archive{ NameTemplate: defaults.NameTemplate, }, - Build: config.Build{ - Binary: "test", - }, + Name: "test", }, Version: "1.2.3", } + type buildTarget struct { + goos, goarch, goarm string + } for key, target := range map[string]buildTarget{ "test_1.2.3_darwin_amd64": {"darwin", "amd64", ""}, "test_1.2.3_linux_arm64": {"linux", "arm64", ""}, "test_1.2.3_linux_armv7": {"linux", "arm", "7"}, } { t.Run(key, func(t *testing.T) { - name, err := nameFor(ctx, target) + name, err := For(ctx, target.goos, target.goarch, target.goarm) assert.NoError(err) assert.Equal(key, name) }) From d286dffd524c2934de2f01d09363d3684a2fd25c Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:15:45 -0300 Subject: [PATCH 06/28] docs --- internal/name/name.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/name/name.go b/internal/name/name.go index 1f5a19616..a3ac20a54 100644 --- a/internal/name/name.go +++ b/internal/name/name.go @@ -1,3 +1,5 @@ +// Package name provides name template logic for the final archive, formulae, +// etc. package name import ( @@ -16,6 +18,7 @@ type nameData struct { Binary string } +// For returns the name for the given context, goos, goarch and goarm. func For(ctx *context.Context, goos, goarch, goarm string) (string, error) { var data = nameData{ Os: replace(ctx.Config.Archive.Replacements, goos), From 45a24a1101d138b58a35cabc678e98e72d068f2b Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:27:13 -0300 Subject: [PATCH 07/28] improved build --- context/context.go | 14 +++++++++++--- pipeline/build/build.go | 27 ++++++++++++++++----------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/context/context.go b/context/context.go index c3d6541e7..cf92af2f6 100644 --- a/context/context.go +++ b/context/context.go @@ -36,18 +36,26 @@ type Context struct { Snapshot bool } -var lock sync.Mutex +var artifactLock sync.Mutex +var archiveLock sync.Mutex // AddArtifact adds a file to upload list func (ctx *Context) AddArtifact(file string) { - lock.Lock() - defer lock.Unlock() + artifactLock.Lock() + defer artifactLock.Unlock() file = strings.TrimPrefix(file, ctx.Config.Dist) file = strings.Replace(file, "/", "", -1) ctx.Artifacts = append(ctx.Artifacts, file) log.WithField("artifact", file).Info("registered") } +func (ctx *Context) AddArchive(key, file string) { + archiveLock.Lock() + defer archiveLock.Unlock() + ctx.Archives[key] = file + log.WithField("key", key).WithField("archive", file).Info("added") +} + // New context func New(config config.Project) *Context { return &Context{ diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 2cc61a61d..65ad02627 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -13,6 +13,7 @@ import ( "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/ext" + "github.com/goreleaser/goreleaser/internal/name" "golang.org/x/sync/errgroup" ) @@ -42,19 +43,14 @@ func runPipeOnBuild(ctx *context.Context, build config.Build) error { sem := make(chan bool, 4) var g errgroup.Group for _, target := range buildTargets(build) { - name, err := nameFor(ctx, target) - if err != nil { - return err - } - ctx.Archives[build.Binary+target.String()] = name - sem <- true target := target + build := build g.Go(func() error { defer func() { <-sem }() - return doBuild(ctx, build, name, target) + return doBuild(ctx, build, target) }) } if err := g.Wait(); err != nil { @@ -72,9 +68,18 @@ func runHook(env []string, hook string) error { return run(runtimeTarget, cmd, env) } -func doBuild(ctx *context.Context, build config.Build, name string, target buildTarget) error { - output := filepath.Join(ctx.Config.Dist, name, build.Binary+ext.For(target.goos)) - log.WithField("binary", output).Info("building") +func doBuild(ctx *context.Context, build config.Build, target buildTarget) error { + folder, err := name.For(ctx, target.goos, target.goarch, target.goarm) + if err != nil { + return err + } + var binary = filepath.Join( + ctx.Config.Dist, + folder, + build.Binary+ext.For(target.goos), + ) + ctx.AddArchive(build.Binary+target.String(), binary) + log.WithField("binary", binary).Info("building") cmd := []string{"go", "build"} if build.Flags != "" { cmd = append(cmd, strings.Fields(build.Flags)...) @@ -83,7 +88,7 @@ func doBuild(ctx *context.Context, build config.Build, name string, target build if err != nil { return err } - cmd = append(cmd, "-ldflags="+flags, "-o", output, build.Main) + cmd = append(cmd, "-ldflags="+flags, "-o", binary, build.Main) return run(target, cmd, build.Env) } From 7d01a5a8a49dcd6c2e7c5cf4165635ec47fae409 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:30:24 -0300 Subject: [PATCH 08/28] renamed archives to binaries, pointed more changes --- context/context.go | 9 +++++---- pipeline/archive/archive.go | 3 ++- pipeline/brew/brew.go | 3 ++- pipeline/fpm/fpm.go | 6 ++++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/context/context.go b/context/context.go index cf92af2f6..26dcd6669 100644 --- a/context/context.go +++ b/context/context.go @@ -27,7 +27,7 @@ type Context struct { Config config.Project Token string Git GitInfo - Archives map[string]string + Binaries map[string]string Artifacts []string ReleaseNotes string Version string @@ -49,11 +49,12 @@ func (ctx *Context) AddArtifact(file string) { log.WithField("artifact", file).Info("registered") } -func (ctx *Context) AddArchive(key, file string) { +// AddBinary adds a built binary to the current context +func (ctx *Context) AddBinary(key, file string) { archiveLock.Lock() defer archiveLock.Unlock() - ctx.Archives[key] = file - log.WithField("key", key).WithField("archive", file).Info("added") + ctx.Binaries[key] = file + log.WithField("key", key).WithField("binary", file).Info("added") } // New context diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index 49fda5440..ec7a67a6d 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -27,7 +27,8 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { var g errgroup.Group - for platform, archive := range ctx.Archives { + // TODO: fix here + for platform, archive := range ctx.Binaries { archive := archive platform := platform g.Go(func() error { diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 663bf7960..5aa6824b4 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -135,7 +135,8 @@ func doBuildFormula(data templateData) (bytes.Buffer, error) { } func hasDarwinBuilds(ctx *context.Context) bool { - for key := range ctx.Archives { + // TODO: fix here + for key := range ctx.Binaries { if strings.HasSuffix(key, "darwinamd64") { return true } diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 4fafc846d..7745b2b8e 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -44,11 +44,13 @@ func (Pipe) Run(ctx *context.Context) error { for _, build := range ctx.Config.Builds { for _, goarch := range build.Goarch { var key = build.Binary + "linux" + goarch - if ctx.Archives[key] == "" { + // TODO: fix here + if ctx.Binaries[key] == "" { continue } format := format - archive := ctx.Archives[key] + // TODO: fix here + archive := ctx.Binaries[key] arch := goarchToUnix[goarch] g.Go(func() error { return create(ctx, format, archive, arch) From d2d215feaa5b01955108263ec097595d6368aa14 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:31:12 -0300 Subject: [PATCH 09/28] renamed fields --- context/context.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/context/context.go b/context/context.go index 26dcd6669..032fe70dd 100644 --- a/context/context.go +++ b/context/context.go @@ -36,13 +36,13 @@ type Context struct { Snapshot bool } -var artifactLock sync.Mutex -var archiveLock sync.Mutex +var artifactsLock sync.Mutex +var binariesLock sync.Mutex // AddArtifact adds a file to upload list func (ctx *Context) AddArtifact(file string) { - artifactLock.Lock() - defer artifactLock.Unlock() + artifactsLock.Lock() + defer artifactsLock.Unlock() file = strings.TrimPrefix(file, ctx.Config.Dist) file = strings.Replace(file, "/", "", -1) ctx.Artifacts = append(ctx.Artifacts, file) @@ -51,8 +51,8 @@ func (ctx *Context) AddArtifact(file string) { // AddBinary adds a built binary to the current context func (ctx *Context) AddBinary(key, file string) { - archiveLock.Lock() - defer archiveLock.Unlock() + binariesLock.Lock() + defer binariesLock.Unlock() ctx.Binaries[key] = file log.WithField("key", key).WithField("binary", file).Info("added") } From 3c68b894c511fcf94807d585df5c359259bc5feb Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:46:08 -0300 Subject: [PATCH 10/28] archive pipe fixed --- context/context.go | 17 ++++++++++++++--- pipeline/archive/archive.go | 9 ++++----- pipeline/build/build.go | 3 ++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/context/context.go b/context/context.go index 032fe70dd..7b93ba935 100644 --- a/context/context.go +++ b/context/context.go @@ -28,6 +28,7 @@ type Context struct { Token string Git GitInfo Binaries map[string]string + Folders map[string]string Artifacts []string ReleaseNotes string Version string @@ -38,6 +39,7 @@ type Context struct { var artifactsLock sync.Mutex var binariesLock sync.Mutex +var foldersLock sync.Mutex // AddArtifact adds a file to upload list func (ctx *Context) AddArtifact(file string) { @@ -46,7 +48,7 @@ func (ctx *Context) AddArtifact(file string) { file = strings.TrimPrefix(file, ctx.Config.Dist) file = strings.Replace(file, "/", "", -1) ctx.Artifacts = append(ctx.Artifacts, file) - log.WithField("artifact", file).Info("registered") + log.WithField("artifact", file).Info("new artifact") } // AddBinary adds a built binary to the current context @@ -54,7 +56,15 @@ func (ctx *Context) AddBinary(key, file string) { binariesLock.Lock() defer binariesLock.Unlock() ctx.Binaries[key] = file - log.WithField("key", key).WithField("binary", file).Info("added") + log.WithField("key", key).WithField("binary", file).Info("new binary") +} + +// AddFolder adds a built binary to the current context +func (ctx *Context) AddFolder(key, folder string) { + foldersLock.Lock() + defer foldersLock.Unlock() + ctx.Folders[key] = folder + log.WithField("key", key).WithField("folder", folder).Info("new folder") } // New context @@ -62,6 +72,7 @@ func New(config config.Project) *Context { return &Context{ Context: ctx.Background(), Config: config, - Archives: map[string]string{}, + Binaries: map[string]string{}, + Folders: map[string]string{}, } } diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index ec7a67a6d..5d672b04f 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -27,19 +27,18 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { var g errgroup.Group - // TODO: fix here - for platform, archive := range ctx.Binaries { - archive := archive + for platform, folder := range ctx.Folders { + folder := folder platform := platform g.Go(func() error { - return create(ctx, platform, archive) + return create(ctx, platform, folder) }) } return g.Wait() } func create(ctx *context.Context, platform, name string) error { - var folder = filepath.Join(ctx.Config.Dist, ctx.Config.Name) + var folder = filepath.Join(ctx.Config.Dist, name) var format = formatFor(ctx, platform) file, err := os.Create(folder + "." + format) if err != nil { diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 65ad02627..a9294a5c7 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -73,12 +73,13 @@ func doBuild(ctx *context.Context, build config.Build, target buildTarget) error if err != nil { return err } + ctx.AddFolder(target.String(), folder) var binary = filepath.Join( ctx.Config.Dist, folder, build.Binary+ext.For(target.goos), ) - ctx.AddArchive(build.Binary+target.String(), binary) + ctx.AddBinary(build.Binary+target.String(), binary) log.WithField("binary", binary).Info("building") cmd := []string{"go", "build"} if build.Flags != "" { From 40fc46da925611473248595afeb952f739cf4403 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 12:58:48 -0300 Subject: [PATCH 11/28] fixes to archiving and fpm --- pipeline/archive/archive.go | 13 +++++---- pipeline/fpm/fpm.go | 54 +++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index 5d672b04f..ae88ecf36 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -4,6 +4,7 @@ package archive import ( + "io/ioutil" "os" "path/filepath" "strings" @@ -11,7 +12,6 @@ import ( "github.com/apex/log" "github.com/goreleaser/archive" "github.com/goreleaser/goreleaser/context" - "github.com/goreleaser/goreleaser/internal/ext" "github.com/mattn/go-zglob" "golang.org/x/sync/errgroup" ) @@ -58,10 +58,13 @@ func create(ctx *context.Context, platform, name string) error { return err } } - for _, build := range ctx.Config.Builds { - var binary = build.Binary + ext.For(platform) - var bin = filepath.Join(ctx.Config.Dist, name) - if err := archive.Add(binary, filepath.Join(bin, binary)); err != nil { + var path = filepath.Join(ctx.Config.Dist, name) + binaries, err := ioutil.ReadDir(path) + if err != nil { + return err + } + for _, binary := range binaries { + if err := archive.Add(binary.Name(), filepath.Join(path, binary.Name())); err != nil { return err } } diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 7745b2b8e..25a1dfc7d 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -4,19 +4,16 @@ package fpm import ( "errors" "fmt" + "io/ioutil" "os/exec" "path/filepath" + "strings" "github.com/apex/log" "github.com/goreleaser/goreleaser/context" "golang.org/x/sync/errgroup" ) -var goarchToUnix = map[string]string{ - "386": "i386", - "amd64": "x86_64", -} - // ErrNoFPM is shown when fpm cannot be found in $PATH var ErrNoFPM = errors.New("fpm not present in $PATH") @@ -41,30 +38,29 @@ func (Pipe) Run(ctx *context.Context) error { var g errgroup.Group for _, format := range ctx.Config.FPM.Formats { - for _, build := range ctx.Config.Builds { - for _, goarch := range build.Goarch { - var key = build.Binary + "linux" + goarch - // TODO: fix here - if ctx.Binaries[key] == "" { - continue - } - format := format - // TODO: fix here - archive := ctx.Binaries[key] - arch := goarchToUnix[goarch] - g.Go(func() error { - return create(ctx, format, archive, arch) - }) - } + for key, folder := range ctx.Folders { + folder := folder + format := format + arch := archFor(key) + g.Go(func() error { + return create(ctx, format, folder, arch) + }) } } return g.Wait() } -func create(ctx *context.Context, format, archive, arch string) error { - var path = filepath.Join(ctx.Config.Dist, archive) +func archFor(key string) string { + if strings.Contains(key, "386") { + return "i386" + } + return "x86_64" +} + +func create(ctx *context.Context, format, folder, arch string) error { + var path = filepath.Join(ctx.Config.Dist, folder) var file = path + "." + format - log.WithField("file", file).Info("Creating") + log.WithField("file", file).Info("creating fpm archive") var options = []string{ "--input-type", "dir", @@ -99,13 +95,19 @@ func create(ctx *context.Context, format, archive, arch string) error { options = append(options, "--conflicts", conflict) } - for _, build := range ctx.Config.Builds { + files, err := ioutil.ReadDir(path) + if err != nil { + return err + } + for _, file := range files { + // XXX: skip non executable files here? // This basically tells fpm to put the binary in the /usr/local/bin // binary=/usr/local/bin/binary + log.WithField("file", file.Name()).Debug("passed binary to fpm") options = append(options, fmt.Sprintf( "%s=%s", - build.Binary, - filepath.Join("/usr/local/bin", build.Binary), + file.Name(), + filepath.Join("/usr/local/bin", file.Name()), )) } From 736d155884d73297b05f2571175961bcf3bf796e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 13:02:41 -0300 Subject: [PATCH 12/28] fpm only for linux builds --- pipeline/fpm/fpm.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 25a1dfc7d..6dc1d652e 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -39,6 +39,10 @@ func (Pipe) Run(ctx *context.Context) error { var g errgroup.Group for _, format := range ctx.Config.FPM.Formats { for key, folder := range ctx.Folders { + if !strings.Contains(key, "linux") { + log.WithField("key", key).Debug("skipped non-linux builds for fpm") + continue + } folder := folder format := format arch := archFor(key) From bc7eba4860e750ccc0197e46a90eeee851809153 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 13:13:44 -0300 Subject: [PATCH 13/28] archive tests fixed --- pipeline/archive/archive_test.go | 61 +++++++------------------------- 1 file changed, 12 insertions(+), 49 deletions(-) diff --git a/pipeline/archive/archive_test.go b/pipeline/archive/archive_test.go index fc4797ab2..38e2a5d95 100644 --- a/pipeline/archive/archive_test.go +++ b/pipeline/archive/archive_test.go @@ -27,23 +27,21 @@ func TestRunPipe(t *testing.T) { }() var dist = filepath.Join(folder, "dist") assert.NoError(os.Mkdir(dist, 0755)) - assert.NoError(os.Mkdir(filepath.Join(dist, "mybin"), 0755)) - _, err = os.Create(filepath.Join(dist, "mybin", "mybin")) + assert.NoError(os.Mkdir(filepath.Join(dist, "mybin_darwin_amd64"), 0755)) + assert.NoError(os.Mkdir(filepath.Join(dist, "mybin_windows_amd64"), 0755)) + _, err = os.Create(filepath.Join(dist, "mybin_darwin_amd64", "mybin")) assert.NoError(err) - _, err = os.Create(filepath.Join(dist, "mybin", "mybin.exe")) + _, err = os.Create(filepath.Join(dist, "mybin_windows_amd64", "mybin.exe")) assert.NoError(err) _, err = os.Create(filepath.Join(folder, "README.md")) assert.NoError(err) var ctx = &context.Context{ - Archives: map[string]string{ - "darwinamd64": "mybin", - "windowsamd64": "mybin", + Folders: map[string]string{ + "darwinamd64": "mybin_darwin_amd64", + "windowsamd64": "mybin_windows_amd64", }, Config: config.Project{ Dist: dist, - Build: config.Build{ - Binary: "mybin", - }, Archive: config.Archive{ Files: []string{ "README.*", @@ -68,9 +66,8 @@ func TestRunPipe(t *testing.T) { func TestRunPipeDistRemoved(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ - Archives: map[string]string{ - "darwinamd64": "mybin", - "windowsamd64": "mybin", + Folders: map[string]string{ + "darwinamd64": "whatever", }, Config: config.Project{ Dist: "/path/nope", @@ -82,30 +79,11 @@ func TestRunPipeDistRemoved(t *testing.T) { assert.Error(Pipe{}.Run(ctx)) } -func TestFormatFor(t *testing.T) { - var assert = assert.New(t) - var ctx = &context.Context{ - Config: config.Project{ - Archive: config.Archive{ - Format: "tar.gz", - FormatOverrides: []config.FormatOverride{ - { - Goos: "windows", - Format: "zip", - }, - }, - }, - }, - } - assert.Equal("zip", formatFor(ctx, "windowsamd64")) - assert.Equal("tar.gz", formatFor(ctx, "linux386")) -} - func TestRunPipeInvalidGlob(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ - Archives: map[string]string{ - "windowsamd64": "mybin", + Folders: map[string]string{ + "windowsamd64": "whatever", }, Config: config.Project{ Dist: "/tmp", @@ -132,7 +110,7 @@ func TestRunPipeGlobFailsToAdd(t *testing.T) { assert.NoError(os.MkdirAll(filepath.Join(folder, "folder", "another"), 0755)) var ctx = &context.Context{ - Archives: map[string]string{ + Folders: map[string]string{ "windows386": "mybin", }, Config: config.Project{ @@ -146,18 +124,3 @@ func TestRunPipeGlobFailsToAdd(t *testing.T) { } assert.Error(Pipe{}.Run(ctx)) } - -func TestRunPipeBinaryDontExist(t *testing.T) { - var assert = assert.New(t) - folder, err := ioutil.TempDir("", "archivetest") - assert.NoError(err) - var ctx = &context.Context{ - Archives: map[string]string{ - "windows386": "mybin", - }, - Config: config.Project{ - Dist: folder, - }, - } - assert.Error(Pipe{}.Run(ctx)) -} From a9d6f0ab8c20e0777322547138baac80a67cfaed Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 13:21:07 -0300 Subject: [PATCH 14/28] fixed build tests --- pipeline/build/build_test.go | 180 +++++++++++++++++++-------------- pipeline/build/ldflags_test.go | 14 +-- pipeline/build/target_test.go | 55 +++++----- 3 files changed, 135 insertions(+), 114 deletions(-) diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index 3053e4a4b..cf48c0fb6 100644 --- a/pipeline/build/build_test.go +++ b/pipeline/build/build_test.go @@ -29,16 +29,20 @@ func TestRunInvalidCommand(t *testing.T) { func TestBuild(t *testing.T) { assert := assert.New(t) var config = config.Project{ - Build: config.Build{ - Binary: "testing", - Flags: "-n", - Env: []string{"BLAH=1"}, + Builds: []config.Build{ + { + Binary: "testing", + Flags: "-n", + Env: []string{"BLAH=1"}, + }, }, } var ctx = &context.Context{ - Config: config, + Config: config, + Folders: map[string]string{}, + Binaries: map[string]string{}, } - assert.NoError(build(ctx, "build_test", runtimeTarget)) + assert.NoError(doBuild(ctx, ctx.Config.Builds[0], runtimeTarget)) } func TestRunFullPipe(t *testing.T) { @@ -50,25 +54,28 @@ func TestRunFullPipe(t *testing.T) { var post = filepath.Join(folder, "post") var config = config.Project{ Dist: folder, - Build: config.Build{ - Binary: "testing", - Flags: "-v", - Ldflags: "-X main.test=testing", - Hooks: config.Hooks{ - Pre: "touch " + pre, - Post: "touch " + post, - }, - Goos: []string{ - runtime.GOOS, - }, - Goarch: []string{ - runtime.GOARCH, + Builds: []config.Build{ + { + Binary: "testing", + Flags: "-v", + Ldflags: "-X main.test=testing", + Hooks: config.Hooks{ + Pre: "touch " + pre, + Post: "touch " + post, + }, + Goos: []string{ + runtime.GOOS, + }, + Goarch: []string{ + runtime.GOARCH, + }, }, }, } var ctx = &context.Context{ Config: config, - Archives: map[string]string{}, + Folders: map[string]string{}, + Binaries: map[string]string{}, } assert.NoError(Pipe{}.Run(ctx)) assert.True(exists(binary), binary) @@ -83,25 +90,28 @@ func TestRunPipeArmBuilds(t *testing.T) { var binary = filepath.Join(folder, "armtesting") var config = config.Project{ Dist: folder, - Build: config.Build{ - Binary: "armtesting", - Flags: "-v", - Ldflags: "-X main.test=armtesting", - Goos: []string{ - "linux", - }, - Goarch: []string{ - "arm", - "arm64", - }, - Goarm: []string{ - "6", + Builds: []config.Build{ + { + Binary: "armtesting", + Flags: "-v", + Ldflags: "-X main.test=armtesting", + Goos: []string{ + "linux", + }, + Goarch: []string{ + "arm", + "arm64", + }, + Goarm: []string{ + "6", + }, }, }, } var ctx = &context.Context{ Config: config, - Archives: map[string]string{}, + Folders: map[string]string{}, + Binaries: map[string]string{}, } assert.NoError(Pipe{}.Run(ctx)) assert.True(exists(binary), binary) @@ -110,19 +120,22 @@ func TestRunPipeArmBuilds(t *testing.T) { func TestBuildFailed(t *testing.T) { assert := assert.New(t) var config = config.Project{ - Build: config.Build{ - Flags: "-flag-that-dont-exists-to-force-failure", - Goos: []string{ - runtime.GOOS, - }, - Goarch: []string{ - runtime.GOARCH, + Builds: []config.Build{ + { + Flags: "-flag-that-dont-exists-to-force-failure", + Goos: []string{ + runtime.GOOS, + }, + Goarch: []string{ + runtime.GOARCH, + }, }, }, } var ctx = &context.Context{ Config: config, - Archives: map[string]string{}, + Folders: map[string]string{}, + Binaries: map[string]string{}, } assert.Error(Pipe{}.Run(ctx)) } @@ -130,19 +143,22 @@ func TestBuildFailed(t *testing.T) { func TestRunPipeWithInvalidOS(t *testing.T) { assert := assert.New(t) var config = config.Project{ - Build: config.Build{ - Flags: "-v", - Goos: []string{ - "windows", - }, - Goarch: []string{ - "arm", + Builds: []config.Build{ + { + Flags: "-v", + Goos: []string{ + "windows", + }, + Goarch: []string{ + "arm", + }, }, }, } var ctx = &context.Context{ Config: config, - Archives: map[string]string{}, + Folders: map[string]string{}, + Binaries: map[string]string{}, } assert.NoError(Pipe{}.Run(ctx)) } @@ -151,14 +167,16 @@ func TestRunInvalidNametemplate(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ Config: config.Project{ - Build: config.Build{ - Binary: "nametest", - Flags: "-v", - Goos: []string{ - runtime.GOOS, - }, - Goarch: []string{ - runtime.GOARCH, + Builds: []config.Build{ + { + Binary: "nametest", + Flags: "-v", + Goos: []string{ + runtime.GOOS, + }, + Goarch: []string{ + runtime.GOARCH, + }, }, }, Archive: config.Archive{ @@ -172,17 +190,20 @@ func TestRunInvalidNametemplate(t *testing.T) { func TestRunInvalidLdflags(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ - Archives: map[string]string{}, + Folders: map[string]string{}, + Binaries: map[string]string{}, Config: config.Project{ - Build: config.Build{ - Binary: "nametest", - Flags: "-v", - Ldflags: "-s -w -X main.version={{.Version}", - Goos: []string{ - runtime.GOOS, - }, - Goarch: []string{ - runtime.GOARCH, + Builds: []config.Build{ + { + Binary: "nametest", + Flags: "-v", + Ldflags: "-s -w -X main.version={{.Version}", + Goos: []string{ + runtime.GOOS, + }, + Goarch: []string{ + runtime.GOARCH, + }, }, }, }, @@ -193,26 +214,29 @@ func TestRunInvalidLdflags(t *testing.T) { func TestRunPipeFailingHooks(t *testing.T) { assert := assert.New(t) var config = config.Project{ - Build: config.Build{ - Hooks: config.Hooks{}, - Goos: []string{ - runtime.GOOS, - }, - Goarch: []string{ - runtime.GOARCH, + Builds: []config.Build{ + { + Hooks: config.Hooks{}, + Goos: []string{ + runtime.GOOS, + }, + Goarch: []string{ + runtime.GOARCH, + }, }, }, } var ctx = &context.Context{ Config: config, - Archives: map[string]string{}, + Folders: map[string]string{}, + Binaries: map[string]string{}, } t.Run("pre-hook", func(t *testing.T) { - ctx.Config.Build.Hooks.Pre = "exit 1" + ctx.Config.Builds[0].Hooks.Pre = "exit 1" assert.Error(Pipe{}.Run(ctx)) }) t.Run("post-hook", func(t *testing.T) { - ctx.Config.Build.Hooks.Post = "exit 1" + ctx.Config.Builds[0].Hooks.Post = "exit 1" assert.Error(Pipe{}.Run(ctx)) }) } diff --git a/pipeline/build/ldflags_test.go b/pipeline/build/ldflags_test.go index 4db97cc7a..ef01039ec 100644 --- a/pipeline/build/ldflags_test.go +++ b/pipeline/build/ldflags_test.go @@ -11,8 +11,10 @@ import ( func TestLdFlagsFullTemplate(t *testing.T) { assert := assert.New(t) var config = config.Project{ - Build: config.Build{ - Ldflags: "-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}}", + Builds: []config.Build{ + { + Ldflags: "-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}}", + }, }, } var ctx = &context.Context{ @@ -23,7 +25,7 @@ func TestLdFlagsFullTemplate(t *testing.T) { Version: "1.2.3", Config: config, } - flags, err := ldflags(ctx) + flags, err := ldflags(ctx, ctx.Config.Builds[0]) assert.NoError(err) assert.Contains(flags, "-s -w") assert.Contains(flags, "-X main.version=1.2.3") @@ -35,14 +37,14 @@ func TestLdFlagsFullTemplate(t *testing.T) { func TestInvalidTemplate(t *testing.T) { assert := assert.New(t) var config = config.Project{ - Build: config.Build{ - Ldflags: "{invalid{.Template}}}{{}}}", + Builds: []config.Build{ + {Ldflags: "{invalid{.Template}}}{{}}}"}, }, } var ctx = &context.Context{ Config: config, } - flags, err := ldflags(ctx) + flags, err := ldflags(ctx, ctx.Config.Builds[0]) assert.Error(err) assert.Equal(flags, "") } diff --git a/pipeline/build/target_test.go b/pipeline/build/target_test.go index cd6b45158..242f91579 100644 --- a/pipeline/build/target_test.go +++ b/pipeline/build/target_test.go @@ -5,40 +5,35 @@ import ( "testing" "github.com/goreleaser/goreleaser/config" - "github.com/goreleaser/goreleaser/context" "github.com/stretchr/testify/assert" ) func TestAllBuildTargets(t *testing.T) { var assert = assert.New(t) - var ctx = &context.Context{ - Config: config.Project{ - Build: config.Build{ - Goos: []string{ - "linux", - "darwin", - "freebsd", - }, - Goarch: []string{ - "386", - "amd64", - "arm", - "arm64", - }, - Goarm: []string{ - "6", - "7", - }, - Ignore: []config.IgnoredBuild{ - { - Goos: "darwin", - Goarch: "386", - }, { - Goos: "linux", - Goarch: "arm", - Goarm: "7", - }, - }, + var build = config.Build{ + Goos: []string{ + "linux", + "darwin", + "freebsd", + }, + Goarch: []string{ + "386", + "amd64", + "arm", + "arm64", + }, + Goarm: []string{ + "6", + "7", + }, + Ignore: []config.IgnoredBuild{ + { + Goos: "darwin", + Goarch: "386", + }, { + Goos: "linux", + Goarch: "arm", + Goarm: "7", }, }, } @@ -52,7 +47,7 @@ func TestAllBuildTargets(t *testing.T) { {"freebsd", "amd64", ""}, {"freebsd", "arm", "6"}, {"freebsd", "arm", "7"}, - }, buildTargets(ctx)) + }, buildTargets(build)) } func TestValidGoosGoarchCombos(t *testing.T) { From dd56049aba47d73da058fd7163326b0374bbf051 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 13:27:01 -0300 Subject: [PATCH 15/28] fixed fpm tests --- pipeline/fpm/fpm_test.go | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/pipeline/fpm/fpm_test.go b/pipeline/fpm/fpm_test.go index 49f0ca59e..731d88f19 100644 --- a/pipeline/fpm/fpm_test.go +++ b/pipeline/fpm/fpm_test.go @@ -33,18 +33,14 @@ func TestRunPipe(t *testing.T) { _, err = os.Create(filepath.Join(dist, "mybin", "mybin")) assert.NoError(err) var ctx = &context.Context{ - Archives: map[string]string{ - "linuxamd64": "mybin", + Folders: map[string]string{ + "linuxamd64": "mybin", + "linux386": "mybin", + "darwinamd64": "anotherbin", }, Config: config.Project{ + Name: "mybin", Dist: dist, - Build: config.Build{ - Goarch: []string{ - "amd64", - "i386", - }, - Binary: "mybin", - }, FPM: config.FPM{ Formats: []string{"deb"}, Dependencies: []string{"make"}, @@ -85,18 +81,11 @@ func TestCreateFileDoesntExist(t *testing.T) { assert.NoError(os.Mkdir(dist, 0755)) assert.NoError(os.Mkdir(filepath.Join(dist, "mybin"), 0755)) var ctx = &context.Context{ - Archives: map[string]string{ + Folders: map[string]string{ "linuxamd64": "mybin", }, Config: config.Project{ Dist: dist, - Build: config.Build{ - Goarch: []string{ - "amd64", - "i386", - }, - Binary: "mybin", - }, FPM: config.FPM{ Formats: []string{"deb"}, }, From 0359e02b8d8a424c424881a8cb5acfd7b4d3394b Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 20:59:16 -0300 Subject: [PATCH 16/28] brew fixes --- pipeline/brew/brew.go | 22 ++++++---------------- pipeline/brew/brew_test.go | 6 ++---- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 5aa6824b4..d7e916473 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -23,7 +23,7 @@ var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build") const formula = `class {{ .Name }} < Formula desc "{{ .Desc }}" homepage "{{ .Homepage }}" - url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}" + url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}" version "{{ .Version }}" sha256 "{{ .SHA256 }}" @@ -71,7 +71,6 @@ type templateData struct { Version string Caveats string File string - Format string SHA256 string Plist string Install []string @@ -105,7 +104,7 @@ func doRun(ctx *context.Context, client client.Client) error { log.Warn("skipped because release is marked as draft") return nil } - path := filepath.Join(ctx.Config.Brew.Folder, ctx.Config.Name+".rb") + var path = filepath.Join(ctx.Config.Brew.Folder, ctx.Config.Name+".rb") log.WithField("formula", path). WithField("repo", ctx.Config.Brew.GitHub.String()). Info("pushing") @@ -134,21 +133,13 @@ func doBuildFormula(data templateData) (bytes.Buffer, error) { return out, err } -func hasDarwinBuilds(ctx *context.Context) bool { - // TODO: fix here - for key := range ctx.Binaries { - if strings.HasSuffix(key, "darwinamd64") { - return true - } - } - return false -} - func dataFor(ctx *context.Context, client client.Client) (result templateData, err error) { - if !hasDarwinBuilds(ctx) { + var folder = ctx.Folders["darwinamd64"] + if folder == "" { return result, ErrNoDarwin64Build } - var file = ctx.Config.Name + "." + ctx.Config.Archive.Format + // TODO this can be broken by format_overrides + var file = folder + "." + ctx.Config.Archive.Format sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file)) if err != nil { return @@ -162,7 +153,6 @@ func dataFor(ctx *context.Context, client client.Client) (result templateData, e Version: ctx.Version, Caveats: ctx.Config.Brew.Caveats, File: file, - Format: ctx.Config.Archive.Format, // TODO this can be broken by format_overrides SHA256: sum, Dependencies: ctx.Config.Brew.Dependencies, Conflicts: ctx.Config.Brew.Conflicts, diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index 2da710082..389c16f6f 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -29,7 +29,6 @@ func TestSimpleName(t *testing.T) { } var defaultTemplateData = templateData{ - Binary: "test", Desc: "Some desc", Homepage: "https://google.com", Name: "Test", @@ -39,9 +38,8 @@ var defaultTemplateData = templateData{ }, Tag: "v0.1.3", Version: "0.1.3", - File: "test_Darwin_x86_64", + File: "test_Darwin_x86_64.tar.gz", SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68", - Format: "tar.gz", } func assertDefaultTemplateData(t *testing.T, formulae string) { @@ -105,7 +103,7 @@ func TestRunPipe(t *testing.T) { }, }, }, - Archives: map[string]string{ + Folders: map[string]string{ "darwinamd64": "bin", }, Publish: true, From 4fa83104d8c4cbc06cad163af7c19f53aaa9b305 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:13:02 -0300 Subject: [PATCH 17/28] defaults tests --- pipeline/defaults/defaults.go | 55 ++++++++++++++----------- pipeline/defaults/defaults_test.go | 24 ++++++----- pipeline/defaults/remote_test.go | 64 ++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 35 deletions(-) diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index 089c318d9..1ff64d426 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -41,6 +41,7 @@ func (Pipe) Run(ctx *context.Context) error { if ctx.Config.Brew.Install == "" { var installs []string for _, build := range ctx.Config.Builds { + // TODO: check for OSX builds only installs = append( installs, fmt.Sprintf(`bin.install "%s"`, build.Binary), @@ -66,31 +67,37 @@ func setReleaseDefaults(ctx *context.Context) error { } func setBuildDefaults(ctx *context.Context) { - var builds []config.Build - log.WithField("builds", ctx.Config.Builds).Debug("builds cleaned") - for _, build := range ctx.Config.Builds { - if build.Binary == "" { - build.Binary = ctx.Config.Release.GitHub.Name - } - if build.Main == "" { - build.Main = "." - } - if len(build.Goos) == 0 { - build.Goos = []string{"linux", "darwin"} - } - if len(build.Goarch) == 0 { - build.Goarch = []string{"amd64", "386"} - } - if len(build.Goarm) == 0 { - build.Goarm = []string{"6"} - } - if build.Ldflags == "" { - build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" - } - builds = append(builds, build) + for i, build := range ctx.Config.Builds { + ctx.Config.Builds[i] = buildWithDefaults(ctx, build) } - ctx.Config.Builds = builds - log.WithField("builds", ctx.Config.Builds).Debug("set") + if len(ctx.Config.Builds) == 0 { + ctx.Config.Builds = []config.Build{ + buildWithDefaults(ctx, config.Build{}), + } + } + log.WithField("builds", ctx.Config.Builds).Info("set") +} + +func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { + if build.Binary == "" { + build.Binary = ctx.Config.Release.GitHub.Name + } + if build.Main == "" { + build.Main = "." + } + if len(build.Goos) == 0 { + build.Goos = []string{"linux", "darwin"} + } + if len(build.Goarch) == 0 { + build.Goarch = []string{"amd64", "386"} + } + if len(build.Goarm) == 0 { + build.Goarm = []string{"6"} + } + if build.Ldflags == "" { + build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" + } + return build } func setArchiveDefaults(ctx *context.Context) error { diff --git a/pipeline/defaults/defaults_test.go b/pipeline/defaults/defaults_test.go index bdd9051a9..c40f73c08 100644 --- a/pipeline/defaults/defaults_test.go +++ b/pipeline/defaults/defaults_test.go @@ -15,33 +15,32 @@ func TestDescription(t *testing.T) { } func TestFillBasicData(t *testing.T) { - assert := assert.New(t) - + var assert = assert.New(t) var ctx = &context.Context{ Config: config.Project{}, } assert.NoError(Pipe{}.Run(ctx)) - assert.Equal("goreleaser", ctx.Config.Release.GitHub.Owner) assert.Equal("goreleaser", ctx.Config.Release.GitHub.Name) - assert.Equal("goreleaser", ctx.Config.Build.Binary) - assert.Equal(".", ctx.Config.Build.Main) + assert.NotEmpty(ctx.Config.Builds) + assert.Equal("goreleaser", ctx.Config.Builds[0].Binary) + assert.Equal(".", ctx.Config.Builds[0].Main) + assert.Contains(ctx.Config.Builds[0].Goos, "darwin") + assert.Contains(ctx.Config.Builds[0].Goos, "linux") + assert.Contains(ctx.Config.Builds[0].Goarch, "386") + assert.Contains(ctx.Config.Builds[0].Goarch, "amd64") assert.Equal("tar.gz", ctx.Config.Archive.Format) - assert.Contains(ctx.Config.Build.Goos, "darwin") - assert.Contains(ctx.Config.Build.Goos, "linux") - assert.Contains(ctx.Config.Build.Goarch, "386") - assert.Contains(ctx.Config.Build.Goarch, "amd64") assert.Contains(ctx.Config.Brew.Install, "bin.install \"goreleaser\"") assert.NotEmpty( ctx.Config.Archive.NameTemplate, - ctx.Config.Build.Ldflags, + ctx.Config.Builds[0].Ldflags, ctx.Config.Archive.Files, ) } func TestFillPartial(t *testing.T) { - assert := assert.New(t) + var assert = assert.New(t) var ctx = &context.Context{ Config: config.Project{ @@ -56,6 +55,9 @@ func TestFillPartial(t *testing.T) { "glob/*", }, }, + Builds: []config.Build{ + {Binary: "testreleaser"}, + }, }, } assert.NoError(Pipe{}.Run(ctx)) diff --git a/pipeline/defaults/remote_test.go b/pipeline/defaults/remote_test.go index 2469d7024..23500dff0 100644 --- a/pipeline/defaults/remote_test.go +++ b/pipeline/defaults/remote_test.go @@ -1,8 +1,10 @@ package defaults import ( + "reflect" "testing" + "github.com/goreleaser/goreleaser/config" "github.com/stretchr/testify/assert" ) @@ -24,3 +26,65 @@ func TestExtractReporFromHttpsURL(t *testing.T) { repo := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git") assert.Equal("goreleaser/goreleaser", repo.String()) } + +func Test_remoteRepo(t *testing.T) { + tests := []struct { + name string + wantResult config.Repo + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotResult, err := remoteRepo() + if (err != nil) != tt.wantErr { + t.Errorf("remoteRepo() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotResult, tt.wantResult) { + t.Errorf("remoteRepo() = %v, want %v", gotResult, tt.wantResult) + } + }) + } +} + +func Test_extractRepoFromURL(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want config.Repo + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := extractRepoFromURL(tt.args.s); !reflect.DeepEqual(got, tt.want) { + t.Errorf("extractRepoFromURL() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_toRepo(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want config.Repo + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := toRepo(tt.args.s); !reflect.DeepEqual(got, tt.want) { + t.Errorf("toRepo() = %v, want %v", got, tt.want) + } + }) + } +} From 19e1c8a3a32070b0be94112a2288f1d569015050 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:14:57 -0300 Subject: [PATCH 18/28] checksums tests --- pipeline/checksums/checksums_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index b050d9946..8aec3751b 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -25,9 +25,7 @@ func TestPipe(t *testing.T) { var ctx = &context.Context{ Config: config.Project{ Dist: folder, - Build: config.Build{ - Binary: binary, - }, + Name: binary, }, } ctx.AddArtifact(file) From 460f4af33194100d7b4e2a500d87a62bb6cc32d4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:17:25 -0300 Subject: [PATCH 19/28] readme --- README.md | 150 +++++++++++++++++++++++++++--------------------------- 1 file changed, 76 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 728306c4f..7f0f21533 100644 --- a/README.md +++ b/README.md @@ -61,14 +61,14 @@ By default GoReleaser will build the current directory, but you can change the b ```yml # goreleaser.yml # Build customization -build: - binary: drum-roll - goos: - - windows - - darwin - - linux - goarch: - - amd64 +builds: + - binary: drum-roll + goos: + - windows + - darwin + - linux + goarch: + - amd64 ``` PS: Invalid GOOS/GOARCH combinations will automatically be skipped. @@ -82,15 +82,15 @@ Another useful feature is to add files to archives, this is very useful for inte ```yml # goreleaser.yml # Build customization -build: - main: main.go - binary: drum-roll - goos: - - windows - - darwin - - linux - goarch: - - amd64 +builds: + - main: main.go + binary: drum-roll + goos: + - windows + - darwin + - linux + goarch: + - amd64 # Archive customization archive: format: tar.gz @@ -176,72 +176,74 @@ We'll cover all customizations available bellow: ```yml # goreleaser.yml -build: - # Path to main.go file or main package. - # Default is `.` - main: ./cmd/main.go +builds: + # You can have multiple builds, its a common yaml list + - + # Path to main.go file or main package. + # Default is `.` + main: ./cmd/main.go - # Name of the binary. - # Default is the name of the project directory. - binary: program + # Name of the binary. + # Default is the name of the project directory. + binary: program - # Custom build tags. - # Default is empty - flags: -tags dev + # Custom build tags. + # Default is empty + flags: -tags dev - # Custom ldflags template. - # This is parsed with Golang template engine and the following variables - # are available: - # - Date - # - Commit - # - Tag - # - Version (Tag with the `v` prefix stripped) - # The default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}` - # Date format is `2006-01-02_15:04:05` - ldflags: -s -w -X main.build={{.Version}} + # Custom ldflags template. + # This is parsed with Golang template engine and the following variables + # are available: + # - Date + # - Commit + # - Tag + # - Version (Tag with the `v` prefix stripped) + # The default is `-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}` + # Date format is `2006-01-02_15:04:05` + ldflags: -s -w -X main.build={{.Version}} - # Custom environment variables to be set durign the builds. - # Default is empty - env: - - CGO_ENABLED=0 + # Custom environment variables to be set durign the builds. + # Default is empty + env: + - CGO_ENABLED=0 - # GOOS list to build in. - # For more info refer to https://golang.org/doc/install/source#environment - # Defaults are darwin and linux - goos: - - freebsd - - windows + # GOOS list to build in. + # For more info refer to https://golang.org/doc/install/source#environment + # Defaults are darwin and linux + goos: + - freebsd + - windows - # GOARCH to build in. - # For more info refer to https://golang.org/doc/install/source#environment - # Defaults are 386 and amd64 - goarch: - - amd64 - - arm - - arm64 + # GOARCH to build in. + # For more info refer to https://golang.org/doc/install/source#environment + # Defaults are 386 and amd64 + goarch: + - amd64 + - arm + - arm64 - # GOARM to build in when GOARCH is arm. - # For more info refer to https://golang.org/doc/install/source#environment - # Defaults are 6 - goarm: - - 6 - - 7 + # GOARM to build in when GOARCH is arm. + # For more info refer to https://golang.org/doc/install/source#environment + # Defaults are 6 + goarm: + - 6 + - 7 - # List of combinations of GOOS + GOARCH + GOARM to ignore. - # Default is empty. - ignore: - - goos: darwin - goarch: 386 - - goos: linux - goarch: arm - goarm: 7 + # List of combinations of GOOS + GOARCH + GOARM to ignore. + # Default is empty. + ignore: + - goos: darwin + goarch: 386 + - goos: linux + goarch: arm + goarm: 7 - # Hooks can be used to customize the final binary, for example, to run - # generator or whatever you want. - # Default is both hooks empty. - hooks: - pre: rice embed-go - post: ./script.sh + # Hooks can be used to customize the final binary, for example, to run + # generator or whatever you want. + # Default is both hooks empty. + hooks: + pre: rice embed-go + post: ./script.sh ``` ### Archive customization From cc5f80b3a9b13010a04de55a86b01c7cfb3a8491 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:27:30 -0300 Subject: [PATCH 20/28] added backwards compat to single build --- config/config.go | 6 ++++-- pipeline/defaults/defaults.go | 2 +- pipeline/defaults/defaults_test.go | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 4323aa9a2..7040876af 100644 --- a/config/config.go +++ b/config/config.go @@ -108,7 +108,9 @@ type Project struct { FPM FPM `yaml:",omitempty"` Snapshot Snapshot `yaml:",omitempty"` - // Build Build `yaml:",omitempty"` // deprecated, remove + // this is a hack ¯\_(ツ)_/¯ + SingleBuild Build `yaml:"build,omitempty"` + // test only property indicating the path to the dist folder Dist string `yaml:"-"` } @@ -129,6 +131,6 @@ func LoadReader(fd io.Reader) (config Project, err error) { return config, err } err = yaml.Unmarshal(data, &config) - log.WithField("config", config).Debug("loaded") + log.WithField("config", config).Debug("loaded config file") return } diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index 1ff64d426..2e45865e5 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -72,7 +72,7 @@ func setBuildDefaults(ctx *context.Context) { } if len(ctx.Config.Builds) == 0 { ctx.Config.Builds = []config.Build{ - buildWithDefaults(ctx, config.Build{}), + buildWithDefaults(ctx, ctx.Config.SingleBuild), } } log.WithField("builds", ctx.Config.Builds).Info("set") diff --git a/pipeline/defaults/defaults_test.go b/pipeline/defaults/defaults_test.go index c40f73c08..b20fb3a12 100644 --- a/pipeline/defaults/defaults_test.go +++ b/pipeline/defaults/defaults_test.go @@ -64,6 +64,21 @@ func TestFillPartial(t *testing.T) { assert.Len(ctx.Config.Archive.Files, 1) } +func TestFillSingleBuild(t *testing.T) { + var assert = assert.New(t) + + var ctx = &context.Context{ + Config: config.Project{ + SingleBuild: config.Build{ + Main: "testreleaser", + }, + }, + } + assert.NoError(Pipe{}.Run(ctx)) + assert.Len(ctx.Config.Builds, 1) + assert.Equal(ctx.Config.Builds[0].Binary, "goreleaser") +} + func TestNotAGitRepo(t *testing.T) { var assert = assert.New(t) folder, err := ioutil.TempDir("", "goreleasertest") From 0410f5bc7270b3f4d792f4d25e9e60f7a39c03f2 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:31:55 -0300 Subject: [PATCH 21/28] cleaning up --- context/context.go | 17 +++-------------- pipeline/build/build.go | 1 - pipeline/build/build_test.go | 33 +++++++++++++-------------------- 3 files changed, 16 insertions(+), 35 deletions(-) diff --git a/context/context.go b/context/context.go index 7b93ba935..d36195401 100644 --- a/context/context.go +++ b/context/context.go @@ -27,7 +27,6 @@ type Context struct { Config config.Project Token string Git GitInfo - Binaries map[string]string Folders map[string]string Artifacts []string ReleaseNotes string @@ -38,7 +37,6 @@ type Context struct { } var artifactsLock sync.Mutex -var binariesLock sync.Mutex var foldersLock sync.Mutex // AddArtifact adds a file to upload list @@ -51,14 +49,6 @@ func (ctx *Context) AddArtifact(file string) { log.WithField("artifact", file).Info("new artifact") } -// AddBinary adds a built binary to the current context -func (ctx *Context) AddBinary(key, file string) { - binariesLock.Lock() - defer binariesLock.Unlock() - ctx.Binaries[key] = file - log.WithField("key", key).WithField("binary", file).Info("new binary") -} - // AddFolder adds a built binary to the current context func (ctx *Context) AddFolder(key, folder string) { foldersLock.Lock() @@ -70,9 +60,8 @@ func (ctx *Context) AddFolder(key, folder string) { // New context func New(config config.Project) *Context { return &Context{ - Context: ctx.Background(), - Config: config, - Binaries: map[string]string{}, - Folders: map[string]string{}, + Context: ctx.Background(), + Config: config, + Folders: map[string]string{}, } } diff --git a/pipeline/build/build.go b/pipeline/build/build.go index a9294a5c7..49a1e4874 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -79,7 +79,6 @@ func doBuild(ctx *context.Context, build config.Build, target buildTarget) error folder, build.Binary+ext.For(target.goos), ) - ctx.AddBinary(build.Binary+target.String(), binary) log.WithField("binary", binary).Info("building") cmd := []string{"go", "build"} if build.Flags != "" { diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index cf48c0fb6..e2f1938b9 100644 --- a/pipeline/build/build_test.go +++ b/pipeline/build/build_test.go @@ -38,9 +38,8 @@ func TestBuild(t *testing.T) { }, } var ctx = &context.Context{ - Config: config, - Folders: map[string]string{}, - Binaries: map[string]string{}, + Config: config, + Folders: map[string]string{}, } assert.NoError(doBuild(ctx, ctx.Config.Builds[0], runtimeTarget)) } @@ -73,9 +72,8 @@ func TestRunFullPipe(t *testing.T) { }, } var ctx = &context.Context{ - Config: config, - Folders: map[string]string{}, - Binaries: map[string]string{}, + Config: config, + Folders: map[string]string{}, } assert.NoError(Pipe{}.Run(ctx)) assert.True(exists(binary), binary) @@ -109,9 +107,8 @@ func TestRunPipeArmBuilds(t *testing.T) { }, } var ctx = &context.Context{ - Config: config, - Folders: map[string]string{}, - Binaries: map[string]string{}, + Config: config, + Folders: map[string]string{}, } assert.NoError(Pipe{}.Run(ctx)) assert.True(exists(binary), binary) @@ -133,9 +130,8 @@ func TestBuildFailed(t *testing.T) { }, } var ctx = &context.Context{ - Config: config, - Folders: map[string]string{}, - Binaries: map[string]string{}, + Config: config, + Folders: map[string]string{}, } assert.Error(Pipe{}.Run(ctx)) } @@ -156,9 +152,8 @@ func TestRunPipeWithInvalidOS(t *testing.T) { }, } var ctx = &context.Context{ - Config: config, - Folders: map[string]string{}, - Binaries: map[string]string{}, + Config: config, + Folders: map[string]string{}, } assert.NoError(Pipe{}.Run(ctx)) } @@ -190,8 +185,7 @@ func TestRunInvalidNametemplate(t *testing.T) { func TestRunInvalidLdflags(t *testing.T) { var assert = assert.New(t) var ctx = &context.Context{ - Folders: map[string]string{}, - Binaries: map[string]string{}, + Folders: map[string]string{}, Config: config.Project{ Builds: []config.Build{ { @@ -227,9 +221,8 @@ func TestRunPipeFailingHooks(t *testing.T) { }, } var ctx = &context.Context{ - Config: config, - Folders: map[string]string{}, - Binaries: map[string]string{}, + Config: config, + Folders: map[string]string{}, } t.Run("pre-hook", func(t *testing.T) { ctx.Config.Builds[0].Hooks.Pre = "exit 1" From 2becfecc8b6f4e74f5eb932289bd8c975e38c6fd Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:34:32 -0300 Subject: [PATCH 22/28] coverage increased --- context/context_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/context/context_test.go b/context/context_test.go index 617c81d8c..4a599a754 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -31,3 +31,27 @@ func TestMultipleArtifactAdds(t *testing.T) { assert.Len(ctx.Artifacts, len(list)) assert.Contains(ctx.Artifacts, "a", "b", "c", "d") } + +func TestMultipleFolderAdds(t *testing.T) { + var assert = assert.New(t) + var list = map[string]string{ + "key-a": "folder/a", + "key-b": "folder/b", + "key-c": "folder/c", + "key-d": "folder/d", + } + var ctx = New(config.Project{ + Dist: "dist", + }) + var g errgroup.Group + for k, f := range list { + f := f + k := k + g.Go(func() error { + ctx.AddFolder(k, f) + return nil + }) + } + assert.NoError(g.Wait()) + assert.Len(ctx.Folders, len(list)) +} From aaa1851427ea1c22ead702564c070dea36867429 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:38:37 -0300 Subject: [PATCH 23/28] increased fpm coverage --- pipeline/fpm/fpm_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pipeline/fpm/fpm_test.go b/pipeline/fpm/fpm_test.go index 731d88f19..a5b7d2d42 100644 --- a/pipeline/fpm/fpm_test.go +++ b/pipeline/fpm/fpm_test.go @@ -93,3 +93,12 @@ func TestCreateFileDoesntExist(t *testing.T) { } assert.Error(Pipe{}.Run(ctx)) } + +func TestCreatePathDoesntExist(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{} + assert.Contains( + create(ctx, "tar.gz", "/nope/noooo", "windowsarm64").Error(), + "no such file", + ) +} From b41b2f7bd8be53a0f5f8f2633d473bd3744c5293 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 21:52:04 -0300 Subject: [PATCH 24/28] cleaning up --- pipeline/defaults/defaults.go | 1 - pipeline/defaults/remote_test.go | 64 -------------------------------- 2 files changed, 65 deletions(-) diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index 2e45865e5..b0075ec56 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -75,7 +75,6 @@ func setBuildDefaults(ctx *context.Context) { buildWithDefaults(ctx, ctx.Config.SingleBuild), } } - log.WithField("builds", ctx.Config.Builds).Info("set") } func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { diff --git a/pipeline/defaults/remote_test.go b/pipeline/defaults/remote_test.go index 23500dff0..2469d7024 100644 --- a/pipeline/defaults/remote_test.go +++ b/pipeline/defaults/remote_test.go @@ -1,10 +1,8 @@ package defaults import ( - "reflect" "testing" - "github.com/goreleaser/goreleaser/config" "github.com/stretchr/testify/assert" ) @@ -26,65 +24,3 @@ func TestExtractReporFromHttpsURL(t *testing.T) { repo := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git") assert.Equal("goreleaser/goreleaser", repo.String()) } - -func Test_remoteRepo(t *testing.T) { - tests := []struct { - name string - wantResult config.Repo - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotResult, err := remoteRepo() - if (err != nil) != tt.wantErr { - t.Errorf("remoteRepo() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotResult, tt.wantResult) { - t.Errorf("remoteRepo() = %v, want %v", gotResult, tt.wantResult) - } - }) - } -} - -func Test_extractRepoFromURL(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want config.Repo - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := extractRepoFromURL(tt.args.s); !reflect.DeepEqual(got, tt.want) { - t.Errorf("extractRepoFromURL() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_toRepo(t *testing.T) { - type args struct { - s string - } - tests := []struct { - name string - args args - want config.Repo - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := toRepo(tt.args.s); !reflect.DeepEqual(got, tt.want) { - t.Errorf("toRepo() = %v, want %v", got, tt.want) - } - }) - } -} From b93061570c8d922780d749fa35f8d19ea8c760c7 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 22:06:40 -0300 Subject: [PATCH 25/28] fixing one todo --- pipeline/defaults/defaults.go | 22 +++++++++++++++++++++- pipeline/defaults/defaults_test.go | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index b0075ec56..ccfe7edd7 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -41,7 +41,9 @@ func (Pipe) Run(ctx *context.Context) error { if ctx.Config.Brew.Install == "" { var installs []string for _, build := range ctx.Config.Builds { - // TODO: check for OSX builds only + if !isBrewBuild(build) { + continue + } installs = append( installs, fmt.Sprintf(`bin.install "%s"`, build.Binary), @@ -54,6 +56,24 @@ func (Pipe) Run(ctx *context.Context) error { return err } +func isBrewBuild(build config.Build) bool { + for _, ignore := range build.Ignore { + if ignore.Goos == "darwin" && ignore.Goarch == "amd64" { + return false + } + } + return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64") +} + +func contains(ss []string, s string) bool { + for _, zs := range ss { + if zs == s { + return true + } + } + return false +} + func setReleaseDefaults(ctx *context.Context) error { if ctx.Config.Release.GitHub.Name != "" { return nil diff --git a/pipeline/defaults/defaults_test.go b/pipeline/defaults/defaults_test.go index b20fb3a12..9d4ff3b49 100644 --- a/pipeline/defaults/defaults_test.go +++ b/pipeline/defaults/defaults_test.go @@ -57,11 +57,19 @@ func TestFillPartial(t *testing.T) { }, Builds: []config.Build{ {Binary: "testreleaser"}, + {Goos: []string{"linux"}}, + { + Binary: "another", + Ignore: []config.IgnoredBuild{ + {Goos: "darwin", Goarch: "amd64"}, + }, + }, }, }, } assert.NoError(Pipe{}.Run(ctx)) assert.Len(ctx.Config.Archive.Files, 1) + assert.Equal(`bin.install "testreleaser"`, ctx.Config.Brew.Install) } func TestFillSingleBuild(t *testing.T) { From a3c1e2b7894c32922c176dcac8782d5cc167e744 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 22:11:50 -0300 Subject: [PATCH 26/28] fixed another todo --- internal/archiveformat/format.go | 19 ++++++++++++++++++ internal/archiveformat/format_test.go | 28 +++++++++++++++++++++++++++ pipeline/archive/archive.go | 13 ++----------- pipeline/brew/brew.go | 8 +++++--- 4 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 internal/archiveformat/format.go create mode 100644 internal/archiveformat/format_test.go diff --git a/internal/archiveformat/format.go b/internal/archiveformat/format.go new file mode 100644 index 000000000..ce87f215a --- /dev/null +++ b/internal/archiveformat/format.go @@ -0,0 +1,19 @@ +// Package archiveformat provides functions to get the format of given package +// based on the config +package archiveformat + +import ( + "strings" + + "github.com/goreleaser/goreleaser/context" +) + +// For return the archive format, considering overrides and all that +func For(ctx *context.Context, platform string) string { + for _, override := range ctx.Config.Archive.FormatOverrides { + if strings.HasPrefix(platform, override.Goos) { + return override.Format + } + } + return ctx.Config.Archive.Format +} diff --git a/internal/archiveformat/format_test.go b/internal/archiveformat/format_test.go new file mode 100644 index 000000000..76396648d --- /dev/null +++ b/internal/archiveformat/format_test.go @@ -0,0 +1,28 @@ +package archiveformat + +import ( + "testing" + + "github.com/goreleaser/goreleaser/config" + "github.com/goreleaser/goreleaser/context" + "github.com/stretchr/testify/assert" +) + +func TestFormatFor(t *testing.T) { + var assert = assert.New(t) + var ctx = &context.Context{ + Config: config.Project{ + Archive: config.Archive{ + Format: "tar.gz", + FormatOverrides: []config.FormatOverride{ + { + Goos: "windows", + Format: "zip", + }, + }, + }, + }, + } + assert.Equal("zip", For(ctx, "windowsamd64")) + assert.Equal("tar.gz", For(ctx, "linux386")) +} diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index ae88ecf36..1806538a6 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -7,11 +7,11 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "github.com/apex/log" "github.com/goreleaser/archive" "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/mattn/go-zglob" "golang.org/x/sync/errgroup" ) @@ -39,7 +39,7 @@ func (Pipe) Run(ctx *context.Context) error { func create(ctx *context.Context, platform, name string) error { var folder = filepath.Join(ctx.Config.Dist, name) - var format = formatFor(ctx, platform) + var format = archiveformat.For(ctx, platform) file, err := os.Create(folder + "." + format) if err != nil { return err @@ -82,12 +82,3 @@ func findFiles(ctx *context.Context) (result []string, err error) { } return } - -func formatFor(ctx *context.Context, platform string) string { - for _, override := range ctx.Config.Archive.FormatOverrides { - if strings.HasPrefix(platform, override.Goos) { - return override.Format - } - } - return ctx.Config.Archive.Format -} diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index d7e916473..3f3cc4a81 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -13,6 +13,7 @@ import ( "github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/goreleaser/goreleaser/internal/client" ) @@ -20,6 +21,8 @@ import ( // contain darwin and/or goarch doesn't contain amd64) var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build") +const platform = "darwinamd64" + const formula = `class {{ .Name }} < Formula desc "{{ .Desc }}" homepage "{{ .Homepage }}" @@ -134,12 +137,11 @@ func doBuildFormula(data templateData) (bytes.Buffer, error) { } func dataFor(ctx *context.Context, client client.Client) (result templateData, err error) { - var folder = ctx.Folders["darwinamd64"] + var folder = ctx.Folders[platform] if folder == "" { return result, ErrNoDarwin64Build } - // TODO this can be broken by format_overrides - var file = folder + "." + ctx.Config.Archive.Format + var file = folder + "." + archiveformat.For(ctx, platform) sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file)) if err != nil { return From 6158285994ad1e16cda3d918e38a18bb7b7d32c4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 1 Jul 2017 22:42:10 -0300 Subject: [PATCH 27/28] project name, templates, readme, etc --- README.md | 17 +++++++++++++---- config/config.go | 14 +++++++------- internal/client/github.go | 2 +- internal/name/name.go | 26 ++++++++++++++------------ internal/name/name_test.go | 6 +++--- pipeline/brew/brew.go | 4 ++-- pipeline/checksums/checksums.go | 2 +- pipeline/checksums/checksums_test.go | 4 ++-- pipeline/defaults/defaults.go | 4 ++-- pipeline/fpm/fpm.go | 2 +- pipeline/fpm/fpm_test.go | 4 ++-- 11 files changed, 48 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 7f0f21533..74bb1f4b6 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ PS: Invalid GOOS/GOARCH combinations will automatically be skipped. This configuration specifies the build operating systems to Windows, Linux and MacOS using 64bit architecture, the name of the binaries is `drum-roll`. -GoReleaser will then archive the result binaries of each Os/Arch into a separate file. The default format is `{{.Binary}}_{{.Os}}_{{.Arch}}`. +GoReleaser will then archive the result binaries of each Os/Arch into a separate file. The default format is `{{.ProjectName}}_{{.Os}}_{{.Arch}}`. You can change the archives name and format. You can also replace the OS and the Architecture with your own. Another useful feature is to add files to archives, this is very useful for integrating assets like resource files. @@ -172,6 +172,15 @@ defaults are sensible and fit for most projects. We'll cover all customizations available bellow: +### Project name + +```yml +# goreleaser.yml +# The name of the project. It is used in the name of the brew formula, archives, +# etc. Defaults to the name of the git project. +project_name: myproject +``` + ### Build customization ```yml @@ -254,14 +263,14 @@ archive: # You can change the name of the archive. # This is parsed with Golang template engine and the following variables # are available: - # - Binary + # - ProjectName # - Tag # - Version (Tag with the `v` prefix stripped) # - Os # - Arch # - Arm (ARM version) - # The default is `{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}` - name_template: "{{.Binary}}_{{.Version}}_{{.Os}}_{{.Arch}}" + # The default is `{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}` + name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" # Archive format. Valid options are `tar.gz` and `zip`. # Default is `tar.gz` diff --git a/config/config.go b/config/config.go index 7040876af..d709c42bd 100644 --- a/config/config.go +++ b/config/config.go @@ -100,13 +100,13 @@ type Snapshot struct { // Project includes all project configuration type Project struct { - Name string `yaml:",omitempty"` - Release Release `yaml:",omitempty"` - Brew Homebrew `yaml:",omitempty"` - Builds []Build `yaml:",omitempty"` - Archive Archive `yaml:",omitempty"` - FPM FPM `yaml:",omitempty"` - Snapshot Snapshot `yaml:",omitempty"` + ProjectName string `yaml:"project_name,omitempty"` + Release Release `yaml:",omitempty"` + Brew Homebrew `yaml:",omitempty"` + Builds []Build `yaml:",omitempty"` + Archive Archive `yaml:",omitempty"` + FPM FPM `yaml:",omitempty"` + Snapshot Snapshot `yaml:",omitempty"` // this is a hack ¯\_(ツ)_/¯ SingleBuild Build `yaml:"build,omitempty"` diff --git a/internal/client/github.go b/internal/client/github.go index 26cfab78a..630cff588 100644 --- a/internal/client/github.go +++ b/internal/client/github.go @@ -36,7 +36,7 @@ func (c *githubClient) CreateFile( }, Content: content.Bytes(), Message: github.String( - ctx.Config.Name + " version " + ctx.Git.CurrentTag, + ctx.Config.ProjectName + " version " + ctx.Git.CurrentTag, ), } diff --git a/internal/name/name.go b/internal/name/name.go index a3ac20a54..7bf006ee7 100644 --- a/internal/name/name.go +++ b/internal/name/name.go @@ -10,23 +10,25 @@ import ( ) type nameData struct { - Os string - Arch string - Arm string - Version string - Tag string - Binary string + Os string + Arch string + Arm string + Version string + Tag string + Binary string // deprecated + ProjectName string } // For returns the name for the given context, goos, goarch and goarm. func For(ctx *context.Context, goos, goarch, goarm string) (string, error) { var data = nameData{ - Os: replace(ctx.Config.Archive.Replacements, goos), - Arch: replace(ctx.Config.Archive.Replacements, goarch), - Arm: replace(ctx.Config.Archive.Replacements, goarm), - Version: ctx.Version, - Tag: ctx.Git.CurrentTag, - Binary: ctx.Config.Name, + Os: replace(ctx.Config.Archive.Replacements, goos), + Arch: replace(ctx.Config.Archive.Replacements, goarch), + Arm: replace(ctx.Config.Archive.Replacements, goarm), + Version: ctx.Version, + Tag: ctx.Git.CurrentTag, + Binary: ctx.Config.ProjectName, + ProjectName: ctx.Config.ProjectName, } var out bytes.Buffer diff --git a/internal/name/name_test.go b/internal/name/name_test.go index a4327adf1..80c5f7eff 100644 --- a/internal/name/name_test.go +++ b/internal/name/name_test.go @@ -20,7 +20,7 @@ func TestNameFor(t *testing.T) { "amd64": "x86_64", }, }, - Name: "test", + ProjectName: "test", } var ctx = &context.Context{ Config: config, @@ -42,7 +42,7 @@ func TestInvalidNameTemplate(t *testing.T) { Archive: config.Archive{ NameTemplate: "{{.Binary}_{{.Os}}_{{.Arch}}_{{.Version}}", }, - Name: "test", + ProjectName: "test", }, Git: context.GitInfo{ CurrentTag: "v1.2.3", @@ -60,7 +60,7 @@ func TestNameDefaltTemplate(t *testing.T) { Archive: config.Archive{ NameTemplate: defaults.NameTemplate, }, - Name: "test", + ProjectName: "test", }, Version: "1.2.3", } diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 3f3cc4a81..5aceddeb6 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -107,7 +107,7 @@ func doRun(ctx *context.Context, client client.Client) error { log.Warn("skipped because release is marked as draft") return nil } - var path = filepath.Join(ctx.Config.Brew.Folder, ctx.Config.Name+".rb") + var path = filepath.Join(ctx.Config.Brew.Folder, ctx.Config.ProjectName+".rb") log.WithField("formula", path). WithField("repo", ctx.Config.Brew.GitHub.String()). Info("pushing") @@ -147,7 +147,7 @@ func dataFor(ctx *context.Context, client client.Client) (result templateData, e return } return templateData{ - Name: formulaNameFor(ctx.Config.Name), + Name: formulaNameFor(ctx.Config.ProjectName), Desc: ctx.Config.Brew.Description, Homepage: ctx.Config.Brew.Homepage, Repo: ctx.Config.Release.GitHub, diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index 4c2060948..717b79a21 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -26,7 +26,7 @@ func (Pipe) Run(ctx *context.Context) (err error) { file, err := os.OpenFile( filepath.Join( ctx.Config.Dist, - fmt.Sprintf("%v_checksums.txt", ctx.Config.Name), + fmt.Sprintf("%v_checksums.txt", ctx.Config.ProjectName), ), os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644, diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index 8aec3751b..4cd8c5bd9 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -24,8 +24,8 @@ func TestPipe(t *testing.T) { assert.NoError(ioutil.WriteFile(file, []byte("some string"), 0644)) var ctx = &context.Context{ Config: config.Project{ - Dist: folder, - Name: binary, + Dist: folder, + ProjectName: binary, }, } ctx.AddArtifact(file) diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index ccfe7edd7..0289b52e6 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -34,8 +34,8 @@ func (Pipe) Run(ctx *context.Context) error { if err := setReleaseDefaults(ctx); err != nil { return err } - if ctx.Config.Name == "" { - ctx.Config.Name = ctx.Config.Release.GitHub.Name + if ctx.Config.ProjectName == "" { + ctx.Config.ProjectName = ctx.Config.Release.GitHub.Name } setBuildDefaults(ctx) if ctx.Config.Brew.Install == "" { diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 6dc1d652e..0f8c8c1cf 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -69,7 +69,7 @@ func create(ctx *context.Context, format, folder, arch string) error { var options = []string{ "--input-type", "dir", "--output-type", format, - "--name", ctx.Config.Name, + "--name", ctx.Config.ProjectName, "--version", ctx.Version, "--architecture", arch, "--chdir", path, diff --git a/pipeline/fpm/fpm_test.go b/pipeline/fpm/fpm_test.go index a5b7d2d42..e376c6614 100644 --- a/pipeline/fpm/fpm_test.go +++ b/pipeline/fpm/fpm_test.go @@ -39,8 +39,8 @@ func TestRunPipe(t *testing.T) { "darwinamd64": "anotherbin", }, Config: config.Project{ - Name: "mybin", - Dist: dist, + ProjectName: "mybin", + Dist: dist, FPM: config.FPM{ Formats: []string{"deb"}, Dependencies: []string{"make"}, From 3b51b44e357393bd746b9af64ff4d11981586d9f Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 2 Jul 2017 12:27:16 -0300 Subject: [PATCH 28/28] fixed new tests --- pipeline/brew/brew_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index cd169e684..e9392f1c6 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -138,7 +138,7 @@ func TestRunPipeFormatOverride(t *testing.T) { }, }, }, - Archives: map[string]string{ + Folders: map[string]string{ "darwinamd64": "bin", }, Publish: true, @@ -166,7 +166,7 @@ func TestRunPipeArchiveDoesntExist(t *testing.T) { }, }, }, - Archives: map[string]string{ + Folders: map[string]string{ "darwinamd64": "bin", }, Publish: true,