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

112 lines
2.7 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 (
2016-12-30 09:27:35 -02:00
"log"
2016-12-29 18:10:11 -02:00
"os"
2017-01-14 14:06:57 -02:00
"path/filepath"
"strings"
2016-12-29 18:10:11 -02:00
2017-01-14 20:01:32 -02:00
"github.com/goreleaser/goreleaser/context"
2017-05-11 00:05:51 -03:00
"github.com/goreleaser/goreleaser/internal/ext"
2017-05-13 18:09:42 -03:00
"github.com/goreleaser/goreleaser/internal/tar"
"github.com/goreleaser/goreleaser/internal/zip"
2017-05-14 11:04:23 -03:00
"github.com/mattn/go-zglob"
2016-12-30 09:59:24 -02:00
"golang.org/x/sync/errgroup"
2016-12-28 22:53:56 -02:00
)
2016-12-28 22:23:39 -02:00
// Pipe for archive
2016-12-30 09:27:35 -02:00
type Pipe struct{}
2017-01-14 19:41:32 +01:00
// Description of the pipe
2017-01-14 15:14:35 -02:00
func (Pipe) Description() string {
2017-01-19 10:04:41 +01:00
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
for platform, archive := range ctx.Archives {
2017-01-14 12:34:22 -02:00
archive := archive
platform := platform
2017-01-14 12:34:22 -02:00
g.Go(func() error {
if ctx.Config.Archive.Skip {
return skip(ctx, platform, archive)
}
return create(ctx, platform, archive)
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
2017-01-14 19:41:32 +01:00
// Archive represents a compression archive files from disk can be written to.
2017-01-06 13:23:49 -02:00
type Archive interface {
Close() error
Add(name, path string) error
}
func create(ctx *context.Context, platform, name string) error {
var folder = filepath.Join(ctx.Config.Dist, name)
var format = formatFor(ctx, platform)
file, err := os.Create(folder + "." + format)
2016-12-28 22:53:56 -02:00
if err != nil {
return err
}
2017-04-22 10:29:53 -03:00
log.Println("Creating", file.Name())
2017-01-06 13:23:49 -02:00
defer func() { _ = file.Close() }()
var archive = archiveFor(file, format)
2017-01-06 13:23:49 -02:00
defer func() { _ = archive.Close() }()
2017-05-11 00:05:51 -03:00
files, err := findFiles(ctx)
2017-01-14 12:51:09 -02:00
if err != nil {
return err
}
for _, f := range files {
2017-05-11 00:05:51 -03:00
if err = archive.Add(f, f); err != nil {
2017-01-14 12:51:09 -02:00
return err
}
}
2017-05-11 00:05:51 -03:00
var binary = ctx.Config.Build.Binary + ext.For(platform)
if err := archive.Add(binary, filepath.Join(folder, binary)); err != nil {
return err
}
ctx.AddArtifact(file.Name())
2017-01-14 12:51:09 -02:00
return nil
2016-12-28 22:53:56 -02:00
}
func skip(ctx *context.Context, platform, name string) error {
log.Println("Skip archiving")
var binary = filepath.Join(ctx.Config.Dist, name+ext.For(platform))
ctx.AddArtifact(binary)
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, err
}
result = append(result, files...)
}
return
}
2017-01-09 12:46:20 -02:00
func archiveFor(file *os.File, format string) Archive {
if format == "zip" {
return zip.New(file)
2016-12-28 22:53:56 -02:00
}
2017-01-09 12:46:20 -02:00
return tar.New(file)
2016-12-28 22:53:56 -02:00
}
func formatFor(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
}