@@ -3,10 +3,12 @@ package main
33import (
44 "fmt"
55 "os"
6+ "os/exec"
67 "path/filepath"
78 "runtime"
89 "strings"
910
11+ "github.com/Bananenpro/cli"
1012 "github.com/code-game-project/go-utils/cgfile"
1113 cgExec "github.com/code-game-project/go-utils/exec"
1214 "github.com/code-game-project/go-utils/modules"
@@ -22,19 +24,26 @@ func Build() error {
2224 if err != nil {
2325 return err
2426 }
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" )
2533
2634 switch config .Type {
2735 case "client" :
28- return buildClient (config .Game , data .Output , config .URL )
36+ return buildClient (config .Game , data .Output , config .URL , data . OS , data . Arch )
2937 case "server" :
30- return buildServer (data .Output )
38+ return buildServer (data .Output , data . OS , data . Arch )
3139 default :
3240 return fmt .Errorf ("Unknown project type: %s" , config .Type )
3341 }
3442}
3543
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 )
3847 if err != nil {
3948 return err
4049 }
@@ -46,21 +55,47 @@ func buildClient(gameName, output, url string) error {
4655
4756 cmdArgs := []string {"build" , "-o" , out , "-ldflags" , fmt .Sprintf ("-X %s/%s.URL=%s" , packageName , gamePackageName , url )}
4857
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
5172}
5273
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 )
5577 if err != nil {
5678 return err
5779 }
5880 cmdArgs := []string {"build" , "-o" , out }
5981 _ , 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
6196}
6297
63- func getOutputName (output string , isServer bool ) (string , error ) {
98+ func getOutputName (output string , isServer bool , operatingSystem string ) (string , error ) {
6499 absRoot , err := filepath .Abs ("." )
65100 if err != nil {
66101 return "" , err
@@ -72,7 +107,7 @@ func getOutputName(output string, isServer bool) (string, error) {
72107 }
73108 }
74109
75- if runtime .GOOS == "windows" && ! strings .HasSuffix (output , ".exe" ) {
110+ if (( operatingSystem == "" && runtime .GOOS == "windows" ) || operatingSystem == "windows" ) && ! strings .HasSuffix (output , ".exe" ) {
76111 output += ".exe"
77112 }
78113
0 commit comments