From 68e9c68855aa6b3d4b1d4a40836e836d5f560979 Mon Sep 17 00:00:00 2001 From: Aleksandr Chebotov <47745270+al-cheb@users.noreply.github.com> Date: Fri, 15 Jan 2021 13:42:37 +0300 Subject: [PATCH] [Windows] Add shell symlinks (#2395) * add shell symlinks * fix paths * add a wrapper * fix target output * fix tests --- .../win/post-generation/Msys2FirstLaunch.ps1 | 2 ++ .../scripts/Installers/Configure-Shell.ps1 | 23 +++++++++++++++++++ .../SoftwareReport/SoftwareReport.Common.psm1 | 11 +++++++++ .../SoftwareReport.Generator.ps1 | 4 ++++ images/win/scripts/Tests/Shell.Tests.ps1 | 13 +++++++++++ images/win/windows2016.json | 3 ++- images/win/windows2019.json | 3 ++- 7 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 images/win/post-generation/Msys2FirstLaunch.ps1 create mode 100644 images/win/scripts/Installers/Configure-Shell.ps1 create mode 100644 images/win/scripts/Tests/Shell.Tests.ps1 diff --git a/images/win/post-generation/Msys2FirstLaunch.ps1 b/images/win/post-generation/Msys2FirstLaunch.ps1 new file mode 100644 index 000000000..a0c8e3424 --- /dev/null +++ b/images/win/post-generation/Msys2FirstLaunch.ps1 @@ -0,0 +1,2 @@ +# create user profile at the first launch +$null = cmd /c "C:\msys64\usr\bin\bash.exe -leo pipefail -c 'echo $SHELL' 2>&1" \ No newline at end of file diff --git a/images/win/scripts/Installers/Configure-Shell.ps1 b/images/win/scripts/Installers/Configure-Shell.ps1 new file mode 100644 index 000000000..659ae0fd0 --- /dev/null +++ b/images/win/scripts/Installers/Configure-Shell.ps1 @@ -0,0 +1,23 @@ +# Create shells folder +$shellPath = "C:\shells" +New-Item -Path $shellPath -ItemType Directory | Out-Null + +# add a wrapper for C:\msys64\usr\bin\bash.exe +@' +@echo off +setlocal +IF NOT DEFINED MSYS2_PATH_TYPE set MSYS2_PATH_TYPE=strict +IF NOT DEFINED MSYSTEM set MSYSTEM=mingw64 +set CHERE_INVOKING=1 +C:\msys64\usr\bin\bash.exe -leo pipefail %* +'@ | Out-File -FilePath "$shellPath\msys2bash.cmd" + +# gitbash <--> C:\Program Files\Git\bin\bash.exe +New-Item -ItemType SymbolicLink -Path "$shellPath\gitbash.exe" -Target "$env:ProgramFiles\Git\bin\bash.exe" | Out-Null + +# WSL is available on Windows Server 2019 +if (Test-IsWin19) +{ + # wslbash <--> C:\Windows\System32\bash.exe + New-Item -ItemType SymbolicLink -Path "$shellPath\wslbash.exe" -Target "$env:SystemRoot\System32\bash.exe" | Out-Null +} diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 index d92bdbc6e..746356cd5 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 @@ -304,6 +304,17 @@ function Get-CachedDockerImagesTableData { } } +function Get-ShellTarget { + $shells = Get-ChildItem C:\shells -File | Select-Object Name, @{n="Target";e={ + if ($_.Name -eq "msys2bash.cmd") { + "C:\msys64\usr\bin\bash.exe" + } else { + @($_.Target)[0] + } + }} | Sort-Object Name + $shells | New-MDTable -Columns ([ordered]@{Name = "left"; Target = "left";}) +} + function Get-PacmanVersion { $msys2BinDir = "C:\msys64\usr\bin" $pacmanPath = Join-Path $msys2BinDir "pacman.exe" diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 b/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 index 605b57c21..4cfbde9f7 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1 @@ -142,6 +142,10 @@ $markdown += New-MDList -Style Unordered -Lines @( (Get-SeleniumWebDriverVersion -Driver "iexplorer") ) +$markdown += New-MDHeader "Shells" -Level 3 +$markdown += Get-ShellTarget +$markdown += New-MDNewLine + $markdown += New-MDHeader "MSYS2" -Level 3 $markdown += Get-PacmanVersion $markdown += New-MDNewLine diff --git a/images/win/scripts/Tests/Shell.Tests.ps1 b/images/win/scripts/Tests/Shell.Tests.ps1 new file mode 100644 index 000000000..8809597f2 --- /dev/null +++ b/images/win/scripts/Tests/Shell.Tests.ps1 @@ -0,0 +1,13 @@ +Describe "Shell" { + $shellTestCases = @( + @{Name = "C:\shells\gitbash.exe"; Target = "$env:ProgramFiles\Git\bin\bash.exe"}, + @{Name = "C:\shells\msys2bash.cmd"; Target = $null} + ) + if (Test-IsWin19) { + $shellTestCases += @{Name = "C:\shells\wslbash.exe"; Target = "$env:SystemRoot\System32\bash.exe"} + } + + It " target to " -TestCases $shellTestCases { + (Get-Item $Name).Target | Should -BeExactly $Target + } +} diff --git a/images/win/windows2016.json b/images/win/windows2016.json index 782691b71..9383a2a17 100644 --- a/images/win/windows2016.json +++ b/images/win/windows2016.json @@ -330,7 +330,8 @@ "type": "powershell", "scripts": [ "{{ template_dir }}/scripts/Installers/Install-WindowsUpdates.ps1", - "{{ template_dir }}/scripts/Installers/Configure-DynamicPort.ps1" + "{{ template_dir }}/scripts/Installers/Configure-DynamicPort.ps1", + "{{ template_dir }}/scripts/Installers/Configure-Shell.ps1" ], "elevated_user": "{{user `install_user`}}", "elevated_password": "{{user `install_password`}}" diff --git a/images/win/windows2019.json b/images/win/windows2019.json index 43567f368..39b58c003 100644 --- a/images/win/windows2019.json +++ b/images/win/windows2019.json @@ -323,7 +323,8 @@ "type": "powershell", "scripts": [ "{{ template_dir }}/scripts/Installers/Install-WindowsUpdates.ps1", - "{{ template_dir }}/scripts/Installers/Configure-DynamicPort.ps1" + "{{ template_dir }}/scripts/Installers/Configure-DynamicPort.ps1", + "{{ template_dir }}/scripts/Installers/Configure-Shell.ps1" ], "elevated_user": "{{user `install_user`}}", "elevated_password": "{{user `install_password`}}"