From cdb61a56e1e5d87f8a02e603ae549890bcba2451 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 10:54:36 -0300 Subject: [PATCH 1/8] feat: deprecate replacements, add title tmpl Signed-off-by: Carlos A Becker --- internal/builders/golang/build.go | 4 +-- internal/builders/golang/build_test.go | 2 +- internal/exec/exec.go | 7 +++-- internal/http/http.go | 6 ++-- internal/pipe/archive/archive.go | 4 +-- internal/pipe/aur/aur.go | 2 +- internal/pipe/brew/brew.go | 2 +- internal/pipe/chocolatey/chocolatey.go | 4 +-- internal/pipe/krew/krew.go | 2 +- internal/pipe/nfpm/nfpm.go | 2 +- internal/pipe/sbom/sbom.go | 3 +- internal/pipe/scoop/scoop.go | 4 +-- internal/pipe/snapcraft/snapcraft.go | 2 +- internal/tmpl/tmpl.go | 24 ++++++++++++++-- internal/tmpl/tmpl_test.go | 4 +-- pkg/config/config.go | 6 ++-- www/docs/customization/templates.md | 1 + www/docs/deprecations.md | 39 ++++++++++++++++++++++++++ 18 files changed, 88 insertions(+), 30 deletions(-) diff --git a/internal/builders/golang/build.go b/internal/builders/golang/build.go index 0d3c6366d7c..227a6b6a911 100644 --- a/internal/builders/golang/build.go +++ b/internal/builders/golang/build.go @@ -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 } @@ -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 { diff --git a/internal/builders/golang/build_test.go b/internal/builders/golang/build_test.go index 40f63800edd..a18babe7800 100644 --- a/internal/builders/golang/build_test.go +++ b/internal/builders/golang/build_test.go @@ -889,7 +889,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") diff --git a/internal/exec/exec.go b/internal/exec/exec.go index 296a8ff1161..fefdf88aae2 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -155,6 +155,7 @@ 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 @@ -163,7 +164,7 @@ func resolveCommand(ctx *context.Context, publisher config.Publisher, artifact * dir := publisher.Dir if dir != "" { dir, err = tmpl.New(ctx). - WithArtifact(artifact, replacements). + WithArtifactReplacements(artifact, replacements). Apply(dir) if err != nil { return nil, err @@ -173,7 +174,7 @@ func resolveCommand(ctx *context.Context, publisher config.Publisher, artifact * cmd := publisher.Cmd if cmd != "" { cmd, err = tmpl.New(ctx). - WithArtifact(artifact, replacements). + WithArtifactReplacements(artifact, replacements). Apply(cmd) if err != nil { return nil, err @@ -188,7 +189,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). + WithArtifactReplacements(artifact, replacements). Apply(e) if err != nil { return nil, err diff --git a/internal/http/http.go b/internal/http/http.go index 148c81435e5..dc5133bb45e 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -386,10 +386,11 @@ 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 } return tmpl.New(ctx). - WithArtifact(artifact, replacements). + WithArtifactReplacements(artifact, replacements). Apply(upload.Target) } @@ -399,9 +400,10 @@ 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 } return tmpl.New(ctx). - WithArtifact(artifact, replacements). + WithArtifactReplacements(artifact, replacements). Apply(headerValue) } diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index 4b20472e638..cf554b56551 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -138,7 +138,7 @@ 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) + template := tmpl.New(ctx).WithArtifactReplacements(binaries[0], arch.Replacements) format := packageFormat(arch, binaries[0].Goos) return doCreate(ctx, arch, binaries, format, template) } @@ -245,7 +245,7 @@ func wrapFolder(a config.Archive) string { func skip(ctx *context.Context, archive config.Archive, binaries []*artifact.Artifact) error { for _, binary := range binaries { name, err := tmpl.New(ctx). - WithArtifact(binary, archive.Replacements). + WithArtifactReplacements(binary, archive.Replacements). Apply(archive.NameTemplate) if err != nil { return err diff --git a/internal/pipe/aur/aur.go b/internal/pipe/aur/aur.go index d8bb6a5be9e..93f2ca18367 100644 --- a/internal/pipe/aur/aur.go +++ b/internal/pipe/aur/aur.go @@ -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 } diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go index 85b497a585f..5001af1cf2a 100644 --- a/internal/pipe/brew/brew.go +++ b/internal/pipe/brew/brew.go @@ -346,7 +346,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 } diff --git a/internal/pipe/chocolatey/chocolatey.go b/internal/pipe/chocolatey/chocolatey.go index bf0a47cce15..f7c57bb2326 100644 --- a/internal/pipe/chocolatey/chocolatey.go +++ b/internal/pipe/chocolatey/chocolatey.go @@ -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 } diff --git a/internal/pipe/krew/krew.go b/internal/pipe/krew/krew.go index 5bab918eb0b..f9183181793 100644 --- a/internal/pipe/krew/krew.go +++ b/internal/pipe/krew/krew.go @@ -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 } diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go index 186c728fea2..f9feac73bb3 100644 --- a/internal/pipe/nfpm/nfpm.go +++ b/internal/pipe/nfpm/nfpm.go @@ -167,7 +167,7 @@ func create(ctx *context.Context, fpm config.NFPM, format string, binaries []*ar return err } t := tmpl.New(ctx). - WithArtifact(binaries[0], overridden.Replacements). + WithArtifactReplacements(binaries[0], overridden.Replacements). WithExtraFields(tmpl.Fields{ "Release": fpm.Release, "Epoch": fpm.Epoch, diff --git a/internal/pipe/sbom/sbom.go b/internal/pipe/sbom/sbom.go index 6784baf9025..9e7f30da52d 100644 --- a/internal/pipe/sbom/sbom.go +++ b/internal/pipe/sbom/sbom.go @@ -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 { diff --git a/internal/pipe/scoop/scoop.go b/internal/pipe/scoop/scoop.go index 912f3dd8f90..79747c06296 100644 --- a/internal/pipe/scoop/scoop.go +++ b/internal/pipe/scoop/scoop.go @@ -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 } diff --git a/internal/pipe/snapcraft/snapcraft.go b/internal/pipe/snapcraft/snapcraft.go index be6e54cdf86..2e4fc371eab 100644 --- a/internal/pipe/snapcraft/snapcraft.go +++ b/internal/pipe/snapcraft/snapcraft.go @@ -212,7 +212,7 @@ 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) folder, err := tmpl.New(ctx). - WithArtifact(binaries[0], snap.Replacements). + WithArtifactReplacements(binaries[0], snap.Replacements). Apply(snap.NameTemplate) if err != nil { return err diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go index 84c771d9ce9..30837a673e2 100644 --- a/internal/tmpl/tmpl.go +++ b/internal/tmpl/tmpl.go @@ -14,6 +14,8 @@ import ( "github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/pkg/build" "github.com/goreleaser/goreleaser/pkg/context" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) // Template holds data that can be applied to a template string. @@ -138,8 +140,10 @@ func (t *Template) WithExtraFields(f Fields) *Template { return t } -// WithArtifact populates Fields from the artifact and replacements. -func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]string) *Template { +// WithArtifactReplacements populates Fields from the artifact and replacements. +// +// Deprecated: use WithArtifact instead. +func (t *Template) WithArtifactReplacements(a *artifact.Artifact, replacements map[string]string) *Template { t.fields[osKey] = replace(replacements, a.Goos) t.fields[arch] = replace(replacements, a.Goarch) t.fields[arm] = replace(replacements, a.Goarm) @@ -152,6 +156,20 @@ func (t *Template) WithArtifact(a *artifact.Artifact, replacements map[string]st return t } +// WithArtifact populates Fields from the artifact. +func (t *Template) WithArtifact(a *artifact.Artifact) *Template { + t.fields[osKey] = a.Goos + t.fields[arch] = a.Goarch + t.fields[arm] = a.Goarm + t.fields[mips] = a.Gomips + t.fields[amd64] = a.Goamd64 + t.fields[binary] = artifact.ExtraOr(*a, binary, t.fields[projectName].(string)) + t.fields[artifactName] = a.Name + t.fields[artifactExt] = artifact.ExtraOr(*a, artifact.ExtraExt, "") + t.fields[artifactPath] = a.Path + return t +} + func (t *Template) WithBuildOptions(opts build.Options) *Template { return t.WithExtraFields(buildOptsToFields(opts)) } @@ -185,6 +203,7 @@ func (t *Template) Apply(s string) (string, error) { "trim": strings.TrimSpace, "trimprefix": strings.TrimPrefix, "trimsuffix": strings.TrimSuffix, + "title": cases.Title(language.English).String, "dir": filepath.Dir, "abs": filepath.Abs, "incmajor": incMajor, @@ -237,6 +256,7 @@ func (t *Template) ApplySingleEnvOnly(s string) (string, error) { return out.String(), err } +// deprecated: will be removed soon. func replace(replacements map[string]string, original string) string { result := replacements[original] if result == "" { diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go index 13ba4d796b0..bab82a36d6c 100644 --- a/internal/tmpl/tmpl_test.go +++ b/internal/tmpl/tmpl_test.go @@ -80,7 +80,7 @@ func TestWithArtifact(t *testing.T) { expect := expect t.Run(expect, func(t *testing.T) { t.Parallel() - result, err := New(ctx).WithArtifact( + result, err := New(ctx).WithArtifactReplacements( &artifact.Artifact{ Name: "not-this-binary", Path: "/tmp/foo.exe", @@ -103,7 +103,7 @@ func TestWithArtifact(t *testing.T) { t.Run("artifact without binary name", func(t *testing.T) { t.Parallel() - result, err := New(ctx).WithArtifact( + result, err := New(ctx).WithArtifactReplacements( &artifact.Artifact{ Name: "another-binary", Goarch: "amd64", diff --git a/pkg/config/config.go b/pkg/config/config.go index 95ceaf8d32e..f745f82c87c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -476,7 +476,7 @@ type Archive struct { ID string `yaml:"id,omitempty" json:"id,omitempty"` Builds []string `yaml:"builds,omitempty" json:"builds,omitempty"` NameTemplate string `yaml:"name_template,omitempty" json:"name_template,omitempty"` - Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` + Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` // Deprecated: use templates instead Format string `yaml:"format,omitempty" json:"format,omitempty"` FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"` WrapInDirectory string `yaml:"wrap_in_directory,omitempty" json:"wrap_in_directory,omitempty" jsonschema:"oneof_type=string;boolean"` @@ -654,7 +654,7 @@ type NFPMOverridables struct { Release string `yaml:"release,omitempty" json:"release,omitempty"` Prerelease string `yaml:"prerelease,omitempty" json:"prerelease,omitempty"` VersionMetadata string `yaml:"version_metadata,omitempty" json:"version_metadata,omitempty"` - Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` + Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` // Deprecated: use templates instead Dependencies []string `yaml:"dependencies,omitempty" json:"dependencies,omitempty"` Recommends []string `yaml:"recommends,omitempty" json:"recommends,omitempty"` Suggests []string `yaml:"suggests,omitempty" json:"suggests,omitempty"` @@ -741,7 +741,7 @@ type SnapcraftLayoutMetadata struct { // Snapcraft config. type Snapcraft struct { NameTemplate string `yaml:"name_template,omitempty" json:"name_template,omitempty"` - Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` + Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` // Deprecated: use templates instead. Publish bool `yaml:"publish,omitempty" json:"publish,omitempty"` ID string `yaml:"id,omitempty" json:"id,omitempty"` diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md index ced3cd8de8d..d4f5cf70841 100644 --- a/www/docs/customization/templates.md +++ b/www/docs/customization/templates.md @@ -116,6 +116,7 @@ Usage |Description `abs .ArtifactPath` |returns an absolute representation of path. See [Abs](https://golang.org/pkg/path/filepath/#Abs) `filter "text" "regex"` |keeps only the lines matching the given regex, analogous to `grep -E`. Since v1.6. `reverseFilter "text" "regex"`|keeps only the lines **not** matching the given regex, analogous to `grep -vE`. Since v1.6. +`title "foo"` |"titlenize" the string using english as language, analagous to `cases.Title(language.English).String()`. Since v1.14. With all those fields, you may be able to compose the name of your artifacts pretty much the way you want: diff --git a/www/docs/deprecations.md b/www/docs/deprecations.md index 9c37e251a24..e428906ecef 100644 --- a/www/docs/deprecations.md +++ b/www/docs/deprecations.md @@ -36,6 +36,45 @@ Description. --> + +### archives.replacements + +> since 2022-11-24 (v1.14.0) + +The `replacements` will be removed soon from the archives section, as it was +never handled correctly when multiple archives were being used, and it also +causes confusion in other places. + +You can still get the same features by abusing the `name_template` property. + +=== "Before" + + ``` yaml + archives: + - id: foo + name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 + ``` + +=== "After" + ``` yaml + archives: + - id: foo + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + ``` + +Those two configurations will yield the same results. + ### nfpms.maintainer > since 2022-05-07 (v1.9.0) From 1e9fde4ab48e542c2cce84a763ee3bb49a801063 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 14:35:44 -0300 Subject: [PATCH 2/8] fix: deprecation warnings et al --- internal/pipe/archive/archive.go | 4 ++ internal/pipe/nfpm/nfpm.go | 3 + internal/pipe/snapcraft/snapcraft.go | 4 ++ www/docs/customization/archive.md | 10 --- www/docs/customization/nfpm.md | 10 --- www/docs/customization/snapcraft.md | 10 --- www/docs/customization/templates.md | 20 +++--- www/docs/deprecations.md | 96 +++++++++++++++++++++++++--- 8 files changed, 109 insertions(+), 48 deletions(-) diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index cf554b56551..cd8957b3ae0 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -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" @@ -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() diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go index f9feac73bb3..bfdbb19a6c5 100644 --- a/internal/pipe/nfpm/nfpm.go +++ b/internal/pipe/nfpm/nfpm.go @@ -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) } diff --git a/internal/pipe/snapcraft/snapcraft.go b/internal/pipe/snapcraft/snapcraft.go index 2e4fc371eab..b80314a62f3 100644 --- a/internal/pipe/snapcraft/snapcraft.go +++ b/internal/pipe/snapcraft/snapcraft.go @@ -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" @@ -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() diff --git a/www/docs/customization/archive.md b/www/docs/customization/archive.md index 6b6d4ca41b5..230d05be8cd 100644 --- a/www/docs/customization/archive.md +++ b/www/docs/customization/archive.md @@ -40,16 +40,6 @@ archives: # - `{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}` name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" - # Replacements for GOOS and GOARCH in the archive name. - # Keys should be valid GOOSs or GOARCHs. - # Values are the respective replacements. - # Default is empty. - replacements: - amd64: 64-bit - 386: 32-bit - darwin: macOS - linux: Tux - # Set this to true if you want all files in the archive to be in a single directory. # If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz', # you'll get a folder 'goreleaser_Linux_arm64'. diff --git a/www/docs/customization/nfpm.md b/www/docs/customization/nfpm.md index bff5201331c..d278ac933b2 100644 --- a/www/docs/customization/nfpm.md +++ b/www/docs/customization/nfpm.md @@ -29,16 +29,6 @@ nfpms: - foo - bar - # Replacements for GOOS and GOARCH in the package name. - # Keys should be valid GOOSs or GOARCHs. - # Values are the respective replacements. - # Default is empty. - replacements: - amd64: 64-bit - 386: 32-bit - darwin: macOS - linux: Tux - # Your app's vendor. # Default is empty. vendor: Drum Roll Inc. diff --git a/www/docs/customization/snapcraft.md b/www/docs/customization/snapcraft.md index 4abf0b0c4c0..c65303e2d0e 100644 --- a/www/docs/customization/snapcraft.md +++ b/www/docs/customization/snapcraft.md @@ -29,16 +29,6 @@ snapcrafts: # Default: `{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}` name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" - # Replacements for GOOS and GOARCH in the package name. - # Keys should be valid GOOSs or GOARCHs. - # Values are the respective replacements. - # Default is empty. - replacements: - amd64: 64-bit - 386: 32-bit - darwin: macOS - linux: Tux - # The name of the snap. This is optional. # Default is project name. name: drumroll diff --git a/www/docs/customization/templates.md b/www/docs/customization/templates.md index d4f5cf70841..a653c3cf282 100644 --- a/www/docs/customization/templates.md +++ b/www/docs/customization/templates.md @@ -104,19 +104,19 @@ On all fields, you have these available functions: Usage |Description ------------------------------|------------------------------------------------------------------------------------------------------------------------------ -`replace "v1.2" "v" ""` |replaces all matches. See [ReplaceAll](https://golang.org/pkg/strings/#ReplaceAll) +`replace "v1.2" "v" ""` |replaces all matches. See [ReplaceAll](https://golang.org/pkg/strings/#ReplaceAll). `split "1.2" "."` |split string at separator. See [Split](https://golang.org/pkg/strings/#Split). Since v1.11. -`time "01/02/2006"` |current UTC time in the specified format (this is not deterministic, a new time for every call) -`tolower "V1.2"` |makes input string lowercase. See [ToLower](https://golang.org/pkg/strings/#ToLower) -`toupper "v1.2"` |makes input string uppercase. See [ToUpper](https://golang.org/pkg/strings/#ToUpper) -`trim " v1.2 "` |removes all leading and trailing white space. See [TrimSpace](https://golang.org/pkg/strings/#TrimSpace) -`trimprefix "v1.2" "v"` |removes provided leading prefix string, if present. See [TrimPrefix](https://golang.org/pkg/strings/#TrimPrefix) -`trimsuffix "1.2v" "v"` |removes provided trailing suffix string, if present. See [TrimSuffix](https://pkg.go.dev/strings#TrimSuffix) -`dir .Path` |returns all but the last element of path, typically the path's directory. See [Dir](https://golang.org/pkg/path/filepath/#Dir) -`abs .ArtifactPath` |returns an absolute representation of path. See [Abs](https://golang.org/pkg/path/filepath/#Abs) +`time "01/02/2006"` |current UTC time in the specified format (this is not deterministic, a new time for every call). +`tolower "V1.2"` |makes input string lowercase. See [ToLower](https://golang.org/pkg/strings/#ToLower). +`toupper "v1.2"` |makes input string uppercase. See [ToUpper](https://golang.org/pkg/strings/#ToUpper). +`trim " v1.2 "` |removes all leading and trailing white space. See [TrimSpace](https://golang.org/pkg/strings/#TrimSpace). +`trimprefix "v1.2" "v"` |removes provided leading prefix string, if present. See [TrimPrefix](https://golang.org/pkg/strings/#TrimPrefix). +`trimsuffix "1.2v" "v"` |removes provided trailing suffix string, if present. See [TrimSuffix](https://pkg.go.dev/strings#TrimSuffix). +`dir .Path` |returns all but the last element of path, typically the path's directory. See [Dir](https://golang.org/pkg/path/filepath/#Dir). +`abs .ArtifactPath` |returns an absolute representation of path. See [Abs](https://golang.org/pkg/path/filepath/#Abs). `filter "text" "regex"` |keeps only the lines matching the given regex, analogous to `grep -E`. Since v1.6. `reverseFilter "text" "regex"`|keeps only the lines **not** matching the given regex, analogous to `grep -vE`. Since v1.6. -`title "foo"` |"titlenize" the string using english as language, analagous to `cases.Title(language.English).String()`. Since v1.14. +`title "foo"` |"titlenize" the string using english as language. See [Title](https://pkg.go.dev/golang.org/x/text/cases#Title). Since v1.14. With all those fields, you may be able to compose the name of your artifacts pretty much the way you want: diff --git a/www/docs/deprecations.md b/www/docs/deprecations.md index e428906ecef..6ddd12a64b7 100644 --- a/www/docs/deprecations.md +++ b/www/docs/deprecations.md @@ -36,7 +36,6 @@ Description. --> - ### archives.replacements > since 2022-11-24 (v1.14.0) @@ -52,13 +51,13 @@ You can still get the same features by abusing the `name_template` property. ``` yaml archives: - id: foo - 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 }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 ``` === "After" @@ -75,6 +74,87 @@ You can still get the same features by abusing the `name_template` property. Those two configurations will yield the same results. + +### nfpms.replacements + +> since 2022-11-24 (v1.14.0) + +The `replacements` will be removed soon from the nFPMs section. + +You can still get the same features by abusing the `file_name_template` property. + +=== "Before" + + ``` yaml + nfpms: + - id: foo + file_name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 + ``` + +=== "After" + ``` yaml + nfpms: + - id: foo + file_name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + ``` + +Those two configurations will yield the same results. + +Generally speaking, is probably best to use `{{ .ConventionalFileName }}` +instead of custom templates. + +### snapcrafts.replacements + +> since 2022-11-24 (v1.14.0) + +The `replacements` will be removed soon from the Snapcrafts section. + +You can still get the same features by abusing the `name_template` property. + +=== "Before" + + ``` yaml + snapcrafts: + - id: foo + name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 + ``` + +=== "After" + ``` yaml + snapcrafts: + - id: foo + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + ``` + +Those two configurations will yield the same results. + +Generally speaking, is probably best to use `{{ .ConventionalFileName }}` +instead of custom templates. + + + ### nfpms.maintainer > since 2022-05-07 (v1.9.0) From 6db4dbb70d64a956b9a2cfa2aeb52bf849993323 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 15:44:05 -0300 Subject: [PATCH 3/8] chore: ignore linters Signed-off-by: Carlos A Becker --- internal/exec/exec.go | 16 +++++++--------- internal/http/http.go | 3 +++ internal/pipe/archive/archive.go | 2 ++ internal/pipe/nfpm/nfpm.go | 1 + internal/pipe/snapcraft/snapcraft.go | 1 + 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/exec/exec.go b/internal/exec/exec.go index fefdf88aae2..30ab40cd63e 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -162,10 +162,12 @@ func resolveCommand(ctx *context.Context, publisher config.Publisher, artifact * } dir := publisher.Dir + + // nolint:staticcheck + tpl := tmpl.New(ctx). + WithArtifactReplacements(artifact, replacements) if dir != "" { - dir, err = tmpl.New(ctx). - WithArtifactReplacements(artifact, replacements). - Apply(dir) + dir, err = tpl.Apply(dir) if err != nil { return nil, err } @@ -173,9 +175,7 @@ func resolveCommand(ctx *context.Context, publisher config.Publisher, artifact * cmd := publisher.Cmd if cmd != "" { - cmd, err = tmpl.New(ctx). - WithArtifactReplacements(artifact, replacements). - Apply(cmd) + cmd, err = tpl.Apply(cmd) if err != nil { return nil, err } @@ -188,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). - WithArtifactReplacements(artifact, replacements). - Apply(e) + e, err = tpl.Apply(e) if err != nil { return nil, err } diff --git a/internal/http/http.go b/internal/http/http.go index dc5133bb45e..880217c2237 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -389,6 +389,8 @@ func resolveTargetTemplate(ctx *context.Context, upload *config.Upload, artifact // will be removed soon anyway replacements = ctx.Config.Archives[0].Replacements } + + // nolint:staticcheck return tmpl.New(ctx). WithArtifactReplacements(artifact, replacements). Apply(upload.Target) @@ -403,6 +405,7 @@ func resolveHeaderTemplate(ctx *context.Context, upload *config.Upload, artifact // will be removed soon anyway replacements = ctx.Config.Archives[0].Replacements } + // nolint:staticcheck return tmpl.New(ctx). WithArtifactReplacements(artifact, replacements). Apply(headerValue) diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go index cd8957b3ae0..efea0c78582 100644 --- a/internal/pipe/archive/archive.go +++ b/internal/pipe/archive/archive.go @@ -142,6 +142,7 @@ func createMeta(ctx *context.Context, arch config.Archive) error { } func create(ctx *context.Context, arch config.Archive, binaries []*artifact.Artifact) error { + // nolint:staticcheck template := tmpl.New(ctx).WithArtifactReplacements(binaries[0], arch.Replacements) format := packageFormat(arch, binaries[0].Goos) return doCreate(ctx, arch, binaries, format, template) @@ -248,6 +249,7 @@ 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). WithArtifactReplacements(binary, archive.Replacements). Apply(archive.NameTemplate) diff --git a/internal/pipe/nfpm/nfpm.go b/internal/pipe/nfpm/nfpm.go index bfdbb19a6c5..efd3767adf0 100644 --- a/internal/pipe/nfpm/nfpm.go +++ b/internal/pipe/nfpm/nfpm.go @@ -169,6 +169,7 @@ func create(ctx *context.Context, fpm config.NFPM, format string, binaries []*ar if err != nil { return err } + // nolint:staticcheck t := tmpl.New(ctx). WithArtifactReplacements(binaries[0], overridden.Replacements). WithExtraFields(tmpl.Fields{ diff --git a/internal/pipe/snapcraft/snapcraft.go b/internal/pipe/snapcraft/snapcraft.go index b80314a62f3..c45b205d77e 100644 --- a/internal/pipe/snapcraft/snapcraft.go +++ b/internal/pipe/snapcraft/snapcraft.go @@ -215,6 +215,7 @@ 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). WithArtifactReplacements(binaries[0], snap.Replacements). Apply(snap.NameTemplate) From b98b00e15c1089c170f2121b215c9613f791ae49 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 15:59:19 -0300 Subject: [PATCH 4/8] chore: dont use deprecated options in .goreleaser.yaml --- .goreleaser.yaml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d7e81b667ad..772436a4957 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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 From 5abb42e11dc7481373ac937927b2bd91a8385045 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 20:29:43 -0300 Subject: [PATCH 5/8] fix: move example config to file, use go:embed Signed-off-by: Carlos A Becker --- cmd/init.go | 2 +- internal/static/config.go | 42 ++++------------------------------ internal/static/config_test.go | 5 ++-- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index eeba3e54155..d7134a9de8a 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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 } diff --git a/internal/static/config.go b/internal/static/config.go index 7538744d117..ff1fc7b4f40 100644 --- a/internal/static/config.go +++ b/internal/static/config.go @@ -3,41 +3,9 @@ // strings in go code really. 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 diff --git a/internal/static/config_test.go b/internal/static/config_test.go index b54868badd4..65905c4af19 100644 --- a/internal/static/config_test.go +++ b/internal/static/config_test.go @@ -1,7 +1,7 @@ package static import ( - "strings" + "bytes" "testing" "github.com/goreleaser/goreleaser/pkg/config" @@ -9,6 +9,7 @@ import ( ) func TestExampleConfig(t *testing.T) { - _, err := config.LoadReader(strings.NewReader(ExampleConfig)) + _, err := config.LoadReader(bytes.NewReader(ExampleConfig)) require.NoError(t, err) + require.NotEmpty(t, ExampleConfig) } From 7f7dc1b7c2a6b8bbc5131f1db55d4fc03f0471e3 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 21:41:23 -0300 Subject: [PATCH 6/8] fix: forgot to add the file Signed-off-by: Carlos A Becker --- internal/static/config.yaml | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 internal/static/config.yaml diff --git a/internal/static/config.yaml b/internal/static/config.yaml new file mode 100644 index 00000000000..d24c44bd546 --- /dev/null +++ b/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 From 7582215b692e5b427cd24a5527479a5e32555ac0 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 21:41:46 -0300 Subject: [PATCH 7/8] docs: godo Signed-off-by: Carlos A Becker --- internal/static/config.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/static/config.go b/internal/static/config.go index ff1fc7b4f40..84672dbae2a 100644 --- a/internal/static/config.go +++ b/internal/static/config.go @@ -1,6 +1,4 @@ -// 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 import _ "embed" From 332f528c768798682e13c68acae260c5fc50b825 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Thu, 24 Nov 2022 23:36:52 -0300 Subject: [PATCH 8/8] test: fix --- cmd/testdata/good.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cmd/testdata/good.yml b/cmd/testdata/good.yml index ad179f8cc21..4641e27d570 100644 --- a/cmd/testdata/good.yml +++ b/cmd/testdata/good.yml @@ -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: