From 906c8b08e3bd396b7f9ec0e7b9a5428cb57ed214 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sun, 17 Dec 2017 17:11:08 -0200 Subject: [PATCH] refactor: fixed fpm pipe --- internal/artifact/artifact.go | 4 +- .../archive => internal/nametemplate}/name.go | 4 +- pipeline/archive/archive.go | 4 +- pipeline/fpm/fpm.go | 55 ++++++++++++------- 4 files changed, 43 insertions(+), 24 deletions(-) rename {pipeline/archive => internal/nametemplate}/name.go (91%) diff --git a/internal/artifact/artifact.go b/internal/artifact/artifact.go index c9eadf0df..293150fd3 100644 --- a/internal/artifact/artifact.go +++ b/internal/artifact/artifact.go @@ -17,6 +17,8 @@ const ( UploadableBinary // Binary is a binary (output of a gobuild) Binary + // LinuxPackage is a linux package generated by fpm or snapcraft + LinuxPackage // DockerImage is a docker image DockerImage // Checksum is a checksum file @@ -58,7 +60,7 @@ func (a Artifact) Platform() string { } // GroupByPlatform groups the artifacts by their platform -func (artifacts *Artifacts) GroupByPlatform() map[string][]Artifact { +func (artifacts Artifacts) GroupByPlatform() map[string][]Artifact { var result = map[string][]Artifact{} for _, a := range artifacts.items { result[a.Platform()] = append(result[a.Platform()], a) diff --git a/pipeline/archive/name.go b/internal/nametemplate/name.go similarity index 91% rename from pipeline/archive/name.go rename to internal/nametemplate/name.go index 6b4804c19..cdf35a7b0 100644 --- a/pipeline/archive/name.go +++ b/internal/nametemplate/name.go @@ -1,4 +1,4 @@ -package archive +package nametemplate import ( "bytes" @@ -8,7 +8,7 @@ import ( "github.com/goreleaser/goreleaser/internal/artifact" ) -func nameFor(ctx *context.Context, a artifact.Artifact) (string, error) { +func Apply(ctx *context.Context, a artifact.Artifact) (string, error) { var out bytes.Buffer t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate) if err != nil { diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index 24f7ca121..c84c2d96e 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -16,6 +16,8 @@ import ( "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/goreleaser/goreleaser/internal/artifact" + "github.com/goreleaser/goreleaser/internal/nametemplate" + "github.com/goreleaser/goreleaser/internal/nametemplate" ) // Pipe for archive @@ -66,7 +68,7 @@ func (Pipe) Default(ctx *context.Context) error { func create(ctx *context.Context, artifacts []artifact.Artifact) error { var format = archiveformat.For(ctx, artifacts[0].Platform()) - folder, err := nameFor(ctx, artifacts[0]) + folder, err := nametemplate.Apply(ctx, artifacts[0]) if err != nil { return err } diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 141367cec..d548f0fea 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -6,14 +6,16 @@ import ( "io/ioutil" "os/exec" "path/filepath" - "strings" "github.com/apex/log" - "github.com/goreleaser/goreleaser/context" - "github.com/goreleaser/goreleaser/internal/linux" - "github.com/goreleaser/goreleaser/pipeline" "github.com/pkg/errors" "golang.org/x/sync/errgroup" + + "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/internal/artifact" + "github.com/goreleaser/goreleaser/internal/linux" + "github.com/goreleaser/goreleaser/internal/nametemplate" + "github.com/goreleaser/goreleaser/pipeline" ) // ErrNoFPM is shown when fpm cannot be found in $PATH @@ -50,28 +52,34 @@ func doRun(ctx *context.Context) error { var g errgroup.Group sem := make(chan bool, ctx.Parallelism) for _, format := range ctx.Config.FPM.Formats { - for platform, groups := range ctx.Binaries { - if !strings.Contains(platform, "linux") { - log.WithField("platform", platform).Debug("skipped non-linux builds for fpm") - continue - } + for platform, artifacts := range ctx.Artifacts.Filter( + artifact.And( + artifact.ByType(artifact.Binary), + artifact.ByGoos("linux"), + ), + ).GroupByPlatform() { sem <- true format := format - arch := linux.Arch(platform) - for folder, binaries := range groups { - g.Go(func() error { - defer func() { - <-sem - }() - return create(ctx, format, folder, arch, binaries) - }) - } + arch := linux.Arch(platform) // TODO: could probably pass artifact.Goarch here + artifacts := artifacts + g.Go(func() error { + defer func() { + <-sem + }() + return create(ctx, format, arch, artifacts) + }) } } return g.Wait() } -func create(ctx *context.Context, format, folder, arch string, binaries []context.Binary) error { +func create(ctx *context.Context, format, arch string, binaries []artifact.Artifact) error { + // TODO: should add template support here probably... for now, let's use + // archive's template + folder, err := nametemplate.Apply(ctx, binaries[0]) + if err != nil { + return err + } var path = filepath.Join(ctx.Config.Dist, folder) var file = path + "." + format var log = log.WithField("format", format).WithField("arch", arch) @@ -111,7 +119,14 @@ func create(ctx *context.Context, format, folder, arch string, binaries []contex if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil { return errors.Wrap(err, string(out)) } - ctx.AddArtifact(file) + ctx.Artifacts.Add(artifact.Artifact{ + Type: artifact.LinuxPackage, + Name: folder + "." + format, + Path: file, + Goos: binaries[0].Goos, + Goarch: binaries[0].Goarch, + Goarm: binaries[0].Goarm, + }) return nil }