1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/cmd/tools/vbin2v.v

123 lines
2.9 KiB
V
Raw Normal View History

module main
import os
import flag
2020-06-24 01:00:37 +03:00
import strings
const (
2020-06-24 01:00:37 +03:00
tool_version = '0.0.4'
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
write_file string
}
2020-06-24 01:00:37 +03:00
fn (context Context) header() string {
mut header_s := ''
header_s += 'module ${context.module_name}\n'
header_s += '\n'
allfiles := context.files.join(' ')
2020-06-24 01:00:37 +03:00
2020-04-26 14:49:31 +03:00
mut options := []string{}
if context.prefix.len > 0 {
options << '-p ${context.prefix}'
}
if context.module_name.len > 0 {
options << '-m ${context.module_name}'
}
if context.write_file.len > 0 {
options << '-w ${context.write_file}'
2020-06-24 01:00:37 +03:00
}
soptions := options.join(' ')
2020-06-24 01:00:37 +03:00
header_s += '// File generated by:\n'
header_s += '// v bin2v ${allfiles} ${soptions}\n'
header_s += '// Please, do not edit this file.\n'
header_s += '// Your changes may be overwritten.\n'
header_s += '\n'
header_s += 'const (\n'
return header_s
}
2020-06-24 01:00:37 +03:00
fn (context Context) footer() string {
return ')\n'
}
2020-06-24 01:00:37 +03:00
fn (context Context) file2v(file string) string {
mut sb := strings.new_builder(1000)
2020-03-19 17:49:07 +03:00
fname := os.file_name(file)
fname_no_dots := fname.replace('.', '_')
byte_name := '${context.prefix}${fname_no_dots}'
fbytes := os.read_bytes(file) or {
eprintln('Error: $err')
2020-06-24 01:00:37 +03:00
return ''
}
fbyte := fbytes[0]
2020-06-24 01:00:37 +03:00
sb.write(' ${byte_name}_len = ${fbytes.len}\n')
sb.write(' ${byte_name} = [ byte(${fbyte}), \n ')
for i := 1; i < fbytes.len; i++ {
b := int(fbytes[i]).str()
2020-06-24 01:00:37 +03:00
sb.write('${b:4s}, ')
if 0 == i % 16 {
2020-06-24 01:00:37 +03:00
sb.write('\n ')
}
}
2020-06-24 01:00:37 +03:00
sb.write('\n]!!\n')
sb.write('\n')
return sb.str()
}
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 09:06:37 +03:00
context.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
2020-06-24 01:00:37 +03:00
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.')
context.write_file = fp.string('write', `w`, '', 'Write directly to a file with the given name.')
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
2020-06-24 01:00:37 +03:00
2020-06-24 15:13:31 +03:00
if !context.write_file.ends_with('.v') {
context.write_file += '.v'
}
if context.write_file.len > 0 {
2020-06-24 15:13:31 +03:00
mut out_file := os.create(context.write_file) or { panic(err) }
2020-06-24 01:00:37 +03:00
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())
}
}