@@ -3,13 +3,16 @@ package client
3
3
import (
4
4
"fmt"
5
5
"net/http"
6
+ "os"
6
7
"path/filepath"
7
8
"strings"
8
9
9
10
_ "embed"
10
11
11
12
"github.com/Bananenpro/cli"
12
13
"github.com/code-game-project/codegame-cli-go/new"
14
+ "github.com/code-game-project/codegame-cli-go/util"
15
+ "github.com/code-game-project/codegame-cli/util/cgfile"
13
16
"github.com/code-game-project/codegame-cli/util/cggenevents"
14
17
"github.com/code-game-project/codegame-cli/util/exec"
15
18
"github.com/code-game-project/codegame-cli/util/external"
@@ -63,7 +66,7 @@ func CreateNewClient(projectName, gameName, serverURL, libraryVersion string, ge
63
66
}
64
67
}
65
68
66
- err = createClientTemplate (projectName , module , gameName , serverURL , libraryURL , generateWrappers , eventNames )
69
+ err = createClientTemplate (module , gameName , serverURL , libraryURL , generateWrappers , eventNames )
67
70
if err != nil {
68
71
return err
69
72
}
@@ -79,12 +82,62 @@ func CreateNewClient(projectName, gameName, serverURL, libraryVersion string, ge
79
82
return nil
80
83
}
81
84
82
- func createClientTemplate (projectName , modulePath , gameName , serverURL , libraryURL string , wrappers bool , eventNames []string ) error {
85
+ func Update (libraryVersion string , config * cgfile.CodeGameFileData ) error {
86
+ libraryURL , libraryTag , err := getClientLibraryURL (libraryVersion )
87
+ if err != nil {
88
+ return err
89
+ }
90
+
91
+ url := baseURL (config .URL , isSSL (config .URL ))
92
+
93
+ var eventNames []string
94
+ cgeVersion , err := cggenevents .GetCGEVersion (url )
95
+ if err != nil {
96
+ return err
97
+ }
98
+
99
+ eventNames , err = cggenevents .GetEventNames (url , cgeVersion )
100
+ if err != nil {
101
+ return err
102
+ }
103
+
104
+ module , err := util .GetModuleName ("" )
105
+ if err != nil {
106
+ return err
107
+ }
108
+
109
+ err = updateClientTemplate (module , config .Game , config .URL , libraryURL , eventNames )
110
+ if err != nil {
111
+ return err
112
+ }
113
+
114
+ cli .BeginLoading ("Updating dependencies..." )
115
+ _ , err = exec .Execute (true , "go" , "get" , "-u" , "./..." )
116
+ if err != nil {
117
+ return err
118
+ }
119
+ _ , err = exec .Execute (true , "go" , "get" , fmt .Sprintf ("%s@%s" , libraryURL , libraryTag ))
120
+ if err != nil {
121
+ return err
122
+ }
123
+ _ , err = exec .Execute (true , "go" , "mod" , "tidy" )
124
+ if err != nil {
125
+ return err
126
+ }
127
+ cli .FinishLoading ()
128
+ return nil
129
+ }
130
+
131
+ func createClientTemplate (modulePath , gameName , serverURL , libraryURL string , wrappers bool , eventNames []string ) error {
83
132
if ! wrappers {
84
- return execClientMainTemplate (projectName , serverURL , libraryURL )
133
+ return execClientMainTemplate (serverURL , libraryURL )
85
134
}
86
135
87
- return execClientWrappersTemplate (projectName , modulePath , gameName , serverURL , libraryURL , eventNames )
136
+ return execClientWrappersTemplate (modulePath , gameName , serverURL , libraryURL , eventNames , false )
137
+ }
138
+
139
+ func updateClientTemplate (modulePath , gameName , serverURL , libraryURL string , eventNames []string ) error {
140
+ return execClientWrappersTemplate (modulePath , gameName , serverURL , libraryURL , eventNames , true )
88
141
}
89
142
90
143
func getClientLibraryURL (clientVersion string ) (url string , tag string , err error ) {
@@ -101,7 +154,7 @@ func getClientLibraryURL(clientVersion string) (url string, tag string, err erro
101
154
return path , tag , nil
102
155
}
103
156
104
- func execClientMainTemplate (projectName , serverURL , libraryURL string ) error {
157
+ func execClientMainTemplate (serverURL , libraryURL string ) error {
105
158
type data struct {
106
159
URL string
107
160
LibraryURL string
@@ -113,11 +166,21 @@ func execClientMainTemplate(projectName, serverURL, libraryURL string) error {
113
166
})
114
167
}
115
168
116
- func execClientWrappersTemplate (projectName , modulePath , gameName , serverURL , libraryURL string , eventNames []string ) error {
169
+ func execClientWrappersTemplate (modulePath , gameName , serverURL , libraryURL string , eventNames []string , update bool ) error {
117
170
gamePackageName := strings .ReplaceAll (strings .ReplaceAll (gameName , "-" , "" ), "_" , "" )
118
-
119
171
gameDir := strings .ReplaceAll (strings .ReplaceAll (gameName , "-" , "" ), "_" , "" )
120
172
173
+ if update {
174
+ cli .Warn ("This action will ERASE and regenerate ALL files in '%s/'.\n You will have to manually update your code to work with the new version." , gameDir )
175
+ ok , err := cli .YesNo ("Continue?" , false )
176
+ if err != nil || ! ok {
177
+ return cli .ErrCanceled
178
+ }
179
+ os .RemoveAll (gameDir )
180
+ } else {
181
+ cli .Warn ("DO NOT EDIT the `%s/` directory. ALL CHANGES WILL BE LOST when running `codegame update`." , gameDir )
182
+ }
183
+
121
184
type event struct {
122
185
Name string
123
186
PascalName string
@@ -148,12 +211,14 @@ func execClientWrappersTemplate(projectName, modulePath, gameName, serverURL, li
148
211
Events : events ,
149
212
}
150
213
151
- err := new .ExecTemplate (wrapperMainTemplate , filepath .Join ("main.go" ), data )
152
- if err != nil {
153
- return err
214
+ if ! update {
215
+ err := new .ExecTemplate (wrapperMainTemplate , filepath .Join ("main.go" ), data )
216
+ if err != nil {
217
+ return err
218
+ }
154
219
}
155
220
156
- err = new .ExecTemplate (wrapperGameTemplate , filepath .Join (gameDir , "game.go" ), data )
221
+ err : = new .ExecTemplate (wrapperGameTemplate , filepath .Join (gameDir , "game.go" ), data )
157
222
if err != nil {
158
223
return err
159
224
}
0 commit comments