/
create.go
108 lines (100 loc) · 2.09 KB
/
create.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
// Created by EldersJavas(EldersJavas&gmail.com)
package app
import (
"embed"
"fmt"
"github.com/EldersJavas/EbiBuilder/tool"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/sysutil"
"github.com/valyala/fasttemplate"
"io"
"io/fs"
"os"
)
var CreateCmd = NewCreateCmd()
//go:embed demo
var CreateFile embed.FS
var CFF fs.FS
var Npj Project
func init() {
f, err := fs.Sub(CreateFile, "demo")
if err != nil {
panic(err)
}
CFF = f
}
func NewCreateCmd() *gcli.Command {
a := &gcli.Command{
Name: "create",
Desc: "create new ebiengine project",
Aliases: []string{"Create", "CREATE"},
Config: func(c *gcli.Command) {
c.StrOpt(&Npj.GameName, "gamename", "n", "Game", "Game project name")
c.BoolOpt(&Npj.IsGomod, "gomod", "gm", true, "Go mod")
//c.StrOpt(&Npj.Iconfile, "icon", "i", "favicon.ico", "icon file")
},
Examples: "ebibuilder create",
Func: func(c *gcli.Command, args []string) error {
err := CreateGame()
if err != nil {
return err
}
return nil
},
Help: "create a new ebiengine project.",
}
return a
}
func CreateGame() error {
tool.StepPrint("Create Start")
err := fsutil.Mkdir(Npj.GameName, 0777)
if err != nil {
return err
}
err = os.Chdir(Npj.GameName)
if err != nil {
return err
}
fi, err := CFF.Open("main.go.tmpl")
if err != nil {
return err
}
asll, err := io.ReadAll(fi)
if err != nil {
return err
}
t := fasttemplate.New(string(asll), "{{", "}}")
s := t.ExecuteString(map[string]interface{}{
"GameName": Npj.GameName,
})
/*tmpl := template.New("demo")
tmpl, err = tmpl.Parse(string(asll))
if err != nil {
return err
}
err = tmpl.Execute(Wr, p)
if err != nil {
return err
}*/
err = os.WriteFile("main.go", []byte(fmt.Sprint(s)), 0777)
if err != nil {
return err
}
pp, err := sysutil.QuickExec("go mod init " + Npj.GameName)
tool.InfoPrint(pp)
if err != nil {
return err
}
pp, err = sysutil.QuickExec("go mod tidy")
tool.InfoPrint(pp)
if err != nil {
return err
}
err = InitConfig()
if err != nil {
return err
}
tool.SuccessPrint("Create Success")
return nil
}