Skip to content

Commit 04fb712

Browse files
committed
Refactor to work with new CLI version
1 parent dd89b73 commit 04fb712

25 files changed

+744
-891
lines changed

build.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"runtime"
8+
"strings"
9+
10+
"github.com/code-game-project/codegame-cli/pkg/cgfile"
11+
cgExec "github.com/code-game-project/codegame-cli/pkg/exec"
12+
"github.com/code-game-project/codegame-cli/pkg/modules"
13+
)
14+
15+
func Build() error {
16+
config, err := cgfile.LoadCodeGameFile("")
17+
if err != nil {
18+
return err
19+
}
20+
21+
data, err := modules.ReadCommandConfig[modules.BuildData]()
22+
if err != nil {
23+
return err
24+
}
25+
26+
switch config.Type {
27+
case "client":
28+
return buildClient(config.Game, data.Output, config.URL)
29+
case "server":
30+
return buildServer(data.Output)
31+
default:
32+
return fmt.Errorf("Unknown project type: %s", config.Type)
33+
}
34+
}
35+
36+
func buildClient(gameName, output, url string) error {
37+
out, err := getOutputName(output, false)
38+
if err != nil {
39+
return err
40+
}
41+
packageName, err := GetGoModuleName("")
42+
if err != nil {
43+
return err
44+
}
45+
gamePackageName := strings.ReplaceAll(strings.ReplaceAll(gameName, "-", ""), "_", "")
46+
47+
cmdArgs := []string{"build", "-o", out, "-ldflags", fmt.Sprintf("-X %s/%s.URL=%s", packageName, gamePackageName, url)}
48+
49+
_, err = cgExec.Execute(false, "go", cmdArgs...)
50+
return err
51+
}
52+
53+
func buildServer(output string) error {
54+
out, err := getOutputName(output, true)
55+
if err != nil {
56+
return err
57+
}
58+
cmdArgs := []string{"build", "-o", out}
59+
_, err = cgExec.Execute(false, "go", cmdArgs...)
60+
return err
61+
}
62+
63+
func getOutputName(output string, isServer bool) (string, error) {
64+
absRoot, err := filepath.Abs(".")
65+
if err != nil {
66+
return "", err
67+
}
68+
if output == "" {
69+
output = filepath.Base(absRoot)
70+
if isServer {
71+
output += "-server"
72+
}
73+
}
74+
75+
if runtime.GOOS == "windows" && !strings.HasSuffix(output, ".exe") {
76+
output += ".exe"
77+
}
78+
79+
if stat, err := os.Stat(output); err == nil && stat.IsDir() {
80+
return "", fmt.Errorf("'%s' already exists and is a directory. Specify another output name with '-o <name>'.", output)
81+
}
82+
83+
return output, nil
84+
}

build/build.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ module github.com/code-game-project/codegame-cli-go
33
go 1.18
44

55
require (
6-
github.com/Bananenpro/cli v0.1.3
7-
github.com/Bananenpro/pflag v1.0.7
6+
github.com/Bananenpro/cli v0.1.5
87
github.com/code-game-project/codegame-cli v0.6.0
98
)
109

10+
replace github.com/code-game-project/codegame-cli => /home/julian/Dokumente/Code/CodeGame/codegame-cli
11+
1112
require (
1213
github.com/AlecAivazis/survey/v2 v2.3.5 // indirect
1314
github.com/adrg/xdg v0.4.0 // indirect
1415
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
1516
github.com/mattn/go-colorable v0.1.12 // indirect
1617
github.com/mattn/go-isatty v0.0.14 // indirect
1718
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
18-
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
19-
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
19+
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 // indirect
20+
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
2021
golang.org/x/text v0.3.7 // indirect
2122
)

go.sum

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ github.com/AlecAivazis/survey/v2 v2.3.5 h1:A8cYupsAZkjaUmhtTYv3sSqc7LO5mp1XDfqe5
22
github.com/AlecAivazis/survey/v2 v2.3.5/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI=
33
github.com/Bananenpro/cli v0.1.3 h1:FmMsU7Vc0h9b311Ki8LhGkcS6fPINUE+ci5jZNVs178=
44
github.com/Bananenpro/cli v0.1.3/go.mod h1:JBXpIAXo/D0rlsfgCViQBicjcJY6UWUldmxvKM+ijRc=
5-
github.com/Bananenpro/pflag v1.0.7 h1:mKWwZ1/XrEsIporK1PRh6psvkhGg3HsTEs/hTK2EMZU=
6-
github.com/Bananenpro/pflag v1.0.7/go.mod h1:AmjL495QkJVOAmSCFZh3RA6Q/a53pg+nD3hwKCsVR2g=
5+
github.com/Bananenpro/cli v0.1.5 h1:6JbIE1BnhzooklfgqjWbAkNhf+AyOpAbSzSNFJAdr9s=
6+
github.com/Bananenpro/cli v0.1.5/go.mod h1:JBXpIAXo/D0rlsfgCViQBicjcJY6UWUldmxvKM+ijRc=
77
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
88
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
99
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
1010
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
11-
github.com/code-game-project/codegame-cli v0.6.0 h1:dOXabsGNqYLkDiPbiq6+iaVbR984wehy3Twie1VytuE=
12-
github.com/code-game-project/codegame-cli v0.6.0/go.mod h1:DeQ4gad7atFDLgOIh2ETEgBfjijd+GO2sU578irahv0=
1311
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
1412
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
1513
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -30,7 +28,6 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQ
3028
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
3129
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3230
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
33-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
3431
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3532
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3633
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
@@ -43,9 +40,13 @@ golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBc
4340
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4441
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c h1:aFV+BgZ4svzjfabn8ERpuB4JI4N6/rdy1iusx77G3oU=
4542
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
43+
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 h1:dyU22nBWzrmTQxtNrr4dzVOvaw35nUYE279vF9UmsI8=
44+
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4645
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
4746
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
4847
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
48+
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
49+
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
4950
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
5051
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
5152
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=

util/module.go renamed to gomod.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
package util
1+
package main
22

33
import (
44
"bufio"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"strings"
8-
9-
"github.com/Bananenpro/cli"
109
)
1110

12-
func GetModuleName(projectRoot string) (string, error) {
11+
func GetGoModuleName(projectRoot string) (string, error) {
1312
path := filepath.Join(projectRoot, "go.mod")
1413
file, err := os.Open(path)
1514
if err != nil {
16-
return "", cli.Error("Failed to open '%s'", path)
15+
return "", fmt.Errorf("Failed to open '%s'", path)
1716
}
1817

1918
scanner := bufio.NewScanner(file)
@@ -23,7 +22,7 @@ func GetModuleName(projectRoot string) (string, error) {
2322
}
2423
}
2524
if scanner.Err() != nil {
26-
cli.Error("Failed to read '%s': %s", path, scanner.Err())
25+
return "", fmt.Errorf("Failed to read '%s': %s", path, scanner.Err())
2726
}
28-
return "", cli.Error("Missing 'module' statement in '%s'", path)
27+
return "", fmt.Errorf("Missing 'module' statement in '%s'", path)
2928
}

0 commit comments

Comments
 (0)