-
Notifications
You must be signed in to change notification settings - Fork 29
/
Set-VSCodeSetting.ps1
53 lines (39 loc) · 1.73 KB
/
Set-VSCodeSetting.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
<#
.SYNOPSIS
Sets a VSCode setting.
.FUNCTIONALITY
VSCode
.LINK
https://code.visualstudio.com/docs/getstarted/settings
.LINK
Get-VSCodeSettingsFile.ps1
.LINK
Set-Json.ps1
.EXAMPLE
Set-VSCodeSetting.ps1 git.autofetch $true -Workspace
Sets {"git.autofetch": true} in the VSCode user settings.
.EXAMPLE
Set-VSCodeSetting.ps1 /powershell.codeFormatting.preset Allman -Workspace
Sets {"powershell.codeFormatting.preset": "Allman"} in the VSCode workspace settings.
.EXAMPLE
Set-VSCodeSetting.ps1 /workbench.colorTheme 'PowerShell ISE' -Workspace
Sets {"workbench.colorTheme": "PowerShell ISE"} in the VSCode workspace settings.
#>
[CmdletBinding()][OutputType([void])] Param(
<#
The full path name of the property to set, as a JSON Pointer, which separates each nested
element name with a /, and literal / is escaped as ~1, and literal ~ is escaped as ~0.
#>
[Parameter(Position=0,Mandatory=$true)][Alias('Name')][AllowEmptyString()][ValidatePattern('\A(?:|/(?:[^~]|~0|~1)*)\z')]
[string] $JsonPointer = '',
# The value of the setting to set.
[Parameter(Position=1,Mandatory=$true)][AllowEmptyString()][AllowEmptyCollection()][AllowNull()]
[psobject] $Value,
# Indicates that the current workspace settings should be set, rather than the user settings.
[switch] $Workspace
)
${settings.json} = Get-VSCodeSettingsFile.ps1 -Workspace:$Workspace
if(!(${settings.json} |Split-Path |Test-Path -PathType Container)) {mkdir (${settings.json} |Split-Path) |Out-Null}
if(!(Test-Path ${settings.json} -PathType Leaf)) {'{}' |Out-File ${settings.json} -Encoding utf8}
$settings = Get-Content ${settings.json} -Raw
$settings |Set-Json.ps1 $JsonPointer $Value -WarnOverwrite |Out-File ${settings.json} -Encoding utf8