2018-07-09 05:47:30 +02:00
|
|
|
// Package tmpl provides templating utilities for goreleser
|
|
|
|
package tmpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
|
|
|
"github.com/masterminds/semver"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-07-09 06:13:09 +02:00
|
|
|
// Template holds data that can be applied to a template string
|
2018-07-09 05:47:30 +02:00
|
|
|
type Template struct {
|
|
|
|
fields fields
|
|
|
|
}
|
|
|
|
|
2018-07-09 07:35:44 +02:00
|
|
|
type fields map[string]interface{}
|
2018-07-09 05:47:30 +02:00
|
|
|
|
2018-07-09 07:35:44 +02:00
|
|
|
const (
|
|
|
|
// general keys
|
|
|
|
kProjectName = "ProjectName"
|
|
|
|
kVersion = "Version"
|
|
|
|
kTag = "Tag"
|
|
|
|
kCommit = "Commit"
|
|
|
|
kMajor = "Major"
|
|
|
|
kMinor = "Minor"
|
|
|
|
kPatch = "Patch"
|
|
|
|
kEnv = "Env"
|
|
|
|
|
|
|
|
// artifact-only keys
|
|
|
|
kOs = "Os"
|
|
|
|
kArch = "Arch"
|
|
|
|
kArm = "Arm"
|
|
|
|
kBinary = "Binary"
|
|
|
|
)
|
2018-07-09 05:47:30 +02:00
|
|
|
|
2018-07-09 06:13:09 +02:00
|
|
|
// New Template
|
2018-07-09 05:47:30 +02:00
|
|
|
func New(ctx *context.Context) *Template {
|
|
|
|
return &Template{
|
|
|
|
fields: fields{
|
2018-07-09 07:35:44 +02:00
|
|
|
kProjectName: ctx.Config.ProjectName,
|
|
|
|
kVersion: ctx.Version,
|
|
|
|
kTag: ctx.Git.CurrentTag,
|
|
|
|
kCommit: ctx.Git.Commit,
|
|
|
|
kEnv: ctx.Env,
|
2018-07-09 05:47:30 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 06:13:09 +02:00
|
|
|
// WithArtifacts populate fields from the artifact and replacements
|
2018-07-09 05:47:30 +02:00
|
|
|
func (t *Template) WithArtifact(a artifact.Artifact, replacements map[string]string) *Template {
|
2018-07-09 07:35:44 +02:00
|
|
|
var binary = a.Extra[kBinary]
|
2018-07-09 05:47:30 +02:00
|
|
|
if binary == "" {
|
2018-07-09 07:35:44 +02:00
|
|
|
binary = t.fields[kProjectName].(string)
|
2018-07-09 05:47:30 +02:00
|
|
|
}
|
2018-07-09 07:35:44 +02:00
|
|
|
t.fields[kOs] = replace(replacements, a.Goos)
|
|
|
|
t.fields[kArch] = replace(replacements, a.Goarch)
|
|
|
|
t.fields[kArm] = replace(replacements, a.Goarm)
|
|
|
|
t.fields[kBinary] = binary
|
2018-07-09 05:47:30 +02:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2018-07-09 06:13:09 +02:00
|
|
|
// Apply applies the given string against the fields stored in the template.
|
2018-07-09 05:47:30 +02:00
|
|
|
func (t *Template) Apply(s string) (string, error) {
|
|
|
|
var out bytes.Buffer
|
|
|
|
tmpl, err := template.New("tmpl").
|
|
|
|
Option("missingkey=error").
|
|
|
|
Funcs(template.FuncMap{
|
|
|
|
"time": func(s string) string {
|
|
|
|
return time.Now().UTC().Format(s)
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-07-09 07:35:44 +02:00
|
|
|
sv, err := semver.NewVersion(t.fields[kTag].(string))
|
2018-07-09 05:47:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "tmpl")
|
|
|
|
}
|
2018-07-09 07:35:44 +02:00
|
|
|
t.fields[kMajor] = sv.Major()
|
|
|
|
t.fields[kMinor] = sv.Minor()
|
|
|
|
t.fields[kPatch] = sv.Patch()
|
2018-07-09 05:47:30 +02:00
|
|
|
|
|
|
|
err = tmpl.Execute(&out, t.fields)
|
|
|
|
return out.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func replace(replacements map[string]string, original string) string {
|
|
|
|
result := replacements[original]
|
|
|
|
if result == "" {
|
|
|
|
return original
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|