-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
161 lines (145 loc) · 4.13 KB
/
main.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
package main
/*
struct Result {
int success;
char* response;
};
*/
import "C"
import (
"encoding/json"
"joelmoss/proscenium/internal/builder"
"joelmoss/proscenium/internal/resolver"
"joelmoss/proscenium/internal/types"
)
// Build the given `path` in the `root`.
//
// BuildOptions
// - path - The path to build relative to `root`.
// - baseUrl - base URL of the Rails app. eg. https://example.com
// - importMap - Path to the import map relative to `root`.
// - envVars - JSON string of environment variables.
// Config:
// - root - The working directory.
// - env - The environment (1 = development, 2 = test, 3 = production)
// - codeSpitting?
// - debug?
//
//export build_to_string
func build_to_string(
filepath *C.char,
baseUrl *C.char,
importMap *C.char,
envVars *C.char,
appRoot *C.char,
gemPath *C.char,
env C.uint,
codeSplitting bool,
engines *C.char,
debug bool,
) C.struct_Result {
types.Config.RootPath = C.GoString(appRoot)
types.Config.GemPath = C.GoString(gemPath)
types.Config.Environment = types.Environment(env)
types.Config.CodeSplitting = codeSplitting
types.Config.Debug = debug
var enginesMap map[string]string
err := json.Unmarshal([]byte(C.GoString(engines)), &enginesMap)
if err == nil {
types.Config.Engines = enginesMap
} else {
return C.struct_Result{C.int(0), C.CString(err.Error())}
}
pathStr := C.GoString(filepath)
success, result := builder.BuildToString(builder.BuildOptions{
Path: pathStr,
BaseUrl: C.GoString(baseUrl),
ImportMapPath: C.GoString(importMap),
EnvVars: C.GoString(envVars),
})
if success {
return C.struct_Result{C.int(1), C.CString(result)}
} else {
return C.struct_Result{C.int(0), C.CString(result)}
}
}
// Build the given `path` in the `root`.
//
// BuildOptions
// - path - The path to build relative to `root`. Multiple paths can be given by separating them
// with a semi-colon.
// - baseUrl - base URL of the Rails app. eg. https://example.com
// - importMap - Path to the import map relative to `root`.
// - envVars - JSON string of environment variables.
// Config:
// - root - The working directory.
// - env - The environment (1 = development, 2 = test, 3 = production)
// - codeSpitting?
// - debug?
//
//export build_to_path
func build_to_path(
filepath *C.char,
baseUrl *C.char,
importMap *C.char,
envVars *C.char,
appRoot *C.char,
gemPath *C.char,
env C.uint,
codeSplitting bool,
engines *C.char,
debug bool,
) C.struct_Result {
types.Config.RootPath = C.GoString(appRoot)
types.Config.GemPath = C.GoString(gemPath)
types.Config.Environment = types.Environment(env)
types.Config.CodeSplitting = codeSplitting
types.Config.Debug = debug
var enginesMap map[string]string
err := json.Unmarshal([]byte(C.GoString(engines)), &enginesMap)
if err == nil {
types.Config.Engines = enginesMap
} else {
return C.struct_Result{C.int(0), C.CString(err.Error())}
}
pathStr := C.GoString(filepath)
success, result := builder.BuildToPath(builder.BuildOptions{
Path: pathStr,
BaseUrl: C.GoString(baseUrl),
ImportMapPath: C.GoString(importMap),
EnvVars: C.GoString(envVars),
})
if success {
return C.struct_Result{C.int(1), C.CString(result)}
} else {
return C.struct_Result{C.int(0), C.CString(result)}
}
}
// Resolve the given `path` relative to the `root`.
//
// ResolveOptions
// - path - The path to build relative to `root`.
// - importMap - Path to the import map relative to `root`.
// Config
// - root - The working directory.
// - env - The environment (1 = development, 2 = test, 3 = production)
// - debug?
//
//export resolve
func resolve(
path *C.char, importMap *C.char, appRoot *C.char, gemPath *C.char, env C.uint, debug bool,
) C.struct_Result {
types.Config.Environment = types.Environment(env)
types.Config.RootPath = C.GoString(appRoot)
types.Config.GemPath = C.GoString(gemPath)
types.Config.Debug = debug
resolvedPath, err := resolver.Resolve(resolver.Options{
Path: C.GoString(path),
ImportMapPath: C.GoString(importMap),
})
if err != nil {
return C.struct_Result{C.int(0), C.CString(string(err.Error()))}
}
return C.struct_Result{C.int(1), C.CString(resolvedPath)}
}
func main() {}