diff --git a/scripts/Windows/Scripts/Software.ps1 b/scripts/Windows/Scripts/Software.ps1 index b10321f2..209c8832 100644 --- a/scripts/Windows/Scripts/Software.ps1 +++ b/scripts/Windows/Scripts/Software.ps1 @@ -1,82 +1,57 @@ . "$PSScriptRoot/Config.ps1"; . "$PSScriptRoot/../Types/InstallerAction.ps1"; -<# - .SYNOPSIS - Gets the name of the software. -#> -function Get-SoftwareName { - $path = ${Function:Install-Software}.File; - - if ($path -ne "$PSCommandPath") { - Split-Path -Leaf (Split-Path -Parent $path); - } else { - $null; - } -} - -<# - .SYNOPSIS - Installs the software. -#> -function Install-Software { } - -<# - .SYNOPSIS - Configures the system for the software. -#> -function Set-SoftwareConfiguration { } - -<# - .SYNOPSIS - Configures a user for the software. - - .PARAMETER Name - The name of the user to configure. -#> -function Set-SoftwareUserConfiguration { - param( - [Parameter(Mandatory = $true)] - [string] $Name - ) -} - $null = New-Module { . "$PSScriptRoot/../Types/InstallerAction.ps1"; $userArgument = "name"; function Start-SoftwareInstaller { param( - [InstallerAction] $Action, + [string] $Name, + [scriptblock] $Installer = { }, + [scriptblock] $Configurator = { }, + [scriptblock] $UserConfigurator = { }, + [InstallerAction] $Action = [InstallerAction]::Install, [hashtable] $Arguments ) - $null = $softwareName; + if (-not $Name) { + $Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName)); + } - if ($null -ne (Get-SoftwareName)) { - $softwareName = "``$(Get-SoftwareName)``"; + if ($null -ne $Name) { + $Name = "``$Name``"; } else { - $softwareName = "unknown software"; + $Name = "unknown software"; } - if (($null -eq $Action) -or ($action -eq ([InstallerAction]::Install))) { - Write-Host "Installing $softwareName…"; - Install-Software @Arguments; - } elseif ($action -eq ([InstallerAction]::Configure)) { - Write-Host "Configuring $softwareName…"; - Set-SoftwareConfiguration @Arguments; + function Invoke-SoftwareInstaller { + param( + [InstallerAction] $Action, + [hashtable] $Arguments + ) - foreach ($user in Get-Users) { - $Arguments.Add($userArgument, $user); - Start-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) @Arguments; - } - } elseif ($action -eq ([InstallerAction]::ConfigureUser)) { - if ((-not $Arguments.Contains($userArgument)) -or ($null -eq $Arguments[$userArgument])) { - $Arguments.Add($userArgument, ($env:UserName)); - } + if ($action -eq ([InstallerAction]::Install)) { + Write-Host "Installing $Name…"; + & $Installer @Arguments; + } elseif ($Action -eq ([InstallerAction]::Configure)) { + Write-Host "Configuring $Name…"; + & $Configurator @Arguments; - Write-Host "Configuring $softwareName for user ``$($Arguments[$userArgument])``…"; - Set-SoftwareUserConfiguration @Arguments; + foreach ($user in Get-Users) { + $Arguments.Add($userArgument, $user); + Invoke-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) -Arguments $Arguments; + } + } elseif ($Action -eq ([InstallerAction]::ConfigureUser)) { + if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) { + $Arguments.Add($userArgument, ($env:UserName)); + } + + Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``…"; + & $UserConfigurator @Arguments; + } } + + Invoke-SoftwareInstaller -Action $Action -Arguments $Arguments; } } diff --git a/scripts/Windows/Software/winget/Manage.ps1 b/scripts/Windows/Software/winget/Manage.ps1 index f45094e4..962153d8 100644 --- a/scripts/Windows/Software/winget/Manage.ps1 +++ b/scripts/Windows/Software/winget/Manage.ps1 @@ -3,28 +3,21 @@ param( [hashtable] $Arguments ) -. "$PSScriptRoot/../../Scripts/Software.ps1"; -. "$PSScriptRoot/../../Types/InstallerAction.ps1"; +. "$PSScriptRoot/../../../Common/Scripts/Software.ps1"; -function Install-Winget { - param( - [InstallerAction] $Action = [InstallerAction]::Install, - [hashtable] $Arguments - ) +& { + param($Parameters) - Start-SoftwareInstaller -Action $Action -Arguments $Arguments; -} - -function Install-Software { - $xamlDownloadLink = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx"; - $downloadLink = "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"; - $file = New-TemporaryFile; - $file = Rename-Item $file "$file.msixbundle" -PassThru; - Invoke-WebRequest "$xamlDownloadLink" -OutFile "$file"; - Add-AppxPackage "$file"; - Invoke-WebRequest "$downloadLink" -OutFile "$file"; - Add-AppxPackage "$file"; - Remove-Item $file; -} - -Install-Winget @PSBoundParameters; + Start-SoftwareInstaller @Parameters ` + -Installer { + $xamlDownloadLink = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx"; + $downloadLink = "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"; + $file = New-TemporaryFile; + $file = Rename-Item $file "$file.msixbundle" -PassThru; + Invoke-WebRequest "$xamlDownloadLink" -OutFile "$file"; + Add-AppxPackage "$file"; + Invoke-WebRequest "$downloadLink" -OutFile "$file"; + Add-AppxPackage "$file"; + Remove-Item $file; + }; +} $PSBoundParameters;