Skip to content

Commit b6c1d0f

Browse files
committed
Implement 'build' command
1 parent 3d0ffe1 commit b6c1d0f

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

build/build.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package build
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/Bananenpro/cli"
11+
cgExec "github.com/code-game-project/codegame-cli/util/exec"
12+
)
13+
14+
func BuildClient(projectRoot, gameName, output, url string) error {
15+
out, err := getOutputName(projectRoot, output)
16+
if err != nil {
17+
return err
18+
}
19+
packageName, err := getPackageName(projectRoot)
20+
if err != nil {
21+
return err
22+
}
23+
gamePackageName := strings.ReplaceAll(strings.ReplaceAll(gameName, "-", ""), "_", "")
24+
25+
cmdArgs := []string{"build", "-o", out, "-ldflags", fmt.Sprintf("-X %s/%s.URL=%s", packageName, gamePackageName, url), filepath.Join(projectRoot, "main.go")}
26+
27+
_, err = cgExec.Execute(false, "go", cmdArgs...)
28+
return err
29+
}
30+
31+
func BuildServer(projectRoot, output string) error {
32+
out, err := getOutputName(projectRoot, output)
33+
if err != nil {
34+
return err
35+
}
36+
cmdArgs := []string{"build", "-o", out, filepath.Join(projectRoot, "main.go")}
37+
_, err = cgExec.Execute(false, "go", cmdArgs...)
38+
return err
39+
}
40+
41+
func getOutputName(projectRoot, output string) (string, error) {
42+
absRoot, err := filepath.Abs(projectRoot)
43+
if err != nil {
44+
return "", err
45+
}
46+
if output == "" {
47+
output = filepath.Base(absRoot)
48+
}
49+
50+
if stat, err := os.Stat(output); err == nil && stat.IsDir() {
51+
return "", cli.Error("'%s' already exists and is a directory. Specify another output name with '-o <name>'.", output)
52+
}
53+
54+
return output, nil
55+
}
56+
57+
func getPackageName(projectRoot string) (string, error) {
58+
path := filepath.Join(projectRoot, "go.mod")
59+
file, err := os.Open(path)
60+
if err != nil {
61+
return "", cli.Error("Failed to open '%s'", path)
62+
}
63+
64+
scanner := bufio.NewScanner(file)
65+
for scanner.Scan() {
66+
if strings.HasPrefix(scanner.Text(), "module ") {
67+
return strings.TrimSuffix(strings.TrimPrefix(scanner.Text(), "module "), "/"), nil
68+
}
69+
}
70+
if scanner.Err() != nil {
71+
cli.Error("Failed to read '%s': %s", path, scanner.Err())
72+
}
73+
return "", cli.Error("Missing 'module' statement in '%s'", path)
74+
}

main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/Bananenpro/cli"
1010
"github.com/Bananenpro/pflag"
11+
"github.com/code-game-project/codegame-cli-go/build"
1112
"github.com/code-game-project/codegame-cli-go/new/client"
1213
"github.com/code-game-project/codegame-cli-go/new/server"
1314
"github.com/code-game-project/codegame-cli-go/run"
@@ -20,6 +21,7 @@ func main() {
2021
fmt.Fprintln(os.Stderr, "\nCommands:")
2122
fmt.Fprintln(os.Stderr, "\tnew \tCreate a new project.")
2223
fmt.Fprintln(os.Stderr, "\trun \tRun the current project.")
24+
fmt.Fprintln(os.Stderr, "\tbuild \tBuild the current project.")
2325
fmt.Fprintln(os.Stderr, "\nOptions:")
2426
pflag.PrintDefaults()
2527
fmt.Fprintln(os.Stderr, "This program expects to be executed inside of the project directory.")
@@ -43,6 +45,8 @@ func main() {
4345
err = newProject(projectName)
4446
case "run":
4547
err = runProject()
48+
case "build":
49+
err = buildProject()
4650
default:
4751
err = cli.Error("Unknown command: %s\n", command)
4852
}
@@ -135,3 +139,38 @@ func runProject() error {
135139

136140
return err
137141
}
142+
143+
func buildProject() error {
144+
flagSet := pflag.NewFlagSet("build", pflag.ExitOnError)
145+
146+
var output string
147+
flagSet.StringVarP(&output, "output", "o", "", "The name of the output file.")
148+
149+
flagSet.Usage = func() {
150+
fmt.Fprintf(os.Stderr, "Usage: %s build [...]\n", os.Args[0])
151+
fmt.Fprintln(os.Stderr, "\nOptions:")
152+
flagSet.PrintDefaults()
153+
}
154+
flagSet.Parse(os.Args[2:])
155+
156+
projectRoot, err := cgfile.FindProjectRootRelative()
157+
if err != nil {
158+
return err
159+
}
160+
161+
data, err := cgfile.LoadCodeGameFile(projectRoot)
162+
if err != nil {
163+
return err
164+
}
165+
166+
switch data.Type {
167+
case "client":
168+
err = build.BuildClient(projectRoot, data.Game, output, data.URL)
169+
case "server":
170+
err = build.BuildServer(projectRoot, output)
171+
default:
172+
err = cli.Error("Unknown project type: %s\n", data.Type)
173+
}
174+
175+
return err
176+
}

0 commit comments

Comments
 (0)