-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
172 lines (155 loc) · 4.63 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Bananenpro/cli"
"github.com/code-game-project/go-utils/cgfile"
"github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/modules"
cp "github.com/otiai10/copy"
)
func Build() error {
config, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
data, err := modules.ReadCommandConfig[modules.BuildData]()
if err != nil {
return err
}
if data.Output == "" {
data.Output = "build"
}
typescript := data.Lang == "ts"
runtime, _ := config.LangConfig["runtime"].(string)
if runtime != "node" && runtime != "bundler" && (typescript || runtime != "browser") {
return fmt.Errorf("Invalid runtime: '%s'", runtime)
}
switch config.Type {
case "client":
return buildClient(config.Game, data.Output, config.URL, typescript, runtime)
case "server":
return buildServer()
default:
return fmt.Errorf("Unknown project type: %s", config.Type)
}
}
func buildClient(gameName, output, url string, typescript bool, runtime string) error {
yes, err := cli.YesNo(fmt.Sprintf("The '%s' directory will be completely overwritten. Continue?", output), false)
if err != nil || !yes {
return cli.ErrCanceled
}
os.RemoveAll(output)
err = os.MkdirAll(output, 0o755)
if err != nil {
return fmt.Errorf("Failed to create output directory: %w", err)
}
cli.BeginLoading("Building...")
if runtime == "node" {
if typescript {
_, err = exec.Execute(true, "npx", "tsc", "--outDir", output)
if err != nil {
return err
}
} else {
err = cp.Copy("src", output, cp.Options{
OnSymlink: func(src string) cp.SymlinkAction {
return cp.Deep
},
})
if err != nil {
return fmt.Errorf("Failed to copy source files to output directory: %s", err)
}
}
} else if runtime == "bundler" {
gameJSPath := filepath.Join("src", gameName, "game.js")
if typescript {
gameJSPath = filepath.Join("src", gameName, "game.ts")
}
err = replaceInFile(gameJSPath, "throw 'Query parameter \"game_url\" must be set.'", fmt.Sprintf("return '%s'", url))
if err != nil {
return err
}
_, err = exec.Execute(true, "npx", "parcel", "build", "--dist-dir", output, "src/index.html")
if err != nil {
return err
}
err = replaceInFile(gameJSPath, fmt.Sprintf("return '%s'", url), "throw 'Query parameter \"game_url\" must be set.'")
if err != nil {
return err
}
} else if runtime == "browser" {
dependencies, err := npmDependencies()
if err != nil {
return err
}
err = cp.Copy(".", output, cp.Options{
Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) {
return src == filepath.Clean(output) || (src != "node_modules" && strings.HasPrefix(src, "node_modules") && !containsAny(src, dependencies)) || src == ".codegame.json" || src == "package.json" || src == "package-lock.json", nil
},
})
if err != nil {
return fmt.Errorf("Failed to copy source files to output directory: %s", err)
}
}
if runtime != "bundler" {
gameJSPath := filepath.Join(output, gameName, "game.js")
if runtime == "node" {
err = replaceInFile(gameJSPath, "throw 'Environment variable `CG_GAME_URL` must be set.'", fmt.Sprintf("return '%s'", url))
} else {
err = replaceInFile(gameJSPath, "throw 'Query parameter \"game_url\" must be set.'", fmt.Sprintf("return '%s'", url))
}
}
if err != nil {
return err
}
cli.FinishLoading()
return nil
}
func npmDependencies() ([]string, error) {
type pkgJSON struct {
Dependencies map[string]string `json:"dependencies"`
}
file, err := os.Open("package.json")
if err != nil {
return nil, fmt.Errorf("failed to open package.json: %w", err)
}
defer file.Close()
var data pkgJSON
err = json.NewDecoder(file).Decode(&data)
if err != nil {
return nil, fmt.Errorf("failed to parse package.json: %w", err)
}
dependencies := make([]string, 0, len(data.Dependencies))
for dep := range data.Dependencies {
dependencies = append(dependencies, strings.Split(dep, "/")[0])
}
return dependencies, nil
}
func containsAny(s string, substrs []string) bool {
for _, sub := range substrs {
if strings.Contains(s, sub) {
return true
}
}
return false
}
func replaceInFile(filename, old, new string) error {
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
}
content = []byte(strings.ReplaceAll(string(content), old, new))
err = os.WriteFile(filename, content, 0o644)
if err != nil {
return fmt.Errorf("Failed to replace '%s' with '%s' in '%s': %s", old, new, filename, err)
}
return nil
}
func buildServer() error {
panic("not implemented")
return nil
}