1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-05 13:15:26 +02:00

feat: allow to use ModulePath on templates (#2128)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-03-22 08:55:01 -03:00 committed by GitHub
parent a62c6792e0
commit 7378edc708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 27 deletions

View File

@ -0,0 +1,34 @@
package gomod
import (
"fmt"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe for env.
type Pipe struct{}
func (Pipe) String() string {
return "loading go mod information"
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
out, err := exec.CommandContext(ctx, "go", "list", "-m").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to get module path: %w: %s", err, string(out))
}
result := strings.TrimSpace(string(out))
if result == "command-line-arguments" {
return pipe.Skip("not a go module")
}
ctx.ModulePath = result
return nil
}

View File

@ -0,0 +1,36 @@
package gomod
import (
"os"
"testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "github.com/goreleaser/goreleaser", ctx.ModulePath)
}
func TestRunOutsideGoModule(t *testing.T) {
require.NoError(t, os.Chdir(t.TempDir()))
ctx := context.New(config.Project{})
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Empty(t, ctx.ModulePath)
}
func TestRunCommandError(t *testing.T) {
os.Unsetenv("PATH")
require.NoError(t, os.Chdir(t.TempDir()))
ctx := context.New(config.Project{})
require.EqualError(t, Pipe{}.Run(ctx), "failed to get module path: exec: \"go\": executable file not found in $PATH: ")
require.Empty(t, ctx.ModulePath)
}
func TestDescription(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
}

View File

@ -4,6 +4,7 @@ package pipeline
import (
"fmt"
"github.com/goreleaser/goreleaser/internal/pipe/gomod"
"github.com/goreleaser/goreleaser/internal/pipe/semver"
"github.com/goreleaser/goreleaser/internal/pipe/sourcearchive"
@ -37,6 +38,7 @@ type Piper interface {
// BuildPipeline contains all build-related pipe implementations in order.
// nolint:gochecknoglobals
var BuildPipeline = []Piper{
gomod.Pipe{}, // setup gomod-related stuff
env.Pipe{}, // load and validate environment variables
git.Pipe{}, // get and validate git repo state
semver.Pipe{}, // parse current tag to a semver

View File

@ -44,6 +44,7 @@ const (
env = "Env"
date = "Date"
timestamp = "Timestamp"
modulePath = "ModulePath"
// artifact-only keys.
osKey = "Os"
@ -72,6 +73,7 @@ func New(ctx *context.Context) *Template {
return &Template{
fields: Fields{
projectName: ctx.Config.ProjectName,
modulePath: ctx.ModulePath,
version: ctx.Version,
rawVersion: rawVersionV,
tag: ctx.Git.CurrentTag,
@ -97,9 +99,9 @@ func New(ctx *context.Context) *Template {
// WithEnvS overrides template's env field with the given KEY=VALUE list of
// environment variables.
func (t *Template) WithEnvS(envs []string) *Template {
var result = map[string]string{}
result := map[string]string{}
for _, env := range envs {
var parts = strings.SplitN(env, "=", 2)
parts := strings.SplitN(env, "=", 2)
result[parts[0]] = parts[1]
}
return t.WithEnv(result)
@ -122,7 +124,7 @@ func (t *Template) WithExtraFields(f Fields) *Template {
// WithArtifact populates Fields from the artifact and replacements.
func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]string) *Template {
var bin = a.Extra[binary]
bin := a.Extra[binary]
if bin == nil {
bin = t.fields[projectName]
}

View File

@ -13,9 +13,10 @@ import (
)
func TestWithArtifact(t *testing.T) {
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "proj",
})
ctx.ModulePath = "github.com/goreleaser/goreleaser"
ctx.Env = map[string]string{
"FOO": "bar",
}
@ -31,21 +32,22 @@ func TestWithArtifact(t *testing.T) {
ctx.Git.FullCommit = "fullcommit"
ctx.Git.ShortCommit = "shortcommit"
for expect, tmpl := range map[string]string{
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"softfloat": "{{.Mips}}",
"1.2.3": "{{.Version}}",
"v1.2.3": "{{.Tag}}",
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
"test-branch": "{{.Branch}}",
"commit": "{{.Commit}}",
"fullcommit": "{{.FullCommit}}",
"shortcommit": "{{.ShortCommit}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
"": "{{.ArtifactUploadHash}}",
"bar": "{{.Env.FOO}}",
"Linux": "{{.Os}}",
"amd64": "{{.Arch}}",
"6": "{{.Arm}}",
"softfloat": "{{.Mips}}",
"1.2.3": "{{.Version}}",
"v1.2.3": "{{.Tag}}",
"1-2-3": "{{.Major}}-{{.Minor}}-{{.Patch}}",
"test-branch": "{{.Branch}}",
"commit": "{{.Commit}}",
"fullcommit": "{{.FullCommit}}",
"shortcommit": "{{.ShortCommit}}",
"binary": "{{.Binary}}",
"proj": "{{.ProjectName}}",
"": "{{.ArtifactUploadHash}}",
"github.com/goreleaser/goreleaser": "{{ .ModulePath }}",
} {
tmpl := tmpl
expect := expect
@ -126,7 +128,7 @@ func TestEnv(t *testing.T) {
out: "",
},
}
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
@ -140,7 +142,7 @@ func TestEnv(t *testing.T) {
}
func TestWithEnv(t *testing.T) {
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
ctx.Env = map[string]string{
"FOO": "BAR",
}
@ -154,7 +156,7 @@ func TestWithEnv(t *testing.T) {
}
func TestFuncMap(t *testing.T) {
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "proj",
})
wd, err := os.Getwd()
@ -293,7 +295,7 @@ func TestInvalidTemplate(t *testing.T) {
}
func TestEnvNotFound(t *testing.T) {
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.2.4"
result, err := New(ctx).Apply("{{.Env.FOO}}")
require.Empty(t, result)
@ -301,10 +303,9 @@ func TestEnvNotFound(t *testing.T) {
}
func TestWithExtraFields(t *testing.T) {
var ctx = context.New(config.Project{})
ctx := context.New(config.Project{})
out, _ := New(ctx).WithExtraFields(Fields{
"MyCustomField": "foo",
}).Apply("{{ .MyCustomField }}")
require.Equal(t, "foo", out)
}

View File

@ -32,7 +32,7 @@ type Env map[string]string
// Copy returns a copy of the environment.
func (e Env) Copy() Env {
var out = Env{}
out := Env{}
for k, v := range e {
out[k] = v
}
@ -42,7 +42,7 @@ func (e Env) Copy() Env {
// Strings returns the current environment as a list of strings, suitable for
// os executions.
func (e Env) Strings() []string {
var result = make([]string, 0, len(e))
result := make([]string, 0, len(e))
for k, v := range e {
result = append(result, k+"="+v)
}
@ -76,6 +76,7 @@ type Context struct {
ReleaseHeader string
ReleaseFooter string
Version string
ModulePath string
Snapshot bool
SkipPostBuildHooks bool
SkipPublish bool

View File

@ -31,6 +31,7 @@ On fields that support templating, these fields are always available:
| `.Env` | a map with system's environment variables |
| `.Date` | current UTC date in RFC 3339 format |
| `.Timestamp` | current UTC time in Unix format |
| `.ModulePath` | the go module path, as reported by `go list -m` |
On fields that are related to a single artifact (e.g., the binary name), you
may have some extra fields: