forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-VSCodeSettingsFile.ps1
92 lines (77 loc) · 2.25 KB
/
Get-VSCodeSettingsFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<#
.SYNOPSIS
Gets the path of the VSCode settings.config file.
.OUTPUTS
System.String containing the path of the settings.config file.
.FUNCTIONALITY
VSCode
.LINK
https://code.visualstudio.com/docs/getstarted/settings
.LINK
https://powershell.github.io/PowerShellEditorServices/api/Microsoft.PowerShell.EditorServices.Extensions.EditorObject.html
.LINK
https://git-scm.com/docs/git-rev-parse
.LINK
Join-Path
.LINK
Get-Command
.LINK
Stop-ThrowError.ps1
.EXAMPLE
Get-VSCodeSettingsFile.ps1
C:\Users\zaphodb\AppData\Roaming\Code\User\settings.json
.EXAMPLE
Get-VSCodeSettingsFile.ps1 -Workspace
C:\Users\zaphodb\GitHub\scripts\.vscode\settings.json
#>
[CmdletBinding()][OutputType([string])] Param(
# Indicates that the current workspace settings should be parsed instead of the user settings.
[switch] $Workspace
)
${settings.json} =
if($Workspace)
{
Use-Command.ps1 git "$env:ProgramFiles\Git\cmd\git.exe" -choco git
if(Get-Variable psEditor -Scope Global -ErrorAction Ignore)
{
Join-Path $psEditor.Workspace.Path .vscode/settings.json
}
elseif ((Get-Command git -ErrorAction Ignore) -and "$(git rev-parse --git-dir)")
{
Join-Path "$(git rev-parse --show-toplevel)" .vscode/settings.json
}
else
{
Write-Warning "Can't detect VSCode workspace or git repo. Assuming current directory."
Join-Path "$PWD" .vscode/settings.json
}
}
else
{
if(!(Test-Path variable:IsWindows))
{
Set-Variable IsWindows ($PSVersionTable.PSEdition -eq 'Desktop' -or $env:OS -eq 'Windows_NT')
}
if($IsWindows)
{
# could also try Resolve-Path "$env:APPDATA\Code*\User\settings.json", but this is better targetted
"$env:APPDATA\Code - Insiders\User\settings.json","$env:APPDATA\Code\User\settings.json" |
Where-Object {Test-Path $_ -PathType Leaf} |
Select-Object -First 1
}
elseif($IsLinux)
{
"$HOME/.config/Code/User/settings.json"
}
elseif($IsMacOS)
{
"$HOME/Library/Application Support/Code/User/settings.json"
}
else
{
Stop-ThrowError.ps1 'Unable to determine location of VSCode settings.json' `
-OperationContext "$([environment]::OSVersion)"
}
}
Write-Verbose "Using VSCode settings ${settings.json}"
${settings.json}