-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (93 loc) · 2.43 KB
/
main.go
File metadata and controls
112 lines (93 loc) · 2.43 KB
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
package main
/*
struct Result {
int success;
char* response;
char* contentHash;
};
struct ResolveResult {
int success;
char* urlPath;
char* absPath;
};
struct CompileResult {
int success;
char* messages;
};
*/
import "C"
import (
"joelmoss/proscenium/internal/builder"
"joelmoss/proscenium/internal/resolver"
"joelmoss/proscenium/internal/types"
)
// Cache the last config JSON to skip unmarshalling when unchanged.
var lastConfigJSON string
func unmarshalConfigIfChanged(configJson *C.char) error {
json := C.GoString(configJson)
if json == lastConfigJSON {
return nil
}
err := types.UnmarshalConfig([]byte(json))
if err != nil {
return err
}
lastConfigJSON = json
return nil
}
//export reset_config
func reset_config() {
types.Config.Reset()
lastConfigJSON = ""
}
// Build the given `path` using the `config`.
//
// - path - The path to build relative to `root`.
// - config
//
//export build_to_string
func build_to_string(filePath *C.char, configJson *C.char) C.struct_Result {
err := unmarshalConfigIfChanged(configJson)
if err != nil {
return C.struct_Result{C.int(0), C.CString(err.Error()), C.CString("")}
}
success, result, contentHash := builder.BuildToString(C.GoString(filePath))
if success {
return C.struct_Result{C.int(1), C.CString(result), C.CString(contentHash)}
}
return C.struct_Result{C.int(0), C.CString(result), C.CString("")}
}
// Resolve the given `path` relative to the `root`.
//
// - path - The path to build relative to `root`.
// - config
//
//export resolve
func resolve(filePath *C.char, configJson *C.char) C.struct_ResolveResult {
err := unmarshalConfigIfChanged(configJson)
if err != nil {
return C.struct_ResolveResult{C.int(0), C.CString(err.Error()), C.CString("")}
}
urlPath, absPath, err := resolver.Resolve(C.GoString(filePath), "")
if err != nil {
return C.struct_ResolveResult{C.int(0), C.CString(string(err.Error())), C.CString("")}
}
return C.struct_ResolveResult{C.int(1), C.CString(urlPath), C.CString(absPath)}
}
// Compile assets using the given `config`.
//
// - config
//
//export compile
func compile(configJson *C.char) C.struct_CompileResult {
err := unmarshalConfigIfChanged(configJson)
if err != nil {
return C.struct_CompileResult{C.int(0), C.CString("")}
}
success, messages := builder.Compile()
if success {
return C.struct_CompileResult{C.int(1), C.CString(messages)}
}
return C.struct_CompileResult{C.int(0), C.CString(messages)}
}
func main() {}