2018-02-17 16:16:06 +02:00
|
|
|
// Package nfpm implements the Pipe interface providing NFPM bindings.
|
|
|
|
package nfpm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/goreleaser/nfpm"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
// blank imports here because the formats implementations need register
|
|
|
|
// themselves
|
|
|
|
_ "github.com/goreleaser/nfpm/deb"
|
|
|
|
_ "github.com/goreleaser/nfpm/rpm"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/filenametemplate"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/linux"
|
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
|
|
|
|
|
|
|
|
// Pipe for fpm packaging
|
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func (Pipe) String() string {
|
|
|
|
return "creating Linux packages with nfpm"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
var fpm = &ctx.Config.NFPM
|
|
|
|
if fpm.Bindir == "" {
|
|
|
|
fpm.Bindir = "/usr/local/bin"
|
|
|
|
}
|
|
|
|
if fpm.NameTemplate == "" {
|
|
|
|
fpm.NameTemplate = defaultNameTemplate
|
|
|
|
}
|
|
|
|
if fpm.Files == nil {
|
|
|
|
fpm.Files = make(map[string]string)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pipe
|
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
|
|
|
if len(ctx.Config.NFPM.Formats) == 0 {
|
|
|
|
return pipeline.Skip("no output formats configured")
|
|
|
|
}
|
|
|
|
return doRun(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doRun(ctx *context.Context) error {
|
2018-02-17 18:28:48 +02:00
|
|
|
var linuxBinaries = ctx.Artifacts.Filter(artifact.And(
|
|
|
|
artifact.ByType(artifact.Binary),
|
|
|
|
artifact.ByGoos("linux"),
|
|
|
|
)).GroupByPlatform()
|
2018-02-17 16:16:06 +02:00
|
|
|
var g errgroup.Group
|
|
|
|
sem := make(chan bool, ctx.Parallelism)
|
|
|
|
for _, format := range ctx.Config.NFPM.Formats {
|
2018-02-17 18:28:48 +02:00
|
|
|
for platform, artifacts := range linuxBinaries {
|
2018-02-17 16:16:06 +02:00
|
|
|
sem <- true
|
|
|
|
format := format
|
|
|
|
arch := linux.Arch(platform)
|
|
|
|
artifacts := artifacts
|
|
|
|
g.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
|
|
|
return create(ctx, format, arch, artifacts)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func create(ctx *context.Context, format, arch string, binaries []artifact.Artifact) error {
|
|
|
|
name, err := filenametemplate.Apply(
|
|
|
|
ctx.Config.NFPM.NameTemplate,
|
|
|
|
filenametemplate.NewFields(ctx, ctx.Config.NFPM.Replacements, binaries...),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-17 20:51:18 +02:00
|
|
|
var files = map[string]string{}
|
|
|
|
for k, v := range ctx.Config.NFPM.Files {
|
|
|
|
files[k] = v
|
|
|
|
}
|
|
|
|
var log = log.WithField("package", name+"."+format)
|
|
|
|
for _, binary := range binaries {
|
|
|
|
src := binary.Path
|
|
|
|
dst := filepath.Join(ctx.Config.NFPM.Bindir, binary.Name)
|
|
|
|
log.WithField("src", src).WithField("dst", dst).Debug("adding binary to package")
|
|
|
|
files[src] = dst
|
|
|
|
}
|
|
|
|
log.WithField("files", files).Debug("all archive files")
|
2018-02-17 16:16:06 +02:00
|
|
|
|
|
|
|
var info = nfpm.Info{
|
|
|
|
Arch: arch,
|
|
|
|
Platform: "linux",
|
|
|
|
Conflicts: ctx.Config.NFPM.Conflicts,
|
|
|
|
Depends: ctx.Config.NFPM.Dependencies,
|
2018-02-19 00:36:52 +02:00
|
|
|
Recommends: ctx.Config.NFPM.Recommends,
|
|
|
|
Suggests: ctx.Config.NFPM.Suggests,
|
2018-02-17 16:16:06 +02:00
|
|
|
Name: ctx.Config.ProjectName,
|
2018-02-24 22:59:08 +02:00
|
|
|
Version: ctx.Git.CurrentTag,
|
2018-02-17 16:16:06 +02:00
|
|
|
Section: "",
|
|
|
|
Priority: "",
|
|
|
|
Maintainer: ctx.Config.NFPM.Maintainer,
|
|
|
|
Description: ctx.Config.NFPM.Description,
|
|
|
|
Vendor: ctx.Config.NFPM.Vendor,
|
|
|
|
Homepage: ctx.Config.NFPM.Homepage,
|
|
|
|
License: ctx.Config.NFPM.License,
|
|
|
|
Bindir: ctx.Config.NFPM.Bindir,
|
2018-02-17 20:51:18 +02:00
|
|
|
Files: files,
|
2018-02-17 23:30:26 +02:00
|
|
|
ConfigFiles: ctx.Config.NFPM.ConfigFiles,
|
2018-02-17 16:16:06 +02:00
|
|
|
}
|
2018-02-17 18:28:48 +02:00
|
|
|
|
2018-04-06 02:34:25 +02:00
|
|
|
if err = nfpm.Validate(info); err != nil {
|
2018-04-06 01:41:12 +02:00
|
|
|
return errors.Wrap(err, "invalid nfpm config")
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:16:06 +02:00
|
|
|
packager, err := nfpm.Get(format)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var path = filepath.Join(ctx.Config.Dist, name+"."+format)
|
|
|
|
log.WithField("file", path).Info("creating")
|
|
|
|
w, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-17 18:28:48 +02:00
|
|
|
defer w.Close() // nolint: errcheck
|
2018-02-17 16:16:06 +02:00
|
|
|
if err := packager.Package(nfpm.WithDefaults(info), w); err != nil {
|
|
|
|
return errors.Wrap(err, "nfpm failed")
|
|
|
|
}
|
2018-02-17 18:28:48 +02:00
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
return errors.Wrap(err, "could not close package file")
|
|
|
|
}
|
2018-02-17 16:16:06 +02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.LinuxPackage,
|
|
|
|
Name: name + "." + format,
|
|
|
|
Path: path,
|
|
|
|
Goos: binaries[0].Goos,
|
|
|
|
Goarch: binaries[0].Goarch,
|
|
|
|
Goarm: binaries[0].Goarm,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|