Refactor __yay_rebuild and __yay_try functions to improve experimental features handling and command execution

This commit is contained in:
Chris Toph 2025-06-16 23:09:34 -04:00
parent 71ffb0166e
commit eb39f490a2
2 changed files with 61 additions and 41 deletions

View file

@ -21,21 +21,23 @@ function __yay_rebuild
set orig (pwd) set orig (pwd)
cd $flake_path cd $flake_path
# Base command # Build the nh command
set -l cmd "nh os switch . -H $host -- --impure" set -l nh_cmd "nh os switch . -H $host -- --impure"
# Add trace if requested # Add trace if requested
if set -q _flag_trace if set -q _flag_trace
set cmd "$cmd --show-trace" set nh_cmd "$nh_cmd --show-trace"
end end
# Add experimental features if requested # Choose execution method based on experimental flag
if set -q _flag_experimental if set -q _flag_experimental
set cmd "$cmd --extra-experimental-features flakes --extra-experimental-features nix-command" # Run nh inside a shell with experimental features
set -l shell_cmd "nix shell nixpkgs#nh --extra-experimental-features \"nix-command flakes\" -c fish -c \"$nh_cmd\""
__yay_run $shell_cmd
else
# Run nh directly
__yay_run $nh_cmd
end end
# Run the command
__yay_run $cmd
cd $orig cd $orig
end end

View file

@ -1,40 +1,45 @@
function __yay_try function __yay_try
set -l all_args $argv set -l all_args $argv
set -l opts h/help # Find -- separator before argparse to avoid argparse consuming it
argparse $opts -- $all_args; or return set -l sep_index (contains --index -- '--' $all_args)
set -l args_before_sep $all_args
set -l cmd_after_sep
if set -q _flag_help; or test (count $all_args) -eq 0 if test $status -eq 0 # Found '--' separator
echo "Usage: yay try PACKAGE [PACKAGE...] [-- COMMAND [ARGS...]]" if test $sep_index -gt 1
echo " -h, --help Show this help message" set args_before_sep $all_args[1..(math $sep_index - 1)]
else
set args_before_sep # empty if -- is first
end
if test $sep_index -lt (count $all_args)
set cmd_after_sep $all_args[(math $sep_index + 1)..-1]
end
end
# Now parse only the arguments before --
set -l opts h/help e/experimental
argparse $opts -- $args_before_sep; or return
if set -q _flag_help; or test (count $args_before_sep) -eq 0 -a (count $cmd_after_sep) -eq 0
echo "Usage: yay try [OPTIONS] PACKAGE [PACKAGE...] [-- COMMAND [ARGS...]]"
echo " -e, --experimental Enable experimental features (nix-command flakes)"
echo " -h, --help Show this help message"
return return
end end
# Initialize variables in function scope # $argv now contains only the packages (after argparse removed flags)
set -l pkgs set -l pkgs $argv
# Build command string from cmd_after_sep
set -l cmd_str set -l cmd_str
set -l sep_index (contains --index -- '--' $all_args) if test (count $cmd_after_sep) -gt 0
set -l escaped_cmd
if test $status -eq 0 # Found '--' separator for arg in $cmd_after_sep
# Extract packages before '--' set escaped_cmd $escaped_cmd (string escape -- "$arg")
if test $sep_index -gt 1
set pkgs $all_args[1..(math $sep_index - 1)]
end end
set cmd_str (string join ' ' -- $escaped_cmd)
# Extract and escape command after '--'
if test $sep_index -lt (count $all_args)
set -l raw_cmd $all_args[(math $sep_index + 1)..-1]
set -l escaped_cmd
for arg in $raw_cmd
set escaped_cmd $escaped_cmd (string escape -- "$arg")
end
set cmd_str (string join ' ' -- $escaped_cmd)
else
echo "Error: no command specified after --" >&2
return 1
end
else # No '--' found
set pkgs $all_args
end end
if test (count $pkgs) -eq 0 if test (count $pkgs) -eq 0
@ -45,11 +50,24 @@ function __yay_try
__yay_green "««« CREATING NIX SHELL »»»" __yay_green "««« CREATING NIX SHELL »»»"
__yay_yellow "Loading packages: $pkgs" __yay_yellow "Loading packages: $pkgs"
set -l pkg_str (string join ' ' -- $pkgs) # Build the base command
set -l base_cmd "nix shell"
if set -q _flag_experimental
set base_cmd "$base_cmd --extra-experimental-features \"nix-command flakes\""
end
# Convert package names to nixpkgs# format
set -l nix_pkgs
for pkg in $pkgs
set nix_pkgs $nix_pkgs "nixpkgs#$pkg"
end
set -l pkg_str (string join ' ' -- $nix_pkgs)
if test -n "$cmd_str" if test -n "$cmd_str"
__yay_yellow "Running: $cmd_str" __yay_yellow "Running: $cmd_str"
__yay_run "nix-shell -p $pkg_str --run \"$cmd_str\"" __yay_run "$base_cmd $pkg_str -c fish -c \"$cmd_str\""
else else
__yay_run "nix-shell -p $pkg_str --command fish" __yay_run "$base_cmd $pkg_str -c fish"
end end
end end