2020-01-13 17:54:18 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
import flag
|
|
|
|
|
|
|
|
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(' ')
|
2020-04-26 13:49:31 +02:00
|
|
|
mut options := []string{}
|
2020-01-13 17:54:18 +01:00
|
|
|
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) {
|
2020-03-19 15:49:07 +01:00
|
|
|
fname := os.file_name(file)
|
2020-01-13 17:54:18 +01:00
|
|
|
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]...')
|
2020-03-19 07:06:37 +01:00
|
|
|
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')
|
2020-03-29 10:08:42 +02:00
|
|
|
if context.show_help {
|
2020-01-13 17:54:18 +01:00
|
|
|
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()
|
|
|
|
}
|