1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

feat: remap extra_files (#2678)

* feat: template on extra_files

* feat: remap extra_files

* docs: wording

* chore: log, more tests
This commit is contained in:
Carlos Alexandro Becker 2021-11-23 22:18:47 -03:00 committed by GitHub
parent b1d16bbee9
commit d202e788a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 16 deletions

View File

@ -17,24 +17,35 @@ func Find(ctx *context.Context, files []config.ExtraFile) (map[string]string, er
t := tmpl.New(ctx)
result := map[string]string{}
for _, extra := range files {
if extra.Glob == "" {
continue
}
glob, err := t.Apply(extra.Glob)
if err != nil {
return result, fmt.Errorf("failed to apply template to glob %q: %w", extra.Glob, err)
}
if glob == "" {
log.Warn("ignoring empty glob")
continue
}
files, err := fileglob.Glob(glob)
if err != nil {
return result, fmt.Errorf("globbing failed for pattern %s: %w", extra.Glob, err)
}
if len(files) > 1 && extra.NameTemplate != "" {
return result, fmt.Errorf("failed to add extra_file: %q -> %q: glob matches multiple files", extra.Glob, extra.NameTemplate)
}
for _, file := range files {
info, err := os.Stat(file)
if err == nil && info.IsDir() {
log.Debugf("ignoring directory %s", file)
continue
}
n, err := t.Apply(extra.NameTemplate)
if err != nil {
return result, fmt.Errorf("failed to apply template to name %q: %w", extra.NameTemplate, err)
}
name := filepath.Base(file)
if n != "" {
name = n
}
if old, ok := result[name]; ok {
log.Warnf("overriding %s with %s for name %s", old, file, name)
}

View File

@ -15,11 +15,9 @@ func TestTemplate(t *testing.T) {
ctx := context.New(config.Project{})
ctx.Env["ONE"] = "1"
files, err := Find(ctx, globs)
require.NoError(t, err)
require.Len(t, files, 1)
require.Equal(t, "testdata/file1.golden", files["file1.golden"])
}
@ -29,7 +27,6 @@ func TestBadTemplate(t *testing.T) {
}
ctx := context.New(config.Project{})
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to glob "./testdata/file{{ .Env.NOPE }}.golden": template: tmpl:1:22: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`)
@ -67,7 +64,6 @@ func TestShouldGetFilesWithSuperStar(t *testing.T) {
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 3)
require.Equal(t, "testdata/file2.golden", files["file2.golden"])
require.Equal(t, "testdata/sub3/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/sub/file5.golden", files["file5.golden"])
@ -81,7 +77,6 @@ func TestShouldGetAllFilesWithGoldenExtension(t *testing.T) {
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 2)
require.Equal(t, "testdata/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/file2.golden", files["file2.golden"])
}
@ -94,9 +89,89 @@ func TestShouldGetAllFilesInsideTestdata(t *testing.T) {
files, err := Find(context.New(config.Project{}), globs)
require.NoError(t, err)
require.Len(t, files, 4)
require.Equal(t, "testdata/sub3/file1.golden", files["file1.golden"])
require.Equal(t, "testdata/file2.golden", files["file2.golden"])
require.Equal(t, "testdata/file3.gold", files["file3.gold"])
require.Equal(t, "testdata/sub/file5.golden", files["file5.golden"])
}
func TestTargetName(t *testing.T) {
globs := []config.ExtraFile{
{
Glob: "./testdata/file1.golden",
NameTemplate: "file1_{{.Tag}}.golden",
},
}
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v1.0.0"
files, err := Find(ctx, globs)
require.NoError(t, err)
require.Len(t, files, 1)
require.Equal(t, "testdata/file1.golden", files["file1_v1.0.0.golden"])
}
func TestTargetInvalidNameTemplate(t *testing.T) {
globs := []config.ExtraFile{
{
Glob: "./testdata/file1.golden",
NameTemplate: "file1_{{.Env.HONK}}.golden",
},
}
ctx := context.New(config.Project{})
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to apply template to name "file1_{{.Env.HONK}}.golden": template: tmpl:1:12: executing "tmpl" at <.Env.HONK>: map has no entry for key "HONK"`)
}
func TestTargetNameMatchesMultipleFiles(t *testing.T) {
globs := []config.ExtraFile{
{
Glob: "./testdata/*",
NameTemplate: "file1.golden",
},
}
ctx := context.New(config.Project{})
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `failed to add extra_file: "./testdata/*" -> "file1.golden": glob matches multiple files`)
}
func TestTargetNameNoMatches(t *testing.T) {
globs := []config.ExtraFile{
{
Glob: "./testdata/file1.silver",
NameTemplate: "file1_{{.Tag}}.golden",
},
}
ctx := context.New(config.Project{})
files, err := Find(ctx, globs)
require.Empty(t, files)
require.EqualError(t, err, `globbing failed for pattern ./testdata/file1.silver: matching "./testdata/file1.silver": file does not exist`)
}
func TestGlobEvalsToEmpty(t *testing.T) {
globs := []config.ExtraFile{
{Glob: `{{ printf "" }}`},
}
ctx := context.New(config.Project{})
files, err := Find(ctx, globs)
require.Empty(t, files)
require.NoError(t, err)
}
func TestTargetNameNoGlob(t *testing.T) {
globs := []config.ExtraFile{
{NameTemplate: "file1.golden"},
}
ctx := context.New(config.Project{})
files, err := Find(ctx, globs)
require.Empty(t, files)
require.NoError(t, err)
}

View File

@ -476,7 +476,8 @@ type Milestone struct {
// ExtraFile on a release.
type ExtraFile struct {
Glob string `yaml:"glob,omitempty"`
Glob string `yaml:"glob,omitempty"`
NameTemplate string `yaml:"name_template,omitempty"`
}
// NFPM config.

View File

@ -53,6 +53,8 @@ blobs:
- glob: ./path/to/file.txt
- glob: ./glob/**/to/**/file/**/*
- glob: ./glob/foo/to/bar/file/foobar/override_from_previous
- glob: ./single_file.txt
name_template: file.txt # note that this only works if glob matches 1 file only
-
provider: gs
bucket: goreleaser-bucket

View File

@ -32,13 +32,15 @@ checksum:
# You can add extra pre-existing files to the checksums file.
# The filename on the checksum will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# This globs can also include templates.
# These globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
- glob: ./glob/**/to/**/file/**/*
- glob: ./glob/foo/to/bar/file/foobar/override_from_previous
- glob: ./single_file.txt
name_template: file.txt # note that this only works if glob matches 1 file only
```
!!! tip

View File

@ -66,13 +66,15 @@ release:
# You can add extra pre-existing files to the release.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# This globs can also include templates.
# These globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
- glob: ./glob/**/to/**/file/**/*
- glob: ./glob/foo/to/bar/file/foobar/override_from_previous
- glob: ./single_file.txt
name_template: file.txt # note that this only works if glob matches 1 file only
```
!!! tip
@ -107,13 +109,17 @@ release:
disable: true
# You can add extra pre-existing files to the release.
# The filename on the release will be the last part of the path (base). If
# another file with the same name exists, the last one found will be used.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# These globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
- glob: ./glob/**/to/**/file/**/*
- glob: ./glob/foo/to/bar/file/foobar/override_from_previous
- glob: ./single_file.txt
name_template: file.txt # note that this only works if glob matches 1 file only
```
!!! tip
@ -152,13 +158,17 @@ release:
disable: true
# You can add extra pre-existing files to the release.
# The filename on the release will be the last part of the path (base). If
# another file with the same name exists, the last one found will be used.
# The filename on the release will be the last part of the path (base).
# If another file with the same name exists, the last one found will be used.
# These globs can also include templates.
#
# Defaults to empty.
extra_files:
- glob: ./path/to/file.txt
- glob: ./glob/**/to/**/file/**/*
- glob: ./glob/foo/to/bar/file/foobar/override_from_previous
- glob: ./single_file.txt
name_template: file.txt # note that this only works if glob matches 1 file only
```
To enable uploading `tar.gz` and `checksums.txt` files you need to add the following to your Gitea config in `app.ini`: