Refactor portable.ps1 to simplify parameter handling and enhance command execution. Removed parameter declarations in favor of direct variable assignments, and added conditional console clearing based on argument presence. Improved argument execution logic for running commands within the prepared environment.

This commit is contained in:
2025-08-16 05:21:12 +07:00
parent 8a4fae89df
commit bfce64e8d3

View File

@@ -1,23 +1,15 @@
param( $EnvName = "default"
[Parameter(Mandatory = $false)] $PythonVersion = "3.9"
[string]$EnvName = "default", $RequirementsFile = ""
$Force = $false
[Parameter(Mandatory = $false)] $Reinstall = $false
[string]$PythonVersion = "3.9",
[Parameter(Mandatory = $false)]
[string]$RequirementsFile = "",
[Parameter(Mandatory = $false)]
[switch]$Force,
[Parameter(Mandatory = $false)]
[switch]$Reinstall
)
try { try {
# Clear the console # Clear the console only when no command is being executed
if ($args.Count -eq 0) {
Clear-Host Clear-Host
}
# Check for miniforge installer and download if not found # Check for miniforge installer and download if not found
if (-not (Test-Path -Path .\miniforge.exe)) { if (-not (Test-Path -Path .\miniforge.exe)) {
@@ -145,6 +137,19 @@ try {
Write-Error "Requirements file $RequirementsFile not found. Skipping requirements installation." Write-Error "Requirements file $RequirementsFile not found. Skipping requirements installation."
} }
} }
# execute arguments: run any command passed to the script within the prepared environment
if ($args.Count -gt 0) {
$exe = $args[0]
if ($args.Count -gt 1) {
$exeArgs = $args[1..($args.Count - 1)]
& $exe @($exeArgs)
}
else {
& $exe
}
exit $LASTEXITCODE
}
} }
catch { catch {
Write-Error "Unexpected error: $_" Write-Error "Unexpected error: $_"