-
-
Save gerane/ad7a9596c25c4fccfa5ab715c42b0214 to your computer and use it in GitHub Desktop.
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
function Get-TargetResource | |
{ | |
[CmdletBinding()] | |
[OutputType([System.Collections.Hashtable])] | |
param | |
( | |
[parameter(Mandatory = $true)] | |
[System.String] | |
$Name, | |
[Parameter()] | |
[Bool] $Insiders | |
) | |
$Configuration = @{ | |
Name = $Name | |
Insiders = $Insiders | |
} | |
try | |
{ | |
if ($vsCodeInstall = Get-VSCodeInstall -Insiders $Insiders) | |
{ | |
if ([version] $vsCodeInstall.DisplayVersion -ge [version]'1.2.0') | |
{ | |
Write-Verbose -Message "Getting a list of installed extensions." | |
$installedExtensions = Get-InstalledExtension -Insiders $Insiders | |
if ($installedExtensions.Name -contains $Name) | |
{ | |
Write-Verbose -Message "${Name} extension is installed." | |
$Configuration.Add('Ensure','Present') | |
$Configuration | |
} | |
else | |
{ | |
Write-Verbose -Message "${Name} extension is not installed." | |
$Configuration.Add('Ensure','Absent') | |
$Configuration | |
} | |
} | |
else | |
{ | |
throw 'VS Code version must be at least 1.2.0' | |
} | |
} | |
} | |
catch | |
{ | |
Write-Error $_ | |
} | |
} | |
function Set-TargetResource | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[parameter(Mandatory = $true)] | |
[System.String] | |
$Name, | |
[Parameter()] | |
[Bool] $Insiders, | |
[ValidateSet("Present","Absent")] | |
[System.String] | |
$Ensure = "Present" | |
) | |
try | |
{ | |
$vsCodeInstall = Get-VSCodeInstall -Insiders $Insiders | |
if ($Ensure -eq 'Present') | |
{ | |
$commandLine = "/c `"$($vsCodeInstall.InstallLocation)\bin\code*.cmd`" --install-extension ${Name}" | |
try | |
{ | |
Write-Verbose -Message "Installing ${Name} extension ..." | |
Start-Process -FilePath "cmd" -ArgumentList $commandLine -Wait -WindowStyle Hidden | |
if ((Get-InstalledExtension -Insiders $Insiders).Name -contains $Name) | |
{ | |
Write-Verbose 'Extension install complete. Restart VS code if open.' | |
} | |
else | |
{ | |
throw 'Extension install failed' | |
} | |
} | |
catch | |
{ | |
Write-Error $_ | |
} | |
} | |
else | |
{ | |
$commandLine = "/c `"$($vsCodeInstall.InstallLocation)\bin\code*.cmd`" --uninstall-extension ${Name}" | |
try | |
{ | |
Write-Verbose -Message "Uninstalling ${Name} extension ..." | |
Start-Process -FilePath "cmd" -ArgumentList $commandLine -Wait -WindowStyle Hidden | |
if ((Get-InstalledExtension -Insiders $Insiders).Name -contains $Name) | |
{ | |
throw 'Extension uninstall failed' | |
} | |
else | |
{ | |
Write-Verbose 'Extension uninstall complete. Restart VS code if open.' | |
} | |
} | |
catch | |
{ | |
Write-Error $_ | |
} | |
} | |
} | |
catch | |
{ | |
Write-Error $_ | |
} | |
} | |
function Test-TargetResource | |
{ | |
[CmdletBinding()] | |
[OutputType([System.Boolean])] | |
param | |
( | |
[parameter(Mandatory = $true)] | |
[System.String] | |
$Name, | |
[ValidateSet("Present","Absent")] | |
[System.String] | |
$Ensure = "Present" | |
) | |
try | |
{ | |
if ($vsCodeInstall = Get-VSCodeInstall -Insiders $Insiders) | |
{ | |
if ([version] $vsCodeInstall.DisplayVersion -ge [version]'1.2.0') | |
{ | |
Write-Verbose -Message "Getting a list of installed extensions." | |
$installedExtensions = Get-InstalledExtension -Insiders $Insiders | |
if ($installedExtensions.Name -contains $Name) | |
{ | |
if ($Ensure -eq 'Present') | |
{ | |
Write-Verbose -Message 'Extension is already installed. No action needed.' | |
return $true | |
} | |
else | |
{ | |
Write-Verbose -Message 'Extension is installed. It will be removed.' | |
return $false | |
} | |
} | |
else | |
{ | |
if ($Ensure -eq 'Present') | |
{ | |
Write-Verbose -Message 'Extension is not installed. It will be installed.' | |
return $false | |
} | |
else | |
{ | |
Write-Verbose -Message 'Extension is not installed. No action not needed.' | |
return $true | |
} | |
} | |
} | |
else | |
{ | |
Write-Verbose 'VS Code install 1.2.0 not found. Set will be skipped.' | |
return $true | |
} | |
} | |
else | |
{ | |
Write-Verbose 'VS Code install not found. Set will be skipped.' | |
return $true | |
} | |
} | |
catch | |
{ | |
Write-Error $_ | |
} | |
} | |
Function Get-InstalledExtension | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter()] | |
[Bool] $Insiders | |
) | |
if ($Insiders) | |
{ | |
$DotPath = '.vscode-insiders' | |
} | |
else | |
{ | |
$DotPath = '.vscode' | |
} | |
$extensionList = @() | |
$extensionPath = "$env:HOMEPATH\$DotPath\extensions" | |
if (Test-Path $extensionPath) | |
{ | |
foreach ($item in (Get-ChildItem $extensionPath)) | |
{ | |
$packageObject = Get-Content -Path "$($item.FullName)\package.json" -Raw -ErrorAction SilentlyContinue | ConvertFrom-Json | |
if ($packageObject) | |
{ | |
$extension = @{} | |
$extension.Add('Name',"$($packageObject.Publisher).$($packageObject.Name)") | |
$extension.Add('Version',$packageObject.version) | |
$extensionList += $extension | |
} | |
else | |
{ | |
Write-Verbose -Message 'No extensions installed.' | |
} | |
} | |
} | |
else | |
{ | |
Write-Verbose -Message "${extensionPath} does not exist. Creating it." | |
$null = New-Item -Path $extensionPath -ItemType Directory -Force | |
} | |
if ($extensionList) | |
{ | |
return $extensionList | |
} | |
} | |
#Function Wait-ForExtensionInstall | |
#{ | |
# [CmdletBinding()] | |
# param ( | |
# [String] | |
# $Name, | |
# | |
# [UInt64] | |
# $RetryIntervalSec = 10, | |
# | |
# [UInt32] | |
# $RetryCount = 10 | |
# ) | |
# | |
# $extensionInstalled = $false | |
# | |
# for ($count = 0; $count -lt $RetryCount; $count++) | |
# { | |
# Write-Verbose "Retry count: $($count+1); waiting for $RetryIntervalSec seconds" | |
# $installedExtensions = Get-InstalledExtension | |
# if ($installedExtensions.Name -contains $Name) | |
# { | |
# $extensionInstalled = $true | |
# break | |
# } | |
# else | |
# { | |
# Start-Sleep -Seconds $RetryIntervalSec | |
# } | |
# } | |
# | |
# if (!$extensionInstalled) | |
# { | |
# throw "$Name extension installed failed" | |
# } | |
# else | |
# { | |
# return $extensionInstalled | |
# } | |
#} | |
Function Get-VSCodeInstall | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter()] | |
[Bool] $Insiders | |
) | |
if ($Insiders) | |
{ | |
$DisplayName = 'Microsoft Visual Studio Code Insiders' | |
} | |
else | |
{ | |
$DisplayName = 'Microsoft Visual Studio Code' | |
} | |
switch ($env:PROCESSOR_ARCHITECTURE) | |
{ | |
'AMD64' { $UninstallKey = 'HKLM:\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*' } | |
'x86' { $UninstallKey = 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*' } | |
} | |
$products = Get-ItemProperty -Path $UninstallKey | Select DisplayName, DisplayVersion, InstallLocation | |
if ($products.DisplayName -contains $DisplayName) | |
{ | |
return $products.Where({$_.DisplayName -eq $DisplayName}) | |
} | |
} | |
Export-ModuleMember -Function *-TargetResource | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment