1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-19 20:57:53 +02:00

refactor: fixed brew pipe

This commit is contained in:
Carlos Alexandro Becker 2017-12-17 16:31:06 -02:00
parent 574b2942b3
commit 248810535e
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
3 changed files with 50 additions and 21 deletions

View File

@ -1,7 +1,10 @@
// Package artifact provides the core artifact storage for goreleaser // Package artifact provides the core artifact storage for goreleaser
package artifact package artifact
import "sync" import (
"fmt"
"sync"
)
// Type defines the type of an artifact // Type defines the type of an artifact
type Type int type Type int
@ -104,12 +107,22 @@ func ByType(t Type) Filter {
// Filter filters the artifact list, returning a new instance. // Filter filters the artifact list, returning a new instance.
// There are some pre-defined filters but anything of the Type Filter // There are some pre-defined filters but anything of the Type Filter
// is accepted. // is accepted.
func (artifacts *Artifacts) Filter(filter Filter) Artifacts { func (artifacts *Artifacts) Filter(filters ...Filter) Artifacts {
var result = New() var result = New()
for _, a := range artifacts.items { for _, a := range artifacts.items {
if filter(a) { if apply(a, filters) {
fmt.Println("add", a)
result.Add(a) result.Add(a)
} }
} }
return result return result
} }
func apply(a Artifact, filters []Filter) bool {
for _, filter := range filters {
if !filter(a) {
return false
}
}
return true
}

View File

@ -13,7 +13,7 @@ func TestAdd(t *testing.T) {
for _, a := range []Artifact{ for _, a := range []Artifact{
{ {
Name: "foo", Name: "foo",
Type: Archive, Type: UploadableArchive,
}, },
{ {
Name: "bar", Name: "bar",
@ -56,11 +56,16 @@ func TestFilter(t *testing.T) {
Name: "check", Name: "check",
Type: Checksum, Type: Checksum,
}, },
{
Name: "checkzumm",
Type: Checksum,
},
} }
var artifacts = New() var artifacts = New()
for _, a := range data { for _, a := range data {
artifacts.Add(a) artifacts.Add(a)
} }
assert.Len(t, artifacts.Filter(ByGoos("linux")).items, 1) assert.Len(t, artifacts.Filter(ByGoos("linux")).items, 1)
assert.Len(t, artifacts.Filter(ByGoos("darwin")).items, 0) assert.Len(t, artifacts.Filter(ByGoos("darwin")).items, 0)
@ -70,8 +75,12 @@ func TestFilter(t *testing.T) {
assert.Len(t, artifacts.Filter(ByGoarm("6")).items, 1) assert.Len(t, artifacts.Filter(ByGoarm("6")).items, 1)
assert.Len(t, artifacts.Filter(ByGoarm("7")).items, 0) assert.Len(t, artifacts.Filter(ByGoarm("7")).items, 0)
assert.Len(t, artifacts.Filter(ByType(Checksum)).items, 1) assert.Len(t, artifacts.Filter(ByType(Checksum)).items, 2)
assert.Len(t, artifacts.Filter(ByType(Binary)).items, 0) assert.Len(t, artifacts.Filter(ByType(Binary)).items, 0)
assert.Len(t, artifacts.Filter(ByType(Checksum), func(a Artifact) bool {
return a.Name == "checkzumm"
}).List(), 1)
} }
func TestGroupByPlatform(t *testing.T) { func TestGroupByPlatform(t *testing.T) {

View File

@ -10,18 +10,22 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/apex/log" "github.com/apex/log"
"github.com/goreleaser/goreleaser/checksum" "github.com/goreleaser/goreleaser/checksum"
"github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/archiveformat"
"github.com/goreleaser/goreleaser/internal/client" "github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/pipeline" "github.com/goreleaser/goreleaser/pipeline"
) )
// ErrNoDarwin64Build when there is no build for darwin_amd64 (goos doesn't // ErrNoDarwin64Build when there is no build for darwin_amd64
// contain darwin and/or goarch doesn't contain amd64) var ErrNoDarwin64Build = errors.New("brew tap requires one darwin amd64 build")
var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build")
// ErrTooManyDarwin64Builds when there are too many builds for darwin_amd64
var ErrTooManyDarwin64Builds = errors.New("brew tap requires at most one darwin amd64 build")
const platform = "darwinamd64" const platform = "darwinamd64"
@ -98,28 +102,31 @@ func doRun(ctx *context.Context, client client.Client) error {
return pipeline.Skip("archive format is binary") return pipeline.Skip("archive format is binary")
} }
var group = ctx.Binaries["darwinamd64"] var archives = ctx.Artifacts.Filter(
if group == nil { artifact.ByGoos("darwin"),
artifact.ByGoarch("amd64"),
artifact.ByGoarch(""),
artifact.ByType(artifact.UploadableArchive),
).List()
if len(archives) == 0 {
return ErrNoDarwin64Build return ErrNoDarwin64Build
} }
var folder string if len(archives) > 0 {
for f := range group { return ErrTooManyDarwin64Builds
folder = f
break
} }
var path = filepath.Join(ctx.Config.Brew.Folder, ctx.Config.ProjectName+".rb") var path = filepath.Join(ctx.Config.Brew.Folder, ctx.Config.ProjectName+".rb")
log.WithField("formula", path). log.WithField("formula", path).
WithField("repo", ctx.Config.Brew.GitHub.String()). WithField("repo", ctx.Config.Brew.GitHub.String()).
Info("pushing") Info("pushing")
content, err := buildFormula(ctx, client, folder) content, err := buildFormula(ctx, client, archives[0])
if err != nil { if err != nil {
return err return err
} }
return client.CreateFile(ctx, content, path) return client.CreateFile(ctx, content, path)
} }
func buildFormula(ctx *context.Context, client client.Client, folder string) (bytes.Buffer, error) { func buildFormula(ctx *context.Context, client client.Client, artifact artifact.Artifact) (bytes.Buffer, error) {
data, err := dataFor(ctx, client, folder) data, err := dataFor(ctx, client, artifact)
if err != nil { if err != nil {
return bytes.Buffer{}, err return bytes.Buffer{}, err
} }
@ -135,9 +142,9 @@ func doBuildFormula(data templateData) (out bytes.Buffer, err error) {
return return
} }
func dataFor(ctx *context.Context, client client.Client, folder string) (result templateData, err error) { func dataFor(ctx *context.Context, client client.Client, artifact artifact.Artifact) (result templateData, err error) {
var file = folder + "." + archiveformat.For(ctx, platform) var file = artifact.Path
sum, err := checksum.SHA256(filepath.Join(ctx.Config.Dist, file)) sum, err := checksum.SHA256(file)
if err != nil { if err != nil {
return return
} }