From 5160bfa62683e7f71ccaca71304b7d0e3fdba69d Mon Sep 17 00:00:00 2001
From: Dibir Magomedsaygitov
<61747324+dibir-magomedsaygitov@users.noreply.github.com>
Date: Fri, 15 May 2020 15:01:45 +0300
Subject: [PATCH] Install Stack, GHC and Cabal to Windows Images (#874)
* install stack
* implement logic that gets 3 latest versions of ghc
---
images/win/Windows2016-Azure.json | 24 ++++++
images/win/Windows2019-Azure.json | 24 ++++++
.../scripts/ImageHelpers/InstallHelpers.ps1 | 5 +-
.../scripts/Installers/Install-Haskell.ps1 | 19 +++++
.../win/scripts/Installers/Install-Stack.ps1 | 20 +++++
.../scripts/Installers/Validate-Haskell.ps1 | 78 +++++++++++++++++++
.../win/scripts/Installers/Validate-Stack.ps1 | 27 +++++++
7 files changed, 196 insertions(+), 1 deletion(-)
create mode 100644 images/win/scripts/Installers/Install-Haskell.ps1
create mode 100644 images/win/scripts/Installers/Install-Stack.ps1
create mode 100644 images/win/scripts/Installers/Validate-Haskell.ps1
create mode 100644 images/win/scripts/Installers/Validate-Stack.ps1
diff --git a/images/win/Windows2016-Azure.json b/images/win/Windows2016-Azure.json
index 3f51de41d450..f05e79eb725e 100644
--- a/images/win/Windows2016-Azure.json
+++ b/images/win/Windows2016-Azure.json
@@ -541,6 +541,18 @@
"{{ template_dir }}/scripts/Installers/Install-TypeScript.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Install-Haskell.ps1"
+ ]
+ },
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Install-Stack.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
@@ -837,6 +849,18 @@
"{{ template_dir }}/scripts/Installers/Validate-TypeScript.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Validate-Haskell.ps1"
+ ]
+ },
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Validate-Stack.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
diff --git a/images/win/Windows2019-Azure.json b/images/win/Windows2019-Azure.json
index 467f81e72cae..78d190f0bfa3 100644
--- a/images/win/Windows2019-Azure.json
+++ b/images/win/Windows2019-Azure.json
@@ -520,6 +520,18 @@
"{{ template_dir }}/scripts/Installers/Install-TypeScript.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Install-Haskell.ps1"
+ ]
+ },
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Install-Stack.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
@@ -822,6 +834,18 @@
"{{ template_dir }}/scripts/Installers/Validate-TypeScript.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Validate-Haskell.ps1"
+ ]
+ },
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Validate-Stack.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1
index 7f3806a13e20..827485a3386f 100644
--- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1
+++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1
@@ -221,12 +221,15 @@ function Start-DownloadWithRetry
(
[Parameter(Mandatory)]
[string] $Url,
- [Parameter(Mandatory)]
[string] $Name,
[string] $DownloadPath = "${env:Temp}",
[int] $Retries = 20
)
+ if ([String]::IsNullOrEmpty($Name)) {
+ $Name = [IO.Path]::GetFileName($Url)
+ }
+
$filePath = Join-Path -Path $DownloadPath -ChildPath $Name
#Default retry logic for the package.
diff --git a/images/win/scripts/Installers/Install-Haskell.ps1 b/images/win/scripts/Installers/Install-Haskell.ps1
new file mode 100644
index 000000000000..19483fd7d7fd
--- /dev/null
+++ b/images/win/scripts/Installers/Install-Haskell.ps1
@@ -0,0 +1,19 @@
+################################################################################
+## File: Install-Haskell.ps1
+## Desc: Install Haskell for Windows
+################################################################################
+
+# Get 3 latest versions of GHC
+[Version[]] $ChocoVersionsOutput = & choco search ghc --allversions --limit-output | Where-Object { $_.StartsWith("ghc|") } | ForEach-Object { $_.TrimStart("ghc|") }
+$MajorMinorGroups = $ChocoVersionsOutput | Sort-Object -Descending | Group-Object { $_.ToString(2) } | Select-Object -First 3
+$VersionsList = $MajorMinorGroups | ForEach-Object { $_.Group | Select-Object -First 1 } | Sort-Object
+
+# The latest version will be installed as a default
+ForEach ($version in $VersionsList)
+{
+ Write-Host "Installing ghc $version..."
+ Choco-Install -PackageName ghc -ArgumentList "--version", $version, "-m"
+}
+
+Write-Host "Installing cabal..."
+Choco-Install -PackageName cabal
diff --git a/images/win/scripts/Installers/Install-Stack.ps1 b/images/win/scripts/Installers/Install-Stack.ps1
new file mode 100644
index 000000000000..6996ac7f62c6
--- /dev/null
+++ b/images/win/scripts/Installers/Install-Stack.ps1
@@ -0,0 +1,20 @@
+################################################################################
+## File: Install-Stack.ps1
+## Desc: Install Stack for Windows
+################################################################################
+
+Write-Host "Get the latest Stack version..."
+$StackReleasesJson = Invoke-RestMethod "https://api.github.com/repos/commercialhaskell/stack/releases/latest"
+$DownloadFilePattern = "windows-x86_64.zip"
+$DownloadUrl = $StackReleasesJson.assets | Where-Object { $_.name.EndsWith($DownloadFilePattern) } | Select-Object -ExpandProperty "browser_download_url" -First 1
+
+Write-Host "Download stack archive"
+$DestinationPath = Join-Path $Env:AGENT_TOOLSDIRECTORY "stack\x64"
+$StackArchivePath = Start-DownloadWithRetry -Url $DownloadUrl
+
+Write-Host "Expand stack archive"
+Extract-7Zip -Path $StackArchivePath -DestinationPath $DestinationPath
+
+New-Item -Name "x64.complete" -Path $DestinationPath
+
+Add-MachinePathItem -PathItem $DestinationPath
\ No newline at end of file
diff --git a/images/win/scripts/Installers/Validate-Haskell.ps1 b/images/win/scripts/Installers/Validate-Haskell.ps1
new file mode 100644
index 000000000000..6d47daad830c
--- /dev/null
+++ b/images/win/scripts/Installers/Validate-Haskell.ps1
@@ -0,0 +1,78 @@
+################################################################################
+## File: Validate-Haskell.ps1
+## Desc: Validate Haskell for Windows
+################################################################################
+
+# GHC validation
+if (Get-Command -Name 'ghc')
+{
+ Write-Host "ghc is on the path"
+}
+else
+{
+ Write-Host "ghc is not on path."
+ exit 1
+}
+
+$SoftwareName = "ghc"
+[String] $DefaultGhcVersion = & ghc --version
+$ChocoPackagesPath = Join-Path $env:ChocolateyInstall "lib"
+[Array] $GhcVersionList = Get-ChildItem -Path $ChocoPackagesPath -Filter "ghc.*" | ForEach-Object { $_.Name.TrimStart("ghc.") }
+
+# Validation that accurate 3 versions of GHC are installed
+if ($GhcVersionList.Count -eq 3)
+{
+ Write-Host "Versions of GHC are accurate"
+}
+else
+{
+ Write-Host "Versions of GHC not accurate"
+ exit 1
+}
+
+# Validation each of GHC version
+ForEach ($version in $GhcVersionList) {
+ $BinGhcPath = Join-Path $env:ChocolateyInstall "lib\ghc.$version\tools\ghc-$version\bin\ghc.exe"
+ if ((& $BinGhcPath --version) -match $version)
+ {
+ Write-Host "ghc $version is valid"
+ }
+ else
+ {
+ Write-Host "ghc $version is not valid"
+ exit 1
+ }
+}
+
+
+$GhcVersionsDescription = $GhcVersionList | ForEach-Object {
+ $DefaultPostfix = if ($DefaultGhcVersion -match $_) { " (default)" } else { "" }
+ "ghc $_ $DefaultPostfix `n"
+}
+
+$Description = @"
+_Version:_
+$GhcVersionsDescription
+"@
+
+Add-SoftwareDetailsToMarkdown -SoftwareName $SoftwareName -DescriptionMarkdown $Description
+
+
+# Cabal validation
+if (Get-Command -Name 'cabal')
+{
+ Write-Host "cabal is on the path"
+}
+else
+{
+ Write-Host "cabal is not on path."
+ exit 1
+}
+
+$SoftwareName = "cabal"
+
+$Description = @"
+_Version:_ $(cabal --version)
+"@
+
+Add-SoftwareDetailsToMarkdown -SoftwareName $SoftwareName -DescriptionMarkdown $Description
\ No newline at end of file
diff --git a/images/win/scripts/Installers/Validate-Stack.ps1 b/images/win/scripts/Installers/Validate-Stack.ps1
new file mode 100644
index 000000000000..d24d89546150
--- /dev/null
+++ b/images/win/scripts/Installers/Validate-Stack.ps1
@@ -0,0 +1,27 @@
+################################################################################
+## File: Validate-Stack.ps1
+## Desc: Validate Stack for Windows
+################################################################################
+
+if (Get-Command -Name 'stack')
+{
+ Write-Host "stack is on the path"
+}
+else
+{
+ Write-Host "stack is not on path."
+ exit 1
+}
+
+$StackVersion = stack --version --quiet
+
+# Adding description of the software to Markdown
+$SoftwareName = "Stack"
+
+$Description = @"
+_Version:_ $StackVersion
+_Environment:_
+* PATH: contains location of stack.exe
+"@
+
+Add-SoftwareDetailsToMarkdown -SoftwareName $SoftwareName -DescriptionMarkdown $Description