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

feat: nfpm conventional file name (#2663)

This commit is contained in:
Carlos Alexandro Becker 2021-11-12 09:07:59 -03:00 committed by GitHub
parent 4330f449e8
commit df0216d585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 112 additions and 23 deletions

View File

@ -157,7 +157,7 @@ scoop:
license: MIT
nfpms:
- file_name_template: '{{ .ProjectName }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
- file_name_template: '{{ .ConventionalFileName }}'
id: packages
homepage: https://goreleaser.com
description: Deliver Go binaries as fast and easily as possible

View File

@ -120,55 +120,51 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
if err != nil {
return err
}
tmpl := tmpl.New(ctx).
t := tmpl.New(ctx).
WithArtifact(binaries[0], overridden.Replacements).
WithExtraFields(tmpl.Fields{
"Release": fpm.Release,
"Epoch": fpm.Epoch,
"PackageName": fpm.PackageName,
})
name, err := tmpl.Apply(overridden.FileNameTemplate)
binDir, err := t.Apply(fpm.Bindir)
if err != nil {
return err
}
binDir, err := tmpl.Apply(fpm.Bindir)
homepage, err := t.Apply(fpm.Homepage)
if err != nil {
return err
}
homepage, err := tmpl.Apply(fpm.Homepage)
description, err := t.Apply(fpm.Description)
if err != nil {
return err
}
description, err := tmpl.Apply(fpm.Description)
debKeyFile, err := t.Apply(overridden.Deb.Signature.KeyFile)
if err != nil {
return err
}
debKeyFile, err := tmpl.Apply(overridden.Deb.Signature.KeyFile)
rpmKeyFile, err := t.Apply(overridden.RPM.Signature.KeyFile)
if err != nil {
return err
}
rpmKeyFile, err := tmpl.Apply(overridden.RPM.Signature.KeyFile)
if err != nil {
return err
}
apkKeyFile, err := tmpl.Apply(overridden.APK.Signature.KeyFile)
apkKeyFile, err := t.Apply(overridden.APK.Signature.KeyFile)
if err != nil {
return err
}
contents := files.Contents{}
for _, content := range overridden.Contents {
src, err := tmpl.Apply(content.Source)
src, err := t.Apply(content.Source)
if err != nil {
return err
}
dst, err := tmpl.Apply(content.Destination)
dst, err := t.Apply(content.Destination)
if err != nil {
return err
}
@ -181,9 +177,10 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
})
}
log := log.WithField("package", fpm.PackageName).WithField("format", format).WithField("arch", arch)
// FPM meta package should not contain binaries at all
if !fpm.Meta {
log := log.WithField("package", name+"."+format).WithField("arch", arch)
for _, binary := range binaries {
src := binary.Path
dst := filepath.Join(binDir, binary.Name)
@ -295,14 +292,25 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
return err
}
path := filepath.Join(ctx.Config.Dist, name+"."+format)
info = nfpm.WithDefaults(info)
name, err := t.WithExtraFields(tmpl.Fields{
"ConventionalFileName": packager.ConventionalFileName(info),
}).Apply(overridden.FileNameTemplate)
if err != nil {
return err
}
if !strings.HasSuffix(name, "."+format) {
name = name + "." + format
}
path := filepath.Join(ctx.Config.Dist, name)
log.WithField("file", path).Info("creating")
w, err := os.Create(path)
if err != nil {
return err
}
defer w.Close()
if err := packager.Package(nfpm.WithDefaults(info), w); err != nil {
if err := packager.Package(info, w); err != nil {
return fmt.Errorf("nfpm failed: %w", err)
}
if err := w.Close(); err != nil {
@ -310,7 +318,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
}
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.LinuxPackage,
Name: name + "." + format,
Name: name,
Path: path,
Goos: binaries[0].Goos,
Goarch: binaries[0].Goarch,

View File

@ -167,8 +167,8 @@ func TestRunPipe(t *testing.T) {
for _, pkg := range packages {
format := pkg.Format()
require.NotEmpty(t, format)
require.Equal(t, pkg.Name, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
require.Equal(t, pkg.ID(), "someid")
require.Equal(t, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format, pkg.Name)
require.Equal(t, "someid", pkg.ID())
require.ElementsMatch(t, []string{
"./testdata/testfile.txt",
"./testdata/testfile.txt",
@ -189,6 +189,74 @@ func TestRunPipe(t *testing.T) {
require.Len(t, ctx.Config.NFPMs[0].Contents, 5, "should not modify the config file list")
}
func TestRunPipeConventionalNameTemplate(t *testing.T) {
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
binPath := filepath.Join(dist, "mybin", "mybin")
f, err := os.Create(binPath)
require.NoError(t, err)
require.NoError(t, f.Close())
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
{
ID: "someid",
Builds: []string{"default"},
Formats: []string{"deb", "rpm", "apk"},
Section: "somesection",
Priority: "standard",
Description: "Some description ",
License: "MIT",
Maintainer: "me@me",
Vendor: "asdf",
Homepage: "https://goreleaser.com/",
Bindir: "/usr/bin",
NFPMOverridables: config.NFPMOverridables{
FileNameTemplate: "{{ .ConventionalFileName }}",
PackageName: "foo",
},
},
},
})
ctx.Version = "1.0.0"
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
for _, goos := range []string{"linux", "darwin"} {
for _, goarch := range []string{"amd64", "386"} {
ctx.Artifacts.Add(&artifact.Artifact{
Name: "subdir/mybin",
Path: binPath,
Goarch: goarch,
Goos: goos,
Type: artifact.Binary,
Extra: map[string]interface{}{
artifact.ExtraID: "default",
},
})
}
}
require.NoError(t, Pipe{}.Run(ctx))
packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
require.Len(t, packages, 6)
for _, pkg := range packages {
format := pkg.Format()
require.NotEmpty(t, format)
require.Contains(t, []string{
"foo_1.0.0_amd64.deb",
"foo_1.0.0_i386.apk",
"foo_1.0.0_i386.deb",
"foo_1.0.0_x86_64.apk",
"foo-1.0.0.i386.rpm",
"foo-1.0.0.x86_64.rpm",
}, pkg.Name, "package name is not expected")
require.Equal(t, "someid", pkg.ID())
require.ElementsMatch(t, []string{binPath}, sources(pkg.ExtraOr(extraFiles, files.Contents{}).(files.Contents)))
require.ElementsMatch(t, []string{"/usr/bin/subdir/mybin"}, destinations(pkg.ExtraOr(extraFiles, files.Contents{}).(files.Contents)))
}
}
func TestInvalidTemplate(t *testing.T) {
makeCtx := func() *context.Context {
ctx := &context.Context{
@ -196,6 +264,7 @@ func TestInvalidTemplate(t *testing.T) {
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
ProjectName: "test",
NFPMs: []config.NFPM{
{
Formats: []string{"deb"},
@ -218,9 +287,11 @@ func TestInvalidTemplate(t *testing.T) {
t.Run("filename_template", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.NFPMs[0].Meta = true
ctx.Config.NFPMs[0].NFPMOverridables = config.NFPMOverridables{
FileNameTemplate: "{{.Foo}",
}
require.NoError(t, Pipe{}.Default(ctx))
require.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`)
})

View File

@ -19,8 +19,9 @@ nfpms:
package_name: foo
# You can change the file name of the package.
#
# Default: `{{ .PackageName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}`
file_name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
file_name_template: "{{ .ConventionalFileName }}"
# Build IDs for the builds you want to create NFPM packages for.
# Defaults to all builds.

View File

@ -6,6 +6,8 @@ Those fields are often suffixed with `_template`, but sometimes they may not
be. The documentation of each section should be explicit about which fields
support templating.
## Common Fields
On fields that support templating, these fields are always available:
| Key | Description |
@ -43,6 +45,8 @@ On fields that support templating, these fields are always available:
[^3]: Will panic if not a semantic version.
[^4]: Composed from the current SCM's download URL and current tag. For instance, on GitHub, it'll be `https://github.com/{owner}/{repo}/releases/tag/{tag}`.
## Single-artifact extra fields
On fields that are related to a single artifact (e.g., the binary name), you
may have some extra fields:
@ -58,13 +62,18 @@ may have some extra fields:
[^5]: Might have been replaced by `archives.replacements`.
On the NFPM name template field, you can use those extra fields as well:
## nFPM extra fields
On the nFPM name template field, you can use those extra fields as well:
| Key | Description |
|----------------|------------------------------------------------------------|
| `.Release` | release from the nfpm config |
| `.Epoch` | epoch from the nfpm config |
| `.PackageName` | package the name. Same as `ProjectName` if not overridden. |
| `.ConventionalFileName` | conventional package file name as provided by nFPM |
## Functions
On all fields, you have these available functions: