-
Notifications
You must be signed in to change notification settings - Fork 6
/
template.go
executable file
·216 lines (195 loc) · 4.75 KB
/
template.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package tto
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"sync"
)
var predefineRegexp = regexp.MustCompilePOSIX(`#\!([^\!-]+):([^#]+?)`)
var lineJoinRegexp = regexp.MustCompile(`\s*\\(\s+)`)
// var lineJoinRegexp = regexp.MustCompile(`\\s*\\\\(\\s+)`)
var tplCommentRegexp = regexp.MustCompile(`/\*+(\s*)#!(.+?)*/`)
type TemplateKind int
const (
// 普通模板
KindNormal = TemplateKind(0)
// 使用所有表生成模板
KindTables = TemplateKind(1)
// 按表前缀生成模板
KindTablePrefix = TemplateKind(2)
)
type Template struct {
path string
template string
predefine map[string]string
kind TemplateKind
mux *sync.RWMutex
}
func NewTemplate(s string, path string, attach bool) *Template {
t := &Template{path: path, mux: &sync.RWMutex{}}
return t.resolve(t.attach(s, attach))
}
func (g *Template) resolve(s string) *Template {
g.mux.Lock()
g.predefine = make(map[string]string)
for _, match := range predefineRegexp.FindAllStringSubmatch(s, -1) {
g.predefine[match[1]] = match[2]
}
g.mux.Unlock()
g.template = g.formatContent(s)
// 识别类型
switch g.predefine["kind"] {
case "1", "tables":
g.kind = KindTables
case "2", "prefix":
g.kind = KindTablePrefix
default:
g.kind = KindNormal
}
return g
}
// 返回模板内容
func (g *Template) formatContent(s string) string {
s = predefineRegexp.ReplaceAllString(s, "")
// 去掉模板注释
s = tplCommentRegexp.ReplaceAllString(s, "")
// 去掉多余的换行
s = lineJoinRegexp.ReplaceAllString(s, "")
return s
}
func (g *Template) Kind() TemplateKind {
return g.kind
}
// 文件路径
func (g *Template) FilePath() string {
return g.path
}
// 获取模板内容
func (g *Template) String() string {
return g.template
}
// 获取预定义的参数
func (g *Template) Predefine(key string) (string, bool) {
g.mux.RLock()
defer g.mux.RUnlock()
n, ok := g.predefine[key]
return n, ok
}
// attach: attach generator copyright at template file first line
func (g *Template) attach(s string, attach bool) string {
if attach {
l := getLangByPath(g.path)
if l == L_Python {
return g.python(s)
}
if l == L_Dart {
return g.dart(s)
}
if len(s) > 1 && s[0] != '/' {
if l != L_Unknown {
return g.copyright(s)
}
}
}
return s
}
func (g *Template) baseContent() string {
return `#
# This file is auto generated by tto v` + BuildVersion + ` !
# If you want to modify this code, please read the guide
# to modify code template.
#
# Get started: https://github.com/ixre/tto
#
# Copyright (C) 2009-{{.global.year}} {{.global.organization}}, All rights reserved.
#
# name : $file_name$
# author : {{.global.user}}
# date : {{.global.time}}
# description :
# history :
#`
}
func (g *Template) python(s string) string {
if strings.HasPrefix(s, "#") && !strings.HasPrefix(s, "#!") {
return s
}
return g.baseContent() + s
}
func (g *Template) copyright(s string) string {
return `/**
* This file is auto generated by tto v` + BuildVersion + ` !
* If you want to modify this code, please read the guide
* to modify code template.
*
* Get started: https://github.com/ixre/tto
*
* Copyright (C) 2009-{{.global.year}} {{.global.organization}}, All rights reserved.
*
* name : $file_name$
* author : {{.global.user}}
* date : {{.global.time}}
* description :
* history :
*/
` + s
}
func (g *Template) dart(s string) string {
if strings.HasPrefix(s, "///") {
return s
}
return strings.Replace(g.baseContent(), "#", "///", -1) + s
}
// TemplatePackageInfo 模板包信息
type TemplatePackageInfo struct {
// 包名
PackageName string `json:"packageName"`
// 模板名称
TemplateName string `json:"templateName"`
// 作者
Author string `json:"author"`
// 模板网址
Url string `json:"url"`
// 版本号
Version string `json:"version"`
// 最后更新时间
LastUpdate string `json:"lastUpdate"`
}
// TemplatePackageJson 模板包信息配置
type templatePackageJson struct {
// 模板名称
Name string `json:"name"`
// 模板作者
Author string `json:"author"`
// 模板网址
Url string `json:"url"`
// 版本号
Version string `json:"version"`
// 最后更新时间
LastUpdate string `json:"lastUpdate"`
}
// ResolveTemplatePackage 解析模板包信息
func ResolveTemplatePackage(path string) *TemplatePackageInfo {
pkgName := path[strings.LastIndex(path, "/")+1:]
ret := &TemplatePackageInfo{
PackageName: pkgName,
TemplateName: pkgName,
Version: "0.1",
LastUpdate: "-",
}
jsonPath := fmt.Sprintf("%s/%s", path, "package.json")
bytes, err := os.ReadFile(jsonPath)
if err == nil {
// 获取模板包信息
var js templatePackageJson
json.Unmarshal(bytes, &js)
ret.Author = js.Author
ret.Url = js.Url
ret.TemplateName = js.Name
ret.Version = js.Version
ret.LastUpdate = js.LastUpdate
}
return ret
}