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

112 lines
2.6 KiB
Go
Raw Normal View History

// Package tmpl provides templating utilities for goreleser
package tmpl
import (
"bytes"
"text/template"
"time"
2018-09-19 18:52:23 +02:00
"github.com/Masterminds/semver"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/pkg/errors"
)
2018-07-09 06:13:09 +02:00
// Template holds data that can be applied to a template string
type Template struct {
fields fields
}
2018-07-09 07:35:44 +02:00
type fields map[string]interface{}
2018-07-09 07:35:44 +02:00
const (
// general keys
2018-07-09 09:01:06 +02:00
projectName = "ProjectName"
version = "Version"
tag = "Tag"
commit = "Commit"
2018-10-03 22:44:31 +02:00
shortCommit = "ShortCommit"
fullCommit = "FullCommit"
gitURL = "GitURL"
2018-07-09 09:01:06 +02:00
major = "Major"
minor = "Minor"
patch = "Patch"
env = "Env"
date = "Date"
timestamp = "Timestamp"
2018-07-09 07:35:44 +02:00
// artifact-only keys
os = "Os"
arch = "Arch"
arm = "Arm"
binary = "Binary"
artifactName = "ArtifactName"
2018-07-09 07:35:44 +02:00
)
2018-07-09 06:13:09 +02:00
// New Template
func New(ctx *context.Context) *Template {
return &Template{
fields: fields{
2018-07-09 09:01:06 +02:00
projectName: ctx.Config.ProjectName,
version: ctx.Version,
tag: ctx.Git.CurrentTag,
commit: ctx.Git.Commit,
2018-10-03 22:44:31 +02:00
shortCommit: ctx.Git.ShortCommit,
fullCommit: ctx.Git.FullCommit,
gitURL: ctx.Git.URL,
2018-07-09 09:01:06 +02:00
env: ctx.Env,
date: time.Now().UTC().Format(time.RFC3339),
timestamp: time.Now().UTC().Unix(),
},
}
}
2018-08-01 14:59:57 +02:00
// WithArtifact populates fields from the artifact and replacements
func (t *Template) WithArtifact(a artifact.Artifact, replacements map[string]string) *Template {
2018-07-09 09:29:15 +02:00
var bin = a.Extra[binary]
if bin == nil {
bin = t.fields[projectName]
}
2018-07-09 09:01:06 +02:00
t.fields[os] = replace(replacements, a.Goos)
t.fields[arch] = replace(replacements, a.Goarch)
t.fields[arm] = replace(replacements, a.Goarm)
t.fields[binary] = bin.(string)
t.fields[artifactName] = a.Name
return t
}
2018-07-09 06:13:09 +02:00
// Apply applies the given string against the fields stored in the template.
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 09:01:06 +02:00
sv, err := semver.NewVersion(t.fields[tag].(string))
if err != nil {
return "", errors.Wrap(err, "tmpl")
}
2018-07-09 09:01:06 +02:00
t.fields[major] = sv.Major()
t.fields[minor] = sv.Minor()
t.fields[patch] = sv.Patch()
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
}