From bfce64e8d3a82a97bd096b4f583e13d17a5634fe Mon Sep 17 00:00:00 2001 From: tuankiet2s Date: Sat, 16 Aug 2025 05:21:12 +0700 Subject: [PATCH] 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. --- portable.ps1 | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/portable.ps1 b/portable.ps1 index 7712c35..8e4ce16 100644 --- a/portable.ps1 +++ b/portable.ps1 @@ -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: $_"