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

Merge pull request #145 from goreleaser/config

changing repo config
This commit is contained in:
Carlos Alexandro Becker 2017-03-23 10:34:14 -03:00 committed by GitHub
commit 09e1e4595c
18 changed files with 183 additions and 163 deletions

View File

@ -57,7 +57,7 @@ By default GoReleaser will build the **main.go** file located in your current di
# goreleaser.yml
# Build customization
build:
binary_name: drum-roll
binary: drum-roll
goos:
- windows
- darwin
@ -68,7 +68,7 @@ build:
This configuration specifies the build operating systems to Windows, Linux and MacOS using 64bit architecture, the name of the binaries is `drum-roll`.
GoReleaser will then archive the result binaries of each Os/Arch into a separate file. The default format is `{{.BinaryName}}_{{.Os}}_{{.Arch}}`.
GoReleaser will then archive the result binaries of each Os/Arch into a separate file. The default format is `{{.Binary}}_{{.Os}}_{{.Arch}}`.
You can change the archives name and format. You can also replace the OS and the Architecture with your own.
Another useful feature is to add files to archives, this is very useful for integrating assets like resource files.
@ -77,7 +77,7 @@ Another useful feature is to add files to archives, this is very useful for inte
# Build customization
build:
main: main.go
binary_name: drum-roll
binary: drum-roll
goos:
- windows
- darwin
@ -170,7 +170,7 @@ build:
# Name of the binary.
# Default is the name of the project directory.
binary_name: program
binary: program
# Custom build tags.
# Default is empty
@ -209,12 +209,12 @@ archive:
# You can change the name of the archive.
# This is parsed with Golang template engine and the following variables
# are available:
# - BinaryName
# - Binary
# - Version
# - Os
# - Arch
# The default is `{{.BinaryName}}_{{.Os}}_{{.Arch}}`
name_template: "{{.BinaryName}}_{{.Version}}_{{.Os}}_{{.Arch}}"
# The default is `{{.Binary}}_{{.Os}}_{{.Arch}}`
name_template: "{{.Binary}}_{{.Version}}_{{.Os}}_{{.Arch}}"
# Archive format. Valid options are `tar.gz` and `zip`.
# Default is `tar.gz`
@ -247,7 +247,9 @@ archive:
release:
# Repo in which the release will be created.
# Default is extracted from the origin remote URL.
repo: user/repo
github:
owner: user
name: repo
```
### Homebrew tap customization
@ -259,7 +261,9 @@ Check [the Homebrew documentation](https://github.com/Homebrew/brew/blob/master/
# goreleaser.yml
brew:
# Reporitory to push the tap to.
repo: user/homebrew-tap
github:
owner: user
name: homebrew-tap
# Folder inside the repository to put the formula.
# Default is the root folder.

View File

@ -6,9 +6,21 @@ import (
yaml "gopkg.in/yaml.v1"
)
// Repo represents any kind of repo (github, gitlab, etc)
type Repo struct {
Owner string
Name string
}
// String of the repo, e.g. owner/name
func (r Repo) String() string {
return r.Owner + "/" + r.Name
}
// Homebrew contains the brew section
type Homebrew struct {
Repo string
Repo string // deprecated!
GitHub Repo
Folder string
Caveats string
Plist string
@ -30,7 +42,8 @@ type Build struct {
Main string
Ldflags string
Flags string
BinaryName string `yaml:"binary_name"`
BinaryName string `yaml:"binary_name"` // deprecated
Binary string
Hooks Hooks
}
@ -44,7 +57,8 @@ type Archive struct {
// Release config used for the GitHub release
type Release struct {
Repo string
Repo string // deprecated!
GitHub Repo
}
// FPM config

View File

@ -13,21 +13,14 @@ type GitInfo struct {
Diff string
}
// Repo owner/name pair
type Repo struct {
Owner, Name string
}
// Context carries along some data through the pipes
type Context struct {
ctx.Context
Config config.Project
Token string
Git GitInfo
ReleaseRepo Repo
BrewRepo Repo
Archives map[string]string
Version string
Config config.Project
Token string
Git GitInfo
Archives map[string]string
Version string
}
// New context

View File

@ -1,5 +1,7 @@
brew:
repo: goreleaser/homebrew-tap
github:
owner: goreleaser
name: homebrew-tap
folder: Formula
dependencies:
- git

View File

@ -15,7 +15,6 @@ import (
"github.com/goreleaser/goreleaser/pipeline/fpm"
"github.com/goreleaser/goreleaser/pipeline/git"
"github.com/goreleaser/goreleaser/pipeline/release"
"github.com/goreleaser/goreleaser/pipeline/repos"
"github.com/goreleaser/goreleaser/pipeline/source"
"github.com/urfave/cli"
)
@ -66,7 +65,6 @@ func main() {
func pipes(buildOnly bool) []pipeline.Pipe {
var pipes = []pipeline.Pipe{
defaults.Pipe{}, // load default configs
repos.Pipe{}, // split repos into owner/name pairs
}
if !buildOnly {
pipes = append(

View File

@ -10,6 +10,7 @@ import (
"github.com/google/go-github/github"
"github.com/goreleaser/goreleaser/clients"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/sha256sum"
)
@ -21,7 +22,7 @@ var ErrNoDarwin64Build = errors.New("brew tap requires a darwin amd64 build")
const formula = `class {{ .Name }} < Formula
desc "{{ .Desc }}"
homepage "{{ .Homepage }}"
url "https://github.com/{{ .Repo }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
url "https://github.com/{{ .Repo.Owner }}/{{ .Repo.Name }}/releases/download/{{ .Tag }}/{{ .File }}.{{ .Format }}"
version "{{ .Version }}"
sha256 "{{ .SHA256 }}"
@ -64,10 +65,10 @@ type templateData struct {
Name string
Desc string
Homepage string
Repo string
Repo config.Repo // FIXME: will not work for anything but github right now.
Tag string
Version string
BinaryName string
Binary string
Caveats string
File string
Format string
@ -88,15 +89,24 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Brew.Repo == "" {
// TODO: remove this block in next release cycle
if ctx.Config.Brew.Repo != "" {
log.Println("The `brew.repo` syntax is deprecated and will soon be removed. Please check the README for more info.")
var ss = strings.Split(ctx.Config.Brew.Repo, "/")
ctx.Config.Brew.GitHub = config.Repo{
Owner: ss[0],
Name: ss[1],
}
}
if ctx.Config.Brew.GitHub.Name == "" {
return nil
}
client := clients.GitHub(ctx)
path := filepath.Join(
ctx.Config.Brew.Folder, ctx.Config.Build.BinaryName+".rb",
ctx.Config.Brew.Folder, ctx.Config.Build.Binary+".rb",
)
log.Println("Pushing", path, "to", ctx.Config.Brew.Repo)
log.Println("Pushing", path, "to", ctx.Config.Brew.GitHub.String())
out, err := buildFormula(ctx, client)
if err != nil {
return err
@ -109,23 +119,35 @@ func (Pipe) Run(ctx *context.Context) error {
},
Content: out.Bytes(),
Message: github.String(
ctx.Config.Build.BinaryName + " version " + ctx.Git.CurrentTag,
ctx.Config.Build.Binary + " version " + ctx.Git.CurrentTag,
),
}
owner := ctx.BrewRepo.Owner
repo := ctx.BrewRepo.Name
file, _, res, err := client.Repositories.GetContents(
ctx, owner, repo, path, &github.RepositoryContentGetOptions{},
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
&github.RepositoryContentGetOptions{},
)
if err != nil && res.StatusCode == 404 {
_, _, err = client.Repositories.CreateFile(
ctx, owner, repo, path, options,
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
options,
)
return err
}
options.SHA = file.SHA
_, _, err = client.Repositories.UpdateFile(ctx, owner, repo, path, options)
_, _, err = client.Repositories.UpdateFile(
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
options,
)
return err
}
@ -139,7 +161,7 @@ func buildFormula(ctx *context.Context, client *github.Client) (bytes.Buffer, er
func doBuildFormula(data templateData) (bytes.Buffer, error) {
var out bytes.Buffer
tmpl, err := template.New(data.BinaryName).Parse(formula)
tmpl, err := template.New(data.Binary).Parse(formula)
if err != nil {
return out, err
}
@ -153,7 +175,9 @@ func dataFor(
var homepage string
var description string
rep, _, err := client.Repositories.Get(
ctx, ctx.ReleaseRepo.Owner, ctx.ReleaseRepo.Name,
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
)
if err != nil {
return
@ -177,13 +201,13 @@ func dataFor(
description = *rep.Description
}
return templateData{
Name: formulaNameFor(ctx.Config.Build.BinaryName),
Name: formulaNameFor(ctx.Config.Build.Binary),
Desc: description,
Homepage: homepage,
Repo: ctx.Config.Release.Repo,
Repo: ctx.Config.Release.GitHub,
Tag: ctx.Git.CurrentTag,
Version: ctx.Version,
BinaryName: ctx.Config.Build.BinaryName,
Binary: ctx.Config.Build.Binary,
Caveats: ctx.Config.Brew.Caveats,
File: file,
Format: ctx.Config.Archive.Format,

View File

@ -3,6 +3,7 @@ package brew
import (
"testing"
"github.com/goreleaser/goreleaser/config"
"github.com/stretchr/testify/assert"
)
@ -19,16 +20,19 @@ func TestSimpleName(t *testing.T) {
}
var defaultTemplateData = templateData{
BinaryName: "test",
Desc: "Some desc",
Homepage: "https://google.com",
Name: "Test",
Repo: "caarlos0/test",
Tag: "v0.1.3",
Version: "0.1.3",
File: "test_Darwin_x86_64",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
Format: "tar.gz",
Binary: "test",
Desc: "Some desc",
Homepage: "https://google.com",
Name: "Test",
Repo: config.Repo{
Owner: "caarlos0",
Name: "test",
},
Tag: "v0.1.3",
Version: "0.1.3",
File: "test_Darwin_x86_64",
SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68",
Format: "tar.gz",
}
func assertDefaultTemplateData(t *testing.T, formulae string) {

View File

@ -44,7 +44,7 @@ func (Pipe) Run(ctx *context.Context) error {
func build(name, goos, goarch string, ctx *context.Context) error {
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Version
output := "dist/" + name + "/" + ctx.Config.Build.BinaryName + extFor(goos)
output := "dist/" + name + "/" + ctx.Config.Build.Binary + extFor(goos)
log.Println("Building", output)
if ctx.Config.Build.Hooks.Pre != "" {
cmd := strings.Fields(ctx.Config.Build.Hooks.Pre)

View File

@ -2,27 +2,41 @@ package build
import (
"bytes"
"log"
"strings"
"text/template"
"github.com/goreleaser/goreleaser/context"
)
type nameData struct {
Os string
Arch string
Version string
BinaryName string
Os string
Arch string
Version string
Binary 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.Build.BinaryName,
Os: replace(ctx.Config.Archive.Replacements, goos),
Arch: replace(ctx.Config.Archive.Replacements, goarch),
Version: ctx.Git.CurrentTag,
Binary: ctx.Config.Build.Binary,
}
// TODO: remove this block in next release cycle
if strings.Contains(ctx.Config.Archive.NameTemplate, ".BinaryName") {
log.Println("The `.BinaryName` in `archive.name_template` is deprecated and will soon be removed. Please check the README for more info.")
ctx.Config.Archive.NameTemplate = strings.Replace(
ctx.Config.Archive.NameTemplate,
".BinaryName",
".Binary",
-1,
)
}
var out bytes.Buffer
t, err := template.New(data.BinaryName).Parse(ctx.Config.Archive.NameTemplate)
t, err := template.New(data.Binary).Parse(ctx.Config.Archive.NameTemplate)
if err != nil {
return "", err
}

View File

@ -21,14 +21,14 @@ func TestNameFor(t *testing.T) {
var config = config.Project{
Archive: config.Archive{
NameTemplate: "{{.BinaryName}}_{{.Os}}_{{.Arch}}_{{.Version}}",
NameTemplate: "{{.Binary}}_{{.Os}}_{{.Arch}}_{{.Version}}",
Replacements: map[string]string{
"darwin": "Darwin",
"amd64": "x86_64",
},
},
Build: config.Build{
BinaryName: "test",
Binary: "test",
},
}
var ctx = &context.Context{

View File

@ -3,6 +3,7 @@ package defaults
import (
"errors"
"io/ioutil"
"log"
"strings"
"github.com/goreleaser/goreleaser/context"
@ -20,16 +21,25 @@ func (Pipe) Description() string {
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Release.Repo == "" {
// TODO: remove this block in next release cycle
if ctx.Config.Release.Repo != "" {
log.Println("The `release.repo` syntax is deprecated and will soon be removed. Please check the README for more info.")
ctx.Config.Release.GitHub = toRepo(ctx.Config.Release.Repo)
}
if ctx.Config.Release.GitHub.Name == "" {
repo, err := remoteRepo()
ctx.Config.Release.Repo = repo
ctx.Config.Release.GitHub = repo
if err != nil {
return errors.New("failed reading repo from git: " + err.Error())
}
}
if ctx.Config.Build.BinaryName == "" {
ctx.Config.Build.BinaryName = strings.Split(ctx.Config.Release.Repo, "/")[1]
// TODO: remove this block in next release cycle
if ctx.Config.Build.BinaryName != "" {
log.Println("The `build.binary_name` syntax is deprecated and will soon be removed. Please check the README for more info.")
ctx.Config.Build.Binary = ctx.Config.Build.BinaryName
}
if ctx.Config.Build.Binary == "" {
ctx.Config.Build.Binary = ctx.Config.Release.GitHub.Name
}
if ctx.Config.Build.Main == "" {
ctx.Config.Build.Main = "."
@ -45,7 +55,7 @@ func (Pipe) Run(ctx *context.Context) error {
}
if ctx.Config.Archive.NameTemplate == "" {
ctx.Config.Archive.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
ctx.Config.Archive.NameTemplate = "{{.Binary}}_{{.Os}}_{{.Arch}}"
}
if ctx.Config.Archive.Format == "" {
ctx.Config.Archive.Format = "tar.gz"
@ -70,7 +80,7 @@ func (Pipe) Run(ctx *context.Context) error {
ctx.Config.Archive.Files = files
}
if ctx.Config.Brew.Install == "" {
ctx.Config.Brew.Install = "bin.install \"" + ctx.Config.Build.BinaryName + "\""
ctx.Config.Brew.Install = "bin.install \"" + ctx.Config.Build.Binary + "\""
}
return nil
}

View File

@ -17,8 +17,9 @@ func TestFillBasicData(t *testing.T) {
assert.NoError(Pipe{}.Run(ctx))
assert.Equal("goreleaser/goreleaser", ctx.Config.Release.Repo)
assert.Equal("goreleaser", ctx.Config.Build.BinaryName)
assert.Equal("goreleaser", ctx.Config.Release.GitHub.Owner)
assert.Equal("goreleaser", ctx.Config.Release.GitHub.Name)
assert.Equal("goreleaser", ctx.Config.Build.Binary)
assert.Equal(".", ctx.Config.Build.Main)
assert.Equal("tar.gz", ctx.Config.Archive.Format)
assert.Contains(ctx.Config.Build.Goos, "darwin")

View File

@ -4,19 +4,21 @@ import (
"errors"
"os/exec"
"strings"
"github.com/goreleaser/goreleaser/config"
)
// remoteRepo gets the repo name from the Git config.
func remoteRepo() (result string, err error) {
func remoteRepo() (result config.Repo, err error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
bts, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(err.Error() + ": " + string(bts))
return result, errors.New(err.Error() + ": " + string(bts))
}
return extractRepoFromURL(string(bts)), nil
}
func extractRepoFromURL(s string) string {
func extractRepoFromURL(s string) config.Repo {
for _, r := range []string{
"git@github.com:",
".git",
@ -25,5 +27,13 @@ func extractRepoFromURL(s string) string {
} {
s = strings.Replace(s, r, "", -1)
}
return s
return toRepo(s)
}
func toRepo(s string) config.Repo {
var ss = strings.Split(s, "/")
return config.Repo{
Owner: ss[0],
Name: ss[1],
}
}

View File

@ -8,19 +8,19 @@ import (
func TestRepoName(t *testing.T) {
assert := assert.New(t)
name, err := remoteRepo()
repo, err := remoteRepo()
assert.NoError(err)
assert.Equal("goreleaser/goreleaser", name)
assert.Equal("goreleaser/goreleaser", repo.String())
}
func TestExtractReporFromGitURL(t *testing.T) {
assert := assert.New(t)
url := extractRepoFromURL("git@github.com:goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", url)
repo := extractRepoFromURL("git@github.com:goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", repo.String())
}
func TestExtractReporFromHttpsURL(t *testing.T) {
assert := assert.New(t)
url := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", url)
repo := extractRepoFromURL("https://github.com/goreleaser/goreleaser.git")
assert.Equal("goreleaser/goreleaser", repo.String())
}

View File

@ -56,7 +56,7 @@ func (Pipe) Run(ctx *context.Context) error {
func create(ctx *context.Context, format, archive, arch string) error {
var path = filepath.Join("dist", archive)
var file = path + ".deb"
var name = ctx.Config.Build.BinaryName
var name = ctx.Config.Build.Binary
log.Println("Creating", file)
var options = []string{

View File

@ -45,21 +45,35 @@ func (Pipe) Run(ctx *context.Context) error {
}
func getOrCreateRelease(client *github.Client, ctx *context.Context) (*github.RepositoryRelease, error) {
owner := ctx.ReleaseRepo.Owner
repo := ctx.ReleaseRepo.Name
data := &github.RepositoryRelease{
Name: github.String(ctx.Git.CurrentTag),
TagName: github.String(ctx.Git.CurrentTag),
Body: github.String(description(ctx.Git.Diff)),
}
r, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, ctx.Git.CurrentTag)
r, _, err := client.Repositories.GetReleaseByTag(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
ctx.Git.CurrentTag,
)
if err != nil {
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.Repo)
r, _, err = client.Repositories.CreateRelease(ctx, owner, repo, data)
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.GitHub.String())
r, _, err = client.Repositories.CreateRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
data,
)
return r, err
}
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Release.Repo)
r, _, err = client.Repositories.EditRelease(ctx, owner, repo, *r.ID, data)
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Release.GitHub.String())
r, _, err = client.Repositories.EditRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
*r.ID,
data,
)
return r, err
}
@ -94,8 +108,8 @@ func upload(ctx *context.Context, client *github.Client, releaseID int, archive,
log.Println("Uploading", file.Name())
_, _, err = client.Repositories.UploadReleaseAsset(
ctx,
ctx.ReleaseRepo.Owner,
ctx.ReleaseRepo.Name,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
releaseID,
&github.UploadOptions{Name: archive},
file,

View File

@ -1,38 +0,0 @@
package repos
import (
"strings"
"github.com/goreleaser/goreleaser/context"
)
// Pipe for brew deployment
type Pipe struct{}
// Description of the pipe
func (Pipe) Description() string {
return "Setting repositories"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
owner, name := split(ctx.Config.Release.Repo)
ctx.ReleaseRepo = context.Repo{
Owner: owner,
Name: name,
}
owner, name = split(ctx.Config.Brew.Repo)
ctx.BrewRepo = context.Repo{
Owner: owner,
Name: name,
}
return
}
func split(pair string) (string, string) {
parts := strings.Split(pair, "/")
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}

View File

@ -1,30 +0,0 @@
package repos
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSplit(t *testing.T) {
assert := assert.New(t)
a, b := split("a/b")
assert.Equal("a", a)
assert.Equal("b", b)
a, b = split("")
assert.Equal("", a)
assert.Equal("", b)
a, b = split("a")
assert.Equal("a", a)
assert.Equal("", b)
a, b = split("a/")
assert.Equal("a", a)
assert.Equal("", b)
a, b = split("/b")
assert.Equal("", a)
assert.Equal("b", b)
}