From: Ardis Lu Date: Mon, 27 May 2024 21:37:51 +0000 (-0700) Subject: Re-wrap logic in a function X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=3a6828166bac9e564234dca5c31865e41aeee513;p=ArdisUtilities Re-wrap logic in a function Required to make "irm ... | iex" work because iex can only run one clause --- diff --git a/install.ps1 b/install.ps1 index 0ef364e..5c2e1e8 100644 --- a/install.ps1 +++ b/install.ps1 @@ -37,60 +37,66 @@ Invoke this script from GitHub, installing ArdisUtilities to the first location in $env:PSModulePath and persisting the module across sessions. #> -[OutputType([System.Void])] -[CmdletBinding()] -param( - [Parameter()] - [Switch]$Persist -) - -begin { - # Create a temporary working folder - $tempFolder = Join-Path $Env:Temp $(New-Guid) - New-Item -Type Directory -Path $tempFolder | Out-Null -} -process { - # Download and extract the repository from GitHub - $zipFile = Join-Path $tempFolder /ArdisUtilities.zip - Invoke-WebRequest 'https://github.com/ardislu/ArdisUtilities/archive/refs/heads/main.zip' -OutFile $zipFile - Expand-Archive $zipFile $tempFolder - $module = Join-Path $tempFolder 'ArdisUtilities-main/ArdisUtilities' - - # Import the module from the extraction - Import-Module $module - - if ($Persist) { - $destination = ($env:PSModulePath -Split ';')[0] - - # If no folder is found from PSModulePath, use default location for the CurrentUser scope. See: - # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath - if (-Not (Test-Path -LiteralPath $destination)) { - if ($IsWindows) { - $destination = "$HOME/Documents/PowerShell/Modules" + +function Install-ArdisUtilities { + [OutputType([System.Void])] + [CmdletBinding()] + param( + [Parameter()] + [Switch]$Persist + ) + + begin { + # Create a temporary working folder + $tempFolder = Join-Path $Env:Temp $(New-Guid) + New-Item -Type Directory -Path $tempFolder | Out-Null + } + + process { + # Download and extract the repository from GitHub + $zipFile = Join-Path $tempFolder /ArdisUtilities.zip + Invoke-WebRequest 'https://github.com/ardislu/ArdisUtilities/archive/refs/heads/main.zip' -OutFile $zipFile + Expand-Archive $zipFile $tempFolder + $module = Join-Path $tempFolder 'ArdisUtilities-main/ArdisUtilities' + + # Import the module from the extraction + Import-Module $module + + if ($Persist) { + $destination = ($env:PSModulePath -Split ';')[0] + + # If no folder is found from PSModulePath, use default location for the CurrentUser scope. See: + # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath + if (-Not (Test-Path -LiteralPath $destination)) { + if ($IsWindows) { + $destination = "$HOME/Documents/PowerShell/Modules" + } + else { + $destination = "$HOME/.local/share/powershell/Modules" + } } - else { - $destination = "$HOME/.local/share/powershell/Modules" + + if (Test-Path -LiteralPath "$destination/ArdisUtilities") { + Remove-Item "$destination/ArdisUtilities" -Force -Recurse } - } - if (Test-Path -LiteralPath "$destination/ArdisUtilities") { - Remove-Item "$destination/ArdisUtilities" -Force -Recurse + Copy-Item -Path $module -Destination $destination -Recurse + Write-Output "Successfully installed ArdisUtilities to `"$(Resolve-Path "$destination/ArdisUtilities")`"." + } + else { + Write-Output "Successfully installed ArdisUtilities in the current session." } - Copy-Item -Path $module -Destination $destination -Recurse - Write-Output "Successfully installed ArdisUtilities to `"$(Resolve-Path "$destination/ArdisUtilities")`"." - } - else { - Write-Output "Successfully installed ArdisUtilities in the current session." + Write-Output "" + Write-Output "Show all ArdisUtilities commands:" + Write-Output "Get-Command -Module ArdisUtilities" } - Write-Output "" - Write-Output "Show all ArdisUtilities commands:" - Write-Output "Get-Command -Module ArdisUtilities" + clean { + # Clean up the temporary folder + Remove-Item $tempFolder -Force -Recurse + } } -clean { - # Clean up the temporary folder - Remove-Item $tempFolder -Force -Recurse -} +Install-ArdisUtilities @args \ No newline at end of file