From eb39f490a252ac063746d093ac71e73327c5de27 Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Mon, 16 Jun 2025 23:09:34 -0400 Subject: [PATCH] Refactor __yay_rebuild and __yay_try functions to improve experimental features handling and command execution --- functions/__yay_rebuild.fish | 22 +++++----- functions/__yay_try.fish | 80 ++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/functions/__yay_rebuild.fish b/functions/__yay_rebuild.fish index 811d3a9..9aaf906 100644 --- a/functions/__yay_rebuild.fish +++ b/functions/__yay_rebuild.fish @@ -21,21 +21,23 @@ function __yay_rebuild set orig (pwd) cd $flake_path - # Base command - set -l cmd "nh os switch . -H $host -- --impure" - + # Build the nh command + set -l nh_cmd "nh os switch . -H $host -- --impure" + # Add trace if requested if set -q _flag_trace - set cmd "$cmd --show-trace" + set nh_cmd "$nh_cmd --show-trace" end - # Add experimental features if requested + # Choose execution method based on experimental flag 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 - # Run the command - __yay_run $cmd - cd $orig -end +end \ No newline at end of file diff --git a/functions/__yay_try.fish b/functions/__yay_try.fish index 2b4c2de..adc3535 100644 --- a/functions/__yay_try.fish +++ b/functions/__yay_try.fish @@ -1,40 +1,45 @@ function __yay_try set -l all_args $argv + + # Find -- separator before argparse to avoid argparse consuming it + set -l sep_index (contains --index -- '--' $all_args) + set -l args_before_sep $all_args + set -l cmd_after_sep + + if test $status -eq 0 # Found '--' separator + if test $sep_index -gt 1 + 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 - set -l opts h/help - argparse $opts -- $all_args; or return + # 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 $all_args) -eq 0 - echo "Usage: yay try PACKAGE [PACKAGE...] [-- COMMAND [ARGS...]]" - echo " -h, --help Show this help message" + 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 end - # Initialize variables in function scope - set -l pkgs + # $argv now contains only the packages (after argparse removed flags) + set -l pkgs $argv + + # Build command string from cmd_after_sep set -l cmd_str - set -l sep_index (contains --index -- '--' $all_args) - - if test $status -eq 0 # Found '--' separator - # Extract packages before '--' - if test $sep_index -gt 1 - set pkgs $all_args[1..(math $sep_index - 1)] + if test (count $cmd_after_sep) -gt 0 + set -l escaped_cmd + for arg in $cmd_after_sep + set escaped_cmd $escaped_cmd (string escape -- "$arg") end - - # 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 + set cmd_str (string join ' ' -- $escaped_cmd) end if test (count $pkgs) -eq 0 @@ -45,11 +50,24 @@ function __yay_try __yay_green "««« CREATING NIX SHELL »»»" __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" __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 - __yay_run "nix-shell -p $pkg_str --command fish" + __yay_run "$base_cmd $pkg_str -c fish" end end \ No newline at end of file