Add bare_map, make_const_pub, new test

This commit is contained in:
ge 2025-04-13 05:44:35 +03:00
parent 2568538cb3
commit 60dfc5f02d
6 changed files with 98 additions and 20 deletions

View File

@ -56,6 +56,38 @@ module main
fn main() { fn main() {
style := embedfs.files['assets/css/style.css']! style := embedfs.files['assets/css/style.css']!
// If `bare_map` parameter is set to `true` use:
// style := embedfs['assets/css/style.css']!
println(style.data.to_string()) println(style.data.to_string())
} }
``` ```
The generated `embedfs` const value example (from `tests/`):
```v okfmt
EmbedFileSystem{
files: {'assets/example.json': EmbedFile{
data: embed_file.EmbedFileData{ len: 22, path: "assets/example.json", apath: "", uncompressed: 846284 }
meta: EmbedFileMetadata{
key: 'assets/example.json'
name: 'example.json'
ext: 'json'
mimetype: 'application/json'
}
}}
}
```
The generated const value if `bare_map` parameter is `true`:
```v okfmt
{'assets/example.json': EmbedFile{
data: embed_file.EmbedFileData{ len: 22, path: "assets/example.json", apath: "", uncompressed: 845da4 }
meta: EmbedFileMetadata{
key: 'assets/example.json'
name: 'example.json'
ext: 'json'
mimetype: 'application/json'
}
}}
```

View File

@ -20,8 +20,12 @@ pub:
module_name string = 'main' module_name string = 'main'
// Name of constant which will contain embedded files // Name of constant which will contain embedded files
const_name string = 'embedfs' const_name string = 'embedfs'
// If true make symbols in generated module public // If true make constant public
make_const_pub bool
// If true make all symbols in generated module public
make_pub bool make_pub bool
// Generate map[string]EmbedFile instead of EmbedFileSystem instance
bare_map bool
} }
struct EmbedFileSpec { struct EmbedFileSpec {
@ -34,6 +38,7 @@ struct EmbedFileSpec {
pub fn (g CodeGenerator) generate() string { pub fn (g CodeGenerator) generate() string {
visible := if g.make_pub == true { 'pub ' } else { '' } visible := if g.make_pub == true { 'pub ' } else { '' }
const_visible := if g.make_pub == true || g.make_const_pub == true { 'pub ' } else { '' }
mut b := strings.new_builder(1024 * 4) mut b := strings.new_builder(1024 * 4)
b.writeln('// !WARNING! This file is generated by embedfs module, do not edit it.') b.writeln('// !WARNING! This file is generated by embedfs module, do not edit it.')
b.writeln('') b.writeln('')
@ -57,21 +62,17 @@ pub fn (g CodeGenerator) generate() string {
b.writeln('\tfiles map[string]EmbedFile') b.writeln('\tfiles map[string]EmbedFile')
b.writeln('}') b.writeln('}')
b.writeln('') b.writeln('')
b.writeln('${visible}const ${g.const_name} = EmbedFileSystem{') if g.bare_map {
b.writeln('${const_visible}const ${g.const_name} = {')
g.write_embed_file_map_item(mut b)
b.writeln('}')
} else {
b.writeln('${const_visible}const ${g.const_name} = EmbedFileSystem{')
b.writeln('\tfiles: {') b.writeln('\tfiles: {')
for filespec in g.get_files() { g.write_embed_file_map_item(mut b)
b.writeln("\t\t'${filespec.key}': EmbedFile{")
b.writeln("\t\t\tdata: \$embed_file('${filespec.path}')")
b.writeln('\t\t\tmeta: EmbedFileMetadata{')
b.writeln("\t\t\t\tkey: '${filespec.key}'")
b.writeln("\t\t\t\tname: '${filespec.name}'")
b.writeln("\t\t\t\text: '${filespec.ext}'")
b.writeln("\t\t\t\tmimetype: '${filespec.mimetype}'")
b.writeln('\t\t\t}')
b.writeln('\t\t},')
}
b.writeln('\t}') b.writeln('\t}')
b.writeln('}') b.writeln('}')
}
return b.str() return b.str()
} }
@ -105,3 +106,17 @@ fn (g CodeGenerator) get_files() []EmbedFileSpec {
} }
return files return files
} }
fn (g CodeGenerator) write_embed_file_map_item(mut b strings.Builder) {
for filespec in g.get_files() {
b.writeln("\t\t'${filespec.key}': EmbedFile{")
b.writeln("\t\t\tdata: \$embed_file('${filespec.path}')")
b.writeln('\t\t\tmeta: EmbedFileMetadata{')
b.writeln("\t\t\t\tkey: '${filespec.key}'")
b.writeln("\t\t\t\tname: '${filespec.name}'")
b.writeln("\t\t\t\text: '${filespec.ext}'")
b.writeln("\t\t\t\tmimetype: '${filespec.mimetype}'")
b.writeln('\t\t\t}')
b.writeln('\t\t},')
}
}

View File

@ -2,6 +2,11 @@ module main
fn main() { fn main() {
println(embedfs) println(embedfs)
$if bare_map ? {
json_file := embedfs['assets/example.json'] or { EmbedFile{} }
println(json_file.data.to_string().trim_space())
} $else {
json_file := embedfs.files['assets/example.json'] or { EmbedFile{} } json_file := embedfs.files['assets/example.json'] or { EmbedFile{} }
println(json_file.data.to_string().trim_space()) println(json_file.data.to_string().trim_space())
} }
}

View File

@ -5,14 +5,30 @@ import v.util.diff
import embedfs import embedfs
fn test_mymod() { fn test_mymod() {
oldpwd := os.getwd()
expected_out := os.read_file('tests/mymod_test.out')! expected_out := os.read_file('tests/mymod_test.out')!
os.chdir('tests/mymod')! os.chdir('tests/mymod')!
gen := embedfs.CodeGenerator{ gen := embedfs.CodeGenerator{
path: 'assets' path: 'assets'
make_pub: false
} }
os.write_file('assets_generated.v', gen.generate())! os.write_file('assets_generated.v', gen.generate())!
ret := os.execute('${os.quoted_path(@VEXE)} run .') ret := os.execute('${os.quoted_path(@VEXE)} run .')
dump(diff.compare_text(ret.output, expected_out)!) dump(diff.compare_text(ret.output, expected_out)!)
assert ret.output == expected_out assert ret.output == expected_out
os.chdir(oldpwd)!
}
fn test_mymod_bare_map() {
oldpwd := os.getwd()
expected_out := os.read_file('tests/mymod_test_bare_map.out')!
os.chdir('tests/mymod')!
gen := embedfs.CodeGenerator{
path: 'assets'
bare_map: true
}
os.write_file('assets_generated.v', gen.generate())!
ret := os.execute('${os.quoted_path(@VEXE)} -d bare_map run .')
dump(diff.compare_text(ret.output, expected_out)!)
assert ret.output == expected_out
os.chdir(oldpwd)!
} }

View File

@ -0,0 +1,10 @@
{'assets/example.json': EmbedFile{
data: embed_file.EmbedFileData{ len: 22, path: "assets/example.json", apath: "", uncompressed: 845da4 }
meta: EmbedFileMetadata{
key: 'assets/example.json'
name: 'example.json'
ext: 'json'
mimetype: 'application/json'
}
}}
{"some": "JSON data"}

2
v.mod
View File

@ -1,7 +1,7 @@
Module { Module {
name: 'embedfs' name: 'embedfs'
description: 'Code generator for embedding directories with files into executables' description: 'Code generator for embedding directories with files into executables'
version: '0.0.1' version: '0.0.2'
license: 'Unlicense' license: 'Unlicense'
dependencies: [] dependencies: []
} }