diff --git a/install.ps1 b/install.ps1 index 1485b82..0ec5c7b 100644 --- a/install.ps1 +++ b/install.ps1 @@ -11,7 +11,6 @@ param( $script:rancherDesktopExe = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\Rancher Desktop.exe" $script:dockerFilesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin" -$script:profilePath = "C:\Users\$env:UserName\Documents\WindowsPowerShell\old-profile.ps1" $script:panicFilePath = "C:\ProgramData\docker\panic.log" $script:dockerPackageUrl = "https://download.docker.com/win/static/stable/x86_64/docker-20.10.8.zip" $script:rancherDesktopUrl = "https://github.com/rancher-sandbox/rancher-desktop/releases/download/v1.1.1/Rancher.Desktop.Setup.1.1.1.exe" @@ -128,24 +127,23 @@ function CreatePowershellProfile { New-Item -Type File -Path $PROFILE -Force } - - Copy-Item $PROFILE "$script:profilePath" - + Write-Host "" >> $PROFILE - Get-Content "profile.ps1" >> "$PROFILE" + Add-Content $PROFILE (Get-Content "profile.ps1") . $PROFILE } function UpdateGitBashProfile { - $search = (Get-Content $script:bashProfilePath | Select-String -Pattern '# generated by rd-installer for Alias support in Git Bash').Matches.Success + $search = (Get-Content $script:bashProfilePath | Select-String -Pattern '#region generated by rd-installer for Alias support in Git Bash').Matches.Success if( -Not $search){ - Add-Content $script:bashProfilePath "# generated by rd-installer for Alias support in Git Bash" + Add-Content $script:bashProfilePath "#region generated by rd-installer for Alias support in Git Bash" Add-Content $script:bashProfilePath "alias docker=""nerdctl""" Add-Content $script:bashProfilePath "alias docker-compose=""nerdctl compose""" Add-Content $script:bashProfilePath "alias dockerw=""/c/Users/$env:UserName/AppData/Local/Programs/Rancher\ Desktop/resources/resources/win32/bin/docker.exe --context win""" Add-Content $script:bashProfilePath "alias dockerw-compose=""/c/Users/$env:UserName/AppData/Local/Programs/Rancher\ Desktop/resources/resources/win32/bin/docker-compose.exe --context win""" + Add-Content $script:bashProfilePath "#endregion" } } @@ -201,22 +199,28 @@ function ActivateWslVpnkit Remove-Item wsl-vpnkit.tar.gz -Force - $search = (Get-Content $PROFILE | Select-String -Pattern '# generated by rd-installer for VPN support in Powershell').Matches.Success + $search = (Get-Content $PROFILE | Select-String -Pattern '#region generated by rd-installer for VPN support in Powershell').Matches.Success if( -Not $search){ - Add-Content $PROFILE "# generated by rd-installer for VPN support in Powershell" + Add-Content $PROFILE "#region generated by rd-installer for VPN support in Powershell" Add-Content $PROFILE "# Start the VPN support" Add-Content $PROFILE "wsl -d wsl-vpnkit service wsl-vpnkit start 2> `$null" + Add-Content $PROFILE "#endregion" + Add-Content $PROFILE "" + } - $search = (Get-Content $script:bashProfilePath | Select-String -Pattern '# generated by rd-installer for VPN support in Git Bash').Matches.Success + $search = (Get-Content $script:bashProfilePath | Select-String -Pattern '#region generated by rd-installer for VPN support in Git Bash').Matches.Success if( -Not $search){ - Add-Content $script:bashProfilePath "# generated by rd-installer for VPN support in Git Bash" + Add-Content $script:bashProfilePath "#region generated by rd-installer for VPN support in Git Bash" Add-Content $script:bashProfilePath "# Start the VPN support" Add-Content $script:bashProfilePath "wsl -d wsl-vpnkit service wsl-vpnkit start 2> /dev/null" + Add-Content $script:bashProfilePath "#endregion" + Add-Content $script:bashProfilePath "" } Write-Host "VPN tool activated." -ForegroundColor Green } + function ChangeFilePermissions { $isReadOnly = Get-ItemProperty -Path $script:panicFilePath 2> $null | Select-Object IsReadOnly diff --git a/profile.ps1 b/profile.ps1 index a4c1a88..724dbdd 100644 --- a/profile.ps1 +++ b/profile.ps1 @@ -1,3 +1,4 @@ +#region generated by rd-installer for Alias support in Powershell # Path to the docker binaries $dockerBinaries = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin" @@ -28,6 +29,4 @@ function dockerw-start { & $dockerBinaries\start.ps1 } - -# Start the VPN support -wsl -d wsl-vpnkit service wsl-vpnkit start 2> $null \ No newline at end of file +#endregion diff --git a/uninstall.ps1 b/uninstall.ps1 index 888168d..9d06c0d 100644 --- a/uninstall.ps1 +++ b/uninstall.ps1 @@ -4,7 +4,7 @@ $script:rancherDesktopUninstallExe = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\Uninstall Rancher Desktop.exe" $script:dockerFilesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin" -$script:profilePath = "C:\Users\$env:UserName\Documents\WindowsPowerShell\old-profile.ps1" +$script:bashProfilePath = "C:\Users\$env:UserName\.bash_profile" #endregion @@ -45,13 +45,52 @@ function UninstallDockerAccessHelper function RestorePowershellProfile { - Write-Host "Removing the alias from your computer..." -ForegroundColor Green - - New-Item -Type File -Path $PROFILE -Force - Get-Content "${script:profilePath}" >> $PROFILE - Remove-Item -Path "${script:profilePath}" -Force + Write-Host "Restoring PowerShell Profile from your computer..." -ForegroundColor Green + + $startMarker = Select-String -Path $PROFILE -Pattern "#region generated by rd-installer" + + if ($startMarker -ne $null) + { + #Delete rd-installer blocks from profile + Set-Content -Path $PROFILE -Value ( (get-content -Path $PROFILE -Raw) -replace '#region generated by rd-installer(?s)(.*)#endregion','' ) + } + + else + { + #Delete profile content from old installations (TO REMOVE after adoption of profiles with regions) + Set-Content -Path $PROFILE -Value (get-content -Path $PROFILE | Select-String -Pattern 'docker' -NotMatch) + Set-Content -Path $PROFILE -Value (get-content -Path $PROFILE | Select-String -Pattern 'nerdctl' -NotMatch) + Set-Content -Path $PROFILE -Value (get-content -Path $PROFILE | Select-String -Pattern '# Start the VPN support' -NotMatch) + Set-Content -Path $PROFILE -Value (get-content -Path $PROFILE | Select-String -Pattern 'wsl -d wsl-vpnkit' -NotMatch) + Set-Content -Path $PROFILE -Value (get-content -Path $PROFILE -Raw | Select-String -Pattern '{*\n.*}' -NotMatch) + } + + Write-Host "PowerShell Profile restored successfully." -ForegroundColor Green +} - Write-Host "Profile restored successfully." -ForegroundColor Green +function RestoreGitBashProfile +{ + Write-Host "Restoring GitBash Profile from your computer..." -ForegroundColor Green + + $startMarker = Select-String -Path $script:bashProfilePath -Pattern "#region generated by rd-installer" + + if ($startMarker -ne $null) + { + #Delete rd-installer blocks from profile + Set-Content -Path $script:bashProfilePath -Value ( (get-content -Path $script:bashProfilePath -Raw) -replace '#region generated by rd-installer(?s)(.*)#endregion','' ) + } + + else + { + #Delete profile content from old installations (TO REMOVE after adoption of profiles with regions) + Set-Content -Path $script:bashProfilePath -Value (get-content -Path $script:bashProfilePath | Select-String -Pattern 'rd-installer' -NotMatch) + Set-Content -Path $script:bashProfilePath -Value (get-content -Path $script:bashProfilePath | Select-String -Pattern 'alias docker' -NotMatch) + Set-Content -Path $script:bashProfilePath -Value (get-content -Path $script:bashProfilePath | Select-String -Pattern '# Start the VPN support' -NotMatch) + Set-Content -Path $script:bashProfilePath -Value (get-content -Path $script:bashProfilePath | Select-String -Pattern 'wsl -d wsl-vpnkit' -NotMatch) + + } + + Write-Host "GitBash Profile restored successfully." -ForegroundColor Green } function DeleteStartScript @@ -91,6 +130,7 @@ function RemoveWslVpnKit RemoveDockerD UninstallDockerAccessHelper RestorePowershellProfile +RestoreGitBashProfile DeleteStartScript RemoveWslVpnKit UninstallRancherDesktop