Skip to content

Commit

Permalink
feat: deprecate replacements (#3589)
Browse files Browse the repository at this point in the history
The replacements thing was always a bit weird, especially on archives.

We can solve that with templates, so, removing I'm deprecating it.

Also did the same on other places that had it the same feature.

Closes #3588

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 25, 2022
1 parent f83a942 commit e546564
Show file tree
Hide file tree
Showing 26 changed files with 247 additions and 123 deletions.
14 changes: 7 additions & 7 deletions .goreleaser.yaml
Expand Up @@ -129,13 +129,13 @@ docker_manifests:
- 'ghcr.io/goreleaser/goreleaser:{{ .Tag }}-arm64'

archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}'
format_overrides:
- goos: windows
format: zip
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Expand Up @@ -31,7 +31,7 @@ func newInitCmd() *initCmd {
defer conf.Close()

log.Infof(boldStyle.Render(fmt.Sprintf("Generating %s file", root.config)))
if _, err := conf.WriteString(static.ExampleConfig); err != nil {
if _, err := conf.Write(static.ExampleConfig); err != nil {
return err
}

Expand Down
7 changes: 0 additions & 7 deletions cmd/testdata/good.yml
Expand Up @@ -9,13 +9,6 @@ before:
builds:
- env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
4 changes: 2 additions & 2 deletions internal/builders/golang/build.go
Expand Up @@ -186,7 +186,7 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
}

if build.ModTimestamp != "" {
modTimestamp, err := tmpl.New(ctx).WithEnvS(env).WithArtifact(a, map[string]string{}).Apply(build.ModTimestamp)
modTimestamp, err := tmpl.New(ctx).WithEnvS(env).WithArtifact(a).Apply(build.ModTimestamp)
if err != nil {
return err
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func processFlags(ctx *context.Context, a *artifact.Artifact, env, flags []strin
}

func processFlag(ctx *context.Context, a *artifact.Artifact, env []string, rawFlag string) (string, error) {
return tmpl.New(ctx).WithEnvS(env).WithArtifact(a, map[string]string{}).Apply(rawFlag)
return tmpl.New(ctx).WithEnvS(env).WithArtifact(a).Apply(rawFlag)
}

func run(ctx *context.Context, command, env []string, dir string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/builders/golang/build_test.go
Expand Up @@ -898,7 +898,7 @@ func TestLdFlagsFullTemplate(t *testing.T) {
Env: map[string]string{"FOO": "123"},
}
artifact := &artifact.Artifact{Goarch: "amd64"}
flags, err := tmpl.New(ctx).WithArtifact(artifact, map[string]string{}).
flags, err := tmpl.New(ctx).WithArtifact(artifact).
Apply(`-s -w -X main.version={{.Version}} -X main.tag={{.Tag}} -X main.date={{.Date}} -X main.commit={{.Commit}} -X "main.foo={{.Env.FOO}}" -X main.time={{ time "20060102" }} -X main.arch={{.Arch}} -X main.commitDate={{.CommitDate}}`)
require.NoError(t, err)
require.Contains(t, flags, "-s -w")
Expand Down
17 changes: 8 additions & 9 deletions internal/exec/exec.go
Expand Up @@ -155,26 +155,27 @@ func resolveCommand(ctx *context.Context, publisher config.Publisher, artifact *

replacements := make(map[string]string)
// TODO: Replacements should be associated only with relevant artifacts/archives
// this is pretty much all wrong and will be removed soon.
archives := ctx.Config.Archives
if len(archives) > 0 {
replacements = archives[0].Replacements
}

dir := publisher.Dir

// nolint:staticcheck
tpl := tmpl.New(ctx).
WithArtifactReplacements(artifact, replacements)
if dir != "" {
dir, err = tmpl.New(ctx).
WithArtifact(artifact, replacements).
Apply(dir)
dir, err = tpl.Apply(dir)
if err != nil {
return nil, err
}
}

cmd := publisher.Cmd
if cmd != "" {
cmd, err = tmpl.New(ctx).
WithArtifact(artifact, replacements).
Apply(cmd)
cmd, err = tpl.Apply(cmd)
if err != nil {
return nil, err
}
Expand All @@ -187,9 +188,7 @@ func resolveCommand(ctx *context.Context, publisher config.Publisher, artifact *

env := make([]string, len(publisher.Env))
for i, e := range publisher.Env {
e, err = tmpl.New(ctx).
WithArtifact(artifact, replacements).
Apply(e)
e, err = tpl.Apply(e)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/http/http.go
Expand Up @@ -386,10 +386,13 @@ func resolveTargetTemplate(ctx *context.Context, upload *config.Upload, artifact
replacements := map[string]string{}
if upload.Mode == ModeBinary {
// TODO: multiple archives here
// will be removed soon anyway
replacements = ctx.Config.Archives[0].Replacements
}

// nolint:staticcheck
return tmpl.New(ctx).
WithArtifact(artifact, replacements).
WithArtifactReplacements(artifact, replacements).
Apply(upload.Target)
}

Expand All @@ -399,9 +402,11 @@ func resolveHeaderTemplate(ctx *context.Context, upload *config.Upload, artifact
replacements := map[string]string{}
if upload.Mode == ModeBinary {
// TODO: multiple archives here
// will be removed soon anyway
replacements = ctx.Config.Archives[0].Replacements
}
// nolint:staticcheck
return tmpl.New(ctx).
WithArtifact(artifact, replacements).
WithArtifactReplacements(artifact, replacements).
Apply(headerValue)
}
10 changes: 8 additions & 2 deletions internal/pipe/archive/archive.go
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/archivefiles"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
Expand Down Expand Up @@ -74,6 +75,9 @@ func (Pipe) Default(ctx *context.Context) error {
archive.NameTemplate = defaultBinaryNameTemplate
}
}
if len(archive.Replacements) != 0 {
deprecate.Notice(ctx, "archives.replacements")
}
ids.Inc(archive.ID)
}
return ids.Validate()
Expand Down Expand Up @@ -138,7 +142,8 @@ func createMeta(ctx *context.Context, arch config.Archive) error {
}

func create(ctx *context.Context, arch config.Archive, binaries []*artifact.Artifact) error {
template := tmpl.New(ctx).WithArtifact(binaries[0], arch.Replacements)
// nolint:staticcheck
template := tmpl.New(ctx).WithArtifactReplacements(binaries[0], arch.Replacements)
format := packageFormat(arch, binaries[0].Goos)
return doCreate(ctx, arch, binaries, format, template)
}
Expand Down Expand Up @@ -244,8 +249,9 @@ func wrapFolder(a config.Archive) string {

func skip(ctx *context.Context, archive config.Archive, binaries []*artifact.Artifact) error {
for _, binary := range binaries {
// nolint:staticcheck
name, err := tmpl.New(ctx).
WithArtifact(binary, archive.Replacements).
WithArtifactReplacements(binary, archive.Replacements).
Apply(archive.NameTemplate)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/aur/aur.go
Expand Up @@ -311,7 +311,7 @@ func dataFor(ctx *context.Context, cfg config.AUR, cl client.Client, artifacts [
}
cfg.URLTemplate = url
}
url, err := tmpl.New(ctx).WithArtifact(art, map[string]string{}).Apply(cfg.URLTemplate)
url, err := tmpl.New(ctx).WithArtifact(art).Apply(cfg.URLTemplate)
if err != nil {
return result, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/brew/brew.go
Expand Up @@ -347,7 +347,7 @@ func dataFor(ctx *context.Context, cfg config.Homebrew, cl client.Client, artifa
cfg.URLTemplate = url
}

url, err := tmpl.New(ctx).WithArtifact(art, map[string]string{}).Apply(cfg.URLTemplate)
url, err := tmpl.New(ctx).WithArtifact(art).Apply(cfg.URLTemplate)
if err != nil {
return result, err
}
Expand Down
4 changes: 1 addition & 3 deletions internal/pipe/chocolatey/chocolatey.go
Expand Up @@ -302,9 +302,7 @@ func dataFor(ctx *context.Context, cl client.Client, choco config.Chocolatey, ar
return result, err
}

url, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(choco.URLTemplate)
url, err := tmpl.New(ctx).WithArtifact(artifact).Apply(choco.URLTemplate)
if err != nil {
return result, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/krew/krew.go
Expand Up @@ -222,7 +222,7 @@ func manifestFor(ctx *context.Context, cfg config.Krew, cl client.Client, artifa
}
cfg.URLTemplate = url
}
url, err := tmpl.New(ctx).WithArtifact(art, map[string]string{}).Apply(cfg.URLTemplate)
url, err := tmpl.New(ctx).WithArtifact(art).Apply(cfg.URLTemplate)
if err != nil {
return result, err
}
Expand Down
6 changes: 5 additions & 1 deletion internal/pipe/nfpm/nfpm.go
Expand Up @@ -59,6 +59,9 @@ func (Pipe) Default(ctx *context.Context) error {
if fpm.Maintainer == "" {
deprecate.NoticeCustom(ctx, "nfpms.maintainer", "`{{ .Property }}` should always be set, check {{ .URL }} for more info")
}
if len(fpm.Replacements) != 0 {
deprecate.Notice(ctx, "nfpms.replacements")
}
ids.Inc(fpm.ID)
}

Expand Down Expand Up @@ -166,8 +169,9 @@ func create(ctx *context.Context, fpm config.NFPM, format string, binaries []*ar
if err != nil {
return err
}
// nolint:staticcheck
t := tmpl.New(ctx).
WithArtifact(binaries[0], overridden.Replacements).
WithArtifactReplacements(binaries[0], overridden.Replacements).
WithExtraFields(tmpl.Fields{
"Release": fpm.Release,
"Epoch": fpm.Epoch,
Expand Down
3 changes: 1 addition & 2 deletions internal/pipe/sbom/sbom.go
Expand Up @@ -238,8 +238,7 @@ func applyTemplate(ctx *context.Context, cfg config.SBOM, a *artifact.Artifact)
}
extraEnvs = appendExtraEnv("artifact", procPath, extraEnvs, env)
extraEnvs = appendExtraEnv("artifactID", a.ID(), extraEnvs, env)

templater = templater.WithArtifact(a, nil)
templater = templater.WithArtifact(a)
}

for _, keyValue := range cfg.Env {
Expand Down
4 changes: 1 addition & 3 deletions internal/pipe/scoop/scoop.go
Expand Up @@ -241,9 +241,7 @@ func dataFor(ctx *context.Context, cl client.Client, artifacts []*artifact.Artif
continue
}

url, err := tmpl.New(ctx).
WithArtifact(artifact, map[string]string{}).
Apply(ctx.Config.Scoop.URLTemplate)
url, err := tmpl.New(ctx).WithArtifact(artifact).Apply(ctx.Config.Scoop.URLTemplate)
if err != nil {
return manifest, err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/pipe/snapcraft/snapcraft.go
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/deprecate"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/pipe"
Expand Down Expand Up @@ -122,6 +123,9 @@ func (Pipe) Default(ctx *context.Context) error {
snap.Builds = append(snap.Builds, b.ID)
}
}
if len(snap.Replacements) != 0 {
deprecate.Notice(ctx, "snapcrafts.replacements")
}
ids.Inc(snap.ID)
}
return ids.Validate()
Expand Down Expand Up @@ -211,8 +215,9 @@ func (Pipe) Publish(ctx *context.Context) error {

func create(ctx *context.Context, snap config.Snapcraft, arch string, binaries []*artifact.Artifact) error {
log := log.WithField("arch", arch)
// nolint:staticcheck
folder, err := tmpl.New(ctx).
WithArtifact(binaries[0], snap.Replacements).
WithArtifactReplacements(binaries[0], snap.Replacements).
Apply(snap.NameTemplate)
if err != nil {
return err
Expand Down
46 changes: 6 additions & 40 deletions internal/static/config.go
@@ -1,43 +1,9 @@
// Package static contains static "text" files, just because embedding real
// static files would be kind of an overengineering right now, so it's just
// strings in go code really.
// Package static contains static "text" files.
package static

// ExampleConfig is the config used within goreleaser init.
const ExampleConfig = `# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
import _ "embed"

# modelines, feel free to remove those if you don't want/use them:
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
`
// ExampleConfig is the config used within goreleaser init.
//
//go:embed config.yaml
var ExampleConfig []byte
44 changes: 44 additions & 0 deletions internal/static/config.yaml
@@ -0,0 +1,44 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}'
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# modelines, feel free to remove those if you don't want/use them:
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

0 comments on commit e546564

Please sign in to comment.