changeloguru/pkg/changelog/markdown.go

132 lines
2.9 KiB
Go
Raw Normal View History

package changelog
import (
"fmt"
2020-11-10 10:36:50 +00:00
"io/ioutil"
"strings"
"time"
"github.com/haunt98/changeloguru/pkg/convention"
)
const (
markdownTitle = "# CHANGELOG"
defaultLinesLen = 10
)
type MarkdownGenerator struct {
path string
version string
t time.Time
}
func NewMarkdownGenerator(path string, version string, t time.Time) *MarkdownGenerator {
return &MarkdownGenerator{
path: path,
version: version,
t: t,
}
}
func (g *MarkdownGenerator) Generate(commits []convention.Commit) error {
2020-11-10 10:36:50 +00:00
lines := g.getLines(commits)
previousLines := g.getPreviousLines()
2020-11-10 10:36:50 +00:00
lines = append(lines, previousLines...)
if err := g.writeLines(lines); err != nil {
return err
}
return nil
}
func (g *MarkdownGenerator) getLines(commits []convention.Commit) []string {
if len(commits) == 0 {
return nil
}
lines := make([]string, 0, defaultLinesLen)
lines = append(lines, markdownTitle)
lines = append(lines, g.composeVersionHeader())
addedLines := make([]string, 0, defaultLinesLen)
fixedLines := make([]string, 0, defaultLinesLen)
othersLines := make([]string, 0, defaultLinesLen)
for _, commit := range commits {
t := getType(commit.Type)
switch t {
case addedType:
2020-11-10 10:12:13 +00:00
addedLines = append(addedLines, g.composeListItem(commit.RawHeader))
case fixedType:
2020-11-10 10:12:13 +00:00
fixedLines = append(fixedLines, g.composeListItem(commit.RawHeader))
case othersType:
2020-11-10 10:12:13 +00:00
othersLines = append(othersLines, g.composeListItem(commit.RawHeader))
default:
continue
}
}
if len(addedLines) != 0 {
lines = append(lines, g.composeTypeHeader(addedType))
lines = append(lines, addedLines...)
}
if len(fixedLines) != 0 {
lines = append(lines, g.composeTypeHeader(fixedType))
lines = append(lines, fixedLines...)
}
if len(othersLines) != 0 {
lines = append(lines, g.composeTypeHeader(othersType))
lines = append(lines, othersLines...)
}
return lines
}
func (g *MarkdownGenerator) getPreviousLines() []string {
2020-11-10 10:36:50 +00:00
prevData, err := ioutil.ReadFile(g.path)
if err != nil {
return nil
2020-11-10 10:36:50 +00:00
}
prevLines := strings.Split(string(prevData), "\n")
finalPrevLines := make([]string, 0, len(prevLines))
for _, prevLine := range prevLines {
prevLine = strings.TrimSpace(prevLine)
if prevLine == "" || prevLine == markdownTitle {
continue
}
finalPrevLines = append(finalPrevLines, prevLine)
}
return finalPrevLines
2020-11-10 10:36:50 +00:00
}
func (g *MarkdownGenerator) writeLines(lines []string) error {
data := strings.Join(lines, "\n\n")
2020-11-10 10:36:50 +00:00
if err := ioutil.WriteFile(g.path, []byte(data), 0644); err != nil {
return err
}
return nil
}
func (g *MarkdownGenerator) composeVersionHeader() string {
year, month, day := g.t.Date()
2020-11-10 10:12:13 +00:00
return fmt.Sprintf("## %s (%d-%d-%d)", g.version, year, month, day)
}
func (g *MarkdownGenerator) composeTypeHeader(t string) string {
2020-11-10 10:12:13 +00:00
return fmt.Sprintf("### %s", t)
}
func (g *MarkdownGenerator) composeListItem(text string) string {
return fmt.Sprintf("- %s", text)
}