/
gen.go
149 lines (131 loc) · 3.7 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"os"
"time"
"fmt"
"strings"
"github.com/gutengo/tagen/strings2"
"github.com/gutengo/fil"
"github.com/gutengo/shell"
"github.com/gutengo/mustache"
"github.com/BurntSushi/toml"
"github.com/fatih/color"
)
var rc map[string]interface{}
var mustacheHelpers map[string]interface{}
func New(templateName, projectPath string) {
template := initialize(templateName, projectPath)
shell.Say(" %s %s\n", color.CyanString("create"), projectPath)
if err := fil.CpDirOnly(template, projectPath); err != nil {
shell.ErrorExit(err)
}
err := fil.Walk(template, func(src string, fi os.FileInfo, e error) error {
if e != nil {
shell.ErrorExit(e)
}
// skip template itself
if src == template {
return nil
}
relSrc, _ := fil.Rel(template, src)
relDest := mustache.Render(relSrc, mustacheHelpers, rc)
dest := projectPath+"/"+relDest
shell.Say(" %s %s\n", color.CyanString("create"), dest)
switch t := fil.TypeFileInfo(fi); t {
case "dir":
if err := fil.CpDirOnly(src, dest); err != nil{
shell.ErrorExit(err)
}
case "symlink":
if err := fil.Cp(src, dest); err != nil{
shell.ErrorExit(err)
}
case "regular":
if err := cpFile(src, dest, rc); err!= nil {
shell.ErrorExit(err)
}
default:
shell.ErrorExit("Unknown file type -- "+t)
}
return nil
})
if err != nil {
shell.ErrorExit(err)
}
}
func Add(templateName, name string) {
template := initialize(templateName, name)
if name == "" { name = templateName }
shell.Say(" %s %s\n", color.CyanString("create"), name)
if err := cpFile(template, name, rc); err!= nil {
shell.ErrorExit(err)
}
}
/*
func List() []string {
return
}
*/
func loadRc(file string) (ret map[string]interface{}) {
if ok, _ := fil.IsNotExist(file); ok {
return map[string]interface{}{}
}
if _, err := toml.DecodeFile(file, &ret); err != nil {
shell.ErrorExit("%s: %s\n", "Load "+file, err)
}
return ret
}
func cpFile(src, dest string, data interface{}) error {
fi, err := fil.Lstat(src)
if err != nil {
return err
}
tmpl, err := mustache.ParseFile(src)
if err != nil {
return fmt.Errorf("%s: %s", src, err.Error())
}
ret := tmpl.Render(mustacheHelpers, data)
if err := fil.WriteFile(dest, []byte(ret), fi.Mode().Perm()); err != nil {
return err
}
return nil
}
func initialize(templateName, projectPath string) string {
mustache.Otag, mustache.Ctag = "%%", "%%"
mustacheHelpers = map[string]interface{}{
"var": func(text string, render mustache.RenderFunc) string {
lines := strings.Split(text, "\n")
for _, line := range lines {
// skip empty lines
if line == "" {
continue
}
if strings.Contains(line, "||=") {
parts := strings.Split(line, "||=")
name, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
if _, ok := mustacheHelpers[name]; !ok {
mustacheHelpers[name] = render(value)
}
} else {
parts := strings.Split(line, "=")
name, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
mustacheHelpers[name] = render(value)
}
}
return ""
},
"year": func() string {
return fmt.Sprintf("%v", time.Now().Year())
},
}
appDir := os.Getenv("HOME")+"/.gutgen"
template := appDir+"/"+templateName
if ok, _ := fil.IsNotExist(template); ok {
shell.ErrorExit("template does not exists -- "+template)
}
rc = loadRc(os.Getenv("HOME") + "/.gutgenrc")
project := fil.Base(projectPath)
rc["project"] = project
rc["Project"] = strings2.ClassCase(project)
return template
}