1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-05 13:15:26 +02:00
This commit is contained in:
Carlos Alexandro Becker 2017-01-14 14:06:57 -02:00
parent f8d0601bdd
commit 9e82470a61
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
15 changed files with 215 additions and 165 deletions

View File

@ -1,45 +0,0 @@
package config
import (
"bytes"
"html/template"
)
// ArchiveConfig config used for the archive
type ArchiveConfig struct {
Format string
NameTemplate string `yaml:"name_template"`
Replacements map[string]string
}
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: replace(config.Archive.Replacements, goos),
Arch: replace(config.Archive.Replacements, 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
}
func replace(replacements map[string]string, original string) string {
result := replacements[original]
if result == "" {
return original
}
return result
}

View File

@ -8,20 +8,21 @@ import (
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)
// 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)
}

View File

@ -21,6 +21,13 @@ type BuildConfig struct {
Ldflags string
}
// ArchiveConfig config used for the archive
type ArchiveConfig struct {
Format string
NameTemplate string `yaml:"name_template"`
Replacements map[string]string
}
// ProjectConfig includes all project configuration
type ProjectConfig struct {
Repo string

View File

@ -1,6 +1,11 @@
package context
import "github.com/goreleaser/releaser/config"
import (
"bytes"
"html/template"
"github.com/goreleaser/releaser/config"
)
// GitInfo includes tags and diffs used in some point
type GitInfo struct {
@ -15,7 +20,7 @@ type Repo struct {
type Context struct {
Config *config.ProjectConfig
Token string
Token *string
Git *GitInfo
Repo *Repo
BrewRepo *Repo
@ -24,6 +29,38 @@ type Context struct {
func New(config config.ProjectConfig) *Context {
return &Context{
Config: config,
Config: &config,
}
}
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
}

15
main.go
View File

@ -11,6 +11,7 @@ import (
"github.com/goreleaser/releaser/pipeline/build"
"github.com/goreleaser/releaser/pipeline/compress"
"github.com/goreleaser/releaser/pipeline/defaults"
"github.com/goreleaser/releaser/pipeline/env"
"github.com/goreleaser/releaser/pipeline/git"
"github.com/goreleaser/releaser/pipeline/release"
"github.com/goreleaser/releaser/pipeline/repos"
@ -21,12 +22,18 @@ import (
var version = "master"
var pipes = []pipeline.Pipe{
// load data, set defaults, etc...
defaults.Pipe{},
valid.Pipe{},
env.Pipe{},
git.Pipe{},
repos.Pipe{},
// validate
valid.Pipe{},
// real work
build.Pipe{},
compress.Pipe{},
repos.Pipe{},
release.Pipe{},
brew.Pipe{},
}
@ -50,10 +57,14 @@ func main() {
return cli.NewExitError(err.Error(), 1)
}
context := context.New(config)
log.SetFlags(0)
for _, pipe := range pipes {
log.Println("Executing pipe", pipe.Name(), "...")
log.SetPrefix(" -> ")
if err := pipe.Run(context); err != nil {
return cli.NewExitError(pipe.Name()+" failed: "+err.Error(), 1)
}
log.SetPrefix("")
}
log.Println("Done!")
return

View File

@ -2,17 +2,15 @@ package brew
import (
"bytes"
"context"
goctx "context"
"log"
"path/filepath"
"strings"
"text/template"
"github.com/google/go-github/github"
"github.com/goreleaser/releaser/config"
"github.com/goreleaser/releaser/context"
"github.com/goreleaser/releaser/sha256sum"
"github.com/goreleaser/releaser/split"
"golang.org/x/oauth2"
)
@ -49,20 +47,24 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context context.Context) error {
if context.Config.Brew.Repo == "" {
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Brew.Repo == "" {
return nil
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Token},
&oauth2.Token{AccessToken: *ctx.Token},
)
tc := oauth2.NewClient(context.Background(), ts)
tc := oauth2.NewClient(goctx.Background(), ts)
client := github.NewClient(tc)
owner, repo := split.OnSlash(config.Brew.Repo)
path := filepath.Join(config.Brew.Folder, config.BinaryName+".rb")
log.Println("Updating", path, "on", config.Brew.Repo, "...")
out, err := buildFormulae(config, client)
owner := ctx.BrewRepo.Owner
repo := ctx.BrewRepo.Name
path := filepath.Join(
ctx.Config.Brew.Folder,
ctx.Config.BinaryName+".rb",
)
log.Println("Updating", path, "on", ctx.Config.Brew.Repo, "...")
out, err := buildFormulae(ctx, client)
if err != nil {
return err
}
@ -73,7 +75,9 @@ func (Pipe) Run(context context.Context) error {
Email: github.String("bot@goreleaser"),
},
Content: out.Bytes(),
Message: github.String(config.BinaryName + " version " + config.Git.CurrentTag),
Message: github.String(
ctx.Config.BinaryName + " version " + ctx.Git.CurrentTag,
),
}
file, _, res, err := client.Repositories.GetContents(
@ -88,8 +92,8 @@ func (Pipe) Run(context context.Context) error {
return err
}
func buildFormulae(config config.ProjectConfig, client *github.Client) (bytes.Buffer, error) {
data, err := dataFor(config, client)
func buildFormulae(ctx *context.Context, client *github.Client) (bytes.Buffer, error) {
data, err := dataFor(ctx, client)
if err != nil {
return bytes.Buffer{}, err
}
@ -106,19 +110,18 @@ func doBuildFormulae(data templateData) (bytes.Buffer, error) {
return out, err
}
func dataFor(config config.ProjectConfig, client *github.Client) (result templateData, err error) {
func dataFor(ctx *context.Context, client *github.Client) (result templateData, err error) {
var homepage string
var description string
owner, repo := split.OnSlash(config.Repo)
rep, _, err := client.Repositories.Get(owner, repo)
rep, _, err := client.Repositories.Get(ctx.Repo.Owner, ctx.Repo.Name)
if err != nil {
return
}
file, err := config.ArchiveName("darwin", "amd64")
file, err := ctx.ArchiveName("darwin", "amd64")
if err != nil {
return
}
sum, err := sha256sum.For("dist/" + file + "." + config.Archive.Format)
sum, err := sha256sum.For("dist/" + file + "." + ctx.Config.Archive.Format)
if err != nil {
return
}
@ -133,15 +136,15 @@ func dataFor(config config.ProjectConfig, client *github.Client) (result templat
description = *rep.Description
}
return templateData{
Name: formulaNameFor(config.BinaryName),
Name: formulaNameFor(ctx.Config.BinaryName),
Desc: description,
Homepage: homepage,
Repo: config.Repo,
Tag: config.Git.CurrentTag,
BinaryName: config.BinaryName,
Caveats: config.Brew.Caveats,
Repo: ctx.Config.Repo,
Tag: ctx.Git.CurrentTag,
BinaryName: ctx.Config.BinaryName,
Caveats: ctx.Config.Brew.Caveats,
File: file,
Format: config.Archive.Format,
Format: ctx.Config.Archive.Format,
SHA256: sum,
}, err
}

View File

@ -20,35 +20,35 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) error {
func (Pipe) Run(ctx *context.Context) error {
var g errgroup.Group
for _, system := range context.Config.Build.Oses {
for _, arch := range context.Config.Build.Arches {
for _, system := range ctx.Config.Build.Oses {
for _, arch := range ctx.Config.Build.Arches {
system := system
arch := arch
name, err := context.Config.ArchiveName(system, arch)
name, err := ctx.ArchiveName(system, arch)
if err != nil {
return err
}
context.Archives = append(context.Archives, name)
ctx.Archives = append(ctx.Archives, name)
g.Go(func() error {
return build(name, system, arch, context)
return build(name, system, arch, ctx)
})
}
}
return g.Wait()
}
func build(name, system, arch string, context *context.Context) error {
ldflags := context.Config.Build.Ldflags + " -X main.version=" + context.Git.CurrentTag
output := "dist/" + name + "/" + context.Config.BinaryName + extFor(system)
func build(name, system, arch string, ctx *context.Context) error {
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
output := "dist/" + name + "/" + ctx.Config.BinaryName + extFor(system)
log.Println("Building", output, "...")
cmd := exec.Command(
"go",
"build",
"-ldflags="+ldflags,
"-o", output,
context.Config.Build.Main,
ctx.Config.Build.Main,
)
cmd.Env = append(
cmd.Env,

View File

@ -4,6 +4,7 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/goreleaser/releaser/context"
"github.com/goreleaser/releaser/pipeline/compress/tar"
@ -20,12 +21,12 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) error {
func (Pipe) Run(ctx *context.Context) error {
var g errgroup.Group
for _, archive := range context.Archives {
for _, archive := range ctx.Archives {
archive := archive
g.Go(func() error {
return create(archive, context)
return create(archive, ctx)
})
}
return g.Wait()
@ -36,26 +37,27 @@ type Archive interface {
Add(name, path string) error
}
func create(archive string, context *context.Context) error {
file, err := os.Create("dist/" + archive + "." + context.Config.Archive.Format)
func create(name string, ctx *context.Context) error {
file, err := os.Create("dist/" + name + "." + ctx.Config.Archive.Format)
log.Println("Creating", file.Name(), "...")
if err != nil {
return err
}
defer func() { _ = file.Close() }()
var archive = archiveFor(file, context.Config.Archive.Format)
var archive = archiveFor(file, ctx.Config.Archive.Format)
defer func() { _ = archive.Close() }()
for _, f := range context.Config.Files {
for _, f := range ctx.Config.Files {
if err := archive.Add(f, f); err != nil {
return err
}
}
files, err := ioutil.ReadDir("dist/" + name)
folder := filepath.Join("dist", name)
files, err := ioutil.ReadDir(folder)
if err != nil {
return err
}
for _, f := range files {
if err := archive.Add(file.Name(), f); err != nil {
if err := archive.Add(file.Name(), filepath.Join(folder, f.Name())); err != nil {
return err
}
}

View File

@ -19,27 +19,27 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) (err error) {
if context.Config.Build.Main == "" {
context.Config.Build.Main = "main.go"
func (Pipe) Run(ctx *context.Context) (err error) {
if ctx.Config.Build.Main == "" {
ctx.Config.Build.Main = "main.go"
}
if len(context.Config.Build.Oses) == 0 {
context.Config.Build.Oses = []string{"linux", "darwin"}
if len(ctx.Config.Build.Oses) == 0 {
ctx.Config.Build.Oses = []string{"linux", "darwin"}
}
if len(context.Config.Build.Arches) == 0 {
context.Config.Build.Arches = []string{"amd64", "386"}
if len(ctx.Config.Build.Arches) == 0 {
ctx.Config.Build.Arches = []string{"amd64", "386"}
}
if context.Config.Build.Ldflags == "" {
context.Config.Build.Ldflags = "-s -w"
if ctx.Config.Build.Ldflags == "" {
ctx.Config.Build.Ldflags = "-s -w"
}
if context.Config.Archive.NameTemplate == "" {
context.Config.Archive.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
if ctx.Config.Archive.NameTemplate == "" {
ctx.Config.Archive.NameTemplate = "{{.BinaryName}}_{{.Os}}_{{.Arch}}"
}
if context.Config.Archive.Format == "" {
context.Config.Archive.Format = "tar.gz"
if ctx.Config.Archive.Format == "" {
ctx.Config.Archive.Format = "tar.gz"
}
if len(context.Config.Archive.Replacements) == 0 {
context.Config.Archive.Replacements = map[string]string{
if len(ctx.Config.Archive.Replacements) == 0 {
ctx.Config.Archive.Replacements = map[string]string{
"darwin": "Darwin",
"linux": "Linux",
"freebsd": "FreeBSD",
@ -50,17 +50,17 @@ func (Pipe) Run(context *context.Context) (err error) {
"amd64": "x86_64",
}
}
if len(context.Config.Files) != 0 {
if len(ctx.Config.Files) != 0 {
return
}
context.Config.Files = []string{}
ctx.Config.Files = []string{}
for _, pattern := range filePatterns {
matches, err := globPath(pattern)
if err != nil {
return err
}
context.Config.Files = append(context.Config.Files, matches...)
ctx.Config.Files = append(ctx.Config.Files, matches...)
}
return
}

28
pipeline/env/env.go vendored Normal file
View File

@ -0,0 +1,28 @@
package env
import (
"errors"
"os"
"github.com/goreleaser/releaser/context"
)
var ErrMissingToken = errors.New("Missing GITHUB_TOKEN")
// Pipe for env
type Pipe struct{}
// Name of the pipe
func (Pipe) Name() string {
return "Env"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) (err error) {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return ErrMissingToken
}
ctx.Token = &token
return
}

View File

@ -11,7 +11,7 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) (err error) {
func (Pipe) Run(ctx *context.Context) (err error) {
tag, err := currentTag()
if err != nil {
return
@ -25,8 +25,10 @@ func (Pipe) Run(context *context.Context) (err error) {
return
}
context.Git.CurrentTag = tag
context.Git.PreviousTag = previous
context.Git.Diff = log
ctx.Git = &context.GitInfo{
CurrentTag: tag,
PreviousTag: previous,
Diff: log,
}
return
}

View File

@ -1,6 +1,6 @@
package pipeline
"github.com/goreleaser/releaser/config"
import "github.com/goreleaser/releaser/context"
// Pipe interface
type Pipe interface {
@ -8,5 +8,5 @@ type Pipe interface {
Name() string
// Run the pipe
Run(config context.Context) error
Run(ctx *context.Context) error
}

View File

@ -1,6 +1,7 @@
package release
import (
goctx "context"
"log"
"os"
"os/exec"
@ -20,43 +21,43 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) error {
func (Pipe) Run(ctx *context.Context) error {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: context.Token},
&oauth2.Token{AccessToken: *ctx.Token},
)
tc := oauth2.NewClient(context.Background(), ts)
tc := oauth2.NewClient(goctx.Background(), ts)
client := github.NewClient(tc)
r, err := getOrCreateRelease(client, context)
r, err := getOrCreateRelease(client, ctx)
if err != nil {
return err
}
var g errgroup.Group
for _, archive := range context.Archives {
for _, archive := range ctx.Archives {
archive := archive
g.Go(func() error {
return upload(client, *r.ID, archive, context)
return upload(client, *r.ID, archive, ctx)
})
}
return g.Wait()
}
func getOrCreateRelease(client *github.Client, context *context.Context) (*github.RepositoryRelease, error) {
owner := context.Repo.Owner
repo := context.Repo.Name
func getOrCreateRelease(client *github.Client, ctx *context.Context) (*github.RepositoryRelease, error) {
owner := ctx.Repo.Owner
repo := ctx.Repo.Name
data := &github.RepositoryRelease{
Name: github.String(context.Git.CurrentTag),
TagName: github.String(context.Git.CurrentTag),
Body: github.String(description(context.Git.Diff)),
Name: github.String(ctx.Git.CurrentTag),
TagName: github.String(ctx.Git.CurrentTag),
Body: github.String(description(ctx.Git.Diff)),
}
r, res, err := client.Repositories.GetReleaseByTag(owner, repo, context.Git.CurrentTag)
if err != nil && res.StatusCode == 404 {
log.Println("Creating release", config.Git.CurrentTag, "on", context.Config.Repo, "...")
r, _, err := client.Repositories.GetReleaseByTag(owner, repo, ctx.Git.CurrentTag)
if err != nil {
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.Config.Repo, "...")
r, _, err = client.Repositories.CreateRelease(owner, repo, data)
return r, err
}
log.Println("Updating existing release", config.Git.CurrentTag, "on", context.Config.Repo, "...")
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Repo, "...")
r, _, err = client.Repositories.EditRelease(owner, repo, *r.ID, data)
return r, err
}
@ -71,8 +72,8 @@ func description(diff string) string {
return result + "\nBuilt with " + string(bts)
}
func upload(client *github.Client, releaseID int, archive string, context *context.Context) error {
archive = archive + "." + context.Config.Archive.Format
func upload(client *github.Client, releaseID int, archive string, ctx *context.Context) error {
archive = archive + "." + ctx.Config.Archive.Format
file, err := os.Open("dist/" + archive)
if err != nil {
return err
@ -80,8 +81,8 @@ func upload(client *github.Client, releaseID int, archive string, context *conte
defer func() { _ = file.Close() }()
log.Println("Uploading", file.Name(), "...")
_, _, err = client.Repositories.UploadReleaseAsset(
context.Repo.Owner,
context.Repo.Name,
ctx.Repo.Owner,
ctx.Repo.Name,
releaseID,
&github.UploadOptions{Name: archive},
file,

View File

@ -1,7 +1,6 @@
package repos
import (
"context"
"strings"
"github.com/goreleaser/releaser/context"
@ -16,13 +15,17 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) (err error) {
owner, name := split(context.Config.Repo)
context.Repo.Owner = owner
context.Repo.Name = name
owner, name = split(context.Config.Brew.Repo)
context.BrewRepo.Owner = owner
context.BrewRepo.Name = name
func (Pipe) Run(ctx *context.Context) (err error) {
owner, name := split(ctx.Config.Repo)
ctx.Repo = &context.Repo{
Owner: owner,
Name: name,
}
owner, name = split(ctx.Config.Brew.Repo)
ctx.BrewRepo = &context.Repo{
Owner: owner,
Name: name,
}
return
}

View File

@ -15,11 +15,11 @@ func (Pipe) Name() string {
}
// Run the pipe
func (Pipe) Run(context *context.Context) (err error) {
if context.Config.BinaryName == "" {
func (Pipe) Run(ctx *context.Context) (err error) {
if ctx.Config.BinaryName == "" {
return errors.New("missing binary_name")
}
if context.Config.Repo == "" {
if ctx.Config.Repo == "" {
return errors.New("missing repo")
}
return