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

Merge pull request #285 from goreleaser/dist

rm dist feature
This commit is contained in:
Carlos Alexandro Becker 2017-07-04 23:14:12 -03:00 committed by GitHub
commit 4a4c9016a0
5 changed files with 124 additions and 2 deletions

View File

@ -34,6 +34,7 @@ type Context struct {
Validate bool
Publish bool
Snapshot bool
RmDist bool
}
var artifactsLock sync.Mutex

View File

@ -14,6 +14,7 @@ import (
"github.com/goreleaser/goreleaser/pipeline/brew"
"github.com/goreleaser/goreleaser/pipeline/build"
"github.com/goreleaser/goreleaser/pipeline/checksums"
"github.com/goreleaser/goreleaser/pipeline/cleandist"
"github.com/goreleaser/goreleaser/pipeline/defaults"
"github.com/goreleaser/goreleaser/pipeline/env"
"github.com/goreleaser/goreleaser/pipeline/fpm"
@ -26,6 +27,7 @@ var pipes = []pipeline.Pipe{
defaults.Pipe{}, // load default configs
git.Pipe{}, // get and validate git repo state
env.Pipe{}, // load and validate environment variables
cleandist.Pipe{}, // ensure ./dist is clean
build.Pipe{}, // build
archive.Pipe{}, // archive (tar.gz, zip, etc)
fpm.Pipe{}, // archive via fpm (deb, rpm, etc)
@ -74,6 +76,7 @@ func Release(flags Flags) error {
log.Info("publishing disabled in snapshot mode")
ctx.Publish = false
}
ctx.RmDist = flags.Bool("rm-dist")
for _, pipe := range pipes {
log.Infof("\033[1m%s\033[0m", strings.ToUpper(pipe.Description()))
if err := pipe.Run(ctx); err != nil {

10
main.go
View File

@ -47,6 +47,10 @@ func main() {
Name: "snapshot",
Usage: "Generate an unversioned snapshot release",
},
cli.BoolFlag{
Name: "rm-dist",
Usage: "Remove ./dist before building",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug mode",
@ -55,7 +59,8 @@ func main() {
app.Action = func(c *cli.Context) error {
log.Infof("running goreleaser %v", version)
if err := goreleaserlib.Release(c); err != nil {
return cli.NewExitError(err.Error(), 1)
log.WithError(err).Error("pipe failed")
return cli.NewExitError("\n", 1)
}
return nil
}
@ -67,7 +72,8 @@ func main() {
Action: func(c *cli.Context) error {
var filename = "goreleaser.yml"
if err := goreleaserlib.InitProject(filename); err != nil {
return cli.NewExitError(err.Error(), 1)
log.WithError(err).Error("failed to init project")
return cli.NewExitError("\n", 1)
}
log.WithField("file", filename).

View File

@ -0,0 +1,46 @@
// Package cleandist provides checks to make sure the dist folder is always
// empty.
package cleandist
import (
"fmt"
"io/ioutil"
"os"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/context"
)
// Pipe for cleandis
type Pipe struct{}
// Description of the pipe
func (Pipe) Description() string {
return "Checking ./dist"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
_, err = os.Stat(ctx.Config.Dist)
if os.IsNotExist(err) {
log.Debug("./dist doesn't exist, moving on")
return nil
}
if ctx.RmDist {
log.Warn("rm-dist is set, removing ./dist")
return os.RemoveAll(ctx.Config.Dist)
}
files, err := ioutil.ReadDir(ctx.Config.Dist)
if err != nil {
return
}
if len(files) > 0 {
log.WithField("files", len(files)).Debug("./dist is not empty")
return fmt.Errorf(
"%s is not empty, remove it before running goreleaser or use the --rm-dist flag",
ctx.Config.Dist,
)
}
log.Debug("./dist is empty")
return
}

View File

@ -0,0 +1,66 @@
package cleandist
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/stretchr/testify/assert"
)
func TestDistDoesNotExist(t *testing.T) {
var assert = assert.New(t)
assert.NoError(
Pipe{}.Run(
&context.Context{
Config: config.Project{
Dist: "/wtf-this-shouldnt-exist",
},
},
),
)
}
func TestPopulatedDistExists(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "disttest")
assert.NoError(err)
var dist = filepath.Join(folder, "dist")
assert.NoError(os.Mkdir(dist, 0755))
_, err = os.Create(filepath.Join(dist, "mybin"))
assert.NoError(err)
var ctx = &context.Context{
Config: config.Project{
Dist: dist,
},
}
assert.Error(Pipe{}.Run(ctx))
ctx.RmDist = true
assert.NoError(Pipe{}.Run(ctx))
_, err = os.Stat(dist)
assert.False(os.IsExist(err))
}
func TestEmptyDistExists(t *testing.T) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "disttest")
assert.NoError(err)
var dist = filepath.Join(folder, "dist")
assert.NoError(os.Mkdir(dist, 0755))
var ctx = &context.Context{
Config: config.Project{
Dist: dist,
},
}
assert.NoError(Pipe{}.Run(ctx))
_, err = os.Stat(dist)
assert.False(os.IsExist(err))
}
func TestDescription(t *testing.T) {
var assert = assert.New(t)
assert.NotEmpty(Pipe{}.Description())
}