-
Notifications
You must be signed in to change notification settings - Fork 0
/
ˍdevmode.go
250 lines (230 loc) · 8.68 KB
/
ˍdevmode.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//go:build debug
package yo
import (
"bytes"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
yolog "yo/log"
yosrv "yo/srv"
. "yo/util"
"yo/util/sl"
"yo/util/str"
esbuild "github.com/evanw/esbuild/pkg/api"
)
var AppSideBuildTimeContainerFileNames []string
func init() {
buildDeployablyNow = doBuildAppDeployablyAndMaybePush
ts2jsInAppSideStaticDir = func() {
FsDirWalk(yosrv.StaticFilesDirName_App, func(fsPath string, fsEntry fs.DirEntry) {
if fsEntry.IsDir() {
return
}
switch {
case str.Ends(fsPath, ".js"):
ts_file_path := FsPathSwapExt(fsPath, ".js", ".ts")
if !FsIsFile(ts_file_path) {
println("rm " + fsPath)
FsDelFile(fsPath)
}
case str.Ends(fsPath, ".ts") && !(str.Ends(fsPath, ".d.ts") || str.Ends(fsPath, ".config.ts")):
js_file_path := FsPathSwapExt(fsPath, ".ts", ".js")
if FsIsNewerThan(fsPath, js_file_path) {
println("ts2js " + fsPath)
TsFile2JsFileViaEsbuild(fsPath)
}
}
})
// if desktop Wails app included, copy relevant __static asset files
if dst_dir := "guis/wails/frontend/src"; FsIsDir(dst_dir) && FsIsDir(yosrv.StaticFilesDirName_App) {
FsDirWalk(yosrv.StaticFilesDirName_App, func(fsPath string, fsEntry fs.DirEntry) {
dst_file := filepath.Join(dst_dir, filepath.Base(fsPath))
if (!(fsEntry.IsDir() || str.Ends(fsPath, ".ts") || str.Ends(fsPath, ".json"))) &&
FsIsNewerThan(fsPath, dst_file) {
FsCopy(fsPath, dst_file)
}
})
}
}
}
func doBuildAppDeployablyAndMaybePush() {
app_name := filepath.Base(FsDirPathCur())
dst_dir_path := filepath.Join(FsDirPathHome(), "rwa", "src-"+app_name)
deploy_dir_path := filepath.Join(FsDirPathHome(), "rwa", "deploy-"+app_name)
yolog.Println("BUILD: misc...")
FsDelDir(dst_dir_path)
FsDirEnsure(dst_dir_path)
FsDirEnsure(deploy_dir_path)
// 1. clear deploy_dir_path except for .git
dotgit_dir_path := filepath.Join(deploy_dir_path, ".git")
FsDirWalk(deploy_dir_path, func(fsPath string, fsEntry fs.DirEntry) {
if str.Begins(fsPath, dotgit_dir_path) {
return
}
if !FsIsDir(fsPath) {
FsDelFile(fsPath)
} else {
panic("TODO since apparently this unexpected-by-design need for dirs here arose: replace this panic with just `DelDir(fsPath)`")
}
})
if via_docker_file := true; via_docker_file { // 2. touch own Dockerfile, preferred choice
FsWrite(filepath.Join(deploy_dir_path, "Dockerfile"), []byte(str.Trim(`
FROM scratch
COPY .env /.env
COPY .env.prod /.env.prod
COPY `+app_name+`.exec /`+app_name+`.exec
`+str.Join(sl.As(AppSideBuildTimeContainerFileNames, func(s string) string { return "COPY " + s + " /" + s }), "\n")+`
COPY --from=alpine:latest /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENTRYPOINT ["/`+app_name+`.exec"]
`)))
} else { // 2. touch railway.toml, the alternative way (implies the Railway.app-default Dockerfile (a full distro))
FsWrite(filepath.Join(deploy_dir_path, "railway.toml"), []byte(str.Trim(`
# note, unused if Dockerfile present too
[build]
builder = "nixpacks"
buildCommand = "chmod +x ./`+app_name+`.exec"
watchPatterns = ["*"]
[deploy]
startCommand = "./`+app_name+`.exec"
restartPolicyType = "ALWAYS"
restartPolicyMaxRetries = 2
# healthcheckPath = "/"
# healthcheckTimeout = 543
`)))
}
// 3. touch go.work
FsWrite(filepath.Join(dst_dir_path, "go.work"), []byte(str.Trim(`
go `+str.TrimPref(runtime.Version(), "go")+`
use ./yo
use ./`+app_name+`
`)))
// 4. copy .go and .env files
for src_dir_path, is_app := range map[string]bool{
".": true,
yosrv.YoDirPath: false,
} {
strip := If(is_app, "", yosrv.YoDirPath)
FsDirWalk(src_dir_path, func(fsPath string, fsEntry fs.DirEntry) {
if str.Ends(fsPath, ".go") || str.Ends(fsPath, "go.mod") {
path_equiv := fsPath[len(strip):]
dst_file_path := filepath.Join(dst_dir_path, If(is_app, app_name, "yo"), path_equiv)
FsDirEnsure(filepath.Dir(dst_file_path))
FsCopy(fsPath, dst_file_path)
} else if is_app && (str.Ends(fsPath, ".env") || str.Ends(fsPath, ".env.prod")) {
FsCopy(fsPath, filepath.Join(deploy_dir_path, fsEntry.Name()))
} else if is_app {
for _, file_name := range AppSideBuildTimeContainerFileNames {
if str.Ends(fsPath, filepath.Join(src_dir_path, file_name)) {
FsCopy(fsPath, filepath.Join(deploy_dir_path, fsEntry.Name()))
}
}
}
})
}
// 5. ensure static files
for src_dir_path, is_app := range map[string]bool{
yosrv.YoStaticDirPath: false,
yosrv.StaticFilesDirName_App: true,
} {
strip := If(is_app, "", yosrv.YoDirPath)
// copy static files other than .ts / .js or sub dirs (all non-script files sit in top level, never in sub dirs)
FsDirWalk(src_dir_path, func(fsPath string, fsEntry fs.DirEntry) {
path_equiv := fsPath[len(strip):]
if (!fsEntry.IsDir()) && (!str.Ends(fsPath, ".js")) && (!str.Ends(fsPath, ".ts")) {
dst_file_path := filepath.Join(dst_dir_path, app_name, path_equiv)
FsDirEnsure(filepath.Dir(dst_file_path))
if !str.Ends(fsPath, ".css") {
FsCopy(fsPath, dst_file_path)
} else {
FsWrite(dst_file_path, cssMinify(FsRead(fsPath)))
}
}
})
// put the generated, un-minified `yo-sdk.ts` into the deployable at `/__yostatic/yo-sdk.ts` for importers' reference or imports
FsCopy(filepath.Join(yosrv.StaticFilesDirName_Yo, yosrv.YoSdkTsFileName), filepath.Join(dst_dir_path, app_name, yosrv.StaticFilesDirName_Yo, yosrv.YoSdkTsFileName))
// bundle+minify .js files
esbuild_options := esbuild.BuildOptions{
Color: esbuild.ColorNever,
Sourcemap: esbuild.SourceMapNone,
Target: esbuild.ESNext,
Platform: esbuild.PlatformBrowser,
Charset: esbuild.CharsetUTF8,
LegalComments: esbuild.LegalCommentsNone,
Format: esbuild.FormatESModule,
EntryPoints: If(is_app,
[]string{yosrv.StaticFilesDirName_App + "/" + app_name + ".js"},
[]string{yosrv.StaticFilesDirName_Yo + "/yo.js", yosrv.StaticFilesDirName_Yo + "/" + yosrv.YoSdkJsFileName}),
Bundle: is_app,
MinifyWhitespace: is_app,
MinifyIdentifiers: is_app,
MinifySyntax: is_app,
TreeShaking: If(is_app, esbuild.TreeShakingTrue, esbuild.TreeShakingFalse),
Outdir: filepath.Join(dst_dir_path, app_name, If(is_app, yosrv.StaticFilesDirName_App, yosrv.StaticFilesDirName_Yo)),
Write: true,
}
result := esbuild.Build(esbuild_options)
for _, msg := range result.Warnings {
panic("esbuild WARNs: " + str.GoLike(msg))
}
for _, msg := range result.Errors {
panic("esbuild ERRs: " + msg.Text + " @ " + str.GoLike(msg.Location))
}
}
// 6. go build
yolog.Println("BUILD: go build...")
cmd_go := exec.Command("go", "build",
"-C", dst_dir_path,
"-o", filepath.Join(deploy_dir_path, app_name+".exec"),
"-buildvcs=false",
"-a",
"-installsuffix", "deploy_"+app_name,
"-ldflags", "-w -s -extldflags \"-static\"",
"-tags", "timetzdata",
"./"+app_name)
cmd_go.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH=amd64")
cmd_out, err := cmd_go.CombinedOutput()
if err != nil {
panic(str.Fmt("%s>>>>%s", err, cmd_out))
}
if os.Getenv("YO_PUSH") != "" {
// 7. git push
yolog.Println("BUILD: push...")
msg_commit := time.Now().Format(time.DateTime)
cmd_git1, cmd_git2, cmd_git3 := exec.Command("git", "add", "-A"), exec.Command("git", "commit", "-m", msg_commit), exec.Command("git", "push", "--force")
cmd_git1.Dir, cmd_git2.Dir, cmd_git3.Dir = deploy_dir_path, deploy_dir_path, deploy_dir_path
for _, cmd_git := range []*exec.Cmd{cmd_git1, cmd_git2, cmd_git3} {
cmd_out, err := cmd_git.CombinedOutput()
if err != nil {
println(str.Fmt("%s>>>>%s", err, cmd_out))
}
}
}
}
func cssMinify(srcCss []byte) []byte {
is_ascii_nonspace_whitespace, is_sep, is_brace_or_paren := func(c byte) bool { return (c == '\n') || (c == '\t') }, func(c byte) bool { return (c == ':') || (c == ';') || (c == ',') }, func(c byte) bool {
return (c == '{') || (c == '}') || (c == '[') || (c == ']') || (c == '(') // || (c == ')') // keep the closing-paren out, generates buggy css in situations like `var(--foo) calc(...)`
}
// ditch comments
for again, start, end := true, []byte("/*"), []byte("*/"); again; {
again = false
if idx2 := bytes.Index(srcCss, end); idx2 > 1 {
if idx1 := bytes.Index(srcCss[:idx2], start); idx1 >= 0 {
again = true
srcCss = append(srcCss[:idx1], srcCss[idx2+2:]...)
}
}
}
// ditch safe-to-ditch whitespace
for i := 0; i < len(srcCss); i++ {
if c := srcCss[i]; is_ascii_nonspace_whitespace(c) || ((c == ' ') &&
(((i > 0) && (srcCss[i-1] == ' ') || is_brace_or_paren(srcCss[i-1]) || is_sep(srcCss[i-1])) ||
((i < (len(srcCss)-1)) && (srcCss[i+1] == ' ') || is_brace_or_paren(srcCss[i+1]) || is_sep(srcCss[i+1])))) {
srcCss = append(srcCss[:i], srcCss[i+1:]...)
i--
}
}
return srcCss
}