Skip to content

Commit 3896aeb

Browse files
committed
Implement 'update' command
1 parent a359c8f commit 3896aeb

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ Create a new project:
1616
codegame new
1717
```
1818

19+
Update event definitions, wrappers and libraries to match the latest game version:
20+
```sh
21+
codegame update
22+
```
23+
1924
Run a project:
2025
```sh
2126
codegame run
2227
```
2328

29+
Build a project:
30+
```sh
31+
codegame build
32+
```
33+
2434
Get information about a game server:
2535
```sh
2636
codegame info <url>

commands/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Build() error {
2626

2727
switch data.Lang {
2828
case "go":
29-
err = modules.Execute("go", "latest", "client", args...)
29+
err = modules.Execute("go", "latest", data.Type, args...)
3030
default:
3131
return cli.Error("'build' is not supported for '%s'", data.Lang)
3232
}

commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Run() error {
2626

2727
switch data.Lang {
2828
case "go":
29-
err = modules.Execute("go", "latest", "client", args...)
29+
err = modules.Execute("go", "latest", data.Type, args...)
3030
default:
3131
return cli.Error("'run' is not supported for '%s'", data.Lang)
3232
}

commands/update.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
"strings"
6+
7+
"github.com/Bananenpro/cli"
8+
"github.com/code-game-project/codegame-cli/util/cgfile"
9+
"github.com/code-game-project/codegame-cli/util/cggenevents"
10+
"github.com/code-game-project/codegame-cli/util/external"
11+
"github.com/code-game-project/codegame-cli/util/modules"
12+
)
13+
14+
func Update() error {
15+
root, err := cgfile.FindProjectRoot()
16+
if err != nil {
17+
return err
18+
}
19+
20+
err = os.Chdir(root)
21+
if err != nil {
22+
return err
23+
}
24+
25+
data, err := cgfile.LoadCodeGameFile("")
26+
if err != nil {
27+
return cli.Error("Failed to load .codegame.json")
28+
}
29+
30+
switch data.Type {
31+
case "client":
32+
return updateClient(data)
33+
case "server":
34+
return updateServer(data)
35+
default:
36+
return cli.Error("Unknown project type: %s", data.Type)
37+
}
38+
39+
}
40+
41+
func updateClient(config *cgfile.CodeGameFileData) error {
42+
baseURL := baseURL(config.URL)
43+
44+
_, cgVersion, err := getCodeGameInfo(baseURL)
45+
if err != nil {
46+
return err
47+
}
48+
49+
cgeVersion, err := cggenevents.GetCGEVersion(baseURL)
50+
if err != nil {
51+
return err
52+
}
53+
54+
switch config.Lang {
55+
case "go":
56+
libraryVersion := external.LibraryVersionFromCGVersion("code-game-project", "go-client", cgVersion)
57+
err = modules.Execute("go", libraryVersion, "client", "update", "--library-version="+libraryVersion)
58+
default:
59+
return cli.Error("'update' is not supported for '%s'", config.Lang)
60+
}
61+
if err != nil {
62+
return err
63+
}
64+
65+
eventsOutput := "."
66+
if config.Lang == "go" {
67+
eventsOutput = strings.ReplaceAll(strings.ReplaceAll(config.Game, "-", ""), "_", "")
68+
}
69+
70+
if config.Lang == "go" || config.Lang == "ts" {
71+
err = cggenevents.CGGenEvents(cgeVersion, eventsOutput, baseURL, config.Lang)
72+
}
73+
74+
return err
75+
}
76+
77+
func updateServer(config *cgfile.CodeGameFileData) error {
78+
switch config.Lang {
79+
case "go":
80+
return modules.Execute("go", "latest", "server", "update")
81+
default:
82+
return cli.Error("'update' is not supported for '%s'", config.Lang)
83+
}
84+
}

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func main() {
2525
fmt.Fprintln(os.Stderr, "The official CodeGame CLI.")
2626
fmt.Fprintln(os.Stderr, "\nCommands:")
2727
fmt.Fprintln(os.Stderr, "\tnew \tCreate a new project.")
28+
fmt.Fprintln(os.Stderr, "\tupdate \tUpdate the current project.")
2829
fmt.Fprintln(os.Stderr, "\trun \tRun the current project.")
2930
fmt.Fprintln(os.Stderr, "\tbuild \tBuild the current project.")
3031
fmt.Fprintln(os.Stderr, "\tinfo \tDisplay some info about a game server.")
@@ -47,6 +48,8 @@ func main() {
4748
switch command {
4849
case "new":
4950
err = commands.New()
51+
case "update":
52+
err = commands.Update()
5053
case "info":
5154
err = commands.Info()
5255
case "docs":

0 commit comments

Comments
 (0)