From da29895afdbf19b452caad1e93a56a541df398a5 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Wed, 19 Aug 2020 15:44:11 -0400
Subject: [PATCH] misc: refactor windows build dependency powershell script

- Rename the script to better reflect its purpose
- Don't install the chocolatey package manager if already installed
- Don't install packages if already installed
- Clean up error management

Gitlab: #581
Change-Id: I0e6fdeacfd465ae3810cfcfd7af21755e4f3ec1e
---
 make-ring.py                      |   2 +-
 scripts/build-package-windows.ps1 | 166 ----------------------
 scripts/install-deps-windows.ps1  | 220 ++++++++++++++++++++++++++++++
 3 files changed, 221 insertions(+), 167 deletions(-)
 delete mode 100644 scripts/build-package-windows.ps1
 create mode 100644 scripts/install-deps-windows.ps1

diff --git a/make-ring.py b/make-ring.py
index 4c09b0da..5c6f0241 100755
--- a/make-ring.py
+++ b/make-ring.py
@@ -204,7 +204,7 @@ def run_dependencies(args):
 
     if args.distribution == WIN32_DISTRIBUTION_NAME:
         run_powersell_cmd(
-            'Set-ExecutionPolicy Unrestricted; .\\scripts\\build-package-windows.ps1')
+            'Set-ExecutionPolicy Unrestricted; .\\scripts\\install-deps-windows.ps1')
 
     elif args.distribution in APT_BASED_DISTROS:
         execute_script(
diff --git a/scripts/build-package-windows.ps1 b/scripts/build-package-windows.ps1
deleted file mode 100644
index efc5a1cc..00000000
--- a/scripts/build-package-windows.ps1
+++ /dev/null
@@ -1,166 +0,0 @@
-# Install Choco
-
-Set-ExecutionPolicy Bypass -Scope Process -Force
-iex ((New-Object System.Net.WebClient).DownloadString("https://chocolatey.org/install.ps1"))
-
-if( $LASTEXITCODE -eq 0 ) {
-    write-host "Choco Installation Succeeded" -ForegroundColor Green
-} else {
-    write-host "Choco Installation Failed" -ForegroundColor Red
-    exit $LASTEXITCODE
-}
-
-# Install 7zip, unzip, wget --version 1.19.4, cmake, git --version 2.10.2
-# pandoc, strawberryperl, msys2
-# Note that: msys2 installes at C:/tools/msys64
-
-Function choco_pack_install($packages) {
-
-    Foreach ($i in $packages){
-        if($i -eq 'wget'){
-            iex ("choco install -fy --allow-downgrade '$i' --version 1.19.4 --acceptlicense")
-        } elseif ($i -eq 'git.install') {
-            iex ("choco install -fy --allow-downgrade '$i' --version 2.10.2 --acceptlicense")
-        } else {
-            iex ("choco install -fy '$i' --acceptlicense")
-        }
-        if( $LASTEXITCODE -ne 0 ) {
-            write-host "Choco Packages Installation Failed" -ForegroundColor Red
-            exit 1
-        }
-    }
-    write-host "Choco Packages Installation Succeeded" -ForegroundColor Green
-}
-
-$packages = [System.Collections.Generic.List[System.Object]]('wget', 'git.install', '7zip', 'unzip', 'cmake', 'pandoc', 'strawberryperl', 'msys2')
-
-if(Test-Path -Path "C:\Program Files\CMake\bin"){
-    # file with path $path does exist
-    $null = $packages.Remove('cmake')
-    write-host "Cmake installed" -ForegroundColor Green
-}
-
-if(Test-Path -Path "C:\Strawberry"){
-    $null = $packages.Remove('strawberryperl')
-    write-host "Strawberry Perl installed" -ForegroundColor Green
-}
-
-if(!(Test-Path -Path "C:\msys64")){
-    $Env:Path += ";C:\tools\msys64\usr\bin"
-    $msys2_path = "C:\tools\msys64\usr\bin"
-    if((Test-Path -Path "C:\tools\msys64")){
-        $null = $packages.Remove('msys2')
-        write-host "MSYS2 64 installed" -ForegroundColor Green
-    }
-} else {
-    $null = $packages.Remove('msys2')
-    write-host "MSYS2 64 installed" -ForegroundColor Green
-    $Env:Path += ";C:\msys64\usr\bin"
-    $msys2_path = "C:\msys64\usr\bin"
-}
-
-choco_pack_install($packages)
-
-# Web installed msys2_64 bit to install make, gcc, perl, diffutils
-
-Function pacman_pack_install($packages) {
-
-    Foreach ($i in $packages){
-        iex ("pacman -S '$i' --noconfirm")
-        if( $LASTEXITCODE -ne 0 ) {
-            write-host "Pacman Packages Installation Failed" -ForegroundColor Red
-            exit 1
-        }
-    }
-    write-host "Pacman Packages Installation Succeeded" -ForegroundColor Green
-}
-
-$packages = [System.Collections.Generic.List[System.Object]]('make', 'gcc', 'perl', 'diffutils')
-
-pacman_pack_install($packages)
-
-# Web Download VSNASM, VSYASM
-
-Function download_file_to_temp($download_name, $url, $output_name) {
-
-    write-host "Downloading $download_name" -ForegroundColor Yellow
-    $output = $env:TEMP + "\$output_name"
-    (New-Object System.Net.WebClient).DownloadFile($url, $output)
-
-    if( $LASTEXITCODE -eq 0 ) {
-        write-host "Download $download_name Succeeded" -ForegroundColor Green
-    } else {
-        write-host "Download $download_name Failed" -ForegroundColor Red
-        exit $LASTEXITCODE
-    }
-}
-
-download_file_to_temp 'VSNASM' "https://github.com/ShiftMediaProject/VSNASM/releases/download/0.5/VSNASM.zip" 'VSNASM.zip'
-download_file_to_temp 'VSYASM' "https://github.com/ShiftMediaProject/VSYASM/releases/download/0.4/VSYASM.zip" 'VSYASM.zip'
-
-# Unzip VSNASM.zip, VSYASM.zip
-
-Function unzip_file_from_temp($unzip_name, $zip_file_name, $unzip_file_output_name) {
-
-    write-host "Unzip $unzip_name" -ForegroundColor Yellow
-    $zip_path = $env:TEMP + "\$zip_file_name"
-    $unzip_path = $env:TEMP + "\$unzip_file_output_name"
-    iex("unzip -o $zip_path -d $unzip_path")
-
-    if( $LASTEXITCODE -eq 0 ) {
-        write-host "Unzip $unzip_name Succeeded" -ForegroundColor Green
-    } else {
-        write-host "Unzip $unzip_name Failed" -ForegroundColor Red
-        exit $LASTEXITCODE
-    }
-}
-
-unzip_file_from_temp 'VSNASM' 'VSNASM.zip' 'VSNASM_UNZIP'
-unzip_file_from_temp 'VSYASM' 'VSYASM.zip' 'VSYASM_UNZIP'
-
-# Generate nasm(VS), yasm.exe (VS)
-
-Function run_batch($batch_cmd, $task_desp) {
-
-    write-host $task_desp -ForegroundColor Yellow
-    Start-Process "cmd.exe" $batch_cmd -Wait -NoNewWindow
-
-    if( $LASTEXITCODE -eq 0 ) {
-        write-host "$task_desp Succeeded" -ForegroundColor Green
-    } else {
-        write-host "$task_desp Failed" -ForegroundColor Red
-        exit $LASTEXITCODE
-    }
-}
-
-$batch_path = "/c set ISINSTANCE=1 && " + $env:TEMP + "\VSNASM_UNZIP\install_script.bat"
-run_batch $batch_path "Generate nasm(VS)"
-
-$batch_path = "/c set ISINSTANCE=1 &&" + $env:TEMP + "\VSYASM_UNZIP\install_script.bat"
-run_batch $batch_path "Generate yasm(VS)"
-
-# Web Download gas-preprocessor.pl, yasm.exe (win64)
-
-download_file_to_temp 'yasm.exe (win64)' "http://www.tortall.net/projects/yasm/releases/yasm-1.3.0-win64.exe" 'yasm.exe'
-download_file_to_temp 'gas-preprocessor.pl' "https://github.com/FFmpeg/gas-preprocessor/blob/master/gas-preprocessor.pl" 'gas-preprocessor.pl'
-
-# Move gas-preprocessor.pl, yasm.exe into msys64
-
-Function move_file_from_temp_to_msys64($file_name, $task_desp) {
-
-    write-host $task_desp -ForegroundColor Yellow
-    $file_path = $env:TEMP + "\$file_name"
-    Move-item -Path $file_path -Destination $msys2_path -Force
-
-    if( $LASTEXITCODE -eq 0 ) {
-        write-host "$task_desp Succeeded" -ForegroundColor Green
-    } else {
-        write-host "$task_desp Failed" -ForegroundColor Red
-        exit $LASTEXITCODE
-    }
-}
-
-move_file_from_temp_to_msys64 'gas-preprocessor.pl' 'Move gas-preprocessor.pl to msys64 folder'
-move_file_from_temp_to_msys64 'yasm.exe' 'Move yasm.exe (win64) to msys64 folder'
-
-write-host "Dependencies Built Finished" -ForegroundColor Green
\ No newline at end of file
diff --git a/scripts/install-deps-windows.ps1 b/scripts/install-deps-windows.ps1
new file mode 100644
index 00000000..4307a659
--- /dev/null
+++ b/scripts/install-deps-windows.ps1
@@ -0,0 +1,220 @@
+<#
+    This script should install dependencies required for building the Jami
+    Qt client on windows.
+
+    Required components not installed:
+    - Visual Studio
+    - build toolchains
+    - SDKs
+    - WiX + WiX Visual Studio extension
+    - Qt + Qt Visual Studio extension
+#>
+
+write-host "Installing jami-qt build dependencies for windows…" -ForegroundColor Green
+
+Set-ExecutionPolicy Bypass -Scope Process -Force
+
+$global:installed_packages = $null
+Function choco_check_package([String] $package, [String] $version = "") {
+    # Query a package listing once
+    if ($null -eq $global:installed_packages) {
+        write-host "Getting installed package list from Chocolatey…" -ForegroundColor DarkCyan
+        $global:installed_packages = choco list -lo
+    }
+    # Check installed packages
+    $result = $global:installed_packages | Where-object {
+        $_.ToLower().StartsWith($package.ToLower())
+    }
+    if ($null -eq $result) {
+        # We don't have the package
+        write-host $package "not found." -ForegroundColor Yellow
+        return $false
+    }
+    if ("" -eq $version) {
+        # We have the package and don't care what version it is
+        write-host $package "found." -ForegroundColor Cyan
+        return $true
+    }
+    # We now check the results for a package of the specified version
+    $parts = $result.Split(' ')
+    Foreach ($part in $parts) {
+        if ($part -eq $version) {
+            # We have the package of the specified version
+            write-host $package $version "found." -ForegroundColor Cyan
+            return $true
+        }
+    }
+    # We don't have the package of the specified version
+    write-host $package $version "not found." -ForegroundColor Yellow
+    return $false
+}
+
+Function install_chocolatey {
+    # Install Chocolatey if not installed already
+    if (!(Test-Path "$($env:ProgramData)\chocolatey\choco.exe")) {
+        Invoke-Expression ((New-Object System.Net.WebClient).DownloadString("https://chocolatey.org/install.ps1"))
+        if ( $LASTEXITCODE -eq 0 ) {
+            write-host "Chocolatey installation succeeded" -ForegroundColor Green
+        }
+        else {
+            write-host "Chocolatey installation failed" -ForegroundColor Red
+            exit $LASTEXITCODE
+        }
+    }
+    else {
+        write-host "Chocolatey already installed" -ForegroundColor DarkGreen
+    }
+}
+
+Function choco_install_package([String] $package, [String] $version = "") {
+    $package_installed = choco_check_package $package $version
+    if ($true -eq $package_installed) {
+        return
+    }
+    if ("" -ne $version) {
+        write-host "Installing" $package "@" $version
+        choco install -fy --allow-downgrade $package --version $version --acceptlicense
+    }
+    else {
+        write-host "Installing" $package
+        choco install -fy --allow-downgrade $package --acceptlicense
+    }
+    if ( $LASTEXITCODE -ne 0 ) {
+        write-host "Choco Packages Installation Failed" -ForegroundColor Red
+        exit 1
+    }
+}
+Function install_choco_packages($packages) {
+    Foreach ($i in $packages) {
+        choco_install_package $i.pkg $i.ver
+    }
+    write-host "Choco Packages Installation Succeeded" -ForegroundColor Green
+}
+
+Function download_file_to_temp($download_name, $url, $output_name) {
+    write-host "Downloading $download_name" -ForegroundColor DarkCyan
+    $output = $env:TEMP + "\$output_name"
+    (New-Object System.Net.WebClient).DownloadFile($url, $output)
+    if ( $LASTEXITCODE -eq 0 ) {
+        write-host "Download $download_name Succeeded" -ForegroundColor Green
+    }
+    else {
+        write-host "Download $download_name Failed" -ForegroundColor Red
+        exit $LASTEXITCODE
+    }
+}
+
+Function unzip_file_from_temp($unzip_name, $zip_file_name, $unzip_file_output_name) {
+    write-host "Unzipping $unzip_name" -ForegroundColor DarkCyan
+    $zip_path = $env:TEMP + "\$zip_file_name"
+    $unzip_path = $env:TEMP + "\$unzip_file_output_name"
+    Invoke-Expression("unzip -o $zip_path -d '$unzip_path'") | Out-Null
+    if ( $LASTEXITCODE -eq 0 ) {
+        write-host "Unzip $unzip_name Succeeded" -ForegroundColor Green
+    }
+    else {
+        write-host "Unzip $unzip_name Failed" -ForegroundColor Red
+        exit $LASTEXITCODE
+    }
+}
+
+Function run_batch($batch_cmd, $task_name) {
+    write-host $task_name -ForegroundColor DarkCyan
+    Start-Process "cmd.exe" $batch_cmd -Wait -NoNewWindow | Out-Null
+    if ( $LASTEXITCODE -eq 0 ) {
+        write-host "$task_name Succeeded" -ForegroundColor Green
+    }
+    else {
+        write-host "$task_name Failed" -ForegroundColor Red
+        exit $LASTEXITCODE
+    }
+}
+
+Function move_file_from_temp_to_msys64($file_name, $task_name) {
+    write-host $task_name -ForegroundColor DarkCyan
+    $file_path = $env:TEMP + "\$file_name"
+    Move-item -Path $file_path -Destination $msys2_path -Force
+    if ($LASTEXITCODE -eq 0) {
+        write-host "$task_name Succeeded" -ForegroundColor Green
+    }
+    else {
+        write-host "$task_name Failed" -ForegroundColor Red
+        exit $LASTEXITCODE
+    }
+}
+
+Function install_msys2_packages($packages) {
+    Foreach ($i in $packages) {
+        Invoke-Expression ("pacman -Q '$i'") | out-null
+        if ($LASTEXITCODE -eq 0) {
+            write-host $i "already installed" -ForegroundColor Cyan
+            continue
+        }
+        Invoke-Expression ("pacman -S '$i' --noconfirm")
+        if ($LASTEXITCODE -ne 0) {
+            write-host "Pacman Packages Installation Failed" -ForegroundColor Red
+            exit 1
+        }
+    }
+    write-host "Pacman Packages Installation Succeeded" -ForegroundColor Green
+}
+
+# Web installed msys2_64 bit to install make, gcc, perl, diffutils
+$msys_packages = @("make", "gcc", "perl", "diffutils")
+
+# Install 7zip, unzip, wget --version 1.19.4, cmake, git --version 2.10.2, pandoc, strawberryperl, msys2
+$choco_packages = @(
+    [pscustomobject]@{pkg = "wget"; ver = "1.19.4" }
+    [pscustomobject]@{pkg = "git.install"; ver = "2.10.2" }
+    [pscustomobject]@{pkg = "7zip"; ver = "" }
+    [pscustomobject]@{pkg = "unzip"; ver = "" }
+    [pscustomobject]@{pkg = "cmake"; ver = "" }
+    [pscustomobject]@{pkg = "pandoc"; ver = "" }
+    [pscustomobject]@{pkg = "strawberryperl"; ver = "" }
+    [pscustomobject]@{pkg = "msys2"; ver = "" }
+)
+
+install_chocolatey
+
+# Check for an existing msys2 install
+# Note that choco installs msys2 in C:/tools/
+if (!(Test-Path -Path "C:\msys64")) {
+    $Env:Path += ";C:\tools\msys64\usr\bin"
+    $msys2_path = "C:\tools\msys64\usr\bin"
+    if ((Test-Path -Path "C:\tools\msys64")) {
+        write-host "MSYS2 64 already installed" -ForegroundColor Green
+    }
+    else {
+        $choco_packages.Add([pscustomobject]@{pkg = "msys2"; ver = "" })
+    }
+}
+else {
+    write-host "MSYS2 64 already installed" -ForegroundColor Green
+    $Env:Path += ";C:\msys64\usr\bin"
+    $msys2_path = "C:\msys64\usr\bin"
+}
+
+install_choco_packages $choco_packages
+install_msys2_packages $msys_packages
+
+# Install VSNASM
+download_file_to_temp 'VSNASM' "https://github.com/ShiftMediaProject/VSNASM/releases/download/0.5/VSNASM.zip" 'VSNASM.zip'
+unzip_file_from_temp 'VSNASM' 'VSNASM.zip' 'VSNASM_UNZIP'
+$batch_path = "/c set ISINSTANCE=1 &&" + $env:TEMP + "\VSNASM_UNZIP\install_script.bat"
+run_batch $batch_path "Install VSNASM"
+
+# Install VSYASM
+download_file_to_temp 'VSYASM' "https://github.com/ShiftMediaProject/VSYASM/releases/download/0.4/VSYASM.zip" 'VSYASM.zip'
+unzip_file_from_temp 'VSYASM' 'VSYASM.zip' 'VSYASM_UNZIP'
+$batch_path = "/c set ISINSTANCE=1 &&" + $env:TEMP + "\VSYASM_UNZIP\install_script.bat"
+run_batch $batch_path "Install VSYASM"
+
+# Install yasm.exe (win64)
+download_file_to_temp 'yasm.exe (win64)' "http://www.tortall.net/projects/yasm/releases/yasm-1.3.0-win64.exe" 'yasm.exe'
+move_file_from_temp_to_msys64 'yasm.exe' 'Move yasm.exe (win64) to msys64 folder'
+
+# Install gas-preprocessor.pl
+download_file_to_temp 'gas-preprocessor.pl' "https://github.com/FFmpeg/gas-preprocessor/blob/master/gas-preprocessor.pl" 'gas-preprocessor.pl'
+move_file_from_temp_to_msys64 'gas-preprocessor.pl' 'Move gas-preprocessor.pl to msys64 folder'
+
+write-host "Done" -ForegroundColor Green
-- 
GitLab