Skip to content

Instantly share code, notes, and snippets.

@larpon
Created October 10, 2023 13:11
Show Gist options
  • Save larpon/110dbfb13a6a795de387e429e81fc039 to your computer and use it in GitHub Desktop.
Save larpon/110dbfb13a6a795de387e429e81fc039 to your computer and use it in GitHub Desktop.
Embedding the contents of a directory with a V script
mut v_code := '
module main
import v.embed_file
fn get_embedded_files() []embed_file.EmbedFileData {
mut embedded_files := []embed_file.EmbedFileData{}
'
output_file := 'auto_generated_embedded_files.v'
for entry in ls('.') or { [] } {
// To make the script somewhat idempotent we filter out non-files, the executable
// of this script and optinally the generated output file `auto_generated_embedded_files.v`
if !is_file(entry) || entry.ends_with(base(@FILE).all_before('.v'))
|| entry.ends_with(output_file) {
continue
}
v_code += "\tembedded_files << \$embed_file('${entry}')\n"
}
v_code += '\treturn embedded_files
}'
write_file(output_file, v_code) or { panic(err) }
module main
fn main() {
// Call the function generated with `gen_embeds_from_dir.vsh`
embedded_files := get_embedded_files()
for embedded_file in embedded_files {
println('Embedded file from: "${embedded_file.path}"')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment