1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-09 13:36:56 +02:00

refactor: fixed fpm pipe

This commit is contained in:
Carlos Alexandro Becker 2017-12-17 17:11:08 -02:00
parent 1982259c29
commit 906c8b08e3
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 43 additions and 24 deletions

View File

@ -17,6 +17,8 @@ const (
UploadableBinary UploadableBinary
// Binary is a binary (output of a gobuild) // Binary is a binary (output of a gobuild)
Binary Binary
// LinuxPackage is a linux package generated by fpm or snapcraft
LinuxPackage
// DockerImage is a docker image // DockerImage is a docker image
DockerImage DockerImage
// Checksum is a checksum file // Checksum is a checksum file
@ -58,7 +60,7 @@ func (a Artifact) Platform() string {
} }
// GroupByPlatform groups the artifacts by their platform // 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{} var result = map[string][]Artifact{}
for _, a := range artifacts.items { for _, a := range artifacts.items {
result[a.Platform()] = append(result[a.Platform()], a) result[a.Platform()] = append(result[a.Platform()], a)

View File

@ -1,4 +1,4 @@
package archive package nametemplate
import ( import (
"bytes" "bytes"
@ -8,7 +8,7 @@ import (
"github.com/goreleaser/goreleaser/internal/artifact" "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 var out bytes.Buffer
t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate) t, err := template.New("archive_name").Parse(ctx.Config.Archive.NameTemplate)
if err != nil { if err != nil {

View File

@ -16,6 +16,8 @@ import (
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/goreleaser/goreleaser/internal/archiveformat"
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/nametemplate"
"github.com/goreleaser/goreleaser/internal/nametemplate"
) )
// Pipe for archive // Pipe for archive
@ -66,7 +68,7 @@ func (Pipe) Default(ctx *context.Context) error {
func create(ctx *context.Context, artifacts []artifact.Artifact) error { func create(ctx *context.Context, artifacts []artifact.Artifact) error {
var format = archiveformat.For(ctx, artifacts[0].Platform()) var format = archiveformat.For(ctx, artifacts[0].Platform())
folder, err := nameFor(ctx, artifacts[0]) folder, err := nametemplate.Apply(ctx, artifacts[0])
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,14 +6,16 @@ import (
"io/ioutil" "io/ioutil"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/apex/log" "github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/linux"
"github.com/goreleaser/goreleaser/pipeline"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/sync/errgroup" "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 // ErrNoFPM is shown when fpm cannot be found in $PATH
@ -50,28 +52,34 @@ func doRun(ctx *context.Context) error {
var g errgroup.Group var g errgroup.Group
sem := make(chan bool, ctx.Parallelism) sem := make(chan bool, ctx.Parallelism)
for _, format := range ctx.Config.FPM.Formats { for _, format := range ctx.Config.FPM.Formats {
for platform, groups := range ctx.Binaries { for platform, artifacts := range ctx.Artifacts.Filter(
if !strings.Contains(platform, "linux") { artifact.And(
log.WithField("platform", platform).Debug("skipped non-linux builds for fpm") artifact.ByType(artifact.Binary),
continue artifact.ByGoos("linux"),
} ),
).GroupByPlatform() {
sem <- true sem <- true
format := format format := format
arch := linux.Arch(platform) arch := linux.Arch(platform) // TODO: could probably pass artifact.Goarch here
for folder, binaries := range groups { artifacts := artifacts
g.Go(func() error { g.Go(func() error {
defer func() { defer func() {
<-sem <-sem
}() }()
return create(ctx, format, folder, arch, binaries) return create(ctx, format, arch, artifacts)
}) })
}
} }
} }
return g.Wait() 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 path = filepath.Join(ctx.Config.Dist, folder)
var file = path + "." + format var file = path + "." + format
var log = log.WithField("format", format).WithField("arch", arch) 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 { if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil {
return errors.Wrap(err, string(out)) 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 return nil
} }