Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.VSCode.DSC initial implementation #57

Merged
merged 10 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pipelines/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ extends:
displayName: 'Publish Pipeline GitDsc'
targetPath: $(Build.SourcesDirectory)\resources\GitDsc\
artifactName: GitDsc
- output: pipelineArtifact
displayName: 'Publish Pipeline Microsoft.VSCode.DSC'
targetPath: $(Build.SourcesDirectory)\resources\Microsoft.VSCode.DSC\
artifactName: Microsoft.VSCode.DSC

steps:
- checkout: self
Expand Down
24 changes: 24 additions & 0 deletions resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@{
RootModule = 'Microsoft.VSCode.DSC.psm1'
ModuleVersion = '0.1.0'
GUID = 'baf2c585-d931-4089-8500-93a5b8de1741'
Author = 'Microsoft Corporation'
CompanyName = 'Microsoft Corporation'
Copyright = '(c) Microsoft Corporation. All rights reserved.'
Description = 'DSC Resource for Visual Studio Code'
PowerShellVersion = '7.2'
DscResourcesToExport = @(
'VSCodeExtension'
)
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @(
'PSDscResource_VSCodeExtension'
)

# Prerelease string of this module
Prerelease = 'alpha'
}
}
}
191 changes: 191 additions & 0 deletions resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest

enum VSCodeEnsure
{
Absent
Present
}

#region DSCResources
[DSCResource()]
class VSCodeExtension
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
{
[DscProperty(Key)]
[string] $Name

[DscProperty()]
[string] $Version

[DscProperty()]
[VSCodeEnsure] $Ensure = [VSCodeEnsure]::Present
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

static [hashtable] $InstalledExtensions

static VSCodeExtension()
{
[VSCodeExtension]::GetInstalledExtensions()
}

static [VSCodeExtension[]] Export()
{
$extensionList = (Invoke-VSCode -Command "--list-extensions --show-versions") -Split [Environment]::NewLine
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

$results = [VSCodeExtension[]]::new($extensionList.length)

for ($i = 0; $i -lt $extensionList.length; $i++)
{
$extensionName, $extensionVersion = $extensionList[$i] -Split '@'
$results[$i] = [VSCodeExtension]@{
Name = $extensionName
Version = $extensionVersion
}
}

return $results
}

[VSCodeExtension] Get()
{
$currentState = [VSCodeExtension]::InstalledExtensions[$this.Name]
if ($null -ne $currentState)
{
return $currentState
}

return [VSCodeExtension]@{
Name = $this.Name
Version = $this.Version
Ensure = [VSCodeEnsure]::Absent
}
}

[bool] Test()
{
$currentState = $this.Get()
if ($currentState.Ensure -ne $this.Ensure)
{
return $false
}

if ($null -ne $this.Version -and $this.Version -ne $currentState.Version)
{
return $false
}

return $true
}

[void] Set()
{
if ($this.Test())
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
{
return
}

if ($this.Ensure -eq [VSCodeEnsure]::Present)
{
$this.Install($false)
}
else
{
$this.Uninstall($false)
}
}

#region VSCodeExtension helper functions
static [void] GetInstalledExtensions()
{
[VSCodeExtension]::InstalledExtensions = @{}
foreach ($extension in [VSCodeExtension]::Export())
{
[VSCodeExtension]::InstalledExtensions[$extension.Name] = $extension
}
}

[string] GetInstallArgument()
{
if ($null -eq $this.Version)
{
return $this.Name
}

return @(
$this.Name
$this.Version
) -join '@'
}

[void] Install([bool] $preTest)
{
if ($preTest -and $this.Test())
{
return
}

Install-VSCodeExtension -Name $this.Name -Version $this.Version
[VSCodeExtension]::GetInstalledExtensions()
}

[void] Install()
{
$this.Install($true)
}

[void] Uninstall([bool] $preTest)
{
Uninstall-VSCodeExtension -Name $this.Name
[VSCodeExtension]::GetInstalledExtensions()
}

[void] Uninstall()
{
$this.Uninstall($true)
}
#endregion VSCodeExtension helper functions
}
#endregion DSCResources

function Install-VSCodeExtension
{
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$Name,
[Parameter(ValueFromPipelineByPropertyName)]
[string]$Version
)

Invoke-VSCode -Command "--install-extension $($this.GetInstallArgument())"
}
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

function Uninstall-VSCodeExtension
{
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$Name
)

Invoke-VSCode -Command "--uninstall-extension $($this.Name)"
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
}

function Invoke-VSCode
{
param (
[Parameter(Mandatory = $true)]
[string]$Command
)

try
{
Invoke-Expression "& `"$env:LocalAppData\Programs\Microsoft VS Code\bin\code.cmd`" $Command"
}
catch
{
throw "VSCode is not installed."
}
}