0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-10 02:10:38 +00:00
git-sv/sv/formatter.go

117 lines
3.3 KiB
Go
Raw Normal View History

package sv
import (
"bytes"
"text/template"
)
type releaseNoteTemplateVariables struct {
Release string
Date string
Sections map[string]ReleaseNoteSection
Order []string
BreakingChanges BreakingChangeSection
}
2020-02-02 03:49:54 +00:00
const (
cglTemplate = `# Changelog
{{- range .}}
{{template "rnTemplate" .}}
---
{{- end}}
`
rnSectionItem = "- {{if .Message.Scope}}**{{.Message.Scope}}:** {{end}}{{.Message.Description}} ({{.Hash}}){{if .Message.Metadata.issue}} ({{.Message.Metadata.issue}}){{end}}"
2020-02-02 03:49:54 +00:00
rnSection = `{{- if .}}{{- if ne .Name ""}}
### {{.Name}}
{{range $k,$v := .Items}}
{{template "rnSectionItem" $v}}
{{- end}}
{{- end}}{{- end}}`
2020-02-02 03:49:54 +00:00
rnSectionBreakingChanges = `{{- if ne .Name ""}}
### {{.Name}}
{{range $k,$v := .Messages}}
- {{$v}}
{{- end}}
{{- end}}`
2020-02-02 03:49:54 +00:00
rnTemplate = `## {{if .Release}}{{.Release}}{{end}}{{if and .Date .Release}} ({{end}}{{.Date}}{{if and .Date .Release}}){{end}}
{{- $sections := .Sections }}
{{- range $key := .Order }}
{{- template "rnSection" (index $sections $key) }}
{{- end}}
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
`
2020-02-02 03:49:54 +00:00
)
// OutputFormatter output formatter interface.
type OutputFormatter interface {
2021-07-31 20:34:40 +00:00
FormatReleaseNote(releasenote ReleaseNote) (string, error)
FormatChangelog(releasenotes []ReleaseNote) (string, error)
}
// OutputFormatterImpl formater for release note and changelog.
type OutputFormatterImpl struct {
releasenoteTemplate *template.Template
2020-02-02 03:49:54 +00:00
changelogTemplate *template.Template
}
// NewOutputFormatter TemplateProcessor constructor.
func NewOutputFormatter() *OutputFormatterImpl {
2020-02-02 03:49:54 +00:00
cgl := template.Must(template.New("cglTemplate").Parse(cglTemplate))
rn := template.Must(cgl.New("rnTemplate").Parse(rnTemplate))
template.Must(rn.New("rnSectionItem").Parse(rnSectionItem))
template.Must(rn.New("rnSection").Parse(rnSection))
template.Must(rn.New("rnSectionBreakingChanges").Parse(rnSectionBreakingChanges))
return &OutputFormatterImpl{releasenoteTemplate: rn, changelogTemplate: cgl}
}
// FormatReleaseNote format a release note.
2021-07-31 20:34:40 +00:00
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
2020-02-02 03:49:54 +00:00
var b bytes.Buffer
2021-07-31 20:34:40 +00:00
if err := p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)); err != nil {
return "", err
}
return b.String(), nil
2020-02-02 03:49:54 +00:00
}
2021-07-31 19:03:58 +00:00
// FormatChangelog format a changelog.
2021-07-31 20:34:40 +00:00
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string, error) {
2021-07-31 19:35:28 +00:00
templateVars := make([]releaseNoteTemplateVariables, len(releasenotes))
for i, v := range releasenotes {
templateVars[i] = releaseNoteVariables(v)
2020-02-02 03:49:54 +00:00
}
var b bytes.Buffer
2021-07-31 20:34:40 +00:00
if err := p.changelogTemplate.Execute(&b, templateVars); err != nil {
return "", err
}
return b.String(), nil
2020-02-02 03:49:54 +00:00
}
func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables {
2021-07-31 19:03:58 +00:00
date := ""
2020-05-01 03:45:08 +00:00
if !releasenote.Date.IsZero() {
date = releasenote.Date.Format("2006-01-02")
}
release := ""
if releasenote.Version != nil {
release = "v" + releasenote.Version.String()
} else if releasenote.Tag != "" {
release = releasenote.Tag
}
2020-02-02 03:49:54 +00:00
return releaseNoteTemplateVariables{
Release: release,
2020-05-01 03:45:08 +00:00
Date: date,
Sections: releasenote.Sections,
Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"},
BreakingChanges: releasenote.BreakingChanges,
}
}