mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
feat: templateable archive.files (#1373)
* feat: support variable substitution in archive.files * Update internal/pipe/archive/archive.go * feat: templateable archive.files Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * docs: templateable archive.files Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> Co-authored-by: Robbie Ostrow <ostrowr@users.noreply.github.com>
This commit is contained in:
parent
139d13cc57
commit
969fb4b804
@ -13,6 +13,7 @@ import (
|
||||
"github.com/apex/log"
|
||||
"github.com/campoy/unique"
|
||||
"github.com/mattn/go-zglob"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/artifact"
|
||||
"github.com/goreleaser/goreleaser/internal/ids"
|
||||
@ -131,8 +132,9 @@ func create(ctx *context.Context, archive config.Archive, binaries []*artifact.A
|
||||
var log = log.WithField("archive", archivePath)
|
||||
log.Info("creating")
|
||||
|
||||
wrap, err := tmpl.New(ctx).
|
||||
WithArtifact(binaries[0], archive.Replacements).
|
||||
template := tmpl.New(ctx).
|
||||
WithArtifact(binaries[0], archive.Replacements)
|
||||
wrap, err := template.
|
||||
Apply(wrapFolder(archive))
|
||||
if err != nil {
|
||||
return err
|
||||
@ -141,7 +143,7 @@ func create(ctx *context.Context, archive config.Archive, binaries []*artifact.A
|
||||
var a = NewEnhancedArchive(archivelib.New(archiveFile), wrap)
|
||||
defer a.Close() // nolint: errcheck
|
||||
|
||||
files, err := findFiles(archive)
|
||||
files, err := findFiles(template, archive)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find files to archive: %s", err.Error())
|
||||
}
|
||||
@ -210,11 +212,15 @@ func skip(ctx *context.Context, archive config.Archive, binaries []*artifact.Art
|
||||
return nil
|
||||
}
|
||||
|
||||
func findFiles(archive config.Archive) (result []string, err error) {
|
||||
func findFiles(template *tmpl.Template, archive config.Archive) (result []string, err error) {
|
||||
for _, glob := range archive.Files {
|
||||
files, err := zglob.Glob(glob)
|
||||
replaced, err := template.Apply(glob)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("globbing failed for pattern %s: %s", glob, err.Error())
|
||||
return result, errors.Wrapf(err, "failed to apply template %s", glob)
|
||||
}
|
||||
files, err := zglob.Glob(replaced)
|
||||
if err != nil {
|
||||
return result, errors.Wrapf(err, "globbing failed for pattern %s", glob)
|
||||
}
|
||||
result = append(result, files...)
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -39,10 +40,12 @@ func TestRunPipe(t *testing.T) {
|
||||
createFakeBinary(t, dist, arch, "mybin")
|
||||
}
|
||||
createFakeBinary(t, dist, "windowsamd64", "mybin.exe")
|
||||
_, err := os.Create(filepath.Join(folder, "README.md"))
|
||||
require.NoError(t, err)
|
||||
for _, tt := range []string{"darwin", "linux", "windows"} {
|
||||
_, err := os.Create(filepath.Join(folder, fmt.Sprintf("README.%s.md", tt)))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(folder, "foo", "bar", "foobar"), 0755))
|
||||
_, err = os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
|
||||
_, err := os.Create(filepath.Join(filepath.Join(folder, "foo", "bar", "foobar", "blah.txt")))
|
||||
require.NoError(t, err)
|
||||
var ctx = context.New(
|
||||
config.Project{
|
||||
@ -54,7 +57,7 @@ func TestRunPipe(t *testing.T) {
|
||||
Builds: []string{"default"},
|
||||
NameTemplate: defaultNameTemplate,
|
||||
Files: []string{
|
||||
"README.*",
|
||||
"README.{{.Os}}.*",
|
||||
"./foo/**/*",
|
||||
},
|
||||
FormatOverrides: []config.FormatOverride{
|
||||
@ -143,16 +146,16 @@ func TestRunPipe(t *testing.T) {
|
||||
|
||||
if format == "tar.gz" {
|
||||
// Check archive contents
|
||||
for _, name := range []string{
|
||||
"foobar_0.0.1_darwin_amd64.tar.gz",
|
||||
"foobar_0.0.1_linux_386.tar.gz",
|
||||
"foobar_0.0.1_linux_armv7.tar.gz",
|
||||
"foobar_0.0.1_linux_mips_softfloat.tar.gz",
|
||||
for name, os := range map[string]string{
|
||||
"foobar_0.0.1_darwin_amd64.tar.gz": "darwin",
|
||||
"foobar_0.0.1_linux_386.tar.gz": "linux",
|
||||
"foobar_0.0.1_linux_armv7.tar.gz": "linux",
|
||||
"foobar_0.0.1_linux_mips_softfloat.tar.gz": "linux",
|
||||
} {
|
||||
require.Equal(
|
||||
t,
|
||||
[]string{
|
||||
"README.md",
|
||||
fmt.Sprintf("README.%s.md", os),
|
||||
"foo/bar",
|
||||
"foo/bar/foobar",
|
||||
"foo/bar/foobar/blah.txt",
|
||||
@ -166,7 +169,7 @@ func TestRunPipe(t *testing.T) {
|
||||
require.Equal(
|
||||
t,
|
||||
[]string{
|
||||
"README.md",
|
||||
"README.windows.md",
|
||||
"foo/bar/foobar/blah.txt",
|
||||
"mybin.exe",
|
||||
},
|
||||
@ -372,6 +375,44 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
|
||||
require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
|
||||
}
|
||||
|
||||
func TestRunPipeInvalidFilesNameTemplate(t *testing.T) {
|
||||
folder, back := testlib.Mktmp(t)
|
||||
defer back()
|
||||
var dist = filepath.Join(folder, "dist")
|
||||
require.NoError(t, os.Mkdir(dist, 0755))
|
||||
require.NoError(t, os.Mkdir(filepath.Join(dist, "darwinamd64"), 0755))
|
||||
_, err := os.Create(filepath.Join(dist, "darwinamd64", "mybin"))
|
||||
require.NoError(t, err)
|
||||
var ctx = context.New(
|
||||
config.Project{
|
||||
Dist: dist,
|
||||
Archives: []config.Archive{
|
||||
{
|
||||
Builds: []string{"default"},
|
||||
NameTemplate: "foo",
|
||||
Format: "zip",
|
||||
Files: []string{
|
||||
"{{.asdsd}",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
ctx.Git.CurrentTag = "v0.0.1"
|
||||
ctx.Artifacts.Add(&artifact.Artifact{
|
||||
Goos: "darwin",
|
||||
Goarch: "amd64",
|
||||
Name: "mybin",
|
||||
Path: filepath.Join("dist", "darwinamd64", "mybin"),
|
||||
Type: artifact.Binary,
|
||||
Extra: map[string]interface{}{
|
||||
"Binary": "mybin",
|
||||
"ID": "default",
|
||||
},
|
||||
})
|
||||
require.EqualError(t, Pipe{}.Run(ctx), `failed to find files to archive: failed to apply template {{.asdsd}: template: tmpl:1: unexpected "}" in operand`)
|
||||
}
|
||||
|
||||
func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
|
||||
folder, back := testlib.Mktmp(t)
|
||||
defer back()
|
||||
|
@ -62,12 +62,12 @@ archives:
|
||||
- goos: windows
|
||||
format: zip
|
||||
|
||||
# Additional files/globs you want to add to the archive.
|
||||
# Additional files/template/globs you want to add to the archive.
|
||||
# Defaults are any files matching `LICENCE*`, `LICENSE*`,
|
||||
# `README*` and `CHANGELOG*` (case-insensitive).
|
||||
files:
|
||||
- LICENSE.txt
|
||||
- README.md
|
||||
- README_{{.Os}}.md
|
||||
- CHANGELOG.md
|
||||
- docs/*
|
||||
- design/*.png
|
||||
|
Loading…
x
Reference in New Issue
Block a user