0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-22 08:20:39 +00:00
git-sv/sv/formatter.go

103 lines
2.8 KiB
Go
Raw Normal View History

package sv
import (
"bytes"
"fmt"
"text/template"
)
type releaseNoteTemplateVariables struct {
Version string
Date string
Sections map[string]ReleaseNoteSection
BreakingChanges []string
}
2020-02-02 03:49:54 +00:00
const (
cglTemplate = `# Changelog
{{- range .}}
{{template "rnTemplate" .}}
---
{{- end}}
`
rnSectionItem = "- {{if .Scope}}**{{.Scope}}:** {{end}}{{.Subject}} ({{.Hash}}){{if .Metadata.issueid}} ({{.Metadata.issueid}}){{end}}"
rnSection = `{{- if .}}
### {{.Name}}
{{range $k,$v := .Items}}
{{template "rnSectionItem" $v}}
{{- end}}
{{- end}}`
2020-02-02 03:49:54 +00:00
rnSectionBreakingChanges = `{{- if .}}
### Breaking Changes
{{range $k,$v := .}}
- {{$v}}
{{- end}}
{{- end}}`
2020-02-02 03:49:54 +00:00
2020-05-01 03:45:08 +00:00
rnTemplate = `## v{{.Version}}{{if .Date}} ({{.Date}}){{end}}
{{- template "rnSection" .Sections.feat}}
{{- template "rnSection" .Sections.fix}}
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
`
2020-02-02 03:49:54 +00:00
)
// OutputFormatter output formatter interface.
type OutputFormatter interface {
FormatReleaseNote(releasenote ReleaseNote) string
2020-02-02 03:49:54 +00:00
FormatChangelog(releasenotes []ReleaseNote) string
}
// 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.
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
2020-02-02 03:49:54 +00:00
var b bytes.Buffer
p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote))
return b.String()
}
// FormatChangelog format a changelog
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string {
var templateVars []releaseNoteTemplateVariables
for _, v := range releasenotes {
templateVars = append(templateVars, releaseNoteVariables(v))
}
var b bytes.Buffer
p.changelogTemplate.Execute(&b, templateVars)
return b.String()
}
func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables {
2020-05-01 03:45:08 +00:00
var date = ""
if !releasenote.Date.IsZero() {
date = releasenote.Date.Format("2006-01-02")
}
2020-02-02 03:49:54 +00:00
return releaseNoteTemplateVariables{
Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()),
2020-05-01 03:45:08 +00:00
Date: date,
Sections: releasenote.Sections,
BreakingChanges: releasenote.BreakingChanges,
}
}