mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
refactor: rm internal/name pkg
Each pipe now does its own templating
This commit is contained in:
parent
3fd9e0f306
commit
eb19e2b5d9
@ -8,7 +8,6 @@ import (
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/name"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
@ -84,12 +83,12 @@ func (c *githubClient) CreateFile(
|
||||
|
||||
func (c *githubClient) CreateRelease(ctx *context.Context, body string) (releaseID int, err error) {
|
||||
var release *github.RepositoryRelease
|
||||
releaseTitle, err := name.ForTitle(ctx)
|
||||
title, err := releaseTitle(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var data = &github.RepositoryRelease{
|
||||
Name: github.String(releaseTitle),
|
||||
Name: github.String(title),
|
||||
TagName: github.String(ctx.Git.CurrentTag),
|
||||
Body: github.String(body),
|
||||
Draft: github.Bool(ctx.Config.Release.Draft),
|
||||
|
24
internal/client/name.go
Normal file
24
internal/client/name.go
Normal file
@ -0,0 +1,24 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
)
|
||||
|
||||
func releaseTitle(ctx *context.Context) (string, error) {
|
||||
var out bytes.Buffer
|
||||
t, err := template.New("github").Parse(ctx.Config.Release.NameTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = t.Execute(&out, struct {
|
||||
ProjectName, Tag, Version string
|
||||
}{
|
||||
ProjectName: ctx.Config.ProjectName,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
Version: ctx.Version,
|
||||
})
|
||||
return out.String(), err
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
// Package name provides name template logic for the final archive, formulae,
|
||||
// etc.
|
||||
package name
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/buildtarget"
|
||||
)
|
||||
|
||||
type nameData struct {
|
||||
Os string
|
||||
Arch string
|
||||
Arm string
|
||||
Version string
|
||||
Tag string
|
||||
Binary string // deprecated
|
||||
ProjectName string
|
||||
}
|
||||
|
||||
// ForBuild return the name for the given context, goos, goarch, goarm and
|
||||
// build, using the build.Binary property instead of project_name.
|
||||
func ForBuild(ctx *context.Context, build config.Build, target buildtarget.Target) (string, error) {
|
||||
return apply(
|
||||
nameData{
|
||||
Os: replace(ctx.Config.Archive.Replacements, target.OS),
|
||||
Arch: replace(ctx.Config.Archive.Replacements, target.Arch),
|
||||
Arm: replace(ctx.Config.Archive.Replacements, target.Arm),
|
||||
Version: ctx.Version,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
Binary: build.Binary,
|
||||
ProjectName: build.Binary,
|
||||
},
|
||||
ctx.Config.Archive.NameTemplate,
|
||||
)
|
||||
}
|
||||
|
||||
// For returns the name for the given context, goos, goarch and goarm.
|
||||
func For(ctx *context.Context, target buildtarget.Target) (string, error) {
|
||||
return apply(
|
||||
nameData{
|
||||
Os: replace(ctx.Config.Archive.Replacements, target.OS),
|
||||
Arch: replace(ctx.Config.Archive.Replacements, target.Arch),
|
||||
Arm: replace(ctx.Config.Archive.Replacements, target.Arm),
|
||||
Version: ctx.Version,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
Binary: ctx.Config.ProjectName,
|
||||
ProjectName: ctx.Config.ProjectName,
|
||||
},
|
||||
ctx.Config.Archive.NameTemplate,
|
||||
)
|
||||
}
|
||||
|
||||
// ForChecksums returns the filename for the checksums file based on its
|
||||
// template
|
||||
func ForChecksums(ctx *context.Context) (string, error) {
|
||||
return apply(
|
||||
nameData{
|
||||
ProjectName: ctx.Config.ProjectName,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
Version: ctx.Version,
|
||||
},
|
||||
ctx.Config.Checksum.NameTemplate,
|
||||
)
|
||||
}
|
||||
|
||||
// ForTitle returns the release title based upon its template
|
||||
func ForTitle(ctx *context.Context) (string, error) {
|
||||
return apply(
|
||||
nameData{
|
||||
ProjectName: ctx.Config.ProjectName,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
Version: ctx.Version,
|
||||
},
|
||||
ctx.Config.Release.NameTemplate,
|
||||
)
|
||||
}
|
||||
|
||||
func apply(data nameData, templateStr string) (string, error) {
|
||||
var out bytes.Buffer
|
||||
t, err := template.New(data.ProjectName).Parse(templateStr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = t.Execute(&out, data)
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
func replace(replacements map[string]string, original string) string {
|
||||
result := replacements[original]
|
||||
if result == "" {
|
||||
return original
|
||||
}
|
||||
return result
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
package name
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/buildtarget"
|
||||
"github.com/goreleaser/goreleaser/pipeline/defaults"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestChecksums(t *testing.T) {
|
||||
var config = config.Project{
|
||||
Checksum: config.Checksum{
|
||||
NameTemplate: "{{.ProjectName }}_{{.Tag}}_{{.Version}}_checksums.txt",
|
||||
},
|
||||
ProjectName: "testcheck",
|
||||
}
|
||||
var ctx = &context.Context{
|
||||
Config: config,
|
||||
Version: "1.0.0",
|
||||
Git: context.GitInfo{
|
||||
CurrentTag: "v1.0.0",
|
||||
},
|
||||
}
|
||||
|
||||
name, err := ForChecksums(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "testcheck_v1.0.0_1.0.0_checksums.txt", name)
|
||||
}
|
||||
|
||||
func TestNameFor(t *testing.T) {
|
||||
var config = config.Project{
|
||||
Archive: config.Archive{
|
||||
NameTemplate: "{{.Binary}}_{{.Os}}_{{.Arch}}_{{.Tag}}_{{.Version}}",
|
||||
Replacements: map[string]string{
|
||||
"darwin": "Darwin",
|
||||
"amd64": "x86_64",
|
||||
},
|
||||
},
|
||||
ProjectName: "test",
|
||||
}
|
||||
var ctx = &context.Context{
|
||||
Config: config,
|
||||
Version: "1.2.3",
|
||||
Git: context.GitInfo{
|
||||
CurrentTag: "v1.2.3",
|
||||
},
|
||||
}
|
||||
|
||||
name, err := For(ctx, buildtarget.New("darwin", "amd64", ""))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test_Darwin_x86_64_v1.2.3_1.2.3", name)
|
||||
}
|
||||
|
||||
func TestNameForBuild(t *testing.T) {
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{
|
||||
Archive: config.Archive{
|
||||
NameTemplate: "{{.Binary}}_{{.Os}}_{{.Arch}}_{{.Tag}}_{{.Version}}",
|
||||
Replacements: map[string]string{
|
||||
"darwin": "Darwin",
|
||||
"amd64": "x86_64",
|
||||
},
|
||||
},
|
||||
ProjectName: "test",
|
||||
},
|
||||
Version: "1.2.3",
|
||||
Git: context.GitInfo{
|
||||
CurrentTag: "v1.2.3",
|
||||
},
|
||||
}
|
||||
|
||||
name, err := ForBuild(
|
||||
ctx,
|
||||
config.Build{Binary: "foo"},
|
||||
buildtarget.New("darwin", "amd64", ""),
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "foo_Darwin_x86_64_v1.2.3_1.2.3", name)
|
||||
}
|
||||
|
||||
func TestInvalidNameTemplate(t *testing.T) {
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{
|
||||
Archive: config.Archive{
|
||||
NameTemplate: "{{.Binary}_{{.Os}}_{{.Arch}}_{{.Version}}",
|
||||
},
|
||||
ProjectName: "test",
|
||||
},
|
||||
Git: context.GitInfo{
|
||||
CurrentTag: "v1.2.3",
|
||||
},
|
||||
}
|
||||
|
||||
_, err := For(ctx, buildtarget.New("darwin", "amd64", ""))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestNameDefaultTemplate(t *testing.T) {
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{
|
||||
Archive: config.Archive{
|
||||
NameTemplate: defaults.NameTemplate,
|
||||
},
|
||||
ProjectName: "test",
|
||||
},
|
||||
Version: "1.2.3",
|
||||
}
|
||||
for key, target := range map[string]buildtarget.Target{
|
||||
"test_1.2.3_darwin_amd64": buildtarget.New("darwin", "amd64", ""),
|
||||
"test_1.2.3_linux_arm64": buildtarget.New("linux", "arm64", ""),
|
||||
"test_1.2.3_linux_armv7": buildtarget.New("linux", "arm", "7"),
|
||||
} {
|
||||
t.Run(key, func(t *testing.T) {
|
||||
name, err := For(ctx, target)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, key, name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameForTitle(t *testing.T) {
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{
|
||||
Release: config.Release{
|
||||
NameTemplate: "{{.ProjectName}}-v{{.Version}}",
|
||||
},
|
||||
ProjectName: "test",
|
||||
},
|
||||
Version: "1.2.3",
|
||||
Git: context.GitInfo{
|
||||
CurrentTag: "v1.2.3",
|
||||
},
|
||||
}
|
||||
|
||||
name, err := ForTitle(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test-v1.2.3", name)
|
||||
}
|
@ -17,7 +17,6 @@ import (
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/buildtarget"
|
||||
"github.com/goreleaser/goreleaser/internal/ext"
|
||||
"github.com/goreleaser/goreleaser/internal/name"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
@ -158,14 +157,14 @@ func runHook(env []string, hook string) error {
|
||||
}
|
||||
|
||||
func doBuild(ctx *context.Context, build config.Build, target buildtarget.Target) error {
|
||||
folder, err := name.For(ctx, target)
|
||||
folder, err := nameFor(ctx, target, ctx.Config.ProjectName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var binaryName = build.Binary + ext.For(target)
|
||||
var prettyName = binaryName
|
||||
if ctx.Config.Archive.Format == "binary" {
|
||||
binaryName, err = name.ForBuild(ctx, build, target)
|
||||
binaryName, err = nameFor(ctx, target, build.Binary)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
37
pipeline/build/name.go
Normal file
37
pipeline/build/name.go
Normal file
@ -0,0 +1,37 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"github.com/goreleaser/goreleaser/internal/buildtarget"
|
||||
)
|
||||
|
||||
func nameFor(ctx *context.Context, target buildtarget.Target, name string) (string, error) {
|
||||
var out bytes.Buffer
|
||||
t, err := template.New(target.String()).Parse(ctx.Config.Archive.NameTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data := struct {
|
||||
Os, Arch, Arm, Version, Tag, ProjectName string
|
||||
}{
|
||||
Os: replace(ctx.Config.Archive.Replacements, target.OS),
|
||||
Arch: replace(ctx.Config.Archive.Replacements, target.Arch),
|
||||
Arm: replace(ctx.Config.Archive.Replacements, target.Arm),
|
||||
Version: ctx.Version,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
ProjectName: name,
|
||||
}
|
||||
err = t.Execute(&out, data)
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
func replace(replacements map[string]string, original string) string {
|
||||
result := replacements[original]
|
||||
if result == "" {
|
||||
return original
|
||||
}
|
||||
return result
|
||||
}
|
@ -24,7 +24,7 @@ func (Pipe) String() string {
|
||||
|
||||
// Run the pipe
|
||||
func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
filename, err := name.ForChecksums(ctx)
|
||||
filename, err := filenameFor(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
24
pipeline/checksums/name.go
Normal file
24
pipeline/checksums/name.go
Normal file
@ -0,0 +1,24 @@
|
||||
package checksums
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
)
|
||||
|
||||
func filenameFor(ctx *context.Context) (string, error) {
|
||||
var out bytes.Buffer
|
||||
t, err := template.New("github").Parse(ctx.Config.Release.NameTemplate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = t.Execute(&out, struct {
|
||||
ProjectName, Tag, Version string
|
||||
}{
|
||||
ProjectName: ctx.Config.ProjectName,
|
||||
Tag: ctx.Git.CurrentTag,
|
||||
Version: ctx.Version,
|
||||
})
|
||||
return out.String(), err
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user