Skip to content

Commit

Permalink
Merge pull request #136 from szymonos/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
szymonos authored Oct 11, 2023
2 parents 353696a + 73260a8 commit 81ed870
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
32 changes: 20 additions & 12 deletions wsl/wsl_install.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Install and set up the specified WSL distro.
Expand Down Expand Up @@ -64,20 +65,28 @@ begin {
}

process {
# *Check if WSL is updated
wsl.exe --update

# *Check the current default version
$wslDefaultVersion = Get-ItemPropertyValue -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss' -Name 'DefaultVersion' -ErrorAction SilentlyContinue
if ($wslDefaultVersion -ne 2) {
Write-Warning 'You are currently using WSL version 1 as default.'
if ((Read-Host -Prompt 'Would you like to switch to WSL 2 (recommended)? [Y/n]') -ne 'n') {
Write-Host 'Setting the default version to WSL 2.'
wsl.exe --set-default-version 2
} else {
Write-Host 'Keeping the default WSL 1 version.'
}
}

# *Install PowerShell
try {
Get-Command pwsh.exe -CommandType Application | Out-Null
} catch {
$scriptPath = Resolve-Path wsl/pwsh_setup.ps1
if (Test-IsAdmin) {
& $scriptPath
# update environment paths
Update-SessionEnvironmentPath
} else {
Start-Process powershell.exe "-NoProfile -File `"$scriptPath`"" -Verb RunAs
Write-Host "`nInstalling PowerShell Core. Complete the installation and run the script again!`n" -ForegroundColor Yellow
exit 0
}
wsl/pwsh_setup.ps1
# update environment paths
Update-SessionEnvironmentPath
}

# *Set up WSL
Expand All @@ -91,10 +100,9 @@ process {
$reposStr = $Repos | Join-Str -Separator ',' -SingleQuote
$sb.Append(" -Repos @($reposStr)") | Out-Null
}
if ($AddCertificate) { $sb.Append(" -AddCertificate") | Out-Null }
if ($AddCertificate) { $sb.Append(' -AddCertificate') | Out-Null }
$sb.Append(" -OmpTheme 'base'") | Out-Null
# run the wsl_setup script
Write-Host '*** WSL Setup ***' -ForegroundColor White
pwsh.exe -NoProfile -Command $sb.ToString()
}

Expand Down
24 changes: 19 additions & 5 deletions wsl/wsl_setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ begin {
$lxss = Get-WslDistro | Where-Object Name -NotMatch '^docker-desktop'
if ($PsCmdlet.ParameterSetName -ne 'Update') {
if ($Distro -notin $lxss.Name) {
$onlineDistros = Get-WslDistro -Online
for ($i = 0; $i -lt 5; $i++) {
if ($onlineDistros = Get-WslDistro -Online) { break }
}
# install online distro
if ($Distro -in $onlineDistros.Name) {
Write-Host "`nspecified distribution not found ($Distro), proceeding to install..." -ForegroundColor Cyan
Expand All @@ -160,6 +162,19 @@ begin {
}
}
# *perform initial distro setup
# disable appending Windows path inside distro to fix mounting issues
$lxss = Get-WslDistro -FromRegistry | Where-Object Name -EQ $Distro
# check if current version is V2
if ($lxss.Version -eq 1) {
Write-Warning "The distribution `"$Distro`" is currently using version 1."
if ((Read-Host -Prompt 'Would you like to convert it to version 2 (recommended)? [Y/n]') -ne 'n') {
Write-Host 'Converting the distro to version 2.'
wsl --set-version $Distro 2
$lxss.Version = 2
} else {
Write-Host 'Keeping the version 1 for the distribution.'
}
}
# enable automount in wsl.conf
$param = @{
Distro = $Distro
Expand All @@ -171,10 +186,9 @@ begin {
}
}
Set-WslConf @param
# disable appending Windows path inside distro to fix mounting issues
$lxss = Get-WslDistro -FromRegistry | Where-Object Name -EQ $Distro
if ($lxss -and $lxss.Flags -ne 13) {
Set-ItemProperty -Path $lxss.PSPath -Name 'Flags' -Value 13
if ($lxss -and $lxss.Flags -notin @(5, 13)) {
$flag = $lxss.Version -eq 1 ? 5 : 13
Set-ItemProperty -Path $lxss.PSPath -Name 'Flags' -Value $flag
Write-Host "`nrestarting WSL to apply changes..." -ForegroundColor Cyan
wsl.exe --shutdown $Distro
}
Expand Down

0 comments on commit 81ed870

Please sign in to comment.