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

Merge pull request #231 from goreleaser/globs

support globs on archive.files
This commit is contained in:
Carlos Alexandro Becker 2017-05-11 22:42:25 -03:00 committed by GitHub
commit f498d48773
16 changed files with 150 additions and 96 deletions

8
Gopkg.lock generated
View File

@ -1,4 +1,4 @@
memo = "85c15b94eaf073e2c6e87a85ac2088f1d61889c8ddcc8542c09145f0f816c6af"
memo = "3cbc12d80513bcbb0ac166d404600ffbc63b1c301b5c6d3722dad30db6f6a549"
[[projects]]
name = "github.com/davecgh/go-spew"
@ -24,6 +24,12 @@ memo = "85c15b94eaf073e2c6e87a85ac2088f1d61889c8ddcc8542c09145f0f816c6af"
packages = ["query"]
revision = "53e6ce116135b80d037921a7fdd5138cf32d7a8a"
[[projects]]
branch = "master"
name = "github.com/mattn/go-zglob"
packages = [".","fastwalk"]
revision = "95345c4e1c0ebc9d16a3284177f09360f4d20fab"
[[projects]]
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]

View File

@ -11,7 +11,7 @@ setup: ## Install all the build and lint dependencies
gometalinter --install
test: ## Run all the tests
gotestcover $(TEST_OPTIONS) -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=30s
gotestcover $(TEST_OPTIONS) -covermode=atomic -coverprofile=coverage.txt $(SOURCE_FILES) -run $(TEST_PATTERN) -timeout=2m
cover: test ## Run all the tests and opens the coverage report
go tool cover -html=coverage.txt

View File

@ -284,13 +284,15 @@ archive:
darwin: macOS
linux: Tux
# Additional files you want to add to the archive.
# Additional files/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
- CHANGELOG.md
- docs/*
- design/*.png
```
### Release customization

10
internal/ext/ext.go Normal file
View File

@ -0,0 +1,10 @@
package ext
import "strings"
func For(platform string) (ext string) {
if strings.HasPrefix(platform, "windows") {
ext = ".exe"
}
return
}

18
internal/ext/ext_test.go Normal file
View File

@ -0,0 +1,18 @@
package ext
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExtWindows(t *testing.T) {
assert.Equal(t, ".exe", For("windows"))
assert.Equal(t, ".exe", For("windowsamd64"))
}
func TestExtOthers(t *testing.T) {
assert.Empty(t, "", For("linux"))
assert.Empty(t, "", For("linuxwin"))
assert.Empty(t, "", For("winasdasd"))
}

View File

@ -4,15 +4,16 @@
package archive
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/ext"
"github.com/goreleaser/goreleaser/pipeline/archive/tar"
"github.com/goreleaser/goreleaser/pipeline/archive/zip"
"github.com/mattn/go-zglob"
"golang.org/x/sync/errgroup"
)
@ -54,24 +55,35 @@ func create(ctx *context.Context, platform, name string) error {
defer func() { _ = file.Close() }()
var archive = archiveFor(file, format)
defer func() { _ = archive.Close() }()
for _, f := range ctx.Config.Archive.Files {
if err = archive.Add(f, f); err != nil {
return err
}
}
files, err := ioutil.ReadDir(folder)
files, err := findFiles(ctx)
if err != nil {
return err
}
for _, f := range files {
if err := archive.Add(f.Name(), filepath.Join(folder, f.Name())); err != nil {
if err = archive.Add(f, f); err != nil {
return err
}
}
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())
return nil
}
func findFiles(ctx *context.Context) (result []string, err error) {
for _, glob := range ctx.Config.Archive.Files {
files, err := zglob.Glob(glob)
if err != nil {
return result, err
}
result = append(result, files...)
}
return
}
func archiveFor(file *os.File, format string) Archive {
if format == "zip" {
return zip.New(file)

View File

@ -30,7 +30,9 @@ func TestRunPipe(t *testing.T) {
assert.NoError(os.Mkdir(filepath.Join(dist, "mybin"), 0755))
_, err = os.Create(filepath.Join(dist, "mybin", "mybin"))
assert.NoError(err)
readme, err := os.Create(filepath.Join(folder, "README.md"))
_, err = os.Create(filepath.Join(dist, "mybin", "mybin.exe"))
assert.NoError(err)
_, err = os.Create(filepath.Join(folder, "README.md"))
assert.NoError(err)
var ctx = &context.Context{
Archives: map[string]string{
@ -39,9 +41,12 @@ func TestRunPipe(t *testing.T) {
},
Config: config.Project{
Dist: dist,
Build: config.Build{
Binary: "mybin",
},
Archive: config.Archive{
Files: []string{
"README.md",
"README.*",
},
FormatOverrides: []config.FormatOverride{
{
@ -58,10 +63,6 @@ func TestRunPipe(t *testing.T) {
assert.NoError(Pipe{}.Run(ctx))
})
}
t.Run("Removed README", func(t *testing.T) {
assert.NoError(os.Remove(readme.Name()))
assert.Error(Pipe{}.Run(ctx))
})
}
func TestRunPipeDistRemoved(t *testing.T) {
@ -99,3 +100,64 @@ func TestFormatFor(t *testing.T) {
assert.Equal("zip", formatFor(ctx, "windowsamd64"))
assert.Equal("tar.gz", formatFor(ctx, "linux386"))
}
func TestRunPipeInvalidGlob(t *testing.T) {
var assert = assert.New(t)
var ctx = &context.Context{
Archives: map[string]string{
"windowsamd64": "mybin",
},
Config: config.Project{
Dist: "/tmp",
Archive: config.Archive{
Files: []string{
"[x-]",
},
},
},
}
assert.Error(Pipe{}.Run(ctx))
}
func TestRunPipeGlobFailsToAdd(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(err)
current, err := os.Getwd()
assert.NoError(err)
assert.NoError(os.Chdir(folder))
defer func() {
assert.NoError(os.Chdir(current))
}()
assert.NoError(os.MkdirAll(filepath.Join(folder, "folder", "another"), 0755))
var ctx = &context.Context{
Archives: map[string]string{
"windows386": "mybin",
},
Config: config.Project{
Dist: folder,
Archive: config.Archive{
Files: []string{
"folder",
},
},
},
}
assert.Error(Pipe{}.Run(ctx))
}
func TestRunPipeBinaryDontExist(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "archivetest")
assert.NoError(err)
var ctx = &context.Context{
Archives: map[string]string{
"windows386": "mybin",
},
Config: config.Project{
Dist: folder,
},
}
assert.Error(Pipe{}.Run(ctx))
}

View File

@ -46,7 +46,7 @@ func (a Archive) Add(name, path string) (err error) {
_ = file.Close()
}()
stat, err := file.Stat()
if err != nil {
if err != nil || stat.IsDir() {
return
}
header := new(tar.Header)

View File

@ -23,9 +23,12 @@ func TestTarGzFile(t *testing.T) {
empty2, err := os.Create(folder + "/empty2.txt")
assert.NoError(err)
assert.NoError(os.Mkdir(folder+"/folder-inside", 0755))
archive := New(file)
assert.NoError(archive.Add("empty.txt", empty.Name()))
assert.Error(archive.Add("dont.txt", empty.Name()+"_nope"))
assert.NoError(archive.Add("empty.txt", folder+"/folder-inside"))
assert.NoError(archive.Close())
assert.Error(archive.Add("empty2.txt", empty2.Name()))
}

View File

@ -31,6 +31,10 @@ func (a Archive) Add(name, path string) (err error) {
if err != nil {
return
}
stat, err := file.Stat()
if err != nil || stat.IsDir() {
return
}
defer func() { _ = file.Close() }()
f, err := a.z.Create(name)
if err != nil {

View File

@ -20,8 +20,11 @@ func TestZipFile(t *testing.T) {
empty, err := os.Create(folder + "/empty.txt")
assert.NoError(err)
assert.NoError(os.Mkdir(folder+"/folder-inside", 0755))
archive := New(file)
assert.NoError(archive.Add("empty.txt", empty.Name()))
assert.NoError(archive.Add("empty.txt", folder+"/folder-inside"))
assert.Error(archive.Add("dont.txt", empty.Name()+"_nope"))
assert.NoError(archive.Close())
}

View File

@ -11,6 +11,7 @@ import (
"strings"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/ext"
"golang.org/x/sync/errgroup"
)
@ -64,7 +65,7 @@ func build(ctx *context.Context, name string, target buildTarget) error {
output := filepath.Join(
ctx.Config.Dist,
name,
ctx.Config.Build.Binary+extFor(target.goos),
ctx.Config.Build.Binary+ext.For(target.goos),
)
log.Println("Building", output)
cmd := []string{"go", "build"}

View File

@ -42,10 +42,3 @@ func replace(replacements map[string]string, original string) string {
}
return result
}
func extFor(goos string) string {
if goos == "windows" {
return ".exe"
}
return ""
}

View File

@ -9,14 +9,6 @@ import (
"github.com/stretchr/testify/assert"
)
func TestExtWindows(t *testing.T) {
assert.Equal(t, ".exe", extFor("windows"))
}
func TestExtOthers(t *testing.T) {
assert.Empty(t, "", extFor("linux"))
}
func TestNameFor(t *testing.T) {
assert := assert.New(t)

View File

@ -4,14 +4,10 @@ package defaults
import (
"fmt"
"io/ioutil"
"strings"
"github.com/goreleaser/goreleaser/context"
)
var defaultFiles = []string{"licence", "license", "readme", "changelog"}
// NameTemplate default name_template for the archive.
const NameTemplate = "{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
@ -98,33 +94,16 @@ func setArchiveDefaults(ctx *context.Context) error {
}
}
if len(ctx.Config.Archive.Files) == 0 {
files, err := findFiles()
if err != nil {
return err
ctx.Config.Archive.Files = []string{
"licence*",
"LICENCE*",
"license*",
"LICENSE*",
"readme*",
"README*",
"changelog*",
"CHANGELOG*",
}
ctx.Config.Archive.Files = files
}
return nil
}
func findFiles() (files []string, err error) {
all, err := ioutil.ReadDir(".")
if err != nil {
return
}
for _, file := range all {
if accept(file.Name()) {
files = append(files, file.Name())
}
}
return
}
func accept(file string) bool {
for _, accepted := range defaultFiles {
if strings.HasPrefix(strings.ToLower(file), accepted) {
return true
}
}
return false
}

View File

@ -52,48 +52,17 @@ func TestFillPartial(t *testing.T) {
Name: "test",
},
},
},
}
assert.NoError(Pipe{}.Run(ctx))
}
func TestFilesFilled(t *testing.T) {
assert := assert.New(t)
var ctx = &context.Context{
Config: config.Project{
Archive: config.Archive{
Files: []string{
"README.md",
"glob/*",
},
},
},
}
assert.NoError(Pipe{}.Run(ctx))
assert.Len(ctx.Config.Archive.Files, 1)
}
func TestAcceptFiles(t *testing.T) {
var files = []string{
"LICENSE.md",
"LIceNSE.txt",
"LICENSE",
"LICENCE.txt",
"LICEncE",
"README",
"READme.md",
"CHANGELOG.txt",
"ChanGELOG.md",
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
assert.True(t, accept(file))
})
}
}
func TestNotAGitRepo(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "goreleasertest")