From 44e904205f61fdc8018a9a1f615f8e7c61187827 Mon Sep 17 00:00:00 2001 From: Luke Duddridge <338863+elduddz@users.noreply.github.com> Date: Tue, 14 Apr 2020 06:51:57 +0100 Subject: [PATCH] Chocolatey Helper (#646) * Add basic choco install wrapper * Use Install-Choco on mingw * move azure-cli install to defence * Rename ChocoInstall to ChocoHelpers --- .../win/scripts/ImageHelpers/ChocoHelpers.ps1 | 33 +++++++++++++++++++ .../scripts/ImageHelpers/ImageHelpers.psm1 | 2 ++ .../scripts/ImageHelpers/InstallHelpers.ps1 | 3 +- .../scripts/ImageHelpers/MarkdownHelpers.ps1 | 2 +- .../scripts/Installers/Install-AzureCli.ps1 | 2 +- .../win/scripts/Installers/Install-MinGW.ps1 | 2 +- 6 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 images/win/scripts/ImageHelpers/ChocoHelpers.ps1 diff --git a/images/win/scripts/ImageHelpers/ChocoHelpers.ps1 b/images/win/scripts/ImageHelpers/ChocoHelpers.ps1 new file mode 100644 index 000000000..f81ad0a1f --- /dev/null +++ b/images/win/scripts/ImageHelpers/ChocoHelpers.ps1 @@ -0,0 +1,33 @@ +function Install-Choco { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string]$install, + [int]$retries = 5 + ) + + begin { } + process { + $condition = $false + $count = 0 + do { + Write-Output "running: powershell choco install $install -y" + powershell choco install $install -y + + $installed = powershell choco list -lo $install --all + $match = (($installed -match "^$install.*").Length -ne 0) + if ($match) { + Write-Output "package installed: $install" + $condition = $true + } + else { + $count++ + if ($count -eq $retries) { + Write-Error "Could not install $install after $count attempts" + exit 1 + } + } + } while ($condition -eq $false) + } + end { } +} \ No newline at end of file diff --git a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 index c40b10989..9e750b8db 100644 --- a/images/win/scripts/ImageHelpers/ImageHelpers.psm1 +++ b/images/win/scripts/ImageHelpers/ImageHelpers.psm1 @@ -4,6 +4,7 @@ param() . $PSScriptRoot\PathHelpers.ps1 . $PSScriptRoot\InstallHelpers.ps1 . $PSScriptRoot\MarkdownHelpers.ps1 +. $PSScriptRoot\ChocoHelpers.ps1 Export-ModuleMember -Function @( 'Test-MachinePath' @@ -26,4 +27,5 @@ Export-ModuleMember -Function @( 'Get-WinVersion' 'Test-IsWin19' 'Test-IsWin16' + 'Install-Choco' ) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index a016e5fd7..13f2fbf9d 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -3,7 +3,8 @@ function Install-MSI Param ( [String]$MsiUrl, - [String]$MsiName + [String]$MsiName, + [int]$retries = 5 ) $exitCode = -1 diff --git a/images/win/scripts/ImageHelpers/MarkdownHelpers.ps1 b/images/win/scripts/ImageHelpers/MarkdownHelpers.ps1 index da5d53e2c..49becbc9b 100644 --- a/images/win/scripts/ImageHelpers/MarkdownHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/MarkdownHelpers.ps1 @@ -1,5 +1,5 @@ function Add-ContentToMarkdown { - [CmdletBinding()] + param( $Content = "" ) diff --git a/images/win/scripts/Installers/Install-AzureCli.ps1 b/images/win/scripts/Installers/Install-AzureCli.ps1 index 4756bce3b..ca96558a5 100644 --- a/images/win/scripts/Installers/Install-AzureCli.ps1 +++ b/images/win/scripts/Installers/Install-AzureCli.ps1 @@ -3,7 +3,7 @@ ## Desc: Install Azure CLI ################################################################################ -choco install azure-cli -y +Install-Choco "azure-cli" $AzureCliExtensionPath = Join-Path $Env:CommonProgramFiles 'AzureCliExtensionDirectory' New-Item -ItemType "directory" -Path $AzureCliExtensionPath diff --git a/images/win/scripts/Installers/Install-MinGW.ps1 b/images/win/scripts/Installers/Install-MinGW.ps1 index fe97ddde7..c2aa0f52c 100644 --- a/images/win/scripts/Installers/Install-MinGW.ps1 +++ b/images/win/scripts/Installers/Install-MinGW.ps1 @@ -5,7 +5,7 @@ Import-Module -Name ImageHelpers -Force -choco install -y mingw +Install-Choco "mingw" # Make a copy of mingw32-make.exe to make.exe, which is a more discoverable name # and so the same command line can be used on Windows as on macOS and Linux