Skip to content

Commit

Permalink
feat: First implement
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed Feb 10, 2018
1 parent a44743e commit 6caf676
Show file tree
Hide file tree
Showing 105 changed files with 20,966 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/angular
Empty file added CHANGELOG.md
Empty file.
33 changes: 33 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Gopkg.toml
@@ -0,0 +1,29 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"

[[constraint]]
name = "github.com/tsuyoshiwada/go-gitcmd"
version = "0.0.1"
16 changes: 16 additions & 0 deletions changelog.tpl.md
@@ -0,0 +1,16 @@
# CHANGELOG

{{range .Versions}}
<a name="{{urlquery .Tag.Name}}"></a>
## {{.Tag.Name}} ({{datetime "2006-01-02" .Tag.Date}})
{{range .CommitGroups}}
### {{.Title}}
{{range .Commits}}
* {{if ne .Scope ""}}**{{.Scope}}:** {{end}}{{.Subject}}{{end}}
{{end}}{{range .NoteGroups}}
### {{.Title}}
{{range .Notes}}
{{.Body}}
{{end}}
{{end}}
{{end}}
179 changes: 179 additions & 0 deletions chglog.go
@@ -0,0 +1,179 @@
package chglog

import (
"io"
"os"
"path/filepath"
"text/template"
"time"

gitcmd "github.com/tsuyoshiwada/go-gitcmd"
)

// Options ...
type Options struct {
Processor Processor
CommitFilters map[string][]string
CommitSortBy string
CommitGroupBy string
CommitGroupSortBy string
CommitGroupTitleMaps map[string]string
HeaderPattern string
HeaderPatternMaps []string
IssuePrefix []string
RefActions []string
MergeNoteTitle string
MergePattern string
MergePatternMaps []string
RevertNoteTitle string
RevertPattern string
RevertPatternMaps []string
NoteKeywords []string
}

// Info ...
type Info struct {
Title string
RepositoryURL string
}

// RenderData ...
type RenderData struct {
Info *Info
Versions []*Version
}

// Config ...
type Config struct {
Bin string
Path string
Template string
Info *Info
Options *Options
}

// Generator ...
type Generator struct {
client gitcmd.Client
config *Config
tagReader *tagReader
tagSelector *tagSelector
commitParser *commitParser
commitExtractor *commitExtractor
}

// NewGenerator ...
func NewGenerator(config *Config) *Generator {
client := gitcmd.New(&gitcmd.Config{
Bin: config.Bin,
})

if config.Options.Processor != nil {
config.Options.Processor.Bootstrap(config)
}

return &Generator{
client: client,
config: config,
tagReader: newTagReader(client),
tagSelector: newTagSelector(),
commitParser: newCommitParser(client, config),
commitExtractor: newCommitExtractor(config.Options),
}
}

// Generate ...
func (gen *Generator) Generate(w io.Writer, query string) error {
back, err := gen.workdir()
if err != nil {
return err
}

versions, err := gen.readVersions(query)
if err != nil {
return err
}

back()

return gen.render(w, versions)
}

func (gen *Generator) readVersions(query string) ([]*Version, error) {
tags, err := gen.tagReader.ReadAll()
if err != nil {
return nil, err
}

first := ""
if query != "" {
tags, first, err = gen.tagSelector.Select(tags, query)
if err != nil {
return nil, err
}
}

versions := []*Version{}

for i, tag := range tags {
var rev string

if i+1 < len(tags) {
rev = tags[i+1].Name + ".." + tag.Name
} else {
if first != "" {
rev = first + ".." + tag.Name
} else {
rev = tag.Name
}
}

commits, err := gen.commitParser.Parse(rev)
if err != nil {
return nil, err
}

commitGroups, noteGroups := gen.commitExtractor.Extract(commits)

versions = append(versions, &Version{
Tag: tag,
CommitGroups: commitGroups,
NoteGroups: noteGroups,
})
}

return versions, nil
}

func (gen *Generator) workdir() (func() error, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}

err = os.Chdir(gen.config.Path)
if err != nil {
return nil, err
}

return func() error {
return os.Chdir(cwd)
}, nil
}

func (gen *Generator) render(w io.Writer, versions []*Version) error {
fmap := template.FuncMap{
"datetime": func(layout string, input time.Time) string {
return input.Format(layout)
},
}

fname := filepath.Base(gen.config.Template)

t := template.Must(template.New(fname).Funcs(fmap).ParseFiles(gen.config.Template))

return t.Execute(w, &RenderData{
Info: gen.config.Info,
Versions: versions,
})
}
22 changes: 22 additions & 0 deletions chglog_test.go
@@ -0,0 +1,22 @@
package chglog

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func workdir(dir string) func() {
cwd, _ := os.Getwd()
os.Chdir(dir)
return func() {
os.Chdir(cwd)
}
}

func TestGenerator(t *testing.T) {
t.Skip("TODO: test")
assert := assert.New(t)
assert.True(true)
}

0 comments on commit 6caf676

Please sign in to comment.