From b18185da18cb6cc464222d8bbd2d2f2ea2ab2499 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 14 Jan 2017 15:08:10 -0200 Subject: [PATCH] even more improvements --- config/archive_config_test.go | 28 --------------------- context/context.go | 44 +++------------------------------ pipeline/brew/brew.go | 5 +--- pipeline/build/build.go | 29 +++++++++------------- pipeline/build/build_test.go | 15 ------------ pipeline/build/name.go | 46 +++++++++++++++++++++++++++++++++++ pipeline/build/name_test.go | 42 ++++++++++++++++++++++++++++++++ 7 files changed, 104 insertions(+), 105 deletions(-) delete mode 100644 config/archive_config_test.go delete mode 100644 pipeline/build/build_test.go create mode 100644 pipeline/build/name.go create mode 100644 pipeline/build/name_test.go diff --git a/config/archive_config_test.go b/config/archive_config_test.go deleted file mode 100644 index fa0d69720..000000000 --- a/config/archive_config_test.go +++ /dev/null @@ -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) -} diff --git a/context/context.go b/context/context.go index 1b1b38ff5..415f929dd 100644 --- a/context/context.go +++ b/context/context.go @@ -1,11 +1,6 @@ package context -import ( - "bytes" - "html/template" - - "github.com/goreleaser/releaser/config" -) +import "github.com/goreleaser/releaser/config" // GitInfo includes tags and diffs used in some point type GitInfo struct { @@ -24,43 +19,12 @@ type Context struct { Git *GitInfo Repo *Repo BrewRepo *Repo - Archives []string + Archives map[string]string } func New(config config.ProjectConfig) *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 -} diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index c9a406b8a..0e3a27b70 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -109,10 +109,7 @@ func dataFor(ctx *context.Context, client *github.Client) (result templateData, if err != nil { return } - file, err := ctx.ArchiveName("darwin", "amd64") - if err != nil { - return - } + file := ctx.Archives["darwinamd64"] sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format) if err != nil { return diff --git a/pipeline/build/build.go b/pipeline/build/build.go index eb7e68e62..4ed7954f5 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -22,26 +22,26 @@ func (Pipe) Name() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { var g errgroup.Group - for _, system := range ctx.Config.Build.Oses { - for _, arch := range ctx.Config.Build.Arches { - system := system - arch := arch - name, err := ctx.ArchiveName(system, arch) + for _, goos := range ctx.Config.Build.Oses { + for _, goarch := range ctx.Config.Build.Arches { + goos := goos + goarch := goarch + name, err := nameFor(ctx, goos, goarch) if err != nil { return err } - ctx.Archives = append(ctx.Archives, name) + ctx.Archives[goos+goarch] = name g.Go(func() error { - return build(name, system, arch, ctx) + return build(name, goos, goarch, ctx) }) } } 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 - output := "dist/" + name + "/" + ctx.Config.BinaryName + extFor(system) + output := "dist/" + name + "/" + ctx.Config.BinaryName + extFor(goos) log.Println("Building", output, "...") cmd := exec.Command( "go", @@ -52,8 +52,8 @@ func build(name, system, arch string, ctx *context.Context) error { ) cmd.Env = append( cmd.Env, - "GOOS="+system, - "GOARCH="+arch, + "GOOS="+goos, + "GOARCH="+goarch, "GOROOT="+os.Getenv("GOROOT"), "GOPATH="+os.Getenv("GOPATH"), ) @@ -65,10 +65,3 @@ func build(name, system, arch string, ctx *context.Context) error { } return nil } - -func extFor(system string) string { - if system == "windows" { - return ".exe" - } - return "" -} diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go deleted file mode 100644 index 9b721ea6c..000000000 --- a/pipeline/build/build_test.go +++ /dev/null @@ -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")) -} diff --git a/pipeline/build/name.go b/pipeline/build/name.go new file mode 100644 index 000000000..049b869e4 --- /dev/null +++ b/pipeline/build/name.go @@ -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 "" +} diff --git a/pipeline/build/name_test.go b/pipeline/build/name_test.go new file mode 100644 index 000000000..62cdc644d --- /dev/null +++ b/pipeline/build/name_test.go @@ -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) +}