You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-11-06 09:09:29 +02:00
feat: nfpm conventional file name (#2663)
This commit is contained in:
committed by
GitHub
parent
4330f449e8
commit
df0216d585
@@ -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,
|
||||
|
||||
@@ -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`)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user