Skip to content

Commit

Permalink
Merge pull request #25 from abcarcol/patch-14
Browse files Browse the repository at this point in the history
Improved profile restoring
  • Loading branch information
albertdb authored May 2, 2022
2 parents 28744ea + 210bf96 commit 1c7ad3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
26 changes: 15 additions & 11 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions profile.ps1
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -28,6 +29,4 @@ function dockerw-start
{
& $dockerBinaries\start.ps1
}

# Start the VPN support
wsl -d wsl-vpnkit service wsl-vpnkit start 2> $null
#endregion
54 changes: 47 additions & 7 deletions uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +130,7 @@ function RemoveWslVpnKit
RemoveDockerD
UninstallDockerAccessHelper
RestorePowershellProfile
RestoreGitBashProfile
DeleteStartScript
RemoveWslVpnKit
UninstallRancherDesktop
Expand Down

0 comments on commit 1c7ad3c

Please sign in to comment.