mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-19 20:57:53 +02:00
refactor: remove old code from context pkg
This commit is contained in:
parent
f09a5396fb
commit
375940841f
@ -9,12 +9,10 @@ package context
|
|||||||
import (
|
import (
|
||||||
ctx "context"
|
ctx "context"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/apex/log"
|
|
||||||
"github.com/goreleaser/goreleaser/config"
|
"github.com/goreleaser/goreleaser/config"
|
||||||
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GitInfo includes tags and diffs used in some point
|
// GitInfo includes tags and diffs used in some point
|
||||||
@ -23,11 +21,6 @@ type GitInfo struct {
|
|||||||
Commit string
|
Commit string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Binary with pretty name and path
|
|
||||||
type Binary struct {
|
|
||||||
Name, Path string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Context carries along some data through the pipes
|
// Context carries along some data through the pipes
|
||||||
type Context struct {
|
type Context struct {
|
||||||
ctx.Context
|
ctx.Context
|
||||||
@ -35,8 +28,7 @@ type Context struct {
|
|||||||
Env map[string]string
|
Env map[string]string
|
||||||
Token string
|
Token string
|
||||||
Git GitInfo
|
Git GitInfo
|
||||||
Binaries map[string]map[string][]Binary
|
Artifacts artifact.Artifacts
|
||||||
Artifacts []string
|
|
||||||
Checksums []string
|
Checksums []string
|
||||||
Dockers []string
|
Dockers []string
|
||||||
ReleaseNotes string
|
ReleaseNotes string
|
||||||
@ -49,63 +41,6 @@ type Context struct {
|
|||||||
Parallelism int
|
Parallelism int
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
artifactsLock sync.Mutex
|
|
||||||
checksumsLock sync.Mutex
|
|
||||||
dockersLock sync.Mutex
|
|
||||||
binariesLock sync.Mutex
|
|
||||||
)
|
|
||||||
|
|
||||||
// AddArtifact adds a file to upload list
|
|
||||||
func (ctx *Context) AddArtifact(file string) {
|
|
||||||
artifactsLock.Lock()
|
|
||||||
defer artifactsLock.Unlock()
|
|
||||||
file = strings.TrimPrefix(file, ctx.Config.Dist+string(filepath.Separator))
|
|
||||||
ctx.Artifacts = append(ctx.Artifacts, file)
|
|
||||||
log.WithField("artifact", file).Info("new release artifact")
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddChecksum adds a checksum file.
|
|
||||||
func (ctx *Context) AddChecksum(file string) {
|
|
||||||
checksumsLock.Lock()
|
|
||||||
defer checksumsLock.Unlock()
|
|
||||||
file = strings.TrimPrefix(file, ctx.Config.Dist+string(filepath.Separator))
|
|
||||||
ctx.Checksums = append(ctx.Checksums, file)
|
|
||||||
log.WithField("checksum", file).Info("new checksum file")
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddDocker adds a docker image to the docker images list
|
|
||||||
func (ctx *Context) AddDocker(image string) {
|
|
||||||
dockersLock.Lock()
|
|
||||||
defer dockersLock.Unlock()
|
|
||||||
ctx.Dockers = append(ctx.Dockers, image)
|
|
||||||
log.WithField("image", image).Info("new docker image")
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddBinary adds a built binary to the current context
|
|
||||||
func (ctx *Context) AddBinary(platform, folder, name, path string) {
|
|
||||||
binariesLock.Lock()
|
|
||||||
defer binariesLock.Unlock()
|
|
||||||
if ctx.Binaries == nil {
|
|
||||||
ctx.Binaries = map[string]map[string][]Binary{}
|
|
||||||
}
|
|
||||||
if ctx.Binaries[platform] == nil {
|
|
||||||
ctx.Binaries[platform] = map[string][]Binary{}
|
|
||||||
}
|
|
||||||
ctx.Binaries[platform][folder] = append(
|
|
||||||
ctx.Binaries[platform][folder],
|
|
||||||
Binary{
|
|
||||||
Name: name,
|
|
||||||
Path: path,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
log.WithField("platform", platform).
|
|
||||||
WithField("folder", folder).
|
|
||||||
WithField("name", name).
|
|
||||||
WithField("path", path).
|
|
||||||
Debug("new binary")
|
|
||||||
}
|
|
||||||
|
|
||||||
// New context
|
// New context
|
||||||
func New(config config.Project) *Context {
|
func New(config config.Project) *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
|
@ -4,81 +4,11 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/goreleaser/goreleaser/config"
|
"github.com/goreleaser/goreleaser/config"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/tj/assert"
|
||||||
"golang.org/x/sync/errgroup"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMultipleAdds(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
var artifacts = []string{
|
var ctx = New(config.Project{})
|
||||||
"dist/a",
|
assert.NotEmpty(t, ctx.Env)
|
||||||
"dist/b",
|
assert.Equal(t, 4, ctx.Parallelism)
|
||||||
"dist/c",
|
|
||||||
"dist/d",
|
|
||||||
}
|
|
||||||
var checksums = []string{
|
|
||||||
"dist/a.sha256",
|
|
||||||
}
|
|
||||||
var dockerfiles = []string{
|
|
||||||
"a/b:1.0.0",
|
|
||||||
"c/d:2.0.0",
|
|
||||||
"e/f:3.0.0",
|
|
||||||
}
|
|
||||||
var ctx = New(config.Project{
|
|
||||||
Dist: "dist",
|
|
||||||
})
|
|
||||||
var g errgroup.Group
|
|
||||||
for _, f := range artifacts {
|
|
||||||
f := f
|
|
||||||
g.Go(func() error {
|
|
||||||
ctx.AddArtifact(f)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
assert.NoError(t, g.Wait())
|
|
||||||
for _, c := range checksums {
|
|
||||||
c := c
|
|
||||||
g.Go(func() error {
|
|
||||||
ctx.AddChecksum(c)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
assert.NoError(t, g.Wait())
|
|
||||||
for _, d := range dockerfiles {
|
|
||||||
d := d
|
|
||||||
g.Go(func() error {
|
|
||||||
ctx.AddDocker(d)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
assert.NoError(t, g.Wait())
|
|
||||||
assert.Len(t, ctx.Artifacts, len(artifacts))
|
|
||||||
assert.Contains(t, ctx.Artifacts, "a", "b", "c", "d")
|
|
||||||
assert.Len(t, ctx.Checksums, len(checksums))
|
|
||||||
assert.Contains(t, ctx.Checksums, "a.sha256")
|
|
||||||
assert.Len(t, ctx.Dockers, len(dockerfiles))
|
|
||||||
assert.Contains(t, ctx.Dockers, "a/b:1.0.0", "c/d:2.0.0", "e/f:3.0.0")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMultipleBinaryAdds(t *testing.T) {
|
|
||||||
var list = map[string]string{
|
|
||||||
"a": "folder/a",
|
|
||||||
"b": "folder/b",
|
|
||||||
"c": "folder/c",
|
|
||||||
"d": "folder/d",
|
|
||||||
}
|
|
||||||
var ctx = New(config.Project{
|
|
||||||
Dist: "dist",
|
|
||||||
})
|
|
||||||
var g errgroup.Group
|
|
||||||
for k, f := range list {
|
|
||||||
f := f
|
|
||||||
k := k
|
|
||||||
g.Go(func() error {
|
|
||||||
ctx.AddBinary("linuxamd64", k, k, f)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
assert.NoError(t, g.Wait())
|
|
||||||
assert.Len(t, ctx.Binaries["linuxamd64"], len(list))
|
|
||||||
assert.Len(t, ctx.Binaries, 1)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user