-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.go
More file actions
121 lines (103 loc) · 3.36 KB
/
build.go
File metadata and controls
121 lines (103 loc) · 3.36 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/Bananenpro/cli"
"github.com/code-game-project/go-utils/cgfile"
cgExec "github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/modules"
)
func Build() error {
config, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
data, err := modules.ReadCommandConfig[modules.BuildData]()
if err != nil {
return err
}
data.OS = strings.ReplaceAll(data.OS, "current", "")
data.OS = strings.ReplaceAll(data.OS, "macos", "darwin")
data.Arch = strings.ReplaceAll(data.Arch, "current", "")
data.Arch = strings.ReplaceAll(data.Arch, "x86", "386")
data.Arch = strings.ReplaceAll(data.Arch, "x64", "amd64")
data.Arch = strings.ReplaceAll(data.Arch, "arm32", "arm")
switch config.Type {
case "client":
return buildClient(config.Game, data.Output, config.URL, data.OS, data.Arch)
case "server":
return buildServer(data.Output, data.OS, data.Arch)
default:
return fmt.Errorf("Unknown project type: %s", config.Type)
}
}
func buildClient(gameName, output, url, operatingSystem, architecture string) error {
cli.BeginLoading("Building...")
out, err := getOutputName(output, false, operatingSystem)
if err != nil {
return err
}
packageName, err := GetGoModuleName("")
if err != nil {
return err
}
gamePackageName := strings.ReplaceAll(strings.ReplaceAll(gameName, "-", ""), "_", "")
cmdArgs := []string{"build", "-o", out, "-ldflags", fmt.Sprintf("-X %s/%s.URL=%s", packageName, gamePackageName, url)}
if _, err = exec.LookPath("go"); err != nil {
return fmt.Errorf("'go' ist not installed!")
}
cmd := exec.Command("go", cmdArgs...)
cmd.Env = append(os.Environ(), "GOOS="+operatingSystem, "GOARCH="+architecture)
buildOutput, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(buildOutput))
return fmt.Errorf("Failed to run 'GOOS=%s GOARCH=%s go %s'", operatingSystem, architecture, strings.Join(cmdArgs, " "))
}
cli.FinishLoading()
return nil
}
func buildServer(output, operatingSystem, architecture string) error {
cli.BeginLoading("Building...")
out, err := getOutputName(output, true, operatingSystem)
if err != nil {
return err
}
cmdArgs := []string{"build", "-o", out}
_, err = cgExec.Execute(false, "go", cmdArgs...)
if _, err = exec.LookPath("go"); err != nil {
return fmt.Errorf("'go' ist not installed!")
}
cmd := exec.Command("go", cmdArgs...)
cmd.Env = append(os.Environ(), "GOOS="+operatingSystem, "GOARCH="+architecture)
err = cmd.Run()
if err != nil {
return fmt.Errorf("Failed to run 'GOOS=%s GOARCH=%s go %s'", operatingSystem, architecture, strings.Join(cmdArgs, " "))
}
cli.FinishLoading()
return nil
}
func getOutputName(output string, isServer bool, operatingSystem string) (string, error) {
absRoot, err := filepath.Abs(".")
if err != nil {
return "", err
}
if output == "" {
output = filepath.Base(absRoot)
if isServer {
if stat, err := os.Stat(output); err == nil && stat.IsDir() {
output += "-server"
}
}
}
if ((operatingSystem == "" && runtime.GOOS == "windows") || operatingSystem == "windows") && !strings.HasSuffix(output, ".exe") {
output += ".exe"
}
if stat, err := os.Stat(output); err == nil && stat.IsDir() {
return "", fmt.Errorf("'%s' already exists and is a directory. Specify another output name with '-o <name>'.", output)
}
return output, nil
}