2020-11-10 10:04:13 +00:00
|
|
|
package changelog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-10 10:36:50 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2020-11-10 10:04:13 +00:00
|
|
|
"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)
|
2020-11-10 10:56:26 +00:00
|
|
|
previousLines := g.getPreviousLines()
|
2020-11-10 10:36:50 +00:00
|
|
|
|
|
|
|
lines = append(lines, previousLines...)
|
|
|
|
|
|
|
|
if err := g.writeLines(lines); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:04:13 +00:00
|
|
|
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))
|
2020-11-10 10:04:13 +00:00
|
|
|
case fixedType:
|
2020-11-10 10:12:13 +00:00
|
|
|
fixedLines = append(fixedLines, g.composeListItem(commit.RawHeader))
|
2020-11-10 10:04:13 +00:00
|
|
|
case othersType:
|
2020-11-10 10:12:13 +00:00
|
|
|
othersLines = append(othersLines, g.composeListItem(commit.RawHeader))
|
2020-11-10 10:04:13 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:59:15 +00:00
|
|
|
if len(addedLines) != 0 {
|
|
|
|
lines = append(lines, g.composeTypeHeader(addedType))
|
|
|
|
lines = append(lines, addedLines...)
|
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2020-11-10 10:59:15 +00:00
|
|
|
if len(fixedLines) != 0 {
|
|
|
|
lines = append(lines, g.composeTypeHeader(fixedType))
|
|
|
|
lines = append(lines, fixedLines...)
|
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
2020-11-10 10:59:15 +00:00
|
|
|
if len(othersLines) != 0 {
|
|
|
|
lines = append(lines, g.composeTypeHeader(othersType))
|
|
|
|
lines = append(lines, othersLines...)
|
|
|
|
}
|
2020-11-10 10:04:13 +00:00
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:56:26 +00:00
|
|
|
func (g *MarkdownGenerator) getPreviousLines() []string {
|
2020-11-10 10:36:50 +00:00
|
|
|
prevData, err := ioutil.ReadFile(g.path)
|
|
|
|
if err != nil {
|
2020-11-10 10:56:26 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:56:26 +00:00
|
|
|
return finalPrevLines
|
2020-11-10 10:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *MarkdownGenerator) writeLines(lines []string) error {
|
2020-11-10 10:56:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-10 10:04:13 +00:00
|
|
|
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)
|
2020-11-10 10:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-10 10:56:26 +00:00
|
|
|
return fmt.Sprintf("- %s", text)
|
2020-11-10 10:04:13 +00:00
|
|
|
}
|