1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-16 03:52:12 +02:00

support globs on archive.files

This commit is contained in:
Carlos Alexandro Becker 2017-05-11 00:05:51 -03:00
parent 5eee70de8b
commit b2821d7e7f
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
11 changed files with 136 additions and 97 deletions

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

@ -2,15 +2,19 @@ homepage: &homepage http://goreleaser.github.io
description: &description Deliver Go binaries as fast and easily as possible
build:
goos:
- linux
# - linux
- darwin
- windows
goarch:
- 386
# - 386
- amd64
- arm
- arm64
# - arm
# - arm64
archive:
files:
- README.*
- internal/**/*.go
- config/*
format_overrides:
- goos: windows
format: zip

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,13 +4,13 @@
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"
"golang.org/x/sync/errgroup"
@ -54,24 +54,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 := filepath.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

@ -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")