]> git.ardis.lu - ArdisUtilities/commitdiff
Re-wrap logic in a function
authorArdis Lu <ardisglu@gmail.com>
Mon, 27 May 2024 21:37:51 +0000 (14:37 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 27 May 2024 21:37:51 +0000 (14:37 -0700)
Required to make "irm ... | iex" work because iex can only run one clause

install.ps1

index 0ef364e01b56ae60d0786eb19d2bee0c0865efdf..5c2e1e83ed2461e6fda87db93fe32b0d32b26cbd 100644 (file)
   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
This page took 0.035724 seconds and 4 git commands to generate.