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

@ -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},')
}
}