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

continuing

This commit is contained in:
Carlos Alexandro Becker 2017-01-06 11:07:41 -02:00
parent 41386c001f
commit 43f1f9c714
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
9 changed files with 92 additions and 48 deletions

View File

@ -9,9 +9,6 @@ import (
"github.com/goreleaser/releaser/config/git"
yaml "gopkg.in/yaml.v1"
"bytes"
"text/template"
"github.com/goreleaser/releaser/uname"
)
var filePatterns = []string{"LICENCE*", "LICENSE*", "README*", "CHANGELOG*"}
@ -68,33 +65,6 @@ func Load(file string) (config ProjectConfig, err error) {
return config, config.validate()
}
type NameData struct {
Goos string
Goarch string
Os string
Arch string
Version string
BinaryName string
}
func (config *ProjectConfig) NameFor(goos, goarch, version string) (string, error) {
var data = NameData{
Goos: goos,
Goarch: goarch,
Os: uname.FromGo(goos),
Arch: uname.FromGo(goarch),
Version: version,
BinaryName: config.BinaryName,
}
var out bytes.Buffer
template, err := template.New(data.BinaryName).Parse(config.NameTemplate)
if err != nil {
}
err = template.Execute(&out, data)
return out.String(), err
}
func (config *ProjectConfig) validate() (err error) {
if config.BinaryName == "" {
return errors.New("missing binary_name")

View File

@ -69,17 +69,6 @@ func TestValidadeMinimalConfig(t *testing.T) {
assert.NoError(config.validate())
}
func TestNameTemplate(t *testing.T) {
assert := assert.New(t)
var config = ProjectConfig{
BinaryName: "test",
NameTemplate : "{{.BinaryName}}_{{.Os}}_{{.Goos}}_{{.Arch}}_{{.Goarch}}_{{.Version}}",
}
name, err := config.NameFor("darwin", "amd64", "v1.2.3")
assert.NoError(err)
assert.Equal("test_Darwin_darwin_x86_64_amd64_v1.2.3", name)
}
func assertFiles(t *testing.T, dir string, files []string) {
assert := assert.New(t)

32
name/name.go Normal file
View File

@ -0,0 +1,32 @@
package name
import (
"bytes"
"text/template"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/uname"
)
type nameData struct {
Os string
Arch string
Version string
BinaryName string
}
func For(config config.ProjectConfig, goos, goarch string) (string, error) {
var data = nameData{
Os: uname.FromGo(goos),
Arch: uname.FromGo(goarch),
Version: config.Git.CurrentTag,
BinaryName: config.BinaryName,
}
var out bytes.Buffer
template, err := template.New(data.BinaryName).Parse(config.NameTemplate)
if err != nil {
return "", err
}
err = template.Execute(&out, data)
return out.String(), err
}

22
name/name_test.go Normal file
View File

@ -0,0 +1,22 @@
package name
import (
"testing"
"github.com/goreleaser/releaser/config"
"github.com/stretchr/testify/assert"
)
func TestNameTemplate(t *testing.T) {
assert := assert.New(t)
var config = config.ProjectConfig{
BinaryName: "test",
NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}_{{.Version}}",
Git: config.GitInfo{
CurrentTag: "v1.2.3",
},
}
name, err := For(config, "darwin", "amd64")
assert.NoError(err)
assert.Equal("test_Darwin_x86_64_v1.2.3", name)
}

View File

@ -13,12 +13,13 @@ import (
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/split"
"golang.org/x/oauth2"
"github.com/goreleaser/releaser/name"
)
const formulae = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .BinaryName }}_#{%x(uname -s).gsub(/\n/, '')}_#{%x(uname -m).gsub(/\n/, '')}.tar.gz"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .File }}.tar.gz"
head "https://github.com/{{ .Repo }}.git"
version "{{ .Tag }}"
@ -36,7 +37,7 @@ end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats string
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats, File string
}
// Pipe for brew deployment
@ -83,6 +84,7 @@ func (Pipe) Run(config config.ProjectConfig) error {
)
return err
}
func sha(client *github.Client, owner, repo, name string, out bytes.Buffer) (*string, error) {
file, _, _, err := client.Repositories.GetContents(
owner, repo, name, &github.RepositoryContentGetOptions{},
@ -129,6 +131,10 @@ func dataFor(config config.ProjectConfig, client *github.Client) (result templat
} else {
description = *rep.Description
}
file, err := fileName(config)
if err != nil {
return result, err
}
return templateData{
Name: formulaNameFor(config.BinaryName),
Desc: description,
@ -137,9 +143,18 @@ func dataFor(config config.ProjectConfig, client *github.Client) (result templat
Tag: config.Git.CurrentTag,
BinaryName: config.BinaryName,
Caveats: config.Brew.Caveats,
File: file,
}, err
}
func fileName(config config.ProjectConfig) (string, error) {
return name.For(
config,
"#{%x(uname -s).gsub(/\n/, '')}",
"#{%x(uname -m).gsub(/\n/, '')}",
)
}
func formulaNameFor(name string) string {
name = strings.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/goreleaser/releaser/config"
)
func TestNameWithDash(t *testing.T) {
@ -25,6 +26,7 @@ var defaultTemplateData = templateData{
Name: "Test",
Repo: "caarlos0/test",
Tag: "v0.1.3",
File: "test_#{%x(uname -s).gsub(/\\n/, '')}_#{%x(uname -m).gsub(/\\n/, '')}",
}
func assertDefaultTemplateData(t *testing.T, formulae string) {
@ -57,3 +59,16 @@ func TestFormulaeNoCaveats(t *testing.T) {
assertDefaultTemplateData(t, formulae)
assert.NotContains(formulae, "def caveats")
}
func TestFilename(t *testing.T) {
assert := assert.New(t)
name, err := fileName(config.ProjectConfig{
BinaryName: "test",
NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}",
Git: config.GitInfo{
CurrentTag:"v1.2.3",
},
})
assert.NoError(err)
assert.Equal("test_#{%x(uname -s).gsub(/\n/, '')}_#{%x(uname -m).gsub(/\n/, '')}", name)
}

View File

@ -8,6 +8,7 @@ import (
"os/exec"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/name"
"golang.org/x/sync/errgroup"
)
@ -36,7 +37,7 @@ func (Pipe) Run(config config.ProjectConfig) error {
func build(system, arch string, config config.ProjectConfig) error {
log.Println("Building", system+"/"+arch, "...")
name, err := config.NameFor(system, arch, config.Git.CurrentTag)
name, err := name.For(config, system, arch)
if err != nil {
return err
}

View File

@ -8,9 +8,8 @@ import (
"os"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/uname"
"github.com/goreleaser/releaser/name"
"golang.org/x/sync/errgroup"
"google.golang.org/appengine/file"
)
// Pipe for compress
@ -37,7 +36,7 @@ func (Pipe) Run(config config.ProjectConfig) error {
}
func create(system, arch string, config config.ProjectConfig) error {
name, err := config.NameFor(system, arch, config.Git.CurrentTag)
name, err := name.For(config, system, arch)
if err != nil {
return err
}

View File

@ -8,6 +8,7 @@ import (
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/name"
"github.com/goreleaser/releaser/split"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
@ -45,7 +46,7 @@ func (Pipe) Run(config config.ProjectConfig) error {
system := system
arch := arch
g.Go(func() error {
name, err := config.NameFor(system, arch, config.Git.CurrentTag)
name, err := name.For(config, system, arch)
if err != nil {
return err
}