diff --git a/context/context.go b/context/context.go index 1160cb3ff..c232a9f0b 100644 --- a/context/context.go +++ b/context/context.go @@ -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 diff --git a/context/context_test.go b/context/context_test.go new file mode 100644 index 000000000..18b3163fd --- /dev/null +++ b/context/context_test.go @@ -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") +} diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index 2ecf5f580..4dd5ccbdb 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -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 } diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index e6abae23d..4df33049f 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -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 } diff --git a/pipeline/release/release.go b/pipeline/release/release.go index 595709ebd..3140eeb7e 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -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) } diff --git a/pipeline/release/release_test.go b/pipeline/release/release_test.go index 24548f98c..434fa3eeb 100644 --- a/pipeline/release/release_test.go +++ b/pipeline/release/release_test.go @@ -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)