new tool: bin2v to convert arbitrary files to a .v static module file
parent
4a82770f16
commit
cb3ac33e18
|
@ -14,6 +14,7 @@ fns.txt
|
|||
/tools/vtest-compiler
|
||||
/tools/vtest-fmt
|
||||
/tools/vfmt
|
||||
/tools/vbin2v
|
||||
/tools/vup
|
||||
/tools/vpm
|
||||
/tools/vcreate
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import (
|
||||
os
|
||||
os.cmdline
|
||||
flag
|
||||
filepath
|
||||
scripting
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
module main
|
||||
|
||||
import os
|
||||
import flag
|
||||
import filepath
|
||||
|
||||
const (
|
||||
tool_version = '0.0.3'
|
||||
tool_description = 'Converts a list of arbitrary files into a single v module file.'
|
||||
)
|
||||
|
||||
struct Context {
|
||||
mut:
|
||||
files []string
|
||||
prefix string
|
||||
show_help bool
|
||||
module_name string
|
||||
}
|
||||
|
||||
fn (context Context) header() {
|
||||
println('module ${context.module_name}')
|
||||
println('')
|
||||
allfiles := context.files.join(' ')
|
||||
mut options := []string
|
||||
if context.prefix.len > 0 {
|
||||
options << '-p ${context.prefix}'
|
||||
}
|
||||
if context.module_name.len > 0 {
|
||||
options << '-m ${context.module_name}'
|
||||
}
|
||||
soptions := options.join(' ')
|
||||
println('// File generated by:')
|
||||
println('// v bin2v ${allfiles} ${soptions}')
|
||||
println('// Please, do not edit this file.')
|
||||
println('// Your changes may be overwritten.')
|
||||
println('')
|
||||
println('const (')
|
||||
}
|
||||
|
||||
fn (context Context) footer() {
|
||||
println(')')
|
||||
}
|
||||
|
||||
fn (context Context) file2v(file string) {
|
||||
fname := filepath.filename(file)
|
||||
fname_no_dots := fname.replace('.', '_')
|
||||
byte_name := '${context.prefix}${fname_no_dots}'
|
||||
fbytes := os.read_bytes(file) or {
|
||||
eprintln('Error: $err')
|
||||
return
|
||||
}
|
||||
fbyte := fbytes[0]
|
||||
println(' ${byte_name}_len = ${fbytes.len}')
|
||||
print(' ${byte_name} = [ byte(${fbyte}), \n ')
|
||||
for i := 1; i < fbytes.len; i++ {
|
||||
b := int(fbytes[i]).str()
|
||||
print('${b:4s}, ')
|
||||
if 0 == i % 16 {
|
||||
print('\n ')
|
||||
}
|
||||
}
|
||||
println('\n]!!')
|
||||
println('')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut context := Context{}
|
||||
mut fp := flag.new_flag_parser(os.args[1..])
|
||||
fp.application('v bin2v')
|
||||
fp.version(tool_version)
|
||||
fp.description(tool_description)
|
||||
fp.arguments_description('FILE [FILE]...')
|
||||
context.show_help = fp.bool_('help', `h`, false, 'Show this help screen.')
|
||||
context.module_name = fp.string_('module', `m`, 'binary', 'Name of the generated module.\n')
|
||||
context.prefix = fp.string_('prefix', `p`, '', 'A prefix put before each resource name.\n')
|
||||
if (context.show_help) {
|
||||
println(fp.usage())
|
||||
exit(0)
|
||||
}
|
||||
files := fp.finalize() or {
|
||||
eprintln('Error: ' + err)
|
||||
exit(1)
|
||||
}
|
||||
real_files := files.filter(it != 'bin2v')
|
||||
if real_files.len == 0 {
|
||||
println(fp.usage())
|
||||
exit(0)
|
||||
}
|
||||
context.files = real_files
|
||||
context.header()
|
||||
for file in real_files {
|
||||
context.file2v(file)
|
||||
}
|
||||
context.footer()
|
||||
}
|
Loading…
Reference in New Issue