mirror of
https://github.com/thegeeklab/git-sv.git
synced 2024-11-12 15:00:39 +00:00
Merge pull request #42 from bvieira/#40-refactor-templates
Refactor: extract templates from code
This commit is contained in:
commit
9eaff5d128
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -17,6 +19,16 @@ const (
|
|||||||
repoConfigFilename = ".sv4git.yml"
|
repoConfigFilename = ".sv4git.yml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
//go:embed resources/templates/*.tpl
|
||||||
|
defaultTemplatesFS embed.FS
|
||||||
|
)
|
||||||
|
|
||||||
|
func templateFS() fs.FS {
|
||||||
|
defaultTemplatesFS, _ := fs.Sub(defaultTemplatesFS, "resources/templates")
|
||||||
|
return defaultTemplatesFS
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
@ -25,7 +37,7 @@ func main() {
|
|||||||
git := sv.NewGit(messageProcessor, cfg.Tag)
|
git := sv.NewGit(messageProcessor, cfg.Tag)
|
||||||
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.Versioning, cfg.CommitMessage)
|
semverProcessor := sv.NewSemVerCommitsProcessor(cfg.Versioning, cfg.CommitMessage)
|
||||||
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotes)
|
releasenotesProcessor := sv.NewReleaseNoteProcessor(cfg.ReleaseNotes)
|
||||||
outputFormatter := sv.NewOutputFormatter()
|
outputFormatter := sv.NewOutputFormatter(templateFS())
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "sv"
|
app.Name = "sv"
|
||||||
@ -146,10 +158,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadCfg() Config {
|
func loadCfg() Config {
|
||||||
envCfg := loadEnvConfig()
|
|
||||||
|
|
||||||
cfg := defaultConfig()
|
cfg := defaultConfig()
|
||||||
|
|
||||||
|
envCfg := loadEnvConfig()
|
||||||
if envCfg.Home != "" {
|
if envCfg.Home != "" {
|
||||||
if homeCfg, err := readConfig(filepath.Join(envCfg.Home, configFilename)); err == nil {
|
if homeCfg, err := readConfig(filepath.Join(envCfg.Home, configFilename)); err == nil {
|
||||||
if merr := merge(&cfg, homeCfg); merr != nil {
|
if merr := merge(&cfg, homeCfg); merr != nil {
|
||||||
@ -160,7 +171,7 @@ func loadCfg() Config {
|
|||||||
|
|
||||||
repoPath, rerr := getRepoPath()
|
repoPath, rerr := getRepoPath()
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
log.Fatal("failed to get repository path, error: ", rerr)
|
log.Fatal("failed to discovery repository top level, error: ", rerr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if repoCfg, err := readConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil {
|
if repoCfg, err := readConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil {
|
||||||
|
6
cmd/git-sv/resources/templates/changelog-md.tpl
Normal file
6
cmd/git-sv/resources/templates/changelog-md.tpl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Changelog
|
||||||
|
{{- range .}}
|
||||||
|
|
||||||
|
{{template "releasenotes-md.tpl" .}}
|
||||||
|
---
|
||||||
|
{{- end}}
|
6
cmd/git-sv/resources/templates/releasenotes-md.tpl
Normal file
6
cmd/git-sv/resources/templates/releasenotes-md.tpl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
## {{if .Release}}{{.Release}}{{end}}{{if and .Date .Release}} ({{end}}{{.Date}}{{if and .Date .Release}}){{end}}
|
||||||
|
{{- $sections := .Sections }}
|
||||||
|
{{- range $key := .Order }}
|
||||||
|
{{- template "rn-md-section.tpl" (index $sections $key) }}
|
||||||
|
{{- end}}
|
||||||
|
{{- template "rn-md-section-breaking-changes.tpl" .BreakingChanges}}
|
@ -0,0 +1,7 @@
|
|||||||
|
{{- if ne .Name ""}}
|
||||||
|
|
||||||
|
### {{.Name}}
|
||||||
|
{{range $k,$v := .Messages}}
|
||||||
|
- {{$v}}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}
|
1
cmd/git-sv/resources/templates/rn-md-section-item.tpl
Normal file
1
cmd/git-sv/resources/templates/rn-md-section-item.tpl
Normal file
@ -0,0 +1 @@
|
|||||||
|
- {{if .Message.Scope}}**{{.Message.Scope}}:** {{end}}{{.Message.Description}} ({{.Hash}}){{if .Message.Metadata.issue}} ({{.Message.Metadata.issue}}){{end}}
|
7
cmd/git-sv/resources/templates/rn-md-section.tpl
Normal file
7
cmd/git-sv/resources/templates/rn-md-section.tpl
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{{- if .}}{{- if ne .Name ""}}
|
||||||
|
|
||||||
|
### {{.Name}}
|
||||||
|
{{range $k,$v := .Items}}
|
||||||
|
{{template "rn-md-section-item.tpl" $v}}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}{{- end}}
|
24
cmd/git-sv/resources_test.go
Normal file
24
cmd/git-sv/resources_test.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_checkTemplatesFiles(t *testing.T) {
|
||||||
|
tests := []string{
|
||||||
|
"resources/templates/changelog-md.tpl",
|
||||||
|
"resources/templates/releasenotes-md.tpl",
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt, func(t *testing.T) {
|
||||||
|
got, err := defaultTemplatesFS.ReadFile(tt)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("missing template error = %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(got) <= 0 {
|
||||||
|
t.Errorf("empty template")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package sv
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io/fs"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,42 +14,6 @@ type releaseNoteTemplateVariables struct {
|
|||||||
BreakingChanges BreakingChangeSection
|
BreakingChanges BreakingChangeSection
|
||||||
}
|
}
|
||||||
|
|
||||||
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}}"
|
|
||||||
|
|
||||||
rnSection = `{{- if .}}{{- if ne .Name ""}}
|
|
||||||
|
|
||||||
### {{.Name}}
|
|
||||||
{{range $k,$v := .Items}}
|
|
||||||
{{template "rnSectionItem" $v}}
|
|
||||||
{{- end}}
|
|
||||||
{{- end}}{{- end}}`
|
|
||||||
|
|
||||||
rnSectionBreakingChanges = `{{- if ne .Name ""}}
|
|
||||||
|
|
||||||
### {{.Name}}
|
|
||||||
{{range $k,$v := .Messages}}
|
|
||||||
- {{$v}}
|
|
||||||
{{- end}}
|
|
||||||
{{- end}}`
|
|
||||||
|
|
||||||
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}}
|
|
||||||
`
|
|
||||||
)
|
|
||||||
|
|
||||||
// OutputFormatter output formatter interface.
|
// OutputFormatter output formatter interface.
|
||||||
type OutputFormatter interface {
|
type OutputFormatter interface {
|
||||||
FormatReleaseNote(releasenote ReleaseNote) (string, error)
|
FormatReleaseNote(releasenote ReleaseNote) (string, error)
|
||||||
@ -57,24 +22,19 @@ type OutputFormatter interface {
|
|||||||
|
|
||||||
// OutputFormatterImpl formater for release note and changelog.
|
// OutputFormatterImpl formater for release note and changelog.
|
||||||
type OutputFormatterImpl struct {
|
type OutputFormatterImpl struct {
|
||||||
releasenoteTemplate *template.Template
|
templates *template.Template
|
||||||
changelogTemplate *template.Template
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewOutputFormatter TemplateProcessor constructor.
|
// NewOutputFormatter TemplateProcessor constructor.
|
||||||
func NewOutputFormatter() *OutputFormatterImpl {
|
func NewOutputFormatter(templatesFS fs.FS) *OutputFormatterImpl {
|
||||||
cgl := template.Must(template.New("cglTemplate").Parse(cglTemplate))
|
tpls := template.Must(template.New("templates").ParseFS(templatesFS, "*"))
|
||||||
rn := template.Must(cgl.New("rnTemplate").Parse(rnTemplate))
|
return &OutputFormatterImpl{templates: tpls}
|
||||||
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.
|
// FormatReleaseNote format a release note.
|
||||||
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
|
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if err := p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)); err != nil {
|
if err := p.templates.ExecuteTemplate(&b, "releasenotes-md.tpl", releaseNoteVariables(releasenote)); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
@ -88,7 +48,7 @@ func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string
|
|||||||
}
|
}
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
if err := p.changelogTemplate.Execute(&b, templateVars); err != nil {
|
if err := p.templates.ExecuteTemplate(&b, "changelog-md.tpl", templateVars); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return b.String(), nil
|
return b.String(), nil
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
package sv
|
package sv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Masterminds/semver/v3"
|
"github.com/Masterminds/semver/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var templatesFS = os.DirFS("../cmd/git-sv/resources/templates")
|
||||||
|
|
||||||
var dateChangelog = `## v1.0.0 (2020-05-01)
|
var dateChangelog = `## v1.0.0 (2020-05-01)
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -55,7 +60,7 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got, err := NewOutputFormatter().FormatReleaseNote(tt.input)
|
got, err := NewOutputFormatter(templatesFS).FormatReleaseNote(tt.input)
|
||||||
if got != tt.want {
|
if got != tt.want {
|
||||||
t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want)
|
t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
@ -85,3 +90,50 @@ func fullReleaseNote(tag string, date time.Time) ReleaseNote {
|
|||||||
}
|
}
|
||||||
return releaseNote(v, tag, date, sections, []string{"break change message"})
|
return releaseNote(v, tag, date, sections, []string{"break change message"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_checkTemplatesExecution(t *testing.T) {
|
||||||
|
tpls := template.Must(template.New("templates").ParseFS(templatesFS, "*"))
|
||||||
|
tests := []struct {
|
||||||
|
template string
|
||||||
|
variables interface{}
|
||||||
|
}{
|
||||||
|
{"changelog-md.tpl", changelogVariables("v1.0.0", "v1.0.1")},
|
||||||
|
{"releasenotes-md.tpl", releaseNotesVariables("v1.0.0")},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.template, func(t *testing.T) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
err := tpls.ExecuteTemplate(&b, tt.template, tt.variables)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("invalid template err = %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(b.Bytes()) <= 0 {
|
||||||
|
t.Errorf("empty template")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func releaseNotesVariables(release string) releaseNoteTemplateVariables {
|
||||||
|
return releaseNoteTemplateVariables{
|
||||||
|
Release: release,
|
||||||
|
Date: "2006-01-02",
|
||||||
|
Sections: map[string]ReleaseNoteSection{
|
||||||
|
"build": newReleaseNoteSection("Build", []GitCommitLog{commitlog("build", map[string]string{})}),
|
||||||
|
"feat": newReleaseNoteSection("Features", []GitCommitLog{commitlog("feat", map[string]string{})}),
|
||||||
|
"fix": newReleaseNoteSection("Bug Fixes", []GitCommitLog{commitlog("fix", map[string]string{})}),
|
||||||
|
},
|
||||||
|
Order: []string{"feat", "fix", "refactor", "perf", "test", "build", "ci", "chore", "docs", "style"},
|
||||||
|
BreakingChanges: BreakingChangeSection{"Breaking Changes", []string{"break change message"}},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func changelogVariables(releases ...string) []releaseNoteTemplateVariables {
|
||||||
|
var variables []releaseNoteTemplateVariables
|
||||||
|
for _, r := range releases {
|
||||||
|
variables = append(variables, releaseNotesVariables(r))
|
||||||
|
}
|
||||||
|
return variables
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user