Skip to content

Commit

Permalink
Pass args to pixi-pack directly instead of parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Nov 27, 2024
1 parent 1151f6a commit f2dd16d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 121 deletions.
65 changes: 18 additions & 47 deletions src/header.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,25 @@ function New-TemporaryDirectory {
}

$TEMPDIR = New-TemporaryDirectory
$PREFIX = ""
$VERBOSE = $false
$QUIET = $false
$UNPACK_SHELL = ""
$USAGE = @"
usage: $($MyInvocation.MyCommand.Name) [options]
Unpacks an environment packed using pixi-pack
-h, --help Print this help message and exit
-o, --output-directory <DIR> Where to unpack the environment
-s, --shell <SHELL> Sets the shell [options: bash, zsh, xonsh, cmd, powershell, fish, nushell]
-v, --verbose Increase logging verbosity
-q, --quiet Decrease logging verbosity
Usage: $($MyInvocation.MyCommand.Name) [OPTIONS]
Arguments:
Path to an environment packed using pixi-pack
Options:
-o, --output-directory <DIR> Where to unpack the environment. The environment will be unpacked into a subdirectory of this path [default: env]
-e, --env-name <NAME> Name of the environment [default: env]
-s, --shell <SHELL> Sets the shell [options: bash, zsh, xonsh, cmd, powershell, fish, nushell]
-v, --verbose Increase logging verbosity
-q, --quiet Decrease logging verbosity
-h, --help Print help
"@

# Parse command-line arguments
$InputArgs = $MyInvocation.UnboundArguments
for ($i = 0; $i -lt $args.Count; $i++) {
switch ($InputArgs[$i]) {
"-o" { $PREFIX = $InputArgs[++$i] }
"--output-directory" { $PREFIX = $InputArgs[++$i] }
"-s" { $UNPACK_SHELL = $InputArgs[++$i] }
"--shell" { $UNPACK_SHELL = $InputArgs[++$i] }
"-v" { $VERBOSE = $true }
"--verbose" { $VERBOSE = $true }
"-q" { $QUIET = $true }
"--quiet" { $QUIET = $true }
"-h" { Write-Output $USAGE; exit 0 }
"--help" { Write-Output $USAGE; exit 0 }
foreach ($arg in $args) {
if ($arg -eq "-h" -or $arg -eq "--help") {
Write-Output $USAGE
exit 0
}
}

Expand Down Expand Up @@ -91,27 +80,9 @@ try {

# Build the command with flags
$arguments = @("unpack")
$arguments += $args | Join-String -Separator ' '

# Use $PREFIX for output directory if it is provided
if ($PREFIX) {
$arguments += "--output-directory"
$arguments += $PREFIX
}

# Handle verbosity/quiet flags
if ($VERBOSE) {
$arguments += "--verbose"
} elseif ($QUIET) {
$arguments += "--quiet"
}

# Add shell flag if provided
if ($UNPACK_SHELL) {
$arguments += "--shell"
$arguments += $UNPACK_SHELL
}

# Finally, add the path to the archive
# Add the path to the archive
$arguments += $archivePath

& $pixiPackPath @arguments
Expand Down
94 changes: 20 additions & 74 deletions src/header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,27 @@

set -euo pipefail
TEMPDIR="$(mktemp -d)"
PREFIX=""
VERBOSE=0
QUIET=0
UNPACK_SHELL=""

USAGE="
usage: $0 [options]
Unpacks an environment packed using pixi-pack
-h, --help Print this help message and exit
-o, --output-directory <DIR> Where to unpack the environment
-s, --shell <SHELL> Sets the shell [options: bash, zsh, xonsh, cmd, powershell, fish, nushell]
-v, --verbose Increase logging verbosity
-q, --quiet Decrease logging verbosity
Usage: $0 [OPTIONS]
Arguments:
Path to an environment packed using pixi-pack
Options:
-o, --output-directory <DIR> Where to unpack the environment. The environment will be unpacked into a subdirectory of this path [default: env]
-e, --env-name <NAME> Name of the environment [default: env]
-s, --shell <SHELL> Sets the shell [options: bash, zsh, xonsh, cmd, powershell, fish, nushell]
-v, --verbose Increase logging verbosity
-q, --quiet Decrease logging verbosity
-h, --help Print help
"
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h)
echo "$USAGE"
exit 0
;;
-v)
VERBOSE=1
shift
;;
-o)
if [[ -n "$2" && "$2" != -* ]]; then
PREFIX="$2"
shift 2
else
echo "Option -o requires an argument" >&2
echo "$USAGE" >&2
exit 1
fi
;;
-s)
if [[ -n "$2" && "$2" != -* ]]; then
UNPACK_SHELL="$2"
shift 2
else
echo "Option -s requires an argument" >&2
echo "$USAGE" >&2
exit 1
fi
;;
-q)
QUIET=1
shift
;;
-*)
echo "Invalid option: $1" >&2
echo "$USAGE" >&2
exit 1
;;
*)
# Stop parsing options when encountering a non-option argument
break
;;
esac

# Check for help flag
for arg in "$@"; do
if [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then
echo "$USAGE"
exit 0
fi
done

archive_begin=$(grep -anm 1 "^@@END_HEADER@@" "$0" | awk -F: '{print $1}')
Expand Down Expand Up @@ -93,21 +53,7 @@ fi

chmod +x "$TEMPDIR/pixi-pack"

VERBOSITY_FLAG=""
[ "$VERBOSE" = "1" ] && VERBOSITY_FLAG="--verbose"
[ "$QUIET" = "1" ] && VERBOSITY_FLAG="--quiet"

OUTPUT_DIR_FLAG=""
[ -n "$PREFIX" ] && OUTPUT_DIR_FLAG="--output-directory $PREFIX"

SHELL_FLAG=""
[ -n "$UNPACK_SHELL" ] && SHELL_FLAG="--shell $UNPACK_SHELL"

CMD="\"$TEMPDIR/pixi-pack\" unpack $OUTPUT_DIR_FLAG $VERBOSITY_FLAG"
if [ -n "$UNPACK_SHELL" ]; then
CMD="$CMD --shell $UNPACK_SHELL"
fi
CMD="$CMD \"$TEMPDIR/archive.tar\""
CMD="\"$TEMPDIR/pixi-pack\" unpack $@ \"$TEMPDIR/archive.tar\""

# Execute the command
eval "$CMD"
Expand Down

0 comments on commit f2dd16d

Please sign in to comment.