embedfs #
Code generator for embedding directories with files into executables
The $embed_file
statement in V embeds only a single file. This module makes it easy to embed entire directories into the final executable.
File embedding in V is done at compile time, and unfortunately there is no way to dynamically embed arbitrary files into an application. The embedfs
module is a simple code generator that creates a separate .v
file with the code for embedding files. That is, the embedfs call must be made before the code is compiled. So embedfs is a build dependency.
v install --git https://github.com/gechandesu/embedfs
Usage
For example you have following file structure:
v.mod
src/
main.v
assets/
css/style.css
js/app.js
Lets embed the assets
directory.
Create embed_assets.vsh
next to your v.mod:
#!/usr/bin/env -S v
import embedfs
chdir('src')!
assets := embedfs.CodeGenerator{
path: 'assets'
}
write_file('assets_generated.v', assets.generate())!
Run it:
v run embed_assets.vsh
Now you have src/assets_generated.v
. Take a look inside it. So you can use embedfs
const in src/main.v
in this way:
module main
fn main() {
style := embedfs.files['assets/css/style.css']!
println(style.data.to_string())
}
struct CodeGenerator #
struct CodeGenerator {
pub:
// Path to file or directory to embed
path string
// Path prefix if you want to add extra prefix for file paths
prefix string
// Glob patterns to match files the must be ignored when generating the code
ignore_patterns []string
// If true set the default MIME-type for files if no MIME-type detected
force_mimetype bool
// Default MIME-type for files. See https://www.iana.org/assignments/media-types/media-types.xhtml
default_mimetype string = 'application/octet-stream'
// Name of generated module
module_name string = 'main'
// Name of constant which will contain embedded files
const_name string = 'embedfs'
// If true make symbols in generated module public
make_pub bool
}
fn (CodeGenerator) generate #
fn (g CodeGenerator) generate() string