-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbindata.v
60 lines (46 loc) · 1.18 KB
/
bindata.v
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
module very
import os
import strings
pub struct Asset {
pub mut:
files map[string]&File = map[string]&File{}
}
pub struct File {
data []u8
}
pub fn (mut asset Asset) find(name string) !&File {
return asset.files[name] or { return error('file `${name}` not exist!') }
}
pub fn (mut asset Asset) scan_dir(dir string) {
os.walk(dir, fn [mut asset, dir] (it string) {
asset.append(it, dir)
})
}
pub fn (mut asset Asset) append(it string, root string) {
if !it.contains('bindata') {
asset.files[it.replace(root, '')] = &File{
data: os.read_bytes(it) or { []u8{} }
}
}
}
pub fn (mut asset Asset) gen() ! {
mut buf := strings.new_builder(1024 * 1024 * 1024 * 5)
buf.write_string(r'module main
import xiusin.very
pub fn byte_file_data() &very.Asset {
mut asset := &very.Asset {}
')
for file, asset_ in asset.files {
mut str := asset_.data.str()
index := str.index(',') or { -1 }
if index > 1 {
substr := str.substr(1, index)
str = str.replace_once('[' + substr, '[u8(' + substr + ')')
}
buf.write_string(' asset.files["${file.trim_left('/')}"] = &very.File { data: ${str} }\n')
}
buf.write_string(r'
return asset
}')
os.write_file('bindata.v', buf.str())!
}