Skip to content

Commit

Permalink
Merge 3ad7430 into 84b8439
Browse files Browse the repository at this point in the history
  • Loading branch information
Masayuki Izumi committed Jul 30, 2017
2 parents 84b8439 + 3ad7430 commit fd74024
Show file tree
Hide file tree
Showing 19 changed files with 1,309 additions and 3 deletions.
34 changes: 34 additions & 0 deletions domain/scaffold/concrete_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package scaffold

// ConcreteEntry is an existing file or directory that created from the scaffold template
type ConcreteEntry interface {
Entry
TemplatePath() string
}

type concreteEntry struct {
Entry
templatePath string
}

// NewConcreteFile returns a new TemplateEntry object treated as a file
func NewConcreteFile(path, content, templatePath string) ConcreteEntry {
return NewConcreteEntry(path, content, false, templatePath)
}

// NewConcreteDir returns a new TemplateEntry object treated as a directory
func NewConcreteDir(path, templatePath string) ConcreteEntry {
return NewConcreteEntry(path, "", true, templatePath)
}

// NewConcreteEntry returns a new Entry object
func NewConcreteEntry(path, content string, dir bool, templatePath string) ConcreteEntry {
return &concreteEntry{
Entry: NewEntry(path, content, dir),
templatePath: templatePath,
}
}

func (e *concreteEntry) TemplatePath() string {
return e.templatePath
}
92 changes: 92 additions & 0 deletions domain/scaffold/concrete_entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package scaffold

import (
"testing"
)

func Test_NewConcreteEntry(t *testing.T) {
cases := []struct {
path string
content string
dir bool
tmplPath string
}{
{
path: "/app",
content: "",
dir: true,
tmplPath: "/{{name}}",
},
{
path: "/app/foo.go",
content: "package app",
dir: false,
tmplPath: "/{{name}}/foo.go",
},
}

for _, c := range cases {
e := NewConcreteEntry(c.path, c.content, c.dir, c.tmplPath)

if actual, expected := e.Path(), c.path; actual != expected {
t.Errorf("Path() returns %q, want %q", actual, expected)
}

if actual, expected := e.Content(), c.content; actual != expected {
t.Errorf("Content() returns %q, want %q", actual, expected)
}

if actual, expected := e.IsDir(), c.dir; actual != expected {
t.Errorf("IsDir() returns %t, want %t", actual, expected)
}

if actual, expected := e.TemplatePath(), c.tmplPath; actual != expected {
t.Errorf("TemplatePath() returns %q, want %q", actual, expected)
}
}
}

func Test_NewConcreteFile(t *testing.T) {
path := "/app/foo.go"
content := "package app"
tmplPath := "/{{name}}/foo.go"
e := NewConcreteFile(path, content, tmplPath)

if actual, expected := e.Path(), path; actual != expected {
t.Errorf("Path() returns %q, want %q", actual, expected)
}

if actual, expected := e.Content(), content; actual != expected {
t.Errorf("Content() returns %q, want %q", actual, expected)
}

if actual, expected := e.IsDir(), false; actual != expected {
t.Errorf("IsDir() returns %t, want %t", actual, expected)
}

if actual, expected := e.TemplatePath(), tmplPath; actual != expected {
t.Errorf("TemplatePath() returns %q, want %q", actual, expected)
}
}

func Test_NewConcreteDir(t *testing.T) {
path := "/app/foo.go"
tmplPath := "/{{name}}/foo.go"
e := NewConcreteDir(path, tmplPath)

if actual, expected := e.Path(), path; actual != expected {
t.Errorf("Path() returns %q, want %q", actual, expected)
}

if actual, expected := e.Content(), ""; actual != expected {
t.Errorf("Content() returns %q, want %q", actual, expected)
}

if actual, expected := e.IsDir(), true; actual != expected {
t.Errorf("IsDir() returns %t, want %t", actual, expected)
}

if actual, expected := e.TemplatePath(), tmplPath; actual != expected {
t.Errorf("TemplatePath() returns %q, want %q", actual, expected)
}
}
35 changes: 35 additions & 0 deletions domain/scaffold/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package scaffold

// Entry represents a filesystem file or directory
type Entry interface {
Path() string
IsDir() bool
Content() string
}

type entry struct {
path string
content string
dir bool
}

// NewEntry returns a new Entry object
func NewEntry(path, content string, dir bool) Entry {
return &entry{
path: path,
content: content,
dir: dir,
}
}

func (e *entry) Path() string {
return e.path
}

func (e *entry) Content() string {
return e.content
}

func (e *entry) IsDir() bool {
return e.dir
}
38 changes: 38 additions & 0 deletions domain/scaffold/entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package scaffold

import "testing"

func Test_NewEntry(t *testing.T) {
cases := []struct {
path string
content string
dir bool
}{
{
path: "/app",
content: "",
dir: true,
},
{
path: "/app/foo.go",
content: "package app",
dir: false,
},
}

for _, c := range cases {
e := NewEntry(c.path, c.content, c.dir)

if actual, expected := e.Path(), c.path; actual != expected {
t.Errorf("Path() returns %q, want %q", actual, expected)
}

if actual, expected := e.Content(), c.content; actual != expected {
t.Errorf("Content() returns %q, want %q", actual, expected)
}

if actual, expected := e.IsDir(), c.dir; actual != expected {
t.Errorf("IsDir() returns %t, want %t", actual, expected)
}
}
}
5 changes: 5 additions & 0 deletions domain/scaffold/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ type Repository interface {
cb ConstructCallback,
conflictedCb ConstructConflictedCallback,
) error

GetScaffolds(tmplsPath string) ([]Scaffold, error)
GetTemplates(s Scaffold) ([]TemplateEntry, error)
GetConcreteEntries(s Scaffold, tmpls []TemplateEntry, v interface{}) (map[string]ConcreteEntry, error)
// Create(e Entry) (bool, error)
}
39 changes: 39 additions & 0 deletions domain/scaffold/repo_mock.go

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

60 changes: 60 additions & 0 deletions domain/scaffold/template_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package scaffold

import (
"github.com/pkg/errors"
)

// TemplateEntry represents a scaffold template entry
type TemplateEntry interface {
Entry
Compile(v interface{}) (Entry, error)
}

type templateEntry struct {
path TemplateString
content TemplateString
dir bool
}

// NewTemplateFile returns a new TemplateEntry object treated as a file
func NewTemplateFile(path, content TemplateString) TemplateEntry {
return NewTemplateEntry(path, content, false)
}

// NewTemplateDir returns a new TemplateEntry object treated as a directory
func NewTemplateDir(path TemplateString) TemplateEntry {
return NewTemplateEntry(path, "", true)
}

// NewTemplateEntry returns a new TemplateEntry object
func NewTemplateEntry(path, content TemplateString, dir bool) TemplateEntry {
return &templateEntry{
path: path,
content: content,
dir: dir,
}
}

func (e *templateEntry) Path() string {
return string(e.path)
}

func (e *templateEntry) IsDir() bool {
return e.dir
}

func (e *templateEntry) Content() string {
return string(e.content)
}

func (e *templateEntry) Compile(v interface{}) (Entry, error) {
path, err := e.path.Compile(string(e.path), v)
if err != nil {
return nil, errors.Wrapf(err, "Could not compile path: %q", e.path)
}
content, err := e.content.Compile(string(e.path), v)
if err != nil {
return nil, errors.Wrapf(err, "Could not compile content: %q", e.path)
}
return NewEntry(path, content, e.dir), nil
}

0 comments on commit fd74024

Please sign in to comment.