1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-15 01:34:21 +02:00

fix: snapshot version handling

This commit is contained in:
Carlos Alexandro Becker
2018-12-13 10:09:36 -02:00
committed by Carlos Alexandro Becker
parent 513473b446
commit a69ef61d60
7 changed files with 73 additions and 37 deletions

View File

@ -17,7 +17,7 @@ install:
- sudo snap install snapcraft --classic - sudo snap install snapcraft --classic
script: script:
- make ci - make ci
- test -n "$TRAVIS_TAG" || go run main.go --snapshot - test -n "$TRAVIS_TAG" || go run main.go --snapshot --debug
after_success: after_success:
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)
- make static - make static

View File

@ -9,7 +9,6 @@ import (
"github.com/goreleaser/goreleaser/internal/deprecate" "github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/git" "github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/pipe" "github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -35,9 +34,7 @@ func (Pipe) Run(ctx *context.Context) error {
} }
ctx.Git = info ctx.Git = info
log.Infof("releasing %s, commit %s", info.CurrentTag, info.Commit) log.Infof("releasing %s, commit %s", info.CurrentTag, info.Commit)
if err := setVersion(ctx); err != nil { ctx.Version = strings.TrimPrefix(ctx.Git.CurrentTag, "v")
return err
}
return validate(ctx) return validate(ctx)
} }
@ -104,20 +101,6 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
}, nil }, nil
} }
func setVersion(ctx *context.Context) error {
if ctx.Snapshot {
snapshotName, err := tmpl.New(ctx).Apply(ctx.Config.Snapshot.NameTemplate)
if err != nil {
return errors.Wrap(err, "failed to generate snapshot name")
}
ctx.Version = snapshotName
return nil
}
// removes usual `v` prefix
ctx.Version = strings.TrimPrefix(ctx.Git.CurrentTag, "v")
return nil
}
func validate(ctx *context.Context) error { func validate(ctx *context.Context) error {
if ctx.Snapshot { if ctx.Snapshot {
return pipe.ErrSnapshotEnabled return pipe.ErrSnapshotEnabled

View File

@ -224,23 +224,6 @@ func TestSnapshotDirty(t *testing.T) {
testlib.AssertSkipped(t, Pipe{}.Run(ctx)) testlib.AssertSkipped(t, Pipe{}.Run(ctx))
} }
func TestShortCommitHash(t *testing.T) {
_, back := testlib.Mktmp(t)
defer back()
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:foo/bar.git")
testlib.GitCommit(t, "first")
var ctx = context.New(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{.Commit}}",
},
})
ctx.Snapshot = true
ctx.Config.Git.ShortHash = true
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
assert.Len(t, ctx.Version, 7)
}
func TestGitNotInPath(t *testing.T) { func TestGitNotInPath(t *testing.T) {
var path = os.Getenv("PATH") var path = os.Getenv("PATH")
defer func() { defer func() {

View File

@ -150,6 +150,8 @@ func create(ctx *context.Context, arch string, binaries []artifact.Artifact) err
metadata.Name = ctx.Config.Snapcraft.Name metadata.Name = ctx.Config.Snapcraft.Name
} }
log.Debugf("metadata: %+v", metadata)
for _, binary := range binaries { for _, binary := range binaries {
log.WithField("path", binary.Path). log.WithField("path", binary.Path).
WithField("name", binary.Name). WithField("name", binary.Name).
@ -168,6 +170,9 @@ func create(ctx *context.Context, arch string, binaries []artifact.Artifact) err
metadata.Apps[binary.Name] = appMetadata metadata.Apps[binary.Name] = appMetadata
destBinaryPath := filepath.Join(primeDir, filepath.Base(binary.Path)) destBinaryPath := filepath.Join(primeDir, filepath.Base(binary.Path))
log.WithField("src", binary.Path).
WithField("dst", destBinaryPath).
Debug("linking")
if err = os.Link(binary.Path, destBinaryPath); err != nil { if err = os.Link(binary.Path, destBinaryPath); err != nil {
return errors.Wrap(err, "failed to link binary") return errors.Wrap(err, "failed to link binary")
} }

View File

@ -1,7 +1,14 @@
// Package snapshot provides the snapshoting functionality to goreleaser. // Package snapshot provides the snapshoting functionality to goreleaser.
package snapshot package snapshot
import "github.com/goreleaser/goreleaser/pkg/context" import (
"fmt"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/pkg/errors"
)
// Pipe for checksums // Pipe for checksums
type Pipe struct{} type Pipe struct{}
@ -17,3 +24,18 @@ func (Pipe) Default(ctx *context.Context) error {
} }
return nil return nil
} }
func (Pipe) Run(ctx *context.Context) error {
if !ctx.Snapshot {
return pipe.Skip("not a snapshot")
}
name, err := tmpl.New(ctx).Apply(ctx.Config.Snapshot.NameTemplate)
if err != nil {
return errors.Wrap(err, "failed to generate snapshot name")
}
if name == "" {
return fmt.Errorf("empty snapshot name")
}
ctx.Version = name
return nil
}

View File

@ -3,6 +3,7 @@ package snapshot
import ( import (
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config" "github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -32,3 +33,43 @@ func TestDefaultSet(t *testing.T) {
assert.NoError(t, Pipe{}.Default(ctx)) assert.NoError(t, Pipe{}.Default(ctx))
assert.Equal(t, "snap", ctx.Config.Snapshot.NameTemplate) assert.Equal(t, "snap", ctx.Config.Snapshot.NameTemplate)
} }
func TestSnapshotNameShortCommitHash(t *testing.T) {
var ctx = context.New(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{.ShortCommit}}",
},
})
ctx.Snapshot = true
ctx.Config.Git.ShortHash = true
ctx.Git.CurrentTag = "v1.2.3"
ctx.Git.ShortCommit = "123"
assert.NoError(t, Pipe{}.Run(ctx))
assert.Equal(t, ctx.Version, "123")
}
func TestSnapshotInvalidNametemplate(t *testing.T) {
var ctx = context.New(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{.ShortCommit}{{{sss}}}",
},
})
ctx.Snapshot = true
assert.EqualError(t, Pipe{}.Run(ctx), `failed to generate snapshot name: template: tmpl:1: unexpected "}" in operand`)
}
func TestSnapshotEmptyFinalName(t *testing.T) {
var ctx = context.New(config.Project{
Snapshot: config.Snapshot{
NameTemplate: "{{ .Commit }}",
},
})
ctx.Snapshot = true
ctx.Git.CurrentTag = "v1.2.3"
assert.EqualError(t, Pipe{}.Run(ctx), "empty snapshot name")
}
func TestNotASnapshot(t *testing.T) {
var ctx = context.New(config.Project{})
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
}

View File

@ -19,6 +19,7 @@ import (
"github.com/goreleaser/goreleaser/internal/pipe/publish" "github.com/goreleaser/goreleaser/internal/pipe/publish"
"github.com/goreleaser/goreleaser/internal/pipe/sign" "github.com/goreleaser/goreleaser/internal/pipe/sign"
"github.com/goreleaser/goreleaser/internal/pipe/snapcraft" "github.com/goreleaser/goreleaser/internal/pipe/snapcraft"
"github.com/goreleaser/goreleaser/internal/pipe/snapshot"
"github.com/goreleaser/goreleaser/pkg/context" "github.com/goreleaser/goreleaser/pkg/context"
) )
@ -36,6 +37,7 @@ var Pipeline = []Piper{
before.Pipe{}, // run global hooks before build before.Pipe{}, // run global hooks before build
git.Pipe{}, // get and validate git repo state git.Pipe{}, // get and validate git repo state
defaults.Pipe{}, // load default configs defaults.Pipe{}, // load default configs
snapshot.Pipe{}, // snapshot version handling
dist.Pipe{}, // ensure ./dist is clean dist.Pipe{}, // ensure ./dist is clean
effectiveconfig.Pipe{}, // writes the actual config (with defaults et al set) to dist effectiveconfig.Pipe{}, // writes the actual config (with defaults et al set) to dist
changelog.Pipe{}, // builds the release changelog changelog.Pipe{}, // builds the release changelog