Skip to content

Commit a359c8f

Browse files
committed
Implement 'build' command
1 parent a25e2e0 commit a359c8f

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

commands/build.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package commands
2+
3+
import (
4+
"os"
5+
6+
"github.com/Bananenpro/cli"
7+
"github.com/code-game-project/codegame-cli/util/cgfile"
8+
"github.com/code-game-project/codegame-cli/util/modules"
9+
)
10+
11+
func Build() error {
12+
rootRelative, err := cgfile.FindProjectRootRelative()
13+
if err != nil {
14+
return err
15+
}
16+
17+
data, err := cgfile.LoadCodeGameFile(rootRelative)
18+
if err != nil {
19+
return cli.Error("Failed to load .codegame.json")
20+
}
21+
22+
args := []string{"build"}
23+
if len(os.Args) > 2 {
24+
args = append(args, os.Args[2:]...)
25+
}
26+
27+
switch data.Lang {
28+
case "go":
29+
err = modules.Execute("go", "latest", "client", args...)
30+
default:
31+
return cli.Error("'build' is not supported for '%s'", data.Lang)
32+
}
33+
if err != nil {
34+
return err
35+
}
36+
return nil
37+
}

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func main() {
2525
fmt.Fprintln(os.Stderr, "The official CodeGame CLI.")
2626
fmt.Fprintln(os.Stderr, "\nCommands:")
2727
fmt.Fprintln(os.Stderr, "\tnew \tCreate a new project.")
28-
fmt.Fprintln(os.Stderr, "\trun \tRun a project.")
28+
fmt.Fprintln(os.Stderr, "\trun \tRun the current project.")
29+
fmt.Fprintln(os.Stderr, "\tbuild \tBuild the current project.")
2930
fmt.Fprintln(os.Stderr, "\tinfo \tDisplay some info about a game server.")
3031
fmt.Fprintln(os.Stderr, "\tdocs \tOpen the CodeGame documentation in a web browser.")
3132
fmt.Fprintln(os.Stderr, "\nAbout: https://code-game.org")
@@ -52,6 +53,8 @@ func main() {
5253
err = commands.Docs()
5354
case "run":
5455
err = commands.Run()
56+
case "build":
57+
err = commands.Build()
5558
default:
5659
cli.Error("Unknown command: %s", strings.ToLower(pflag.Arg(0)))
5760
pflag.Usage()

util/modules/modules.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"os"
8+
"os/exec"
79
"path/filepath"
810
"strings"
911

1012
"github.com/Bananenpro/cli"
1113
"github.com/adrg/xdg"
12-
"github.com/code-game-project/codegame-cli/util/exec"
1314
"github.com/code-game-project/codegame-cli/util/external"
1415
"github.com/code-game-project/codegame-cli/util/semver"
1516
)
@@ -27,8 +28,16 @@ func Execute(name, libraryVersion, projectType string, args ...string) error {
2728
return cli.Error(err.Error())
2829
}
2930

30-
_, err = exec.Execute(false, filepath.Join(modulesPath, name, exeName), args...)
31-
return err
31+
programName := filepath.Join(modulesPath, name, exeName)
32+
if _, err := exec.LookPath(programName); err != nil {
33+
cli.Error("'%s' ist not installed!", programName)
34+
return err
35+
}
36+
cmd := exec.Command(programName, args...)
37+
cmd.Stdin = os.Stdin
38+
cmd.Stdout = os.Stdout
39+
cmd.Stderr = os.Stderr
40+
return cmd.Run()
3241
}
3342

3443
func findModuleVersion(name, libraryVersion, projectType string) (string, error) {

0 commit comments

Comments
 (0)