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(
[Parameter(Mandatory = $false)]
[string]$EnvName = "default",
[Parameter(Mandatory = $false)]
[string]$PythonVersion = "3.9",
[Parameter(Mandatory = $false)]
[string]$RequirementsFile = "",
[Parameter(Mandatory = $false)]
[switch]$Force,
[Parameter(Mandatory = $false)]
[switch]$Reinstall
)
$EnvName = "default"
$PythonVersion = "3.9"
$RequirementsFile = ""
$Force = $false
$Reinstall = $false
try {
# Clear the console
Clear-Host
# Clear the console only when no command is being executed
if ($args.Count -eq 0) {
Clear-Host
}
# Check for miniforge installer and download if not found
if (-not (Test-Path -Path .\miniforge.exe)) {
@@ -145,6 +137,19 @@ try {
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 {
Write-Error "Unexpected error: $_"