mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-24 21:20:40 +00:00
refactor: extract template operation to formatter struct
This commit is contained in:
parent
e18b0cc08f
commit
f4d1499331
@ -76,7 +76,7 @@ func getTagCommits(git sv.Git, tag string) ([]sv.GitCommitLog, error) {
|
|||||||
return git.Log(prev, tag)
|
return git.Log(prev, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor) func(c *cli.Context) error {
|
func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error {
|
||||||
return func(c *cli.Context) error {
|
return func(c *cli.Context) error {
|
||||||
var commits []sv.GitCommitLog
|
var commits []sv.GitCommitLog
|
||||||
var rnVersion semver.Version
|
var rnVersion semver.Version
|
||||||
@ -94,7 +94,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
|
|||||||
}
|
}
|
||||||
|
|
||||||
releasenote := rnProcessor.Create(rnVersion, date, commits)
|
releasenote := rnProcessor.Create(rnVersion, date, commits)
|
||||||
fmt.Println(rnProcessor.Format(releasenote))
|
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,7 +161,7 @@ func getNextVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) (
|
|||||||
return semverProcessor.NextVersion(currentVer, commits), time.Now(), commits, nil
|
return semverProcessor.NextVersion(currentVer, commits), time.Now(), commits, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor) func(c *cli.Context) error {
|
func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *cli.Context) error {
|
||||||
return func(c *cli.Context) error {
|
return func(c *cli.Context) error {
|
||||||
describe := git.Describe()
|
describe := git.Describe()
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ func main() {
|
|||||||
git := sv.NewGit(cfg.BreakingChangePrefixes, cfg.IssueIDPrefixes, cfg.TagPattern)
|
git := sv.NewGit(cfg.BreakingChangePrefixes, cfg.IssueIDPrefixes, cfg.TagPattern)
|
||||||
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes)
|
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.IncludeUnknownTypeAsPatch, cfg.MajorVersionTypes, cfg.MinorVersionTypes, cfg.PatchVersionTypes)
|
||||||
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags)
|
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotesTags)
|
||||||
|
outputFormatter := sv.NewOutputFormatter()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "sv"
|
app.Name = "sv"
|
||||||
@ -46,14 +47,14 @@ func main() {
|
|||||||
Name: "release-notes",
|
Name: "release-notes",
|
||||||
Aliases: []string{"rn"},
|
Aliases: []string{"rn"},
|
||||||
Usage: "generate release notes",
|
Usage: "generate release notes",
|
||||||
Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor),
|
Action: releaseNotesHandler(git, semverProcessor, releasenotesProcessor, outputFormatter),
|
||||||
Flags: []cli.Flag{&cli.StringFlag{Name: "t", Usage: "get release note from tag"}},
|
Flags: []cli.Flag{&cli.StringFlag{Name: "t", Usage: "get release note from tag"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "tag",
|
Name: "tag",
|
||||||
Aliases: []string{"tg"},
|
Aliases: []string{"tg"},
|
||||||
Usage: "generate tag with version based on git commit messages",
|
Usage: "generate tag with version based on git commit messages",
|
||||||
Action: tagHandler(git, semverProcessor, releasenotesProcessor),
|
Action: tagHandler(git, semverProcessor),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
sv/formatter.go
Normal file
59
sv/formatter.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package sv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"text/template"
|
||||||
|
)
|
||||||
|
|
||||||
|
type releaseNoteTemplateVariables struct {
|
||||||
|
Version string
|
||||||
|
Date string
|
||||||
|
Sections map[string]ReleaseNoteSection
|
||||||
|
BreakingChanges []string
|
||||||
|
}
|
||||||
|
|
||||||
|
const rnTemplate = `## v{{.Version}} ({{.Date}})
|
||||||
|
|
||||||
|
{{if .Sections.feat}}### {{.Sections.feat.Name}}
|
||||||
|
{{range $k,$v := .Sections.feat.Items}}
|
||||||
|
- {{if $v.Scope}}**{{$v.Scope}}:** {{end}}{{$v.Subject}} ({{$v.Hash}}) {{if $v.Metadata.issueid}}({{$v.Metadata.issueid}}){{end}}{{end}}{{end}}
|
||||||
|
|
||||||
|
{{if .Sections.fix}}### {{.Sections.fix.Name}}
|
||||||
|
{{range $k,$v := .Sections.fix.Items}}
|
||||||
|
- {{if $v.Scope}}**{{$v.Scope}}:** {{end}}{{$v.Subject}} ({{$v.Hash}}) {{if $v.Metadata.issueid}}({{$v.Metadata.issueid}}){{end}}{{end}}{{end}}
|
||||||
|
|
||||||
|
{{if .BreakingChanges}}### Breaking Changes
|
||||||
|
{{range $k,$v := .BreakingChanges}}
|
||||||
|
- {{$v}}{{end}}
|
||||||
|
{{end}}`
|
||||||
|
|
||||||
|
// OutputFormatter output formatter interface.
|
||||||
|
type OutputFormatter interface {
|
||||||
|
FormatReleaseNote(releasenote ReleaseNote) string
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutputFormatterImpl formater for release note and changelog.
|
||||||
|
type OutputFormatterImpl struct {
|
||||||
|
releasenoteTemplate *template.Template
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOutputFormatter TemplateProcessor constructor.
|
||||||
|
func NewOutputFormatter() *OutputFormatterImpl {
|
||||||
|
template := template.Must(template.New("releasenotes").Parse(rnTemplate))
|
||||||
|
return &OutputFormatterImpl{releasenoteTemplate: template}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatReleaseNote format a release note.
|
||||||
|
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
|
||||||
|
templateVars := releaseNoteTemplateVariables{
|
||||||
|
Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()),
|
||||||
|
Date: releasenote.Date.Format("2006-01-02"),
|
||||||
|
Sections: releasenote.Sections,
|
||||||
|
BreakingChanges: releasenote.BreakingChanges,
|
||||||
|
}
|
||||||
|
|
||||||
|
var b bytes.Buffer
|
||||||
|
p.releasenoteTemplate.Execute(&b, templateVars)
|
||||||
|
return b.String()
|
||||||
|
}
|
@ -1,52 +1,24 @@
|
|||||||
package sv
|
package sv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"text/template"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Masterminds/semver"
|
"github.com/Masterminds/semver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type releaseNoteTemplate struct {
|
|
||||||
Version string
|
|
||||||
Date string
|
|
||||||
Sections map[string]ReleaseNoteSection
|
|
||||||
BreakingChanges []string
|
|
||||||
}
|
|
||||||
|
|
||||||
const markdownTemplate = `## v{{.Version}} ({{.Date}})
|
|
||||||
|
|
||||||
{{if .Sections.feat}}### {{.Sections.feat.Name}}
|
|
||||||
{{range $k,$v := .Sections.feat.Items}}
|
|
||||||
- {{if $v.Scope}}**{{$v.Scope}}:** {{end}}{{$v.Subject}} ({{$v.Hash}}) {{if $v.Metadata.issueid}}({{$v.Metadata.issueid}}){{end}}{{end}}{{end}}
|
|
||||||
|
|
||||||
{{if .Sections.fix}}### {{.Sections.fix.Name}}
|
|
||||||
{{range $k,$v := .Sections.fix.Items}}
|
|
||||||
- {{if $v.Scope}}**{{$v.Scope}}:** {{end}}{{$v.Subject}} ({{$v.Hash}}) {{if $v.Metadata.issueid}}({{$v.Metadata.issueid}}){{end}}{{end}}{{end}}
|
|
||||||
|
|
||||||
{{if .BreakingChanges}}### Breaking Changes
|
|
||||||
{{range $k,$v := .BreakingChanges}}
|
|
||||||
- {{$v}}{{end}}
|
|
||||||
{{end}}`
|
|
||||||
|
|
||||||
// ReleaseNoteProcessor release note processor interface.
|
// ReleaseNoteProcessor release note processor interface.
|
||||||
type ReleaseNoteProcessor interface {
|
type ReleaseNoteProcessor interface {
|
||||||
Create(version semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote
|
Create(version semver.Version, date time.Time, commits []GitCommitLog) ReleaseNote
|
||||||
Format(releasenote ReleaseNote) string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleaseNoteProcessorImpl release note based on commit log.
|
// ReleaseNoteProcessorImpl release note based on commit log.
|
||||||
type ReleaseNoteProcessorImpl struct {
|
type ReleaseNoteProcessorImpl struct {
|
||||||
tags map[string]string
|
tags map[string]string
|
||||||
template *template.Template
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewReleaseNoteProcessor ReleaseNoteProcessor constructor.
|
// NewReleaseNoteProcessor ReleaseNoteProcessor constructor.
|
||||||
func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl {
|
func NewReleaseNoteProcessor(tags map[string]string) *ReleaseNoteProcessorImpl {
|
||||||
template := template.Must(template.New("markdown").Parse(markdownTemplate))
|
return &ReleaseNoteProcessorImpl{tags: tags}
|
||||||
return &ReleaseNoteProcessorImpl{tags: tags, template: template}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create create a release note based on commits.
|
// Create create a release note based on commits.
|
||||||
@ -70,20 +42,6 @@ func (p ReleaseNoteProcessorImpl) Create(version semver.Version, date time.Time,
|
|||||||
return ReleaseNote{Version: version, Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChanges}
|
return ReleaseNote{Version: version, Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChanges}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format format a release note.
|
|
||||||
func (p ReleaseNoteProcessorImpl) Format(releasenote ReleaseNote) string {
|
|
||||||
templateVars := releaseNoteTemplate{
|
|
||||||
Version: fmt.Sprintf("%d.%d.%d", releasenote.Version.Major(), releasenote.Version.Minor(), releasenote.Version.Patch()),
|
|
||||||
Date: releasenote.Date.Format("2006-01-02"),
|
|
||||||
Sections: releasenote.Sections,
|
|
||||||
BreakingChanges: releasenote.BreakingChanges,
|
|
||||||
}
|
|
||||||
|
|
||||||
var b bytes.Buffer
|
|
||||||
p.template.Execute(&b, templateVars)
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReleaseNote release note.
|
// ReleaseNote release note.
|
||||||
type ReleaseNote struct {
|
type ReleaseNote struct {
|
||||||
Version semver.Version
|
Version semver.Version
|
||||||
|
Loading…
Reference in New Issue
Block a user