-
Notifications
You must be signed in to change notification settings - Fork 19
/
plugin.go
55 lines (50 loc) · 1.94 KB
/
plugin.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
package main
import (
"bytes"
"fmt"
"go/format"
"os"
"strings"
"text/template"
"github.com/mgutz/ansi"
)
func getPluginTemplate(modulePath, pluginName string) {
data := PluginTmplData{
ModulePath: modulePath,
PluginName: pluginName,
PluginTitle: strings.Title(pluginName),
}
checkError(os.WriteFile("./"+pluginName+".go", parsePluginTmpl("main", data), 0644))
checkError(os.WriteFile("./router.go", parsePluginTmpl("router", data), 0644))
checkError(os.Mkdir("controller", os.ModePerm))
checkError(os.WriteFile("./controller/common.go", parsePluginTmpl("controller", data), 0644))
checkError(os.WriteFile("./controller/example.go", parsePluginTmpl("controller_example", data), 0644))
checkError(os.Mkdir("guard", os.ModePerm))
checkError(os.WriteFile("./guard/guard.go", parsePluginTmpl("guard", data), 0644))
checkError(os.WriteFile("./guard/example.go", parsePluginTmpl("guard_example", data), 0644))
checkError(os.Mkdir("modules", os.ModePerm))
checkError(os.Mkdir("./modules/language", os.ModePerm))
checkError(os.WriteFile("./modules/language/language.go", parsePluginTmpl("language", data), 0644))
checkError(os.WriteFile("./modules/language/cn.go", parsePluginTmpl("language_cn", data), 0644))
checkError(os.WriteFile("./modules/language/en.go", parsePluginTmpl("language_en", data), 0644))
checkError(os.WriteFile("./Makefile", []byte(pluginTemplate["makefile"]), 0644))
checkError(os.Mkdir("example", os.ModePerm))
checkError(os.Mkdir("tests", os.ModePerm))
fmt.Println()
fmt.Println(ansi.Color(getWord("Generate plugin template success~~🍺🍺"), "green"))
fmt.Println()
}
type PluginTmplData struct {
PluginName string
PluginTitle string
ModulePath string
}
func parsePluginTmpl(name string, data PluginTmplData) []byte {
t, err := template.New("plugin").Parse(pluginTemplate[name])
checkError(err)
buf := new(bytes.Buffer)
checkError(t.Execute(buf, data))
c, err := format.Source(buf.Bytes())
checkError(err)
return c
}