-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
albertrdixon
committed
Mar 10, 2015
1 parent
6299c52
commit 3a896e2
Showing
6 changed files
with
214 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
package stack | ||
|
||
import ( | ||
"testing" | ||
"testing" | ||
) | ||
|
||
var stacktests = []struct { | ||
list []interface{} | ||
list []interface{} | ||
}{ | ||
{[]interface{}{"one", "two", "three"}}, | ||
{[]interface{}{1, 2, 3}}, | ||
{[]interface{}{"one", "two", "three"}}, | ||
{[]interface{}{1, 2, 3}}, | ||
} | ||
|
||
func TestStack(t *testing.T) { | ||
for _, st := range stacktests { | ||
i, s := 0, NewStack() | ||
for _, in := range st.list { | ||
if s.Len() != i { | ||
t.Errorf("stack.Len(): %d, want %d", s.Len(), i) | ||
} | ||
s.Push(in) | ||
i++ | ||
} | ||
for j := 2; j >= 0; j-- { | ||
item := s.Pop() | ||
if st.list[j] != item { | ||
t.Errorf("stack.Pop(): %v, want %v", item, st.list[j]) | ||
} | ||
} | ||
} | ||
for _, st := range stacktests { | ||
i, s := 0, NewStack() | ||
for _, in := range st.list { | ||
if s.Len() != i { | ||
t.Errorf("stack.Len(): %d, want %d", s.Len(), i) | ||
} | ||
s.Push(in) | ||
i++ | ||
} | ||
for j := 2; j >= 0; j-- { | ||
item := s.Pop() | ||
if st.list[j] != item { | ||
t.Errorf("stack.Pop(): %v, want %v", item, st.list[j]) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,115 @@ | ||
package template | ||
|
||
import ( | ||
"crypto/sha1" | ||
"fmt" | ||
"github.com/albertrdixon/tmplnator/stack" | ||
"github.com/oxtoacart/bpool" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
"crypto/sha1" | ||
"fmt" | ||
"github.com/albertrdixon/tmplnator/stack" | ||
"github.com/oxtoacart/bpool" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
type Template struct { | ||
template *template.Template | ||
Sha1 string | ||
Vars map[string]string | ||
Src string | ||
template *template.Template | ||
Sha1 string | ||
Vars map[string]string | ||
Src string | ||
} | ||
|
||
var ( | ||
files map[string]*File | ||
varMap map[string]string | ||
bp *bpool.BufferPool | ||
files map[string]*File | ||
varMap map[string]string | ||
bp *bpool.BufferPool | ||
) | ||
|
||
func (t Template) Write() error { | ||
b := bp.Get() | ||
defer bp.Put(b) | ||
b := bp.Get() | ||
defer bp.Put(b) | ||
|
||
err := t.template.Execute(b, t) | ||
if err != nil { | ||
return err | ||
} | ||
err := t.template.Execute(b, t) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
file := files[t.Sha1] | ||
fmt.Printf("==> Generating %q from %q\n", file.Destination(), t.Src) | ||
if err := os.MkdirAll(file.DestinationDir(), 0755); err != nil { | ||
return err | ||
} | ||
f, err := os.OpenFile(file.Destination(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(file.Mode)) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
_, err = f.Write(b.Bytes()) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
file := files[t.Sha1] | ||
fmt.Printf("==> Generating %q from %q\n", file.Destination(), t.Src) | ||
if err := os.MkdirAll(file.DestinationDir(), 0755); err != nil { | ||
return err | ||
} | ||
f, err := os.OpenFile(file.Destination(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(file.Mode)) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
_, err = f.Write(b.Bytes()) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func ParseDirectory(dir string, prefix string) (*stack.Stack, error) { | ||
st := stack.NewStack() | ||
return st, filepath.Walk(dir, walkfunc(st, prefix)) | ||
st := stack.NewStack() | ||
return st, filepath.Walk(dir, walkfunc(st, prefix)) | ||
} | ||
|
||
func walkfunc(tmplStack *stack.Stack, prefix string) filepath.WalkFunc { | ||
return func(path string, info os.FileInfo, err error) error { | ||
if info.Mode().IsRegular() { | ||
return ParseTemplate(path, prefix, tmplStack) | ||
} | ||
return nil | ||
} | ||
return func(path string, info os.FileInfo, err error) error { | ||
if info.Mode().IsRegular() { | ||
return ParseTemplate(path, prefix, tmplStack) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func ParseTemplate(path string, prefix string, st *stack.Stack) error { | ||
base := filepath.Base(path) | ||
t := new(Template) | ||
t.Src = path | ||
t.Vars = varMap | ||
contents, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
base := filepath.Base(path) | ||
t := new(Template) | ||
t.Src = path | ||
t.Vars = varMap | ||
contents, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.Sha1 = fmt.Sprintf("%x", sha1.New().Sum(contents)) | ||
template, err := newTemplate(path).Parse(string(contents)) | ||
if err != nil { | ||
return err | ||
} | ||
t.Sha1 = fmt.Sprintf("%x", sha1.New().Sum(contents)) | ||
template, err := newTemplate(path).Parse(string(contents)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.template = template | ||
st.Push(t) | ||
files[t.Sha1] = newFile(prefix, base) | ||
return nil | ||
t.template = template | ||
st.Push(t) | ||
files[t.Sha1] = newFile(prefix, base) | ||
return nil | ||
} | ||
|
||
func newTemplate(path string) *template.Template { | ||
return template.New(path).Funcs(newFuncMap()) | ||
return template.New(path).Funcs(newFuncMap()) | ||
} | ||
|
||
func envMap() map[string]string { | ||
env := make(map[string]string, len(os.Environ())) | ||
for _, val := range os.Environ() { | ||
index := strings.Index(val, "=") | ||
env[val[:index]] = val[index+1:] | ||
} | ||
return env | ||
env := make(map[string]string, len(os.Environ())) | ||
for _, val := range os.Environ() { | ||
index := strings.Index(val, "=") | ||
env[val[:index]] = val[index+1:] | ||
} | ||
return env | ||
} | ||
|
||
func iface(list []string) []interface{} { | ||
vals := make([]interface{}, len(list)) | ||
for i, v := range list { | ||
vals[i] = v | ||
} | ||
return vals | ||
vals := make([]interface{}, len(list)) | ||
for i, v := range list { | ||
vals[i] = v | ||
} | ||
return vals | ||
} | ||
|
||
func init() { | ||
bp = bpool.NewBufferPool(48) | ||
varMap = envMap() | ||
files = make(map[string]*File) | ||
bp = bpool.NewBufferPool(48) | ||
varMap = envMap() | ||
files = make(map[string]*File) | ||
} |
Oops, something went wrong.