From 60dfc5f02db3c65b7699079f149f30e54adf577c Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 13 Apr 2025 05:44:35 +0300 Subject: [PATCH] Add `bare_map`, `make_const_pub`, new test --- README.md | 32 +++++++++++++++++++++++++ src/embedfs.v | 45 +++++++++++++++++++++++------------ tests/mymod/main.v | 9 +++++-- tests/mymod_test.v | 20 ++++++++++++++-- tests/mymod_test_bare_map.out | 10 ++++++++ v.mod | 2 +- 6 files changed, 98 insertions(+), 20 deletions(-) create mode 100644 tests/mymod_test_bare_map.out diff --git a/README.md b/README.md index 689aa20..2d9d519 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,38 @@ module main fn main() { 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()) } ``` + +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' + } +}} +``` diff --git a/src/embedfs.v b/src/embedfs.v index 252397d..8e15267 100644 --- a/src/embedfs.v +++ b/src/embedfs.v @@ -20,8 +20,12 @@ pub: module_name string = 'main' // Name of constant which will contain embedded files 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 + // Generate map[string]EmbedFile instead of EmbedFileSystem instance + bare_map bool } struct EmbedFileSpec { @@ -34,6 +38,7 @@ struct EmbedFileSpec { pub fn (g CodeGenerator) generate() string { 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) b.writeln('// !WARNING! This file is generated by embedfs module, do not edit it.') b.writeln('') @@ -57,21 +62,17 @@ pub fn (g CodeGenerator) generate() string { b.writeln('\tfiles map[string]EmbedFile') b.writeln('}') b.writeln('') - b.writeln('${visible}const ${g.const_name} = EmbedFileSystem{') - b.writeln('\tfiles: {') - 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},') + 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: {') + g.write_embed_file_map_item(mut b) + b.writeln('\t}') + b.writeln('}') } - b.writeln('\t}') - b.writeln('}') return b.str() } @@ -105,3 +106,17 @@ fn (g CodeGenerator) get_files() []EmbedFileSpec { } 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},') + } +} diff --git a/tests/mymod/main.v b/tests/mymod/main.v index 3757c61..8f5867a 100644 --- a/tests/mymod/main.v +++ b/tests/mymod/main.v @@ -2,6 +2,11 @@ module main fn main() { println(embedfs) - json_file := embedfs.files['assets/example.json'] or { EmbedFile{} } - println(json_file.data.to_string().trim_space()) + $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{} } + println(json_file.data.to_string().trim_space()) + } } diff --git a/tests/mymod_test.v b/tests/mymod_test.v index b66ae1a..0dbbd38 100644 --- a/tests/mymod_test.v +++ b/tests/mymod_test.v @@ -5,14 +5,30 @@ import v.util.diff import embedfs fn test_mymod() { + oldpwd := os.getwd() expected_out := os.read_file('tests/mymod_test.out')! os.chdir('tests/mymod')! gen := embedfs.CodeGenerator{ - path: 'assets' - make_pub: false + path: 'assets' } os.write_file('assets_generated.v', gen.generate())! ret := os.execute('${os.quoted_path(@VEXE)} run .') dump(diff.compare_text(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)! } diff --git a/tests/mymod_test_bare_map.out b/tests/mymod_test_bare_map.out new file mode 100644 index 0000000..565c6c2 --- /dev/null +++ b/tests/mymod_test_bare_map.out @@ -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"} diff --git a/v.mod b/v.mod index 1c84f64..334d7dd 100644 --- a/v.mod +++ b/v.mod @@ -1,7 +1,7 @@ Module { name: 'embedfs' description: 'Code generator for embedding directories with files into executables' - version: '0.0.1' + version: '0.0.2' license: 'Unlicense' dependencies: [] }