Created
April 22, 2019 09:40
-
-
Save lski/ec926b57dd7392c7455d38189ff5ff39 to your computer and use it in GitHub Desktop.
A backup and restore script for VSCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$root = if($PSScriptRoot) { $PSScriptRoot } else { '.' } | |
function Backup-VsCode( | |
[string] | |
$backupDir = "$root" | |
) { | |
if($null -ne ($code = FindVsCodeExe)) { | |
Extract-VsCodeSettings -backupDir $backupDir | |
Extract-VscodeExtensions -extensionsFile "$backupDir\extensions.txt" -codeExe $code | |
} | |
else { | |
Write-Host "WARNING: Could not find the VSCode executable.`nPlease backup the settings/keybindings/extensions manually" -Foregroundcolor RED | |
} | |
} | |
function Install-VsCode( | |
[string] | |
$restoreDir = "$root" | |
) { | |
# You can change this installer to anything you like really | |
choco install visualstudiocode -y | |
# A choco function to ensure, as much as possible, the PATH has been updated | |
refreshenv | |
if($null -ne ($code = FindVsCodeExe)) { | |
# Ensure that code has loaded at least once to allow it to add the settings/extensions | |
start-process PowerShell.exe -windowstyle hidden -ArgumentList ("& '$code'") | |
TIMEOUT /T 20 | |
if($null -ne $restoreDir) { | |
Write-Verbose "Attempting to restore extensions and settings" | |
Install-VsCodeExtensions -file "$restoreDir\extensions.txt" -codeExe $code | |
Install-VsCodeSettings -restoreDir $restoreDir | |
} | |
} | |
else { | |
Write-Host "WARNING: Could not find the VSCode executable.`nPlease update the settings/keybindings/extensions manually" -Foregroundcolor RED | |
} | |
} | |
## Helper functions | |
function Extract-VscodeExtensions( | |
[string] | |
$extensionsFile = "$root\extensions.txt", | |
[string] | |
$codeExe = "code" | |
) { | |
iex "$codeExe --list-extensions" | % { "$_" } | Add-Content $extensionsFile | |
} | |
function Extract-VsCodeSettings( | |
[string] | |
$backupDir = "$root" | |
) { | |
Copy-Item "$Env:APPDATA\Code\User\settings.json" -Destination "$backupDir\settings.json" -Force | |
Copy-Item "$Env:APPDATA\Code\User\keybindings.json" -Destination "$backupDir\keybindings.json" -Force | |
} | |
function Install-VsCodeSettings( | |
[string] | |
$restoreDir = "$root" | |
) { | |
Copy-Item "$restoreDir\settings.json" -Destination "$Env:APPDATA\Code\User\settings.json" -Force | |
Copy-Item "$restoreDir\keybindings.json" -Destination "$Env:APPDATA\Code\User\keybindings.json" -Force | |
} | |
function Install-VsCodeExtensions( | |
[string]$file = "$root\extentions.txt", | |
[string]$codeExe = "code" | |
) { | |
Get-COntent $file | % { & $codeExe --install-extension $_ } | |
} | |
function FindVsCodeExe() { | |
if($code = (SearchVsCode 'C:\')) { | |
return $code | |
} | |
elseif ($code = (SearchVsCode $env:SCOOP)) { | |
return $code | |
} | |
elseif ($code = (SearchVsCode $env:ChocolateyToolsLocation)) { | |
return $code | |
} | |
return $null | |
} | |
function SearchVsCode([string]$path) { | |
return ( | |
Get-ChildItem -Path $path -Filter code.exe -Recurse -ErrorAction SilentlyContinue -Force ` | |
| %{ $_.FullName } ` | |
| where { $_ -match "vs" } ` | |
| Select -First 1 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment