Skip to content

Commit 055a4dc

Browse files
committed
Implement build command
1 parent 685cf51 commit 055a4dc

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

build.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ package main
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
7+
"runtime"
8+
"strings"
59

10+
"github.com/Bananenpro/cli"
611
"github.com/code-game-project/go-utils/cgfile"
12+
"github.com/code-game-project/go-utils/exec"
713
"github.com/code-game-project/go-utils/modules"
814
)
915

@@ -27,5 +33,75 @@ func Build() error {
2733
}
2834

2935
func buildClient(gameName, output, url string) error {
36+
cli.BeginLoading("Building...")
37+
gameDir := toPascal(gameName)
38+
err := replaceInFile(filepath.Join(gameDir, "Game.cs"), "throw new InvalidOperationException(\"The CG_GAME_URL environment variable must be set.\")", "return \""+url+"\"")
39+
if err != nil {
40+
return err
41+
}
42+
43+
args := []string{"build", "--nologo", "--self-contained", "--configuration", "Release"}
44+
os := getOS()
45+
arch := getArch()
46+
if os != "" && arch != "" {
47+
args = append(args, "--runtime", os+"-"+arch)
48+
} else {
49+
args = append(args, "--use-current-runtime")
50+
}
51+
if output != "" {
52+
args = append(args, "--output", output)
53+
}
54+
_, err = exec.Execute(true, "dotnet", args...)
55+
if err != nil {
56+
return err
57+
}
58+
59+
err = replaceInFile(filepath.Join(gameDir, "Game.cs"), "return \""+url+"\"", "throw new InvalidOperationException(\"The CG_GAME_URL environment variable must be set.\")")
60+
if err != nil {
61+
return err
62+
}
63+
cli.FinishLoading()
64+
3065
return nil
3166
}
67+
68+
func replaceInFile(filename, old, new string) error {
69+
content, err := os.ReadFile(filename)
70+
if err != nil {
71+
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
72+
}
73+
content = []byte(strings.ReplaceAll(string(content), old, new))
74+
err = os.WriteFile(filename, content, 0o644)
75+
if err != nil {
76+
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
77+
}
78+
return nil
79+
}
80+
81+
func getOS() string {
82+
os := runtime.GOOS
83+
switch os {
84+
case "linux":
85+
return "linux"
86+
case "darwin":
87+
return "osx"
88+
case "windows":
89+
return "win"
90+
}
91+
return ""
92+
}
93+
94+
func getArch() string {
95+
os := runtime.GOARCH
96+
switch os {
97+
case "amd64":
98+
return "x64"
99+
case "386":
100+
return "x86"
101+
case "arm64":
102+
return "arm64"
103+
case "arm":
104+
return "arm"
105+
}
106+
return ""
107+
}

new_client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ func CreateNewClient(projectName string) error {
6767
cli.BeginLoading("Installing csharp-client...")
6868
installLibArgs := []string{"add", "package", "CodeGame.Client"}
6969
if data.LibraryVersion != "latest" {
70-
installLibArgs = append(installLibArgs, "--version")
71-
installLibArgs = append(installLibArgs, data.LibraryVersion)
70+
installLibArgs = append(installLibArgs, "--version", data.LibraryVersion)
7271
}
7372
_, err = exec.Execute(true, "dotnet", installLibArgs...)
7473
if err != nil {

0 commit comments

Comments
 (0)