Skip to content

Instantly share code, notes, and snippets.

@michaeltlombardi
Created February 22, 2016 19:53
Show Gist options
  • Save michaeltlombardi/c5c018bf6d562ee1f709 to your computer and use it in GitHub Desktop.
Save michaeltlombardi/c5c018bf6d562ee1f709 to your computer and use it in GitHub Desktop.
Get Visual Studio Code Snippets via PowerShell
function Get-VSCodeSnippet {
<#
.SYNOPSIS
Return the list of Snippets from a VSCode JSON file.
.DESCRIPTION
Given the path to one or more Visual Studio Code JSON files, the function returns all of the associated snippets, their prefix, description, and body, as well as their source file. Useful for discovering snippets after updating an extension or when using it for the first time.
.PARAMETER Path
The full path to one or more Visual Studio Code Snippet JSON files. By default this command will return all of the snippets found in the current user's loaded extensions and in the extensions which shipped directly with Visual Studio Code.
.EXAMPLE
C:\PS> Get-VSCodeSnippet -Path C:\Users\John.Doe\.vscode\extensions\ms-vscode.PowerShell-0.4.1\snippets\PowerShell.json
This command will import the JSON structure of PowerShell.json and convert it into PowerShell Objects representing each snippet.
.INPUTS
System.String
You can pipe a string that contains a path to Get-VSCodeSnippet.
.OUTPUTS
PSCustomObject
Get-VSCodeSnippet returns a PSCustomObject with the properties Prefix (meaning the series of characters which map to the snippet in VSCode), Description, Body (the snippet itself), and Source (the file in which the snippet was discovered).
.NOTES
General notes
#>
[CmdletBinding()]
param(
# Specifies a path to one or more locations. Wildcards are permitted.
[Parameter(Mandatory=$false,
Position=0,
ParameterSetName="Path",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to one or more locations.")]
[ValidateNotNullOrEmpty()]
[SupportsWildcards()]
[string[]]
$Path = [string[]]("$Env:Homedrive\$Env:HomePath\.vscode\extensions\*\snippets\*.json",'C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\*\snippet*\*.json')
)
begin {
#No pre-processing required.
}
process {
ForEach($SnippetsPath in (Get-Item -Path $Path | Select-Object -ExpandProperty PSPath)){
# Plain english Source name stripped of the path and .JSON
$Source = Split-Path -Path $SnippetsPath -Leaf | ForEach-Object {$_.Substring(0,($_.Length)-5)}
# The properties which make up the object(s) returned to the pipeline
$SnippetProperties = @{Name="Source";Expression={$Source}},"Prefix","Description","Body",@{Name="SourcePath";Expression={$SnippetsPath}}
# Load the file into memory, convert from json; because of the structure of these files, the object will load with every snippet as a property of one large object - we output this object to $SnippetsRaw so we can extract the actual snippet data from the query.
$SnippetNames = Get-Content -Path $SnippetsPath |
ConvertFrom-JSON -OutVariable SnippetsRaw |
Get-Member -MemberType NoteProperty |
Select-Object -ExpandProperty Name
ForEach($Name in $SnippetNames){
$SnippetsRaw | Select-Object -ExpandProperty $Name | Select-Object -Property $SnippetProperties
}
}
}
end {
#No post-processing required.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment