1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-17 20:47:50 +02:00

Merge pull request #48 from goreleaser/name-template

WIP: custom naming templates
This commit is contained in:
Carlos Alexandro Becker 2017-01-11 15:03:48 -02:00 committed by GitHub
commit 8f0ce61122
12 changed files with 126 additions and 42 deletions

View File

@ -8,3 +8,7 @@ insert_final_newline = true
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8
[*.md]
indent_size = 2
indent_style = space

View File

@ -24,7 +24,7 @@ lint: ## Run all the linters
--enable=errcheck \
--enable=vet \
--enable=vetshadow \
--deadline=20s \
--deadline=1m \
./...
ci: lint test ## Run all the tests and code checks

View File

@ -31,14 +31,14 @@ You may then run releaser at the root of your repository:
curl -s https://raw.githubusercontent.com/goreleaser/get/master/latest | bash
```
This will build `main.go` file as `my-binary`, for _Darwin_ and _Linux_,
_x86_64_ and _i386_, packaging the binary, `LICENSE.md` and `README.md`
and publish a new github release in the `user/repo` repository with
the `.tar.gz` files there.
This will build `main.go` as `my-binary`, for `Darwin` and `Linux`
(`amd64` and `i386`), archive the binary and common files as `.tar.gz`,
and finally, publish a new github release in the `user/repo` repository with
archives uploaded.
### Homebrew
To push it to a homebrew repo, just add a `brew` section:
To push a basic formulae a homebrew tap repo, just add a `brew` section:
```yaml
repo: user/repo
@ -66,6 +66,22 @@ build:
> `oses` and `arches` should be in `GOOS`/`GOARCH`-compatible format.
### Archive customization
You can customize the name and format of the archive adding an `archive`
section:
```yaml
repo: user/repo
binary_name: my-binary
archive:
name_template: "{{.BinaryName}}_{{.Version}}_{{.Os}}_{{.Arch}}"
format: tar.gz
```
> Default `name_template` is "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
> Valid formats are `tar.gz` and `zip`, default is `tar.gz`
### Add more files
You might also want to change the files that are packaged by adding a `files`
@ -80,6 +96,9 @@ files:
- CHANGELOG.md
```
> By default GoReleaser adds the binary itself, `LICENCE*`, `LICENSE*`,
`README*` and `CHANGELOG*`.
## Wire it with travis-ci
You may want to wire this to auto-deploy your new tags on travis, for example:
@ -100,11 +119,11 @@ And the [homebrew formulae](https://github.com/goreleaser/homebrew-formulae/blob
```rb
class Release < Formula
desc "Deliver Go binaries as fast and easily as possible."
desc "Deliver Go binaries as fast and easily as possible"
homepage "https://github.com/goreleaser/releaser"
url "https://github.com/goreleaser/releaser/releases/download/v0.2.0/release_#{%x(uname -s).gsub(/\n/, '')}_#{%x(uname -m).gsub(/\n/, '')}.tar.gz"
head "https://github.com/goreleaser/releaser.git"
version "v0.2.0"
url "https://github.com/goreleaser/releaser/releases/download/v0.2.8/release_Darwin_x86_64.tar.gz"
version "v0.2.8"
sha256 "9ee30fc358fae8d248a2d7538957089885da321dca3f09e3296fe2058e7fff74"
def install
bin.install "release"

38
config/archive_config.go Normal file
View File

@ -0,0 +1,38 @@
package config
import (
"bytes"
"html/template"
"github.com/goreleaser/releaser/uname"
)
// ArchiveConfig config used for the archive
type ArchiveConfig struct {
Format string
NameTemplate string `yaml:"name_template"`
}
type archiveNameData struct {
Os string
Arch string
Version string
BinaryName string
}
// ArchiveName following the given template
func (config ProjectConfig) ArchiveName(goos, goarch string) (string, error) {
var data = archiveNameData{
Os: uname.FromGo(goos),
Arch: uname.FromGo(goarch),
Version: config.Git.CurrentTag,
BinaryName: config.BinaryName,
}
var out bytes.Buffer
t, err := template.New(data.BinaryName).Parse(config.Archive.NameTemplate)
if err != nil {
return "", err
}
err = t.Execute(&out, data)
return out.String(), err
}

View File

@ -0,0 +1,23 @@
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}}",
},
}
name, err := config.ArchiveName("darwin", "amd64")
assert.NoError(err)
assert.Equal("test_Darwin_x86_64_v1.2.3", name)
}

View File

@ -34,11 +34,6 @@ type GitInfo struct {
Diff string
}
// ArchiveConfig
type ArchiveConfig struct {
Format string
}
// ProjectConfig includes all project configuration
type ProjectConfig struct {
Repo string
@ -112,6 +107,9 @@ func (config *ProjectConfig) fillBasicData() {
if len(config.Build.Arches) == 0 {
config.Build.Arches = []string{"amd64", "386"}
}
if config.Archive.NameTemplate == "" {
config.Archive.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
}
if config.Archive.Format == "" {
config.Archive.Format = "tar.gz"
}

View File

@ -19,7 +19,7 @@ import (
const formulae = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .BinaryName }}_Darwin_x86_64.{{ .Format }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
version "{{ .Tag }}"
sha256 "{{ .SHA256 }}"
@ -37,7 +37,7 @@ end
`
type templateData struct {
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats, Format, SHA256 string
Name, Desc, Homepage, Repo, Tag, BinaryName, Caveats, File, Format, SHA256 string
}
// Pipe for brew deployment
@ -121,7 +121,11 @@ func dataFor(config config.ProjectConfig, client *github.Client) (result templat
if err != nil {
return
}
sum, err := sha256sum.For("dist/" + config.BinaryName + "_Darwin_x86_64." + config.Archive.Format)
file, err := config.ArchiveName("darwin", "amd64")
if err != nil {
return
}
sum, err := sha256sum.For("dist/" + file + config.Archive.Format)
if err != nil {
return
}
@ -143,6 +147,7 @@ func dataFor(config config.ProjectConfig, client *github.Client) (result templat
Tag: config.Git.CurrentTag,
BinaryName: config.BinaryName,
Caveats: config.Brew.Caveats,
File: file,
Format: config.Archive.Format,
SHA256: sum,
}, err

View File

@ -25,6 +25,7 @@ var defaultTemplateData = templateData{
Name: "Test",
Repo: "caarlos0/test",
Tag: "v0.1.3",
File: "test_Darwin_x86_64",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
Format: "tar.gz",
}

View File

@ -8,7 +8,6 @@ import (
"os/exec"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/uname"
"golang.org/x/sync/errgroup"
)
@ -37,11 +36,15 @@ func (Pipe) Run(config config.ProjectConfig) error {
func build(system, arch string, config config.ProjectConfig) error {
log.Println("Building", system+"/"+arch, "...")
name, err := config.ArchiveName(system, arch)
if err != nil {
return err
}
cmd := exec.Command(
"go",
"build",
"-ldflags=-s -w -X main.version="+config.Git.CurrentTag,
"-o", target(system, arch, config.BinaryName),
"-o", "dist/"+name+"/"+config.BinaryName,
config.Build.Main,
)
cmd.Env = append(
@ -54,13 +57,8 @@ func build(system, arch string, config config.ProjectConfig) error {
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
err := cmd.Run()
if err != nil {
if err := cmd.Run(); err != nil {
return errors.New(stdout.String())
}
return nil
}
func target(system, arch, binary string) string {
return "dist/" + binary + "_" + uname.FromGo(system) + "_" + uname.FromGo(arch) + "/" + binary
}

View File

@ -7,7 +7,6 @@ import (
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/pipeline/compress/tar"
"github.com/goreleaser/releaser/pipeline/compress/zip"
"github.com/goreleaser/releaser/uname"
"golang.org/x/sync/errgroup"
)
@ -40,7 +39,11 @@ type Archive interface {
}
func create(system, arch string, config config.ProjectConfig) error {
file, err := os.Create("dist/" + nameFor(system, arch, config.BinaryName) + "." + config.Archive.Format)
name, err := config.ArchiveName(system, arch)
if err != nil {
return err
}
file, err := os.Create("dist/" + name + "." + config.Archive.Format)
log.Println("Creating", file.Name(), "...")
if err != nil {
return err
@ -53,7 +56,7 @@ func create(system, arch string, config config.ProjectConfig) error {
return err
}
}
return archive.Add(config.BinaryName+ext(system), binaryPath(system, arch, config.BinaryName))
return archive.Add(config.BinaryName+extFor(system), "dist/"+name+"/"+config.BinaryName)
}
func archiveFor(file *os.File, format string) Archive {
@ -63,15 +66,7 @@ func archiveFor(file *os.File, format string) Archive {
return tar.New(file)
}
func nameFor(system, arch, binary string) string {
return binary + "_" + uname.FromGo(system) + "_" + uname.FromGo(arch)
}
func binaryPath(system, arch, binary string) string {
return "dist/" + nameFor(system, arch, binary) + "/" + binary
}
func ext(system string) string {
func extFor(system string) string {
if system == "windows" {
return ".exe"
}

View File

@ -7,9 +7,9 @@ import (
)
func TestExtWindows(t *testing.T) {
assert.Equal(t, ext("windows"), ".exe")
assert.Equal(t, extFor("windows"), ".exe")
}
func TestExtOthers(t *testing.T) {
assert.Empty(t, ext("linux"))
assert.Empty(t, extFor("linux"))
}

View File

@ -9,7 +9,6 @@ import (
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/split"
"github.com/goreleaser/releaser/uname"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
)
@ -65,7 +64,11 @@ func description(diff string) string {
func upload(client *github.Client, releaseID int, system, arch string, config config.ProjectConfig) error {
owner, repo := split.OnSlash(config.Repo)
name := config.BinaryName + "_" + uname.FromGo(system) + "_" + uname.FromGo(arch) + "." + config.Archive.Format
name, err := config.ArchiveName(system, arch)
if err != nil {
return err
}
name = name + "." + config.Archive.Format
file, err := os.Open("dist/" + name)
if err != nil {
return err