Skip to content

Commit 463b223

Browse files
committed
Implement build command
1 parent bca12c7 commit 463b223

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

build.go

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package main
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
58

9+
"github.com/Bananenpro/cli"
610
"github.com/code-game-project/go-utils/cgfile"
11+
"github.com/code-game-project/go-utils/exec"
712
"github.com/code-game-project/go-utils/modules"
13+
cp "github.com/otiai10/copy"
814
)
915

1016
func Build() error {
11-
panic("not implemented")
1217
config, err := cgfile.LoadCodeGameFile("")
1318
if err != nil {
1419
return err
@@ -18,21 +23,115 @@ func Build() error {
1823
if err != nil {
1924
return err
2025
}
26+
if data.Output == "" {
27+
data.Output = "build"
28+
}
29+
30+
typescript := data.Lang == "ts"
31+
32+
runtime, _ := config.LangConfig["runtime"].(string)
33+
if runtime != "node" && runtime != "bundler" && (typescript || runtime != "browser") {
34+
return fmt.Errorf("Invalid runtime: '%s'", runtime)
35+
}
2136

2237
switch config.Type {
2338
case "client":
24-
return buildClient(config.Game, data.Output, config.URL)
39+
return buildClient(config.Game, data.Output, config.URL, typescript, runtime)
2540
case "server":
26-
return buildServer(data.Output)
41+
return buildServer()
2742
default:
2843
return fmt.Errorf("Unknown project type: %s", config.Type)
2944
}
3045
}
3146

32-
func buildClient(gameName, output, url string) error {
47+
func buildClient(gameName, output, url string, typescript bool, runtime string) error {
48+
yes, err := cli.YesNo(fmt.Sprintf("The '%s' directory will be completely overridden. Continue?", output), false)
49+
if err != nil || !yes {
50+
return cli.ErrCanceled
51+
}
52+
os.RemoveAll(output)
53+
err = os.MkdirAll(output, 0o755)
54+
if err != nil {
55+
return fmt.Errorf("Failed to create output directory: %w", err)
56+
}
57+
58+
cli.BeginLoading("Building...")
59+
60+
if runtime == "node" {
61+
if typescript {
62+
_, err = exec.Execute(true, "npx", "tsc", "--outDir", output)
63+
if err != nil {
64+
return err
65+
}
66+
} else {
67+
err = cp.Copy("src", output, cp.Options{
68+
OnSymlink: func(src string) cp.SymlinkAction {
69+
return cp.Deep
70+
},
71+
})
72+
if err != nil {
73+
return fmt.Errorf("Failed to copy source files to output directory: %s", err)
74+
}
75+
}
76+
} else if runtime == "bundler" {
77+
gameJSPath := filepath.Join("src", gameName, "game.js")
78+
if typescript {
79+
gameJSPath = filepath.Join("src", gameName, "game.ts")
80+
}
81+
err = replaceInFile(gameJSPath, "throw 'Query parameter \"game_url\" must be set.'", fmt.Sprintf("return '%s'", url))
82+
if err != nil {
83+
return err
84+
}
85+
86+
_, err = exec.Execute(true, "npx", "parcel", "build", "--dist-dir", output, "src/index.html")
87+
if err != nil {
88+
return err
89+
}
90+
91+
err = replaceInFile(gameJSPath, fmt.Sprintf("return '%s'", url), "throw 'Query parameter \"game_url\" must be set.'")
92+
if err != nil {
93+
return err
94+
}
95+
} else if runtime == "browser" {
96+
err = cp.Copy(".", output, cp.Options{
97+
Skip: func(src string) (bool, error) {
98+
return src == "build" || (src != "node_modules" && strings.HasPrefix(src, "node_modules") && !strings.Contains(src, "@code-game-project")) || src == ".codegame.json" || src == "package.json" || src == "package-lock.json", nil
99+
},
100+
})
101+
if err != nil {
102+
return fmt.Errorf("Failed to copy source files to output directory: %s", err)
103+
}
104+
}
105+
106+
if runtime != "bundler" {
107+
gameJSPath := filepath.Join(output, gameName, "game.js")
108+
if runtime == "node" {
109+
err = replaceInFile(gameJSPath, "throw 'Environment variable `CG_GAME_URL` must be set.'", fmt.Sprintf("return '%s'", url))
110+
} else {
111+
err = replaceInFile(gameJSPath, "throw 'Query parameter \"game_url\" must be set.'", fmt.Sprintf("return '%s'", url))
112+
}
113+
}
114+
if err != nil {
115+
return err
116+
}
117+
cli.FinishLoading()
118+
return nil
119+
}
120+
121+
func replaceInFile(filename, old, new string) error {
122+
content, err := os.ReadFile(filename)
123+
if err != nil {
124+
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
125+
}
126+
content = []byte(strings.ReplaceAll(string(content), old, new))
127+
err = os.WriteFile(filename, content, 0o644)
128+
if err != nil {
129+
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
130+
}
33131
return nil
34132
}
35133

36-
func buildServer(output string) error {
134+
func buildServer() error {
135+
panic("not implemented")
37136
return nil
38137
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/mattn/go-colorable v0.1.12 // indirect
1616
github.com/mattn/go-isatty v0.0.14 // indirect
1717
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
18+
github.com/otiai10/copy v1.7.0 // indirect
1819
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
1920
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
2021
golang.org/x/text v0.3.7 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
2626
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
2727
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
2828
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
29+
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
30+
github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
31+
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
32+
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
33+
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
34+
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
2935
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3036
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3137
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

0 commit comments

Comments
 (0)