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

191 lines
4.6 KiB
Go
Raw Normal View History

2017-04-14 15:39:32 -03:00
// Package defaults implements the Pipe interface providing default values
// for missing configuration.
2017-01-14 12:34:22 -02:00
package defaults
import (
2017-05-01 09:59:18 -03:00
"fmt"
2017-06-27 19:20:08 -03:00
"strings"
2017-01-14 12:34:22 -02:00
2017-06-27 19:20:08 -03:00
"github.com/apex/log"
"github.com/goreleaser/goreleaser/config"
2017-01-14 20:01:32 -02:00
"github.com/goreleaser/goreleaser/context"
2017-01-14 12:34:22 -02:00
)
2017-04-24 14:27:21 -03:00
// NameTemplate default name_template for the archive.
2017-08-27 20:45:33 -03:00
const NameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
2017-04-24 14:27:21 -03:00
// ReleaseNameTemplate is the default name for the release.
const ReleaseNameTemplate = "{{.Tag}}"
2017-04-29 12:49:22 +02:00
// SnapshotNameTemplate represents the default format for snapshot release names.
2017-05-01 09:59:18 -03:00
const SnapshotNameTemplate = "SNAPSHOT-{{ .Commit }}"
2017-04-29 12:49:22 +02:00
2017-08-27 20:45:33 -03:00
// ChecksumNameTemplate is the default name_template for the checksum file.
const ChecksumNameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt"
2017-01-14 12:34:22 -02:00
// Pipe for brew deployment
type Pipe struct{}
2017-01-14 19:41:32 +01:00
// Description of the pipe
2017-01-14 15:14:35 -02:00
func (Pipe) Description() string {
2017-01-19 10:04:41 +01:00
return "Setting defaults"
2017-01-14 12:34:22 -02:00
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error { // nolint: gocyclo
2017-05-01 09:59:18 -03:00
ctx.Config.Dist = "dist"
if ctx.Config.Release.NameTemplate == "" {
ctx.Config.Release.NameTemplate = ReleaseNameTemplate
}
2017-04-29 12:49:22 +02:00
if ctx.Config.Snapshot.NameTemplate == "" {
ctx.Config.Snapshot.NameTemplate = SnapshotNameTemplate
}
2017-08-27 20:45:33 -03:00
if ctx.Config.Checksum.NameTemplate == "" {
ctx.Config.Checksum.NameTemplate = ChecksumNameTemplate
}
2017-05-01 09:59:18 -03:00
if err := setReleaseDefaults(ctx); err != nil {
return err
}
2017-07-01 22:42:10 -03:00
if ctx.Config.ProjectName == "" {
ctx.Config.ProjectName = ctx.Config.Release.GitHub.Name
2017-06-27 19:20:08 -03:00
}
2017-05-01 09:59:18 -03:00
setBuildDefaults(ctx)
2017-05-01 09:59:18 -03:00
if ctx.Config.Brew.Install == "" {
2017-06-27 19:20:08 -03:00
var installs []string
for _, build := range ctx.Config.Builds {
2017-07-01 22:06:40 -03:00
if !isBrewBuild(build) {
continue
}
2017-06-27 19:20:08 -03:00
installs = append(
installs,
fmt.Sprintf(`bin.install "%s"`, build.Binary),
)
}
ctx.Config.Brew.Install = strings.Join(installs, "\n")
}
2017-09-11 23:42:36 -03:00
if ctx.Config.Brew.CommitAuthor.Name == "" {
ctx.Config.Brew.CommitAuthor.Name = "goreleaserbot"
}
if ctx.Config.Brew.CommitAuthor.Email == "" {
ctx.Config.Brew.CommitAuthor.Email = "goreleaser@carlosbecker.com"
}
2017-06-27 19:20:08 -03:00
err := setArchiveDefaults(ctx)
2017-09-14 21:19:56 -03:00
setDockerDefaults(ctx)
setFpmDefaults(ctx)
2017-06-27 19:20:08 -03:00
log.WithField("config", ctx.Config).Debug("defaults set")
return err
2017-05-01 09:59:18 -03:00
}
2017-09-14 21:19:56 -03:00
func setDockerDefaults(ctx *context.Context) {
if len(ctx.Config.Dockers) != 1 {
return
}
if ctx.Config.Dockers[0].Goos == "" {
ctx.Config.Dockers[0].Goos = "linux"
}
if ctx.Config.Dockers[0].Goarch == "" {
ctx.Config.Dockers[0].Goarch = "amd64"
}
if ctx.Config.Dockers[0].Binary == "" {
ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary
}
if ctx.Config.Dockers[0].Dockerfile == "" {
ctx.Config.Dockers[0].Dockerfile = "Dockerfile"
}
}
2017-07-01 22:06:40 -03:00
func isBrewBuild(build config.Build) bool {
for _, ignore := range build.Ignore {
if ignore.Goos == "darwin" && ignore.Goarch == "amd64" {
return false
}
}
return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64")
}
func contains(ss []string, s string) bool {
for _, zs := range ss {
if zs == s {
return true
}
}
return false
}
2017-05-01 09:59:18 -03:00
func setReleaseDefaults(ctx *context.Context) error {
if ctx.Config.Release.GitHub.Name != "" {
return nil
}
repo, err := remoteRepo()
if err != nil {
return err
2017-05-01 09:59:18 -03:00
}
ctx.Config.Release.GitHub = repo
return nil
}
func setBuildDefaults(ctx *context.Context) {
2017-07-01 21:13:02 -03:00
for i, build := range ctx.Config.Builds {
ctx.Config.Builds[i] = buildWithDefaults(ctx, build)
}
if len(ctx.Config.Builds) == 0 {
ctx.Config.Builds = []config.Build{
2017-07-01 21:27:30 -03:00
buildWithDefaults(ctx, ctx.Config.SingleBuild),
2017-06-27 20:06:45 -03:00
}
2017-01-14 12:34:22 -02:00
}
2017-07-01 21:13:02 -03:00
}
func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
if build.Binary == "" {
build.Binary = ctx.Config.Release.GitHub.Name
}
if build.Main == "" {
build.Main = "."
}
if len(build.Goos) == 0 {
build.Goos = []string{"linux", "darwin"}
}
if len(build.Goarch) == 0 {
build.Goarch = []string{"amd64", "386"}
}
if len(build.Goarm) == 0 {
build.Goarm = []string{"6"}
}
if build.Ldflags == "" {
build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}"
}
return build
2017-05-01 09:59:18 -03:00
}
2017-05-01 09:59:18 -03:00
func setArchiveDefaults(ctx *context.Context) error {
2017-01-14 14:06:57 -02:00
if ctx.Config.Archive.NameTemplate == "" {
2017-04-24 14:27:21 -03:00
ctx.Config.Archive.NameTemplate = NameTemplate
2017-01-14 12:34:22 -02:00
}
2017-01-14 14:06:57 -02:00
if ctx.Config.Archive.Format == "" {
ctx.Config.Archive.Format = "tar.gz"
2017-01-14 12:34:22 -02:00
}
if len(ctx.Config.Archive.Files) == 0 {
2017-05-11 00:05:51 -03:00
ctx.Config.Archive.Files = []string{
"licence*",
"LICENCE*",
"license*",
"LICENSE*",
"readme*",
"README*",
"changelog*",
"CHANGELOG*",
}
2017-01-14 12:34:22 -02:00
}
2017-01-14 19:41:32 +01:00
return nil
2017-01-14 12:34:22 -02:00
}
func setFpmDefaults(ctx *context.Context) {
if ctx.Config.FPM.Bindir == "" {
ctx.Config.FPM.Bindir = "/usr/local/bin"
}
}