|

refactor(package): thread ScriptCtx through script hooks via build_script_cmd

Author: Chris Tusa <chris.tusa@leafscale.com>
Date: Jul 05, 2026 17:04
Changeset: 0c1e7ff75e51988db07dfe38f4ac9d7a93e08364
Branch: default

Changed files:

Diff

diff -r 5172a0f6f7e0 -r 0c1e7ff75e51 src/core/package.reef
--- a/src/core/package.reef	Sun Jul 05 16:57:05 2026 -0500
+++ b/src/core/package.reef	Sun Jul 05 17:04:23 2026 -0500
@@ -71,20 +71,20 @@
 
     // Install scripts support (version passed as $VERSION to scripts)
     fn has_scripts(pkg_dir: string): bool
-    fn run_pre_install(pkg_dir: string, version: string): bool
-    fn run_post_install(pkg_dir: string, version: string): bool
-    fn run_pre_remove(pkg_dir: string, version: string): bool
-    fn run_post_remove(pkg_dir: string, version: string): bool
+    fn run_pre_install(ctx: ScriptCtx, pkg_dir: string, version: string): bool
+    fn run_post_install(ctx: ScriptCtx, pkg_dir: string, version: string): bool
+    fn run_pre_remove(ctx: ScriptCtx, pkg_dir: string, version: string): bool
+    fn run_post_remove(ctx: ScriptCtx, pkg_dir: string, version: string): bool
 
     // Upgrade scripts ($OLD_VERSION $NEW_VERSION passed to scripts)
-    fn run_pre_upgrade(pkg_dir: string, old_version: string, new_version: string): bool
-    fn run_post_upgrade(pkg_dir: string, old_version: string, new_version: string): bool
+    fn run_pre_upgrade(ctx: ScriptCtx, pkg_dir: string, old_version: string, new_version: string): bool
+    fn run_post_upgrade(ctx: ScriptCtx, pkg_dir: string, old_version: string, new_version: string): bool
 
     // Run scripts from a stored scripts directory (for remove/upgrade operations)
-    fn run_stored_pre_remove(scripts_dir: string, version: string): bool
-    fn run_stored_post_remove(scripts_dir: string, version: string): bool
-    fn run_stored_pre_upgrade(scripts_dir: string, old_version: string, new_version: string): bool
-    fn run_stored_post_upgrade(scripts_dir: string, old_version: string, new_version: string): bool
+    fn run_stored_pre_remove(ctx: ScriptCtx, scripts_dir: string, version: string): bool
+    fn run_stored_post_remove(ctx: ScriptCtx, scripts_dir: string, version: string): bool
+    fn run_stored_pre_upgrade(ctx: ScriptCtx, scripts_dir: string, old_version: string, new_version: string): bool
+    fn run_stored_post_upgrade(ctx: ScriptCtx, scripts_dir: string, old_version: string, new_version: string): bool
 
     // Config file protection
     fn read_config_files(pkg_dir: string, config_files: [string], max_count: int): int
@@ -554,7 +554,7 @@
 end build_script_cmd
 
 // Run a script from the .SCRIPTS directory
-fn run_script(pkg_dir: string, script_name: string, script_args: string): bool
+fn run_script(ctx: ScriptCtx, pkg_dir: string, script_name: string, script_args: string): bool
     let scripts_dir = path.join_path(pkg_dir, ".SCRIPTS")
     let script_path = path.join_path(scripts_dir, script_name)
 
@@ -562,19 +562,8 @@
         return true
     end if
 
-    mut cmd = ""
-
-    // Shell scripts (.sh) - run with zsh
-    if str.ends_with(script_name, ".sh")
-        cmd = "zsh \"" + script_path + "\""
-    else
-        // Default: try to execute directly (binary or has shebang)
-        cmd = "\"" + script_path + "\""
-    end if
-
-    if str.length(script_args) > 0
-        cmd = cmd + " " + script_args
-    end if
+    let is_sh = str.ends_with(script_name, ".sh")
+    let cmd = build_script_cmd(ctx, script_path, script_name, script_args, is_sh)
 
     let pid = process.process_spawn_shell(cmd)
     if pid < 0
@@ -585,7 +574,7 @@
 end run_script
 
 // Run pre-install script (passes $VERSION)
-fn run_pre_install(pkg_dir: string, version: string): bool
+fn run_pre_install(ctx: ScriptCtx, pkg_dir: string, version: string): bool
     if not has_scripts(pkg_dir)
         return true
     end if
@@ -594,13 +583,13 @@
     // Try .sh first, then bare executable
     let sh_path = path.join_path(scripts_dir, "pre-install.sh")
     if file.fileExists(sh_path)
-        return run_script(pkg_dir, "pre-install.sh", args)
+        return run_script(ctx, pkg_dir, "pre-install.sh", args)
     end if
-    return run_script(pkg_dir, "pre-install", args)
+    return run_script(ctx, pkg_dir, "pre-install", args)
 end run_pre_install
 
 // Run post-install script (passes $VERSION)
-fn run_post_install(pkg_dir: string, version: string): bool
+fn run_post_install(ctx: ScriptCtx, pkg_dir: string, version: string): bool
     if not has_scripts(pkg_dir)
         return true
     end if
@@ -608,13 +597,13 @@
     let scripts_dir = path.join_path(pkg_dir, ".SCRIPTS")
     let sh_path = path.join_path(scripts_dir, "post-install.sh")
     if file.fileExists(sh_path)
-        return run_script(pkg_dir, "post-install.sh", args)
+        return run_script(ctx, pkg_dir, "post-install.sh", args)
     end if
-    return run_script(pkg_dir, "post-install", args)
+    return run_script(ctx, pkg_dir, "post-install", args)
 end run_post_install
 
 // Run pre-remove script (passes $VERSION)
-fn run_pre_remove(pkg_dir: string, version: string): bool
+fn run_pre_remove(ctx: ScriptCtx, pkg_dir: string, version: string): bool
     if not has_scripts(pkg_dir)
         return true
     end if
@@ -622,13 +611,13 @@
     let scripts_dir = path.join_path(pkg_dir, ".SCRIPTS")
     let sh_path = path.join_path(scripts_dir, "pre-remove.sh")
     if file.fileExists(sh_path)
-        return run_script(pkg_dir, "pre-remove.sh", args)
+        return run_script(ctx, pkg_dir, "pre-remove.sh", args)
     end if
-    return run_script(pkg_dir, "pre-remove", args)
+    return run_script(ctx, pkg_dir, "pre-remove", args)
 end run_pre_remove
 
 // Run post-remove script (passes $VERSION)
-fn run_post_remove(pkg_dir: string, version: string): bool
+fn run_post_remove(ctx: ScriptCtx, pkg_dir: string, version: string): bool
     if not has_scripts(pkg_dir)
         return true
     end if
@@ -636,13 +625,13 @@
     let scripts_dir = path.join_path(pkg_dir, ".SCRIPTS")
     let sh_path = path.join_path(scripts_dir, "post-remove.sh")
     if file.fileExists(sh_path)
-        return run_script(pkg_dir, "post-remove.sh", args)
+        return run_script(ctx, pkg_dir, "post-remove.sh", args)
     end if
-    return run_script(pkg_dir, "post-remove", args)
+    return run_script(ctx, pkg_dir, "post-remove", args)
 end run_post_remove
 
 // Run pre-upgrade script (passes $OLD_VERSION $NEW_VERSION)
-fn run_pre_upgrade(pkg_dir: string, old_version: string, new_version: string): bool
+fn run_pre_upgrade(ctx: ScriptCtx, pkg_dir: string, old_version: string, new_version: string): bool
     if not has_scripts(pkg_dir)
         return true
     end if
@@ -650,13 +639,13 @@
     let scripts_dir = path.join_path(pkg_dir, ".SCRIPTS")
     let sh_path = path.join_path(scripts_dir, "pre-upgrade.sh")
     if file.fileExists(sh_path)
-        return run_script(pkg_dir, "pre-upgrade.sh", args)
+        return run_script(ctx, pkg_dir, "pre-upgrade.sh", args)
     end if
-    return run_script(pkg_dir, "pre-upgrade", args)
+    return run_script(ctx, pkg_dir, "pre-upgrade", args)
 end run_pre_upgrade
 
 // Run post-upgrade script (passes $OLD_VERSION $NEW_VERSION)
-fn run_post_upgrade(pkg_dir: string, old_version: string, new_version: string): bool
+fn run_post_upgrade(ctx: ScriptCtx, pkg_dir: string, old_version: string, new_version: string): bool
     if not has_scripts(pkg_dir)
         return true
     end if
@@ -664,32 +653,21 @@
     let scripts_dir = path.join_path(pkg_dir, ".SCRIPTS")
     let sh_path = path.join_path(scripts_dir, "post-upgrade.sh")
     if file.fileExists(sh_path)
-        return run_script(pkg_dir, "post-upgrade.sh", args)
+        return run_script(ctx, pkg_dir, "post-upgrade.sh", args)
     end if
-    return run_script(pkg_dir, "post-upgrade", args)
+    return run_script(ctx, pkg_dir, "post-upgrade", args)
 end run_post_upgrade
 
 // Run a script directly from a stored scripts directory
-fn run_stored_script(scripts_dir: string, script_name: string, script_args: string): bool
+fn run_stored_script(ctx: ScriptCtx, scripts_dir: string, script_name: string, script_args: string): bool
     let script_path = path.join_path(scripts_dir, script_name)
 
     if not file.fileExists(script_path)
         return true
     end if
 
-    mut cmd = ""
-
-    // Shell scripts (.sh) - run with zsh
-    if str.ends_with(script_name, ".sh")
-        cmd = "zsh \"" + script_path + "\""
-    else
-        // Default: try to execute directly
-        cmd = "\"" + script_path + "\""
-    end if
-
-    if str.length(script_args) > 0
-        cmd = cmd + " " + script_args
-    end if
+    let is_sh = str.ends_with(script_name, ".sh")
+    let cmd = build_script_cmd(ctx, script_path, script_name, script_args, is_sh)
 
     let pid = process.process_spawn_shell(cmd)
     if pid < 0
@@ -700,55 +678,55 @@
 end run_stored_script
 
 // Run pre-remove script from stored scripts directory (passes $VERSION)
-fn run_stored_pre_remove(scripts_dir: string, version: string): bool
+fn run_stored_pre_remove(ctx: ScriptCtx, scripts_dir: string, version: string): bool
     if not dir.dir_exists(scripts_dir)
         return true
     end if
     let args = "\"" + version + "\""
     let sh_path = path.join_path(scripts_dir, "pre-remove.sh")
     if file.fileExists(sh_path)
-        return run_stored_script(scripts_dir, "pre-remove.sh", args)
+        return run_stored_script(ctx, scripts_dir, "pre-remove.sh", args)
     end if
-    return run_stored_script(scripts_dir, "pre-remove", args)
+    return run_stored_script(ctx, scripts_dir, "pre-remove", args)
 end run_stored_pre_remove
 
 // Run post-remove script from stored scripts directory (passes $VERSION)
-fn run_stored_post_remove(scripts_dir: string, version: string): bool
+fn run_stored_post_remove(ctx: ScriptCtx, scripts_dir: string, version: string): bool
     if not dir.dir_exists(scripts_dir)
         return true
     end if
     let args = "\"" + version + "\""
     let sh_path = path.join_path(scripts_dir, "post-remove.sh")
     if file.fileExists(sh_path)
-        return run_stored_script(scripts_dir, "post-remove.sh", args)
+        return run_stored_script(ctx, scripts_dir, "post-remove.sh", args)
     end if
-    return run_stored_script(scripts_dir, "post-remove", args)
+    return run_stored_script(ctx, scripts_dir, "post-remove", args)
 end run_stored_post_remove
 
 // Run pre-upgrade script from stored scripts directory (passes $OLD_VERSION $NEW_VERSION)
-fn run_stored_pre_upgrade(scripts_dir: string, old_version: string, new_version: string): bool
+fn run_stored_pre_upgrade(ctx: ScriptCtx, scripts_dir: string, old_version: string, new_version: string): bool
     if not dir.dir_exists(scripts_dir)
         return true
     end if
     let args = "\"" + old_version + "\" \"" + new_version + "\""
     let sh_path = path.join_path(scripts_dir, "pre-upgrade.sh")
     if file.fileExists(sh_path)
-        return run_stored_script(scripts_dir, "pre-upgrade.sh", args)
+        return run_stored_script(ctx, scripts_dir, "pre-upgrade.sh", args)
     end if
-    return run_stored_script(scripts_dir, "pre-upgrade", args)
+    return run_stored_script(ctx, scripts_dir, "pre-upgrade", args)
 end run_stored_pre_upgrade
 
 // Run post-upgrade script from stored scripts directory (passes $OLD_VERSION $NEW_VERSION)
-fn run_stored_post_upgrade(scripts_dir: string, old_version: string, new_version: string): bool
+fn run_stored_post_upgrade(ctx: ScriptCtx, scripts_dir: string, old_version: string, new_version: string): bool
     if not dir.dir_exists(scripts_dir)
         return true
     end if
     let args = "\"" + old_version + "\" \"" + new_version + "\""
     let sh_path = path.join_path(scripts_dir, "post-upgrade.sh")
     if file.fileExists(sh_path)
-        return run_stored_script(scripts_dir, "post-upgrade.sh", args)
+        return run_stored_script(ctx, scripts_dir, "post-upgrade.sh", args)
     end if
-    return run_stored_script(scripts_dir, "post-upgrade", args)
+    return run_stored_script(ctx, scripts_dir, "post-upgrade", args)
 end run_stored_post_upgrade
 
 // Read list of config files from .CONFIG file in extracted package