From cb3ac33e1824682ac5a1c6322ea3449e822c4563 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 13 Jan 2020 18:54:18 +0200 Subject: [PATCH] new tool: bin2v to convert arbitrary files to a .v static module file --- .gitignore | 1 + tools/oldv.v | 1 + tools/vbin2v.v | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ v.v | 1 + 4 files changed, 98 insertions(+) create mode 100644 tools/vbin2v.v diff --git a/.gitignore b/.gitignore index c9ae96556d..19753860b0 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ fns.txt /tools/vtest-compiler /tools/vtest-fmt /tools/vfmt +/tools/vbin2v /tools/vup /tools/vpm /tools/vcreate diff --git a/tools/oldv.v b/tools/oldv.v index dedd8a4ec1..e3194bfb0a 100644 --- a/tools/oldv.v +++ b/tools/oldv.v @@ -1,5 +1,6 @@ import ( os + os.cmdline flag filepath scripting diff --git a/tools/vbin2v.v b/tools/vbin2v.v new file mode 100644 index 0000000000..35dc4b6ece --- /dev/null +++ b/tools/vbin2v.v @@ -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() +} diff --git a/v.v b/v.v index 084db240a4..3da4b89037 100644 --- a/v.v +++ b/v.v @@ -15,6 +15,7 @@ import ( const ( known_commands = ['run', 'build', 'version', 'doc'] simple_tools = ['fmt', 'up', 'create', 'test', 'test-fmt', 'test-compiler', 'build-tools', + 'bin2v', 'build-examples', 'build-vbinaries'] )