v.embedfile: make path public (#12614)
							parent
							
								
									ddec89f9ee
								
							
						
					
					
						commit
						0f59d88ba6
					
				| 
						 | 
					@ -2,10 +2,13 @@ module embed_file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const (
 | 
				
			||||||
 | 
						is_used = 1
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://github.com/vlang/rfcs/blob/master/embedding_resources.md
 | 
					// https://github.com/vlang/rfcs/blob/master/embedding_resources.md
 | 
				
			||||||
// EmbedFileData encapsulates functionality for the `$embed_file()` compile time call.
 | 
					// EmbedFileData encapsulates functionality for the `$embed_file()` compile time call.
 | 
				
			||||||
pub struct EmbedFileData {
 | 
					pub struct EmbedFileData {
 | 
				
			||||||
	path  string
 | 
					 | 
				
			||||||
	apath string
 | 
						apath string
 | 
				
			||||||
mut:
 | 
					mut:
 | 
				
			||||||
	compressed        &byte
 | 
						compressed        &byte
 | 
				
			||||||
| 
						 | 
					@ -13,11 +16,12 @@ mut:
 | 
				
			||||||
	free_compressed   bool
 | 
						free_compressed   bool
 | 
				
			||||||
	free_uncompressed bool
 | 
						free_uncompressed bool
 | 
				
			||||||
pub:
 | 
					pub:
 | 
				
			||||||
	len int
 | 
						len  int
 | 
				
			||||||
 | 
						path string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn (ed EmbedFileData) str() string {
 | 
					pub fn (ed EmbedFileData) str() string {
 | 
				
			||||||
	return 'embed_file.EmbedFileData{ len: $ed.len, path: "$ed.path", path: "$ed.apath", uncompressed: ${ptr_str(ed.uncompressed)} }'
 | 
						return 'embed_file.EmbedFileData{ len: $ed.len, path: "$ed.path", apath: "$ed.apath", uncompressed: ${ptr_str(ed.uncompressed)} }'
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[unsafe]
 | 
					[unsafe]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,6 +6,7 @@ fn test_const_embed_file() {
 | 
				
			||||||
	assert file.len == 603
 | 
						assert file.len == 603
 | 
				
			||||||
	fdata := file.data()
 | 
						fdata := file.data()
 | 
				
			||||||
	eprintln('file after .data() call: $file')
 | 
						eprintln('file after .data() call: $file')
 | 
				
			||||||
 | 
						assert file.path == 'v.png'
 | 
				
			||||||
	assert file.len == 603
 | 
						assert file.len == 603
 | 
				
			||||||
	unsafe {
 | 
						unsafe {
 | 
				
			||||||
		assert fdata.vbytes(4) == [byte(0x89), `P`, `N`, `G`]
 | 
							assert fdata.vbytes(4) == [byte(0x89), `P`, `N`, `G`]
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,18 @@
 | 
				
			||||||
 | 
					import v.embed_file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn test_embed_file_with_import() {
 | 
				
			||||||
 | 
						mut file := $embed_file('v.png')
 | 
				
			||||||
 | 
						eprintln('file: $file')
 | 
				
			||||||
 | 
						assert file.len == 603
 | 
				
			||||||
 | 
						fdata := file.data()
 | 
				
			||||||
 | 
						eprintln('file after .data() call: $file')
 | 
				
			||||||
 | 
						assert file.len == 603
 | 
				
			||||||
 | 
						unsafe {
 | 
				
			||||||
 | 
							assert fdata.vbytes(4) == [byte(0x89), `P`, `N`, `G`]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						assert check_file(file)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn check_file(file embed_file.EmbedFileData) bool {
 | 
				
			||||||
 | 
						return file.len == 603
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
		 Before Width: | Height: | Size: 603 B After Width: | Height: | Size: 603 B  | 
| 
						 | 
					@ -98,7 +98,7 @@ fn prepare_bin2v_file(mut fmt_bench benchmark.Benchmark) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn write_bin2v_keep_content() ? {
 | 
					fn write_bin2v_keep_content() ? {
 | 
				
			||||||
	img0 := os.join_path('vlib', 'v', 'embed_file', 'v.png')
 | 
						img0 := os.join_path('vlib', 'v', 'embed_file', 'tests', 'v.png')
 | 
				
			||||||
	img1 := os.join_path('tutorials', 'building_a_simple_web_blog_with_vweb', 'img', 'time.png')
 | 
						img1 := os.join_path('tutorials', 'building_a_simple_web_blog_with_vweb', 'img', 'time.png')
 | 
				
			||||||
	os.rm(b2v_keep_path) ?
 | 
						os.rm(b2v_keep_path) ?
 | 
				
			||||||
	res := os.execute('$vexe bin2v -w $b2v_keep_path $img0 $img1')
 | 
						res := os.execute('$vexe bin2v -w $b2v_keep_path $img0 $img1')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,7 +14,12 @@ fn (mut g Gen) embed_file_is_prod_mode() bool {
 | 
				
			||||||
fn (mut g Gen) gen_embed_file_init(node ast.ComptimeCall) {
 | 
					fn (mut g Gen) gen_embed_file_init(node ast.ComptimeCall) {
 | 
				
			||||||
	g.writeln('(v__embed_file__EmbedFileData){')
 | 
						g.writeln('(v__embed_file__EmbedFileData){')
 | 
				
			||||||
	g.writeln('\t\t.path = ${ctoslit(node.embed_file.rpath)},')
 | 
						g.writeln('\t\t.path = ${ctoslit(node.embed_file.rpath)},')
 | 
				
			||||||
	g.writeln('\t\t.apath = ${ctoslit(node.embed_file.apath)},')
 | 
						if g.embed_file_is_prod_mode() {
 | 
				
			||||||
 | 
							// apath is not needed in production and may leak information
 | 
				
			||||||
 | 
							g.writeln('\t\t.apath = ${ctoslit('')},')
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							g.writeln('\t\t.apath = ${ctoslit(node.embed_file.apath)},')
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	file_size := os.file_size(node.embed_file.apath)
 | 
						file_size := os.file_size(node.embed_file.apath)
 | 
				
			||||||
	if file_size > 5242880 {
 | 
						if file_size > 5242880 {
 | 
				
			||||||
		eprintln('Warning: embedding of files >= ~5MB is currently not supported')
 | 
							eprintln('Warning: embedding of files >= ~5MB is currently not supported')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,6 +26,7 @@ v__embed_file__EmbedFileIndexEntry* v__embed_file__find_index_entry_by_path(void
 | 
				
			||||||
 | 
					
 | 
				
			||||||
v__embed_file__EmbedFileData my_source = (v__embed_file__EmbedFileData){
 | 
					v__embed_file__EmbedFileData my_source = (v__embed_file__EmbedFileData){
 | 
				
			||||||
.path = _SLIT("embed.vv"),
 | 
					.path = _SLIT("embed.vv"),
 | 
				
			||||||
 | 
					.apath = _SLIT(""),
 | 
				
			||||||
.compressed = v__embed_file__find_index_entry_by_path((voidptr)_v_embed_file_index, _SLIT("embed.vv"))->data,
 | 
					.compressed = v__embed_file__find_index_entry_by_path((voidptr)_v_embed_file_index, _SLIT("embed.vv"))->data,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Initializations for module v.embed_file :
 | 
					// Initializations for module v.embed_file :
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -125,7 +125,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
 | 
				
			||||||
				epath = abs_path
 | 
									epath = abs_path
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		p.register_auto_import('v.embed_file')
 | 
							p.register_auto_import('v.preludes.embed_file')
 | 
				
			||||||
		return ast.ComptimeCall{
 | 
							return ast.ComptimeCall{
 | 
				
			||||||
			scope: 0
 | 
								scope: 0
 | 
				
			||||||
			is_embed: true
 | 
								is_embed: true
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					module embed_file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// This prelude is loaded in every v program that uses `$embed_file`,
 | 
				
			||||||
 | 
					// in both the main executable, and in the shared library.
 | 
				
			||||||
 | 
					import v.embed_file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						no_warning_embed_file_is_used = embed_file.is_used
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
		Loading…
	
		Reference in New Issue