1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00

Merge pull request #167 from goreleaser/artifact-list

fixed old TODO: artifact list on context
This commit is contained in:
Carlos Alexandro Becker 2017-04-14 12:32:42 -03:00 committed by GitHub
commit f619404b23
6 changed files with 69 additions and 43 deletions

View File

@ -2,6 +2,9 @@ package context
import (
ctx "context"
"log"
"strings"
"sync"
"github.com/goreleaser/goreleaser/config"
)
@ -17,11 +20,24 @@ type GitInfo struct {
// Context carries along some data through the pipes
type Context struct {
ctx.Context
Config config.Project
Token string
Git GitInfo
Archives map[string]string
Version string
Config config.Project
Token string
Git GitInfo
Archives map[string]string
Artifacts []string
Version string
}
var lock sync.Mutex
// AddArtifact adds a file to upload list
func (ctx *Context) AddArtifact(file string) {
lock.Lock()
defer lock.Unlock()
file = strings.TrimPrefix(file, ctx.Config.Dist)
file = strings.Replace(file, "/", "", -1)
ctx.Artifacts = append(ctx.Artifacts, file)
log.Println("Registered artifact", file)
}
// New context

34
context/context_test.go Normal file
View File

@ -0,0 +1,34 @@
package context
import (
"testing"
"golang.org/x/sync/errgroup"
"github.com/goreleaser/goreleaser/config"
"github.com/stretchr/testify/assert"
)
func TestMultipleArtifactAdds(t *testing.T) {
var assert = assert.New(t)
var list = []string{
"dist/a",
"dist/b",
"dist/c",
"dist/d",
}
var ctx = New(config.Project{
Dist: "dist",
})
var g errgroup.Group
for _, f := range list {
f := f
g.Go(func() error {
ctx.AddArtifact(f)
return nil
})
}
assert.NoError(g.Wait())
assert.Len(ctx.Artifacts, len(list))
assert.Contains(ctx.Artifacts, "a", "b", "c", "d")
}

View File

@ -26,7 +26,7 @@ func (Pipe) Run(ctx *context.Context) error {
for _, archive := range ctx.Archives {
archive := archive
g.Go(func() error {
return create(archive, ctx)
return create(ctx, archive)
})
}
return g.Wait()
@ -38,7 +38,7 @@ type Archive interface {
Add(name, path string) error
}
func create(name string, ctx *context.Context) error {
func create(ctx *context.Context, name string) error {
folder := filepath.Join(ctx.Config.Dist, name)
file, err := os.Create(folder + "." + ctx.Config.Archive.Format)
log.Println("Creating", file.Name())
@ -62,6 +62,7 @@ func create(name string, ctx *context.Context) error {
return err
}
}
ctx.AddArtifact(file.Name())
return nil
}

View File

@ -83,5 +83,6 @@ func create(ctx *context.Context, format, archive, arch string) error {
if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil {
return errors.New(string(out))
}
ctx.AddArtifact(file)
return nil
}

View File

@ -31,39 +31,22 @@ func doRun(ctx *context.Context, client clients.Client) error {
return err
}
var g errgroup.Group
for _, archive := range ctx.Archives {
archive := archive
for _, artifact := range ctx.Artifacts {
artifact := artifact
g.Go(func() error {
return upload(ctx, client, releaseID, archive, ctx.Config.Archive.Format)
return upload(ctx, client, releaseID, artifact)
})
for _, format := range ctx.Config.FPM.Formats {
format := format
g.Go(func() error {
return upload(ctx, client, releaseID, archive, format)
})
}
}
return g.Wait()
}
func upload(ctx *context.Context, client clients.Client, releaseID int, archive, format string) error {
archive = archive + "." + format
var path = filepath.Join(ctx.Config.Dist, archive)
// In case the file doesn't exist, we just ignore it.
// We do this because we can get invalid combinations of archive+format here,
// like darwinamd64 + deb or something like that.
// It's assumed that the archive pipe would fail the entire thing in case it fails to
// generate some archive, as well fpm pipe is expected to fail if something wrong happens.
// So, here, we just assume IsNotExist as an expected error.
// TODO: maybe add a list of files to upload in the context so we don't have to do this.
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil
}
func upload(ctx *context.Context, client clients.Client, releaseID int, artifact string) error {
var path = filepath.Join(ctx.Config.Dist, artifact)
file, err := os.Open(path)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
log.Println("Uploading", file.Name())
return client.Upload(ctx, releaseID, archive, file)
return client.Upload(ctx, releaseID, artifact, file)
}

View File

@ -21,9 +21,9 @@ func TestRunPipe(t *testing.T) {
assert := assert.New(t)
folder, err := ioutil.TempDir("", "gorelasertest")
assert.NoError(err)
_, err = os.Create(filepath.Join(folder, "bin.tar.gz"))
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
assert.NoError(err)
_, err = os.Create(filepath.Join(folder, "bin.deb"))
debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
assert.NoError(err)
var ctx = &context.Context{
Git: context.GitInfo{
@ -31,25 +31,16 @@ func TestRunPipe(t *testing.T) {
},
Config: config.Project{
Dist: folder,
Archive: config.Archive{
Format: "tar.gz",
},
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
},
FPM: config.FPM{
Formats: []string{
"deb",
},
},
},
Archives: map[string]string{
"darwinamd64": "bin",
},
}
ctx.AddArtifact(tarfile.Name())
ctx.AddArtifact(debfile.Name())
client := &DummyClient{}
assert.NoError(doRun(ctx, client))
assert.True(client.CreatedRelease)