1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

152 lines
4.1 KiB
Go
Raw Normal View History

2017-04-14 15:39:32 -03:00
// Package archive implements the pipe interface with the intent of
// archiving and compressing the binaries, readme, and other artifacts. It
// also provides an Archive interface which represents an archiving format.
package archive
2016-12-28 22:23:39 -02:00
2016-12-28 22:53:56 -02:00
import (
"fmt"
2016-12-29 18:10:11 -02:00
"os"
2017-07-13 20:22:10 -03:00
"path/filepath"
2017-12-17 18:01:58 -02:00
"strings"
2016-12-29 18:10:11 -02:00
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
2017-12-17 15:50:09 -02:00
"github.com/mattn/go-zglob"
"golang.org/x/sync/errgroup"
2017-06-18 18:30:39 -03:00
"github.com/goreleaser/archive"
2017-01-14 20:01:32 -02:00
"github.com/goreleaser/goreleaser/context"
2017-12-17 15:50:09 -02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2017-12-17 17:11:08 -02:00
"github.com/goreleaser/goreleaser/internal/nametemplate"
2016-12-28 22:53:56 -02:00
)
2016-12-28 22:23:39 -02:00
2017-12-18 21:32:31 -02:00
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
// Pipe for archive
2016-12-30 09:27:35 -02:00
type Pipe struct{}
func (Pipe) String() string {
return "creating archives"
2016-12-30 09:27:35 -02:00
}
2016-12-30 12:41:59 -02:00
// Run the pipe
2017-01-14 14:06:57 -02:00
func (Pipe) Run(ctx *context.Context) error {
2016-12-30 09:59:24 -02:00
var g errgroup.Group
2017-12-17 15:50:09 -02:00
var filtered = ctx.Artifacts.Filter(artifact.ByType(artifact.Binary))
for _, artifacts := range filtered.GroupByPlatform() {
artifacts := artifacts
2017-01-14 12:34:22 -02:00
g.Go(func() error {
if ctx.Config.Archive.Format == "binary" {
2017-12-17 15:50:09 -02:00
return skip(ctx, artifacts)
}
2017-12-17 15:50:09 -02:00
return create(ctx, artifacts)
2017-01-14 12:34:22 -02:00
})
2016-12-28 22:53:56 -02:00
}
2016-12-30 09:59:24 -02:00
return g.Wait()
2016-12-28 22:23:39 -02:00
}
2016-12-28 22:53:56 -02:00
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Archive.NameTemplate == "" {
2017-12-18 21:32:31 -02:00
ctx.Config.Archive.NameTemplate = defaultNameTemplate
}
if ctx.Config.Archive.Format == "" {
ctx.Config.Archive.Format = "tar.gz"
}
if len(ctx.Config.Archive.Files) == 0 {
ctx.Config.Archive.Files = []string{
"licence*",
"LICENCE*",
"license*",
"LICENSE*",
"readme*",
"README*",
"changelog*",
"CHANGELOG*",
}
}
return nil
}
2017-12-17 15:50:09 -02:00
func create(ctx *context.Context, artifacts []artifact.Artifact) error {
var format = packageFormat(ctx, artifacts[0].Goos)
2017-12-17 18:01:58 -02:00
folder, err := nametemplate.Apply(ctx, artifacts[0], ctx.Config.ProjectName)
2017-12-17 15:50:09 -02:00
if err != nil {
return err
}
archivePath := filepath.Join(ctx.Config.Dist, folder+"."+format)
archiveFile, err := os.Create(archivePath)
if err != nil {
return fmt.Errorf("failed to create directory %s: %s", archivePath, err.Error())
}
2017-12-17 23:04:29 -02:00
defer archiveFile.Close() // nolint: errcheck
2017-12-17 15:50:09 -02:00
log.WithField("archive", archivePath).Info("creating")
var a = archive.New(archiveFile)
2017-12-17 23:04:29 -02:00
defer a.Close() // nolint: errcheck
2017-12-17 15:50:09 -02:00
files, err := findFiles(ctx)
if err != nil {
return fmt.Errorf("failed to find files to archive: %s", err.Error())
}
for _, f := range files {
if err = a.Add(wrap(ctx, f, folder), f); err != nil {
return fmt.Errorf("failed to add %s to the archive: %s", f, err.Error())
}
2017-12-17 15:50:09 -02:00
}
for _, binary := range artifacts {
if err := a.Add(wrap(ctx, binary.Name, folder), binary.Path); err != nil {
return fmt.Errorf("failed to add %s -> %s to the archive: %s", binary.Path, binary.Name, err.Error())
}
2017-05-11 00:05:51 -03:00
}
2017-12-17 15:50:09 -02:00
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive,
2017-12-17 15:50:09 -02:00
Name: folder + "." + format,
Path: archivePath,
Goos: artifacts[0].Goos,
Goarch: artifacts[0].Goarch,
Goarm: artifacts[0].Goarm,
})
2017-01-14 12:51:09 -02:00
return nil
2016-12-28 22:53:56 -02:00
}
2017-12-17 15:50:09 -02:00
func skip(ctx *context.Context, artifacts []artifact.Artifact) error {
for _, a := range artifacts {
log.WithField("binary", a.Name).Info("skip archiving")
2017-12-18 21:32:31 -02:00
name, err := nametemplate.Apply(ctx, a, a.Extra["Binary"])
2017-12-17 18:01:58 -02:00
if err != nil {
return err
}
a.Type = artifact.UploadableBinary
2017-12-17 22:28:24 -02:00
a.Name = name + a.Extra["Ext"]
2017-12-17 15:50:09 -02:00
ctx.Artifacts.Add(a)
2017-07-03 00:57:39 -03:00
}
return nil
}
2017-05-11 00:05:51 -03:00
func findFiles(ctx *context.Context) (result []string, err error) {
for _, glob := range ctx.Config.Archive.Files {
2017-05-11 12:43:04 -03:00
files, err := zglob.Glob(glob)
2017-05-11 00:05:51 -03:00
if err != nil {
return result, fmt.Errorf("globbing failed for pattern %s: %s", glob, err.Error())
2017-05-11 00:05:51 -03:00
}
result = append(result, files...)
}
return
}
// Wrap archive files with folder if set in config.
func wrap(ctx *context.Context, name, folder string) string {
if ctx.Config.Archive.WrapInDirectory {
return filepath.Join(folder, name)
}
return name
}
func packageFormat(ctx *context.Context, platform string) string {
for _, override := range ctx.Config.Archive.FormatOverrides {
if strings.HasPrefix(platform, override.Goos) {
return override.Format
}
}
return ctx.Config.Archive.Format
}