From 7d9e4b8598ed661b80897c4ee4dac31e93397be7 Mon Sep 17 00:00:00 2001
From: Moritz Maxeiner <mm@ucw.sh>
Date: Tue, 10 Dec 2019 14:26:28 +0100
Subject: [PATCH] windows: More use of powershell profile, enable toolchain by
 default

- Enable use within Dockerfile RUN commands
- use $pshome/profile.d directory for additional profile files
- move must-run function into $pshome/profile.d/must-run.ps1
- add $pshome/profile.d/vsdevenv.ps1
- enable MSVC 15.9 toolchain by default
---
 windows1809/base/Dockerfile                      | 11 +++++++----
 windows1809/base/powershell/MSVC-15.9.ps1        |  3 +++
 .../{Profile.ps1 => powershell/Must-Run.ps1}     |  1 +
 windows1809/base/powershell/Profile.ps1          |  2 ++
 windows1809/base/powershell/VSDevEnv.ps1         | 16 ++++++++++++++++
 5 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 windows1809/base/powershell/MSVC-15.9.ps1
 rename windows1809/base/{Profile.ps1 => powershell/Must-Run.ps1} (90%)
 create mode 100644 windows1809/base/powershell/Profile.ps1
 create mode 100644 windows1809/base/powershell/VSDevEnv.ps1

diff --git a/windows1809/base/Dockerfile b/windows1809/base/Dockerfile
index 2afe701..9d25512 100644
--- a/windows1809/base/Dockerfile
+++ b/windows1809/base/Dockerfile
@@ -2,11 +2,12 @@
 
 FROM microsoft/dotnet-framework:3.5-runtime-windowsservercore-ltsc2019
 
-SHELL ["powershell", "-NoProfile", "-ExecutionPolicy Bypass", "-Command", "$ErrorActionPreference = 'Stop';"]
+SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
+RUN mkdir $PSHome/profile.d | out-null
+ADD powershell/Profile.ps1 /Windows/System32/WindowsPowerShell/v1.0/Profile.ps1
 
-COPY Profile.ps1 C:/Users/ContainerAdministrator/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
-RUN . $Profile; \
-    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
+ADD powershell/Must-Run.ps1 /Windows/System32/WindowsPowerShell/v1.0/profile.d/Must-Run.ps1
+RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
     Invoke-Expression ((New-Object Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')); \
     Must-Run choco install -y dotnetfx --version 4.7.2.20180712; \
     Must-Run choco install -y cmake --version 3.15.5 --installargs "ADD_CMAKE_TO_PATH=System"; \
@@ -22,6 +23,8 @@ RUN . $Profile; \
     Must-Run choco install -y visualstudio2017buildtools; \
     Must-Run choco install -y visualstudio2017-workload-vctools; \
     Start-Process "\"C:/Program Files (x86)/Microsoft Visual Studio/Installer/vs_installer.exe\"" -NoNewWindow -Wait -ArgumentList 'modify --installPath \"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools\" --quiet --norestart --remove Microsoft.VisualStudio.Component.VC.CMake.Project --add Microsoft.VisualStudio.Component.VC.Tools.14.16 --add Microsoft.VisualStudio.Component.VC.ATL'
+ADD powershell/VSDevEnv.ps1 /Windows/System32/WindowsPowerShell/v1.0/profile.d/VSDevEnv.ps1
+ADD powershell/MSVC-15.9.ps1 /Windows/System32/WindowsPowerShell/v1.0/profile.d/MSVC-15.9.ps1
 
 COPY qtifwsilent.qs C:/qtifwsilent.qs
 RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
diff --git a/windows1809/base/powershell/MSVC-15.9.ps1 b/windows1809/base/powershell/MSVC-15.9.ps1
new file mode 100644
index 0000000..3bfbf4b
--- /dev/null
+++ b/windows1809/base/powershell/MSVC-15.9.ps1
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: MIT
+
+VSDevEnv -arch='amd64' -vcvars_ver='14.16'
diff --git a/windows1809/base/Profile.ps1 b/windows1809/base/powershell/Must-Run.ps1
similarity index 90%
rename from windows1809/base/Profile.ps1
rename to windows1809/base/powershell/Must-Run.ps1
index c8dfc19..34d631c 100644
--- a/windows1809/base/Profile.ps1
+++ b/windows1809/base/powershell/Must-Run.ps1
@@ -1,3 +1,4 @@
+# SPDX-License-Identifier: MIT
 
 #
 # Sets exit to error when the command fails
diff --git a/windows1809/base/powershell/Profile.ps1 b/windows1809/base/powershell/Profile.ps1
new file mode 100644
index 0000000..961a6ed
--- /dev/null
+++ b/windows1809/base/powershell/Profile.ps1
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: MIT
+ls "$pshome/profile.d/*.ps1" | % { . $_.FullName }
diff --git a/windows1809/base/powershell/VSDevEnv.ps1 b/windows1809/base/powershell/VSDevEnv.ps1
new file mode 100644
index 0000000..8dbd927
--- /dev/null
+++ b/windows1809/base/powershell/VSDevEnv.ps1
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: MIT
+
+#
+# Sets up Visual Studio Development environment
+#
+function VSDevEnv {
+    pushd "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/Common7/Tools"
+    cmd /c "VsDevCmd.bat $args & set" |
+    foreach {
+        if ($_ -match "=") {
+            $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
+        }
+    }
+    popd
+}
+
-- 
GitLab