-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
103 lines (82 loc) · 2.33 KB
/
compile.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
package server
import (
"fmt"
"os"
"path/filepath"
"time"
bash "github.com/tkdeng/gobash"
regex "github.com/tkdeng/goregex"
"github.com/tkdeng/goutil"
"github.com/tkdeng/staticweb"
)
func compile() {
os.MkdirAll(Config.Root, 0755)
os.MkdirAll(Config.Root+"/pages", 0755)
os.MkdirAll(Config.Root+"/theme", 0755)
os.MkdirAll(Config.Root+"/assets", 0755)
os.MkdirAll(Config.Root+"/db", 0755)
os.MkdirAll(Config.Root+"/wasm", 0755)
if Config.PublicURI != "" {
os.MkdirAll(Config.Root+"/public", 0755)
}
//todo: sandbox downloads directory
// os.MkdirAll(Config.Root+"/downloads", 2600)
PrintMsg("warn", "Compiling Server Pages...", 50, false)
os.RemoveAll(Config.Root + "/pages.dist")
if err := os.Mkdir(Config.Root+"/pages.dist", 0755); err != nil {
panic(err)
}
compTemplates()
PrintMsg("warn", "Compiling WASM Scripts...", 50, false)
os.RemoveAll(Config.Root + "/wasm.dist")
if err := os.Mkdir(Config.Root+"/wasm.dist", 0755); err != nil {
panic(err)
}
compWasm()
PrintMsg("confirm", "Compiled Server!", 50, true)
}
func compTemplates() {
staticweb.Live(Config.Root+"/pages", Config.Root+"/pages.dist")
}
func compWasm() {
dirList, err := os.ReadDir(Config.Root + "/wasm")
if err != nil {
fmt.Println(err)
return
}
for _, dir := range dirList {
if dir.IsDir() {
if path, err := goutil.JoinPath(Config.Root+"/wasm", dir.Name()); err == nil {
compileWasmFile(path, dir.Name())
}
}
}
lastUpdate := time.Now().UnixMilli()
fw := goutil.FileWatcher()
fw.OnAny = func(path, op string) {
if now := time.Now().UnixMilli(); now-lastUpdate > 1000 {
lastUpdate = now
path = string(regex.Comp(`^(%1[\\/][\w_-]+).*$`, Config.Root+"/wasm").RepStr([]byte(path), []byte("$1")))
compileWasmFile(path, filepath.Base(path))
}
}
fw.WatchDir(Config.Root + "/wasm")
}
func compileWasmFile(path string, fileName string) {
if buf, err := os.ReadFile(path + "/go.mod"); err == nil {
//* Compile Go WASM
regex.Comp(`(?m)^module\s+(.+)$`).RepFunc(buf, func(data func(int) []byte) []byte {
fileName = string(data(1))
return nil
})
out, err := goutil.JoinPath(Config.Root+"/wasm.dist", fileName+".wasm")
if err != nil {
fmt.Println(err)
return
}
_, err = bash.Run([]string{`go`, `build`, `-o`, out}, path, []string{"GOOS=js", "GOARCH=wasm"})
if err != nil {
fmt.Println(err)
}
}
}