@@ -13,38 +13,55 @@ import (
13
13
"github.com/code-game-project/codegame-cli/external"
14
14
)
15
15
16
- //go:embed templates/main.go.tmpl
17
- var goMainTemplate string
16
+ //go:embed templates/go/client/ main.go.tmpl
17
+ var goClientMainTemplate string
18
18
19
- func newClientGo (projectName , serverURL , cgVersion string ) error {
20
- err := createGoTemplate (projectName , serverURL )
19
+ //go:embed templates/go/server/main.go.tmpl
20
+ var goServerMainTemplate string
21
+
22
+ //go:embed templates/go/server/game.go.tmpl
23
+ var goServerGameTemplate string
24
+
25
+ //go:embed templates/go/server/events.cge.tmpl
26
+ var goServerCGETemplate string
27
+
28
+ //go:embed templates/go/server/events.go.tmpl
29
+ var goServerEventsTemplate string
30
+
31
+ func newGoClient (projectName , serverURL , cgVersion string ) error {
32
+ err := createGoClientTemplate (projectName , serverURL )
21
33
if err != nil {
22
34
return err
23
35
}
24
36
25
- err = installGoLibrary (projectName , cgVersion )
37
+ err = installGoClientLibrary (projectName , cgVersion )
26
38
if err != nil {
27
39
return err
28
40
}
29
41
30
42
cli .Begin ("Cleaning up..." )
31
- err = external .ExecuteInDir (projectName , "go" , "mod" , "tidy" )
32
- if err != nil {
33
- return err
34
- }
35
43
36
44
if ! external .IsInstalled ("goimports" ) {
37
45
cli .Warn ("Failed to add import statements: 'goimports' is not installed!" )
38
46
return nil
39
47
}
40
48
41
49
external .ExecuteInDir (projectName , "goimports" , "-w" , "main.go" )
50
+
51
+ out , err := external .ExecuteInDirHidden (projectName , "go" , "mod" , "tidy" )
52
+ if err != nil {
53
+ if out != "" {
54
+ cli .Error (out )
55
+ }
56
+ return err
57
+ }
58
+
42
59
cli .Finish ()
43
60
44
61
return nil
45
62
}
46
63
47
- func createGoTemplate (projectName , serverURL string ) error {
64
+ func createGoClientTemplate (projectName , serverURL string ) error {
48
65
module , err := cli .Input ("Project module path:" )
49
66
if err != nil {
50
67
return err
@@ -59,7 +76,7 @@ func createGoTemplate(projectName, serverURL string) error {
59
76
return err
60
77
}
61
78
62
- tmpl , err := template .New ("main.go" ).Parse (goMainTemplate )
79
+ tmpl , err := template .New ("main.go" ).Parse (goClientMainTemplate )
63
80
if err != nil {
64
81
return err
65
82
}
@@ -81,17 +98,18 @@ func createGoTemplate(projectName, serverURL string) error {
81
98
return err
82
99
}
83
100
84
- func installGoLibrary (projectName , cgVersion string ) error {
101
+ func installGoClientLibrary (projectName , cgVersion string ) error {
85
102
cli .Begin ("Fetching correct client library version..." )
86
103
87
104
clientVersion := external .ClientVersionFromCGVersion ("code-game-project" , "go-client" , cgVersion )
88
105
89
106
if clientVersion == "latest" {
90
- out , err := external .ExecuteInDirHidden (projectName , "go" , "get" )
91
- if err != nil && out != "" {
92
- cli .Error (out )
107
+ var err error
108
+ clientVersion , err = external .LatestGithubTag ("code-game-project" , "go-client" )
109
+ if err != nil {
110
+ return err
93
111
}
94
- return err
112
+ clientVersion = strings . TrimPrefix ( strings . Join ( strings . Split ( clientVersion , "." )[: 2 ], "." ), "v" )
95
113
}
96
114
97
115
majorVersion := strings .Split (clientVersion , "." )[0 ]
@@ -115,3 +133,148 @@ func installGoLibrary(projectName, cgVersion string) error {
115
133
cli .Finish ()
116
134
return nil
117
135
}
136
+
137
+ func newGoServer (projectName string ) error {
138
+ err := createGoServerTemplate (projectName )
139
+ if err != nil {
140
+ return err
141
+ }
142
+
143
+ err = installGoServerLibrary (projectName )
144
+ if err != nil {
145
+ return err
146
+ }
147
+
148
+ cli .Begin ("Cleaning up..." )
149
+
150
+ if ! external .IsInstalled ("goimports" ) {
151
+ cli .Warn ("Failed to add import statements: 'goimports' is not installed!" )
152
+ return nil
153
+ }
154
+
155
+ external .ExecuteInDir (projectName , "goimports" , "-w" , "main.go" )
156
+ packageDir := strings .ReplaceAll (strings .ReplaceAll (projectName , "_" , "" ), "-" , "" )
157
+ external .ExecuteInDir (projectName , "goimports" , "-w" , filepath .Join (packageDir , "game.go" ))
158
+
159
+ out , err := external .ExecuteInDirHidden (projectName , "go" , "mod" , "tidy" )
160
+ if err != nil {
161
+ if out != "" {
162
+ cli .Error (out )
163
+ }
164
+ return err
165
+ }
166
+
167
+ cli .Finish ()
168
+
169
+ return nil
170
+ }
171
+
172
+ func createGoServerTemplate (projectName string ) error {
173
+ module , err := cli .Input ("Project module path:" )
174
+ if err != nil {
175
+ return err
176
+ }
177
+
178
+ cli .Begin ("Creating project template..." )
179
+ out , err := external .ExecuteInDirHidden (projectName , "go" , "mod" , "init" , module )
180
+ if err != nil {
181
+ if out != "" {
182
+ cli .Error (out )
183
+ }
184
+ return err
185
+ }
186
+
187
+ cgeVersion , err := external .LatestCGEVersion ()
188
+ if err != nil {
189
+ return err
190
+ }
191
+
192
+ err = executeGoServerTemplate (goServerMainTemplate , "main.go" , projectName , cgeVersion )
193
+ if err != nil {
194
+ return err
195
+ }
196
+
197
+ err = executeGoServerTemplate (goServerCGETemplate , "events.cge" , projectName , cgeVersion )
198
+ if err != nil {
199
+ return err
200
+ }
201
+
202
+ packageName := strings .ReplaceAll (strings .ReplaceAll (projectName , "_" , "" ), "-" , "" )
203
+
204
+ err = executeGoServerTemplate (goServerGameTemplate , filepath .Join (packageName , "game.go" ), projectName , cgeVersion )
205
+ if err != nil {
206
+ return err
207
+ }
208
+
209
+ err = executeGoServerTemplate (goServerEventsTemplate , filepath .Join (packageName , "events.go" ), projectName , cgeVersion )
210
+ if err != nil {
211
+ return err
212
+ }
213
+
214
+ cli .Finish ()
215
+ return nil
216
+ }
217
+
218
+ func executeGoServerTemplate (templateText , fileName , projectName , cgeVersion string ) error {
219
+ tmpl , err := template .New (fileName ).Parse (templateText )
220
+ if err != nil {
221
+ return err
222
+ }
223
+
224
+ err = os .MkdirAll (filepath .Join (projectName , filepath .Dir (fileName )), 0755 )
225
+ if err != nil {
226
+ return err
227
+ }
228
+
229
+ file , err := os .Create (filepath .Join (projectName , fileName ))
230
+ if err != nil {
231
+ return err
232
+ }
233
+ defer file .Close ()
234
+
235
+ type data struct {
236
+ Name string
237
+ PackageName string
238
+ SnakeCaseName string
239
+ CGEVersion string
240
+ }
241
+
242
+ return tmpl .Execute (file , data {
243
+ Name : projectName ,
244
+ PackageName : strings .ReplaceAll (strings .ReplaceAll (projectName , "_" , "" ), "-" , "" ),
245
+ SnakeCaseName : strings .ReplaceAll (projectName , "-" , "_" ),
246
+ CGEVersion : cgeVersion ,
247
+ })
248
+ }
249
+
250
+ func installGoServerLibrary (projectName string ) error {
251
+ cli .Begin ("Fetching latest server library version..." )
252
+ tag , err := external .LatestGithubTag ("code-game-project" , "go-server" )
253
+ if err != nil {
254
+ return err
255
+ }
256
+ majorVersion := strings .TrimPrefix (strings .Split (tag , "." )[0 ], "v" )
257
+
258
+ path := "github.com/code-game-project/go-server/cg"
259
+ if majorVersion != "0" && majorVersion != "1" {
260
+ path = fmt .Sprintf ("github.com/code-game-project/go-server/v%s/cg" , majorVersion )
261
+ }
262
+ cli .Finish ()
263
+
264
+ cli .Begin ("Installing dependencies..." )
265
+
266
+ out , err := external .ExecuteInDirHidden (projectName , "go" , "get" , path )
267
+ if err != nil {
268
+ cli .Error (out )
269
+ return err
270
+ }
271
+
272
+ out , err = external .ExecuteInDirHidden (projectName , "go" , "get" , "github.com/spf13/pflag" )
273
+ if err != nil {
274
+ cli .Error (out )
275
+ return err
276
+ }
277
+
278
+ cli .Finish ()
279
+ return nil
280
+ }
0 commit comments