@@ -3,10 +3,12 @@ package main
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "os/exec"
6
7
"path/filepath"
7
8
"runtime"
8
9
"strings"
9
10
11
+ "github.com/Bananenpro/cli"
10
12
"github.com/code-game-project/go-utils/cgfile"
11
13
cgExec "github.com/code-game-project/go-utils/exec"
12
14
"github.com/code-game-project/go-utils/modules"
@@ -22,19 +24,26 @@ func Build() error {
22
24
if err != nil {
23
25
return err
24
26
}
27
+ data .OS = strings .ReplaceAll (data .OS , "current" , "" )
28
+ data .OS = strings .ReplaceAll (data .OS , "macos" , "darwin" )
29
+ data .Arch = strings .ReplaceAll (data .Arch , "current" , "" )
30
+ data .Arch = strings .ReplaceAll (data .Arch , "x86" , "386" )
31
+ data .Arch = strings .ReplaceAll (data .Arch , "x64" , "amd64" )
32
+ data .Arch = strings .ReplaceAll (data .Arch , "arm32" , "arm" )
25
33
26
34
switch config .Type {
27
35
case "client" :
28
- return buildClient (config .Game , data .Output , config .URL )
36
+ return buildClient (config .Game , data .Output , config .URL , data . OS , data . Arch )
29
37
case "server" :
30
- return buildServer (data .Output )
38
+ return buildServer (data .Output , data . OS , data . Arch )
31
39
default :
32
40
return fmt .Errorf ("Unknown project type: %s" , config .Type )
33
41
}
34
42
}
35
43
36
- func buildClient (gameName , output , url string ) error {
37
- out , err := getOutputName (output , false )
44
+ func buildClient (gameName , output , url , operatingSystem , architecture string ) error {
45
+ cli .BeginLoading ("Building..." )
46
+ out , err := getOutputName (output , false , operatingSystem )
38
47
if err != nil {
39
48
return err
40
49
}
@@ -46,21 +55,47 @@ func buildClient(gameName, output, url string) error {
46
55
47
56
cmdArgs := []string {"build" , "-o" , out , "-ldflags" , fmt .Sprintf ("-X %s/%s.URL=%s" , packageName , gamePackageName , url )}
48
57
49
- _ , err = cgExec .Execute (false , "go" , cmdArgs ... )
50
- return err
58
+ if _ , err = exec .LookPath ("go" ); err != nil {
59
+ return fmt .Errorf ("'go' ist not installed!" )
60
+ }
61
+
62
+ cmd := exec .Command ("go" , cmdArgs ... )
63
+ cmd .Env = append (os .Environ (), "GOOS=" + operatingSystem , "GOARCH=" + architecture )
64
+
65
+ buildOutput , err := cmd .CombinedOutput ()
66
+ if err != nil {
67
+ fmt .Println (string (buildOutput ))
68
+ return fmt .Errorf ("Failed to run 'GOOS=%s GOARCH=%s go %s'" , operatingSystem , architecture , strings .Join (cmdArgs , " " ))
69
+ }
70
+ cli .FinishLoading ()
71
+ return nil
51
72
}
52
73
53
- func buildServer (output string ) error {
54
- out , err := getOutputName (output , true )
74
+ func buildServer (output , operatingSystem , architecture string ) error {
75
+ cli .BeginLoading ("Building..." )
76
+ out , err := getOutputName (output , true , operatingSystem )
55
77
if err != nil {
56
78
return err
57
79
}
58
80
cmdArgs := []string {"build" , "-o" , out }
59
81
_ , err = cgExec .Execute (false , "go" , cmdArgs ... )
60
- return err
82
+
83
+ if _ , err = exec .LookPath ("go" ); err != nil {
84
+ return fmt .Errorf ("'go' ist not installed!" )
85
+ }
86
+
87
+ cmd := exec .Command ("go" , cmdArgs ... )
88
+ cmd .Env = append (os .Environ (), "GOOS=" + operatingSystem , "GOARCH=" + architecture )
89
+
90
+ err = cmd .Run ()
91
+ if err != nil {
92
+ return fmt .Errorf ("Failed to run 'GOOS=%s GOARCH=%s go %s'" , operatingSystem , architecture , strings .Join (cmdArgs , " " ))
93
+ }
94
+ cli .FinishLoading ()
95
+ return nil
61
96
}
62
97
63
- func getOutputName (output string , isServer bool ) (string , error ) {
98
+ func getOutputName (output string , isServer bool , operatingSystem string ) (string , error ) {
64
99
absRoot , err := filepath .Abs ("." )
65
100
if err != nil {
66
101
return "" , err
@@ -72,7 +107,7 @@ func getOutputName(output string, isServer bool) (string, error) {
72
107
}
73
108
}
74
109
75
- if runtime .GOOS == "windows" && ! strings .HasSuffix (output , ".exe" ) {
110
+ if (( operatingSystem == "" && runtime .GOOS == "windows" ) || operatingSystem == "windows" ) && ! strings .HasSuffix (output , ".exe" ) {
76
111
output += ".exe"
77
112
}
78
113
0 commit comments