module vmakepkg import json2 import vile.scheme import os // the body goes through vpac inside the transaction root. paths in the bundle // are relative to the recipe, including helpers loaded by a later hook. struct RecipeHook { recipe string files map[string]string } const hook_header = '#!/usr/bin/env -S vpac --package-hook\n' fn hook_relative_path(name string) bool { return !name.contains('\x00') && !name.split('/').any(it in ['', '.', '..']) } fn write_recipe_hook(recipe string, work string, helpers []string) !string { name := os.base(recipe) base := os.real_path(os.dir(recipe)) + '/' mut files := { name: os.read_file(recipe)! } for rel in helpers { path := os.join_path(base, rel) if !hook_relative_path(rel) || !os.real_path(path).starts_with(base) || !os.is_file(path) { return error('invalid native hook file: ${rel}') } files[rel] = os.read_file(path)! } path := os.join_path(work, 'hook') os.write_file(path, hook_header + json2.encode(RecipeHook{ recipe: name, files: files }))! return path } pub fn package_hook(args []string) ! { if args.len < 5 || args[1] != '--nspkg-hook' || args[2] !in recipe_hooks || args.len != 4 + recipe_hook_arity(args[2]) { return error('invalid native hook arguments') } source := os.read_file(args[0])! if !source.starts_with(hook_header) { return error('unknown recipe hook format') } bundle := json2.decode[RecipeHook](source[hook_header.len..])! if bundle.recipe !in ['PKGBUILD.scm', 'PKGBUILD.vile'] || bundle.recipe !in bundle.files { return error('missing recipe in native hook') } dir := private_dir(os.temp_dir())! defer { os.rmdir_all(dir) or { eprintln('remove staged hook: ${err}') } } for name, contents in bundle.files { if !hook_relative_path(name) { return error('invalid path in native hook') } path := os.join_path(dir, name) os.mkdir_all(os.dir(path))! os.write_file(path, contents)! } path := os.join_path(dir, bundle.recipe) os.setenv('VPAC_PKGNAME', args[3], true) mut vm := recipe_new_machine()! meta := recipe_load_in(mut vm, path)! if meta.has(args[2]) { vm.call(meta.get(args[2]), args[4..].map(scheme.string_value(it)))! } }