You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-09-16 09:26:52 +02:00
even more improvements
This commit is contained in:
@@ -1,28 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNameTemplate(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
// var config = ProjectConfig{
|
|
||||||
// BinaryName: "test",
|
|
||||||
// Git: GitInfo{
|
|
||||||
// CurrentTag: "v1.2.3",
|
|
||||||
// },
|
|
||||||
// Archive: ArchiveConfig{
|
|
||||||
// NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}_{{.Version}}",
|
|
||||||
// Replacements: map[string]string{
|
|
||||||
// "darwin": "Darwin",
|
|
||||||
// "amd64": "x86_64",
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
// name, err := config.ArchiveName("darwin", "amd64")
|
|
||||||
// assert.NoError(err)
|
|
||||||
// assert.Equal("test_Darwin_x86_64_v1.2.3", name)
|
|
||||||
assert.True(true)
|
|
||||||
}
|
|
@@ -1,11 +1,6 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import "github.com/goreleaser/releaser/config"
|
||||||
"bytes"
|
|
||||||
"html/template"
|
|
||||||
|
|
||||||
"github.com/goreleaser/releaser/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GitInfo includes tags and diffs used in some point
|
// GitInfo includes tags and diffs used in some point
|
||||||
type GitInfo struct {
|
type GitInfo struct {
|
||||||
@@ -24,43 +19,12 @@ type Context struct {
|
|||||||
Git *GitInfo
|
Git *GitInfo
|
||||||
Repo *Repo
|
Repo *Repo
|
||||||
BrewRepo *Repo
|
BrewRepo *Repo
|
||||||
Archives []string
|
Archives map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config config.ProjectConfig) *Context {
|
func New(config config.ProjectConfig) *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
Config: &config,
|
Config: &config,
|
||||||
|
Archives: map[string]string{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type archiveNameData struct {
|
|
||||||
Os string
|
|
||||||
Arch string
|
|
||||||
Version string
|
|
||||||
BinaryName string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ArchiveName
|
|
||||||
func (context *Context) ArchiveName(goos, goarch string) (string, error) {
|
|
||||||
var data = archiveNameData{
|
|
||||||
Os: replace(context.Config.Archive.Replacements, goos),
|
|
||||||
Arch: replace(context.Config.Archive.Replacements, goarch),
|
|
||||||
Version: context.Git.CurrentTag,
|
|
||||||
BinaryName: context.Config.BinaryName,
|
|
||||||
}
|
|
||||||
var out bytes.Buffer
|
|
||||||
t, err := template.New(data.BinaryName).Parse(context.Config.Archive.NameTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
err = t.Execute(&out, data)
|
|
||||||
return out.String(), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func replace(replacements map[string]string, original string) string {
|
|
||||||
result := replacements[original]
|
|
||||||
if result == "" {
|
|
||||||
return original
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
@@ -109,10 +109,7 @@ func dataFor(ctx *context.Context, client *github.Client) (result templateData,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file, err := ctx.ArchiveName("darwin", "amd64")
|
file := ctx.Archives["darwinamd64"]
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format)
|
sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@@ -22,26 +22,26 @@ func (Pipe) Name() string {
|
|||||||
// Run the pipe
|
// Run the pipe
|
||||||
func (Pipe) Run(ctx *context.Context) error {
|
func (Pipe) Run(ctx *context.Context) error {
|
||||||
var g errgroup.Group
|
var g errgroup.Group
|
||||||
for _, system := range ctx.Config.Build.Oses {
|
for _, goos := range ctx.Config.Build.Oses {
|
||||||
for _, arch := range ctx.Config.Build.Arches {
|
for _, goarch := range ctx.Config.Build.Arches {
|
||||||
system := system
|
goos := goos
|
||||||
arch := arch
|
goarch := goarch
|
||||||
name, err := ctx.ArchiveName(system, arch)
|
name, err := nameFor(ctx, goos, goarch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx.Archives = append(ctx.Archives, name)
|
ctx.Archives[goos+goarch] = name
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
return build(name, system, arch, ctx)
|
return build(name, goos, goarch, ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return g.Wait()
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func build(name, system, arch string, ctx *context.Context) error {
|
func build(name, goos, goarch string, ctx *context.Context) error {
|
||||||
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
|
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
|
||||||
output := "dist/" + name + "/" + ctx.Config.BinaryName + extFor(system)
|
output := "dist/" + name + "/" + ctx.Config.BinaryName + extFor(goos)
|
||||||
log.Println("Building", output, "...")
|
log.Println("Building", output, "...")
|
||||||
cmd := exec.Command(
|
cmd := exec.Command(
|
||||||
"go",
|
"go",
|
||||||
@@ -52,8 +52,8 @@ func build(name, system, arch string, ctx *context.Context) error {
|
|||||||
)
|
)
|
||||||
cmd.Env = append(
|
cmd.Env = append(
|
||||||
cmd.Env,
|
cmd.Env,
|
||||||
"GOOS="+system,
|
"GOOS="+goos,
|
||||||
"GOARCH="+arch,
|
"GOARCH="+goarch,
|
||||||
"GOROOT="+os.Getenv("GOROOT"),
|
"GOROOT="+os.Getenv("GOROOT"),
|
||||||
"GOPATH="+os.Getenv("GOPATH"),
|
"GOPATH="+os.Getenv("GOPATH"),
|
||||||
)
|
)
|
||||||
@@ -65,10 +65,3 @@ func build(name, system, arch string, ctx *context.Context) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extFor(system string) string {
|
|
||||||
if system == "windows" {
|
|
||||||
return ".exe"
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
@@ -1,15 +0,0 @@
|
|||||||
package build
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestExtWindows(t *testing.T) {
|
|
||||||
assert.Equal(t, extFor("windows"), ".exe")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestExtOthers(t *testing.T) {
|
|
||||||
assert.Empty(t, extFor("linux"))
|
|
||||||
}
|
|
46
pipeline/build/name.go
Normal file
46
pipeline/build/name.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/goreleaser/releaser/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type nameData struct {
|
||||||
|
Os string
|
||||||
|
Arch string
|
||||||
|
Version string
|
||||||
|
BinaryName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func nameFor(ctx *context.Context, goos, goarch string) (string, error) {
|
||||||
|
var data = nameData{
|
||||||
|
Os: replace(ctx.Config.Archive.Replacements, goos),
|
||||||
|
Arch: replace(ctx.Config.Archive.Replacements, goarch),
|
||||||
|
Version: ctx.Git.CurrentTag,
|
||||||
|
BinaryName: ctx.Config.BinaryName,
|
||||||
|
}
|
||||||
|
var out bytes.Buffer
|
||||||
|
t, err := template.New(data.BinaryName).Parse(ctx.Config.Archive.NameTemplate)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
err = t.Execute(&out, data)
|
||||||
|
return out.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func replace(replacements map[string]string, original string) string {
|
||||||
|
result := replacements[original]
|
||||||
|
if result == "" {
|
||||||
|
return original
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func extFor(goos string) string {
|
||||||
|
if goos == "windows" {
|
||||||
|
return ".exe"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
42
pipeline/build/name_test.go
Normal file
42
pipeline/build/name_test.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package build
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/goreleaser/releaser/config"
|
||||||
|
"github.com/goreleaser/releaser/context"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExtWindows(t *testing.T) {
|
||||||
|
assert.Equal(t, extFor("windows"), ".exe")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtOthers(t *testing.T) {
|
||||||
|
assert.Empty(t, extFor("linux"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNameFor(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
var config = &config.ProjectConfig{
|
||||||
|
BinaryName: "test",
|
||||||
|
Archive: config.ArchiveConfig{
|
||||||
|
NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}_{{.Version}}",
|
||||||
|
Replacements: map[string]string{
|
||||||
|
"darwin": "Darwin",
|
||||||
|
"amd64": "x86_64",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
var ctx = &context.Context{
|
||||||
|
Config: config,
|
||||||
|
Git: &context.GitInfo{
|
||||||
|
CurrentTag: "v1.2.3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
name, err := nameFor(ctx, "darwin", "amd64")
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal("test_Darwin_x86_64_v1.2.3", name)
|
||||||
|
}
|
Reference in New Issue
Block a user