bin2v: allow direct file output

pull/5470/head
Lukas Neubert 2020-06-24 00:00:37 +02:00 committed by GitHub
parent 56749877ea
commit d4f0fe12ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 28 deletions

View File

@ -2,9 +2,10 @@ module main
import os import os
import flag import flag
import strings
const ( const (
tool_version = '0.0.3' tool_version = '0.0.4'
tool_description = 'Converts a list of arbitrary files into a single v module file.' tool_description = 'Converts a list of arbitrary files into a single v module file.'
) )
@ -14,12 +15,15 @@ mut:
prefix string prefix string
show_help bool show_help bool
module_name string module_name string
write_file bool
} }
fn (context Context) header() { fn (context Context) header() string {
println('module ${context.module_name}') mut header_s := ''
println('') header_s += 'module ${context.module_name}\n'
header_s += '\n'
allfiles := context.files.join(' ') allfiles := context.files.join(' ')
mut options := []string{} mut options := []string{}
if context.prefix.len > 0 { if context.prefix.len > 0 {
options << '-p ${context.prefix}' options << '-p ${context.prefix}'
@ -27,39 +31,48 @@ fn (context Context) header() {
if context.module_name.len > 0 { if context.module_name.len > 0 {
options << '-m ${context.module_name}' options << '-m ${context.module_name}'
} }
if context.write_file {
options << '-w'
}
soptions := options.join(' ') soptions := options.join(' ')
println('// File generated by:')
println('// v bin2v ${allfiles} ${soptions}') header_s += '// File generated by:\n'
println('// Please, do not edit this file.') header_s += '// v bin2v ${allfiles} ${soptions}\n'
println('// Your changes may be overwritten.') header_s += '// Please, do not edit this file.\n'
println('') header_s += '// Your changes may be overwritten.\n'
println('const (') header_s += '\n'
header_s += 'const (\n'
return header_s
} }
fn (context Context) footer() { fn (context Context) footer() string {
println(')') return ')\n'
} }
fn (context Context) file2v(file string) { fn (context Context) file2v(file string) string {
mut sb := strings.new_builder(1000)
fname := os.file_name(file) fname := os.file_name(file)
fname_no_dots := fname.replace('.', '_') fname_no_dots := fname.replace('.', '_')
byte_name := '${context.prefix}${fname_no_dots}' byte_name := '${context.prefix}${fname_no_dots}'
fbytes := os.read_bytes(file) or { fbytes := os.read_bytes(file) or {
eprintln('Error: $err') eprintln('Error: $err')
return return ''
} }
fbyte := fbytes[0] fbyte := fbytes[0]
println(' ${byte_name}_len = ${fbytes.len}') sb.write(' ${byte_name}_len = ${fbytes.len}\n')
print(' ${byte_name} = [ byte(${fbyte}), \n ') sb.write(' ${byte_name} = [ byte(${fbyte}), \n ')
for i := 1; i < fbytes.len; i++ { for i := 1; i < fbytes.len; i++ {
b := int(fbytes[i]).str() b := int(fbytes[i]).str()
print('${b:4s}, ') sb.write('${b:4s}, ')
if 0 == i % 16 { if 0 == i % 16 {
print('\n ') sb.write('\n ')
} }
} }
println('\n]!!') sb.write('\n]!!\n')
println('') sb.write('\n')
return sb.str()
} }
fn main() { fn main() {
@ -70,8 +83,9 @@ fn main() {
fp.description(tool_description) fp.description(tool_description)
fp.arguments_description('FILE [FILE]...') fp.arguments_description('FILE [FILE]...')
context.show_help = fp.bool('help', `h`, false, 'Show this help screen.') 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.module_name = fp.string('module', `m`, 'binary', 'Name of the generated module.')
context.prefix = fp.string('prefix', `p`, '', 'A prefix put before each resource name.\n') context.prefix = fp.string('prefix', `p`, '', 'A prefix put before each resource name.')
context.write_file = fp.bool('write', `w`, false, 'Write directly to a file. The module name is used as filename.')
if context.show_help { if context.show_help {
println(fp.usage()) println(fp.usage())
exit(0) exit(0)
@ -86,9 +100,20 @@ fn main() {
exit(0) exit(0)
} }
context.files = real_files context.files = real_files
context.header()
for file in real_files { if context.write_file {
context.file2v(file) mut out_file := os.create('${context.module_name}.v') or { panic(err) }
out_file.write(context.header())
for file in real_files {
out_file.write(context.file2v(file))
}
out_file.write(context.footer())
}
else {
println(context.header())
for file in real_files {
println(context.file2v(file))
}
println(context.footer())
} }
context.footer()
} }

View File

@ -11,3 +11,4 @@ Options:
-h, --help Show this help screen. -h, --help Show this help screen.
-m, --module <string> Name of the generated module. -m, --module <string> Name of the generated module.
-p, --prefix <string> A prefix put before each resource name. -p, --prefix <string> A prefix put before each resource name.
-w, --write Write directly to a file. The module name is used as filename.