-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
100 lines (86 loc) · 2.62 KB
/
update.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
package main
import (
"errors"
"fmt"
"strings"
"github.com/Bananenpro/cli"
"github.com/code-game-project/go-utils/cgfile"
"github.com/code-game-project/go-utils/cggenevents"
"github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/external"
"github.com/code-game-project/go-utils/modules"
"github.com/code-game-project/go-utils/server"
)
func Update(projectName string) error {
config, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
data, err := modules.ReadCommandConfig[modules.UpdateData]()
if err != nil {
return err
}
if data.LibraryVersion == "latest" {
data.LibraryVersion, err = external.LatestGithubTag("code-game-project", "java-client")
data.LibraryVersion = strings.TrimPrefix(data.LibraryVersion, "v")
if err != nil {
return fmt.Errorf("Failed to determine latest library version: %w", err)
}
}
switch config.Type {
case "client":
return updateClient(projectName, data.LibraryVersion, config)
default:
return fmt.Errorf("Unknown project type: %s", config.Type)
}
}
func updateClient(projectName, libraryVersion string, config *cgfile.CodeGameFileData) error {
api, err := server.NewAPI(config.URL)
if err != nil {
return err
}
info, err := api.FetchGameInfo()
if err != nil {
return err
}
if info.DisplayName == "" {
info.DisplayName = info.Name
}
cge, err := api.GetCGEFile()
if err != nil {
return err
}
cgeVersion, err := cggenevents.ParseCGEVersion(cge)
if err != nil {
return err
}
eventNames, commandNames, err := cggenevents.GetEventNames(api.BaseURL(), cgeVersion)
if err != nil {
return err
}
cgConf, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
packageConf, ok := cgConf.LangConfig["package"]
if !ok {
return errors.New("Missing language config field `package` in .codegame.json!")
}
packageName := packageConf.(string)
if packageConf == "" {
return errors.New("Empty language config field `package` in .codegame.json!")
}
err = updateClientTemplate(libraryVersion, projectName, packageName, config.Game, info.DisplayName, eventNames, commandNames)
if err != nil {
return err
}
cli.BeginLoading("Updating java-client...")
exec.Execute(true, "mvn", "dependency:resolve")
exec.Execute(true, "mvn", "dependency:sources")
exec.Execute(true, "mvn", "dependency:resolve", "-Dclassifier=javadoc")
cli.FinishLoading()
return nil
}
func updateClientTemplate(libraryVersion, projectName, packageName, gameName, displayName string, eventNames, commandNames []string) error {
return execClientTemplate(libraryVersion, projectName, packageName, gameName, displayName, eventNames, commandNames, true)
}