2020-02-02 01:40:31 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type releaseNoteTemplateVariables struct {
|
|
|
|
Version string
|
|
|
|
Date string
|
|
|
|
Sections map[string]ReleaseNoteSection
|
2021-07-31 00:03:34 +00:00
|
|
|
Order []string
|
2021-02-15 04:47:20 +00:00
|
|
|
BreakingChanges BreakingChangeSection
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:49:54 +00:00
|
|
|
const (
|
|
|
|
cglTemplate = `# Changelog
|
|
|
|
{{- range .}}
|
|
|
|
|
|
|
|
{{template "rnTemplate" .}}
|
|
|
|
---
|
|
|
|
{{- end}}
|
|
|
|
`
|
|
|
|
|
2021-02-14 02:35:31 +00:00
|
|
|
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
|
|
|
|
2021-07-31 00:03:34 +00:00
|
|
|
rnSection = `{{- if .}}{{- if ne .Name ""}}
|
2020-02-02 01:40:31 +00:00
|
|
|
|
2020-02-02 02:51:29 +00:00
|
|
|
### {{.Name}}
|
|
|
|
{{range $k,$v := .Items}}
|
|
|
|
{{template "rnSectionItem" $v}}
|
2020-02-02 02:05:39 +00:00
|
|
|
{{- end}}
|
2021-07-31 00:03:34 +00:00
|
|
|
{{- end}}{{- end}}`
|
2020-02-02 03:49:54 +00:00
|
|
|
|
2021-02-15 04:47:20 +00:00
|
|
|
rnSectionBreakingChanges = `{{- if ne .Name ""}}
|
2020-02-02 01:40:31 +00:00
|
|
|
|
2021-02-15 04:47:20 +00:00
|
|
|
### {{.Name}}
|
|
|
|
{{range $k,$v := .Messages}}
|
2020-02-02 02:51:29 +00:00
|
|
|
- {{$v}}
|
2020-02-02 02:05:39 +00:00
|
|
|
{{- end}}
|
2020-02-02 02:51:29 +00:00
|
|
|
{{- end}}`
|
2020-02-02 03:49:54 +00:00
|
|
|
|
2021-01-25 20:16:56 +00:00
|
|
|
rnTemplate = `## {{if .Version}}v{{.Version}}{{end}}{{if and .Date .Version}} ({{end}}{{.Date}}{{if and .Version .Date}}){{end}}
|
2021-07-31 00:03:34 +00:00
|
|
|
{{- $sections := .Sections }}
|
|
|
|
{{- range $key := .Order }}
|
|
|
|
{{- template "rnSection" (index $sections $key) }}
|
|
|
|
{{- end}}
|
2020-02-02 02:51:29 +00:00
|
|
|
{{- template "rnSectionBreakingChanges" .BreakingChanges}}
|
2020-02-02 02:05:39 +00:00
|
|
|
`
|
2020-02-02 03:49:54 +00:00
|
|
|
)
|
2020-02-02 01:40:31 +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
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFormatterImpl formater for release note and changelog.
|
|
|
|
type OutputFormatterImpl struct {
|
|
|
|
releasenoteTemplate *template.Template
|
2020-02-02 03:49:54 +00:00
|
|
|
changelogTemplate *template.Template
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
2020-02-02 01:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
2021-01-25 20:16:56 +00:00
|
|
|
|
|
|
|
var version = ""
|
|
|
|
if releasenote.Version != nil {
|
2021-04-10 20:18:14 +00:00
|
|
|
version = releasenote.Version.String()
|
2021-01-25 20:16:56 +00:00
|
|
|
}
|
2020-02-02 03:49:54 +00:00
|
|
|
return releaseNoteTemplateVariables{
|
2021-01-25 20:16:56 +00:00
|
|
|
Version: version,
|
2020-05-01 03:45:08 +00:00
|
|
|
Date: date,
|
2020-02-02 01:40:31 +00:00
|
|
|
Sections: releasenote.Sections,
|
2021-07-31 00:03:34 +00:00
|
|
|
Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"},
|
2020-02-02 01:40:31 +00:00
|
|
|
BreakingChanges: releasenote.BreakingChanges,
|
|
|
|
}
|
|
|
|
}
|