1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-18 03:56:52 +02:00

feat: package name on nfpm file name template (#2097)

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2021-03-09 07:57:43 -03:00 committed by GitHub
parent d01af30b5c
commit c9ffa78c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 77 deletions

View File

@ -26,7 +26,7 @@ import (
"github.com/goreleaser/goreleaser/pkg/context"
)
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}"
const defaultNameTemplate = "{{ .PackageName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}"
// Pipe for nfpm packaging.
type Pipe struct{}
@ -37,9 +37,9 @@ func (Pipe) String() string {
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
var ids = ids.New("nfpms")
ids := ids.New("nfpms")
for i := range ctx.Config.NFPMs {
var fpm = &ctx.Config.NFPMs[i]
fpm := &ctx.Config.NFPMs[i]
if fpm.ID == "" {
fpm.ID = "default"
}
@ -132,7 +132,7 @@ func (Pipe) Run(ctx *context.Context) error {
}
func doRun(ctx *context.Context, fpm config.NFPM) error {
var linuxBinaries = ctx.Artifacts.Filter(artifact.And(
linuxBinaries := ctx.Artifacts.Filter(artifact.And(
artifact.ByType(artifact.Binary),
artifact.ByGoos("linux"),
artifact.ByIDs(fpm.Builds...),
@ -140,7 +140,7 @@ func doRun(ctx *context.Context, fpm config.NFPM) error {
if len(linuxBinaries) == 0 {
return fmt.Errorf("no linux binaries found for builds %v", fpm.Builds)
}
var g = semerrgroup.New(ctx.Parallelism)
g := semerrgroup.New(ctx.Parallelism)
for _, format := range fpm.Formats {
for platform, artifacts := range linuxBinaries {
format := format
@ -177,19 +177,20 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
name, err := tmpl.New(ctx).
WithArtifact(binaries[0], overridden.Replacements).
WithExtraFields(tmpl.Fields{
"Release": fpm.Release,
"Epoch": fpm.Epoch,
"Release": fpm.Release,
"Epoch": fpm.Epoch,
"PackageName": fpm.PackageName,
}).
Apply(overridden.FileNameTemplate)
if err != nil {
return err
}
var contents = append(files.Contents{}, overridden.Contents...)
contents := append(files.Contents{}, overridden.Contents...)
// FPM meta package should not contain binaries at all
if !fpm.Meta {
var log = log.WithField("package", name+"."+format).WithField("arch", arch)
log := log.WithField("package", name+"."+format).WithField("arch", arch)
for _, binary := range binaries {
src := binary.Path
dst := filepath.Join(fpm.Bindir, binary.Name)
@ -203,7 +204,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
log.WithField("files", destinations(contents)).Debug("all archive files")
var info = &nfpm.Info{
info := &nfpm.Info{
Arch: arch,
Platform: "linux",
Name: fpm.PackageName,
@ -293,7 +294,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
return err
}
var path = filepath.Join(ctx.Config.Dist, name+"."+format)
path := filepath.Join(ctx.Config.Dist, name+"."+format)
log.WithField("file", path).Info("creating")
w, err := os.Create(path)
if err != nil {
@ -324,7 +325,7 @@ func create(ctx *context.Context, fpm config.NFPM, format, arch string, binaries
}
func destinations(contents files.Contents) []string {
var result = make([]string, 0, len(contents))
result := make([]string, 0, len(contents))
for _, f := range contents {
result = append(result, f.Destination)
}

View File

@ -20,7 +20,7 @@ func TestDescription(t *testing.T) {
}
func TestRunPipeNoFormats(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Version: "1.0.0",
Git: context.GitInfo{
CurrentTag: "v1.0.0",
@ -37,7 +37,7 @@ func TestRunPipeNoFormats(t *testing.T) {
}
func TestRunPipeInvalidFormat(t *testing.T) {
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "nope",
NFPMs: []config.NFPM{
{
@ -73,14 +73,14 @@ func TestRunPipeInvalidFormat(t *testing.T) {
}
func TestRunPipe(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
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")
_, err := os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@ -153,12 +153,12 @@ func TestRunPipe(t *testing.T) {
}
}
require.NoError(t, Pipe{}.Run(ctx))
var packages = ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
require.Len(t, packages, 6)
for _, pkg := range packages {
var format = pkg.ExtraOr("Format", "").(string)
format := pkg.ExtraOr("Format", "").(string)
require.NotEmpty(t, format)
require.Equal(t, pkg.Name, "mybin_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
require.Equal(t, pkg.Name, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
require.Equal(t, pkg.ExtraOr("ID", ""), "someid")
require.ElementsMatch(t, []string{
"/usr/share/testfile.txt",
@ -172,7 +172,7 @@ func TestRunPipe(t *testing.T) {
}
func TestInvalidNameTemplate(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
@ -201,7 +201,7 @@ func TestInvalidNameTemplate(t *testing.T) {
}
func TestNoBuildsFound(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Parallelism: runtime.NumCPU(),
Artifacts: artifact.New(),
Config: config.Project{
@ -226,11 +226,11 @@ func TestNoBuildsFound(t *testing.T) {
}
func TestCreateFileDoesntExist(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var ctx = context.New(config.Project{
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))
ctx := context.New(config.Project{
Dist: dist,
ProjectName: "asd",
NFPMs: []config.NFPM{
@ -267,11 +267,11 @@ func TestCreateFileDoesntExist(t *testing.T) {
}
func TestInvalidConfig(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var ctx = context.New(config.Project{
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))
ctx := context.New(config.Project{
Dist: dist,
NFPMs: []config.NFPM{
{
@ -296,7 +296,7 @@ func TestInvalidConfig(t *testing.T) {
}
func TestDefault(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Config: config.Project{
ProjectName: "foobar",
NFPMs: []config.NFPM{
@ -316,7 +316,7 @@ func TestDefault(t *testing.T) {
}
func TestDefaultDeprecatedOptions(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Config: config.Project{
ProjectName: "foobar",
NFPMs: []config.NFPM{
@ -365,7 +365,7 @@ func TestDefaultDeprecatedOptions(t *testing.T) {
}
func TestDefaultSet(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Config: config.Project{
Builds: []config.Build{
{ID: "foo"},
@ -389,7 +389,7 @@ func TestDefaultSet(t *testing.T) {
}
func TestOverrides(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Config: config.Project{
NFPMs: []config.NFPM{
{
@ -416,14 +416,14 @@ func TestOverrides(t *testing.T) {
}
func TestDebSpecificConfig(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
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")
_, err := os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@ -489,14 +489,14 @@ func TestDebSpecificConfig(t *testing.T) {
}
func TestRPMSpecificConfig(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
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")
_, err := os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@ -562,14 +562,14 @@ func TestRPMSpecificConfig(t *testing.T) {
}
func TestAPKSpecificConfig(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
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")
_, err := os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@ -636,7 +636,7 @@ func TestAPKSpecificConfig(t *testing.T) {
}
func TestSeveralNFPMsWithTheSameID(t *testing.T) {
var ctx = &context.Context{
ctx := &context.Context{
Config: config.Project{
NFPMs: []config.NFPM{
{
@ -652,14 +652,14 @@ func TestSeveralNFPMsWithTheSameID(t *testing.T) {
}
func TestMeta(t *testing.T) {
var folder = t.TempDir()
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
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")
_, err := os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{
@ -728,12 +728,12 @@ func TestMeta(t *testing.T) {
}
}
require.NoError(t, Pipe{}.Run(ctx))
var packages = ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
packages := ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List()
require.Len(t, packages, 4)
for _, pkg := range packages {
var format = pkg.ExtraOr("Format", "").(string)
format := pkg.ExtraOr("Format", "").(string)
require.NotEmpty(t, format)
require.Equal(t, pkg.Name, "mybin_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
require.Equal(t, pkg.Name, "foo_1.0.0_Tux_"+pkg.Goarch+"-10-20."+format)
require.Equal(t, pkg.ExtraOr("ID", ""), "someid")
require.ElementsMatch(t, []string{
"/usr/share/testfile.txt",
@ -748,13 +748,13 @@ func TestMeta(t *testing.T) {
func TestSkipSign(t *testing.T) {
folder, err := ioutil.TempDir("", "archivetest")
require.NoError(t, err)
var dist = filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0755))
require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
var binPath = filepath.Join(dist, "mybin", "mybin")
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")
_, err = os.Create(binPath)
require.NoError(t, err)
var ctx = context.New(config.Project{
ctx := context.New(config.Project{
ProjectName: "mybin",
Dist: dist,
NFPMs: []config.NFPM{

View File

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

View File

@ -47,10 +47,11 @@ may have some 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 |
| Key | Description |
|----------------|------------------------------------------------------------|
| `.Release` | Release from the nfpm config |
| `.Epoch` | Epoch from the nfpm config |
| `.PackageName` | Package the name. Same as `ProjectName` if not overridden. |
On all fields, you have these available functions: