From 7cfc7d236ea4ab40700e7e7eb2678cdd5c0b6968 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Tue, 10 Sep 2024 08:26:15 -0700 Subject: [PATCH 1/8] initial implementation for VSCode dsc --- .../Microsoft.VSCode.DSC.psd1 | 24 ++++ .../Microsoft.VSCode.DSC.psm1 | 130 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 create mode 100644 resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 new file mode 100644 index 00000000..70ed4fe6 --- /dev/null +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 @@ -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' + } +} +} diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 new file mode 100644 index 00000000..4ffb549f --- /dev/null +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 @@ -0,0 +1,130 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +$ErrorActionPreference = "Stop" +Set-StrictMode -Version Latest + +enum Ensure +{ + Absent + Present +} + +#region DSCResources +[DSCResource()] +class VSCodeExtension +{ + # Key required. Do not set. + [DscProperty(Key)] + [string] $Name + + [DscProperty()] + [string] $Version + + [DscProperty()] + [Ensure] $Ensure = [Ensure]::Present + + [DscProperty(NotConfigurable)] + [hashtable] $InstalledExtensions = @{} + + [VSCodeExtension] Get() + { + # Get all installed extensions + # Check if visual studio code is installed. + + Assert-VSCode + + $currentState = [VSCodeExtension]::new() + $currentState.Ensure = [Ensure]::Absent + $currentState.Name = $this.Name + $currentState.Version = $this.Version + $extensionList = (Invoke-VSCode -Command "--list-extensions --show-versions") -Split [Environment]::NewLine + + foreach ($extension in $extensionList) + { + $info = $extension -Split '@' + $extensionName = $info[0] + $extensionVersion = $info[1] + $currentState.InstalledExtensions[$extensionName] = $extensionVersion + } + + foreach ($extension in $currentState.InstalledExtensions.Keys) + { + if ($extension -eq $this.Name) + { + # if version parameter is provided. + if ($null -ne $this.Version) + { + if ($this.Version -eq $currentState.InstalledExtensions[$this.Name]) + { + $currentState.Ensure = [Ensure]::Present + } + } + else + { + $currentState.Ensure = [Ensure]::Present + } + + break + } + } + + return $currentState + } + + [bool] Test() + { + $currentState = $this.Get() + return $currentState.Ensure -eq $this.Ensure + } + + [void] Set() + { + if ($this.Test()) + { + return + } + + if ($this.Ensure -eq [Ensure]::Present) + { + $extensionArg = $this.Name + + if ($null -ne $this.Version) + { + $extensionArg += "@$($this.Version)" + } + + Invoke-VSCode -Command "--install-extension $($extensionArg)" + } + else + { + Invoke-VSCode -Command "--uninstall-extension $($this.Name)" + } + + } +} + +function Assert-VSCode +{ + # Refresh session $path value before invoking 'code' + $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") + try + { + Invoke-VSCode -Command '--help' + return + } + catch + { + throw "VSCode is not installed" + } +} + +function Invoke-VSCode +{ + param ( + [Parameter(Mandatory = $true)] + [string]$Command + ) + + return Invoke-Expression -Command "code $Command" +} From 3606754ff22b2d98b288f2b27cd2809ca1bb1a49 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Tue, 10 Sep 2024 08:27:21 -0700 Subject: [PATCH 2/8] fix alignment --- .../Microsoft.VSCode.DSC.psd1 | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 index 70ed4fe6..602c5b2c 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 @@ -1,24 +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' - ) + 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' + # Prerelease string of this module + Prerelease = 'alpha' + } } } -} From 287cae001b36535b8c92aa440604c5140c58a009 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Tue, 10 Sep 2024 08:32:02 -0700 Subject: [PATCH 3/8] fix pipeline --- pipelines/azure-pipelines.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pipelines/azure-pipelines.yml b/pipelines/azure-pipelines.yml index e370b59a..76c3c025 100644 --- a/pipelines/azure-pipelines.yml +++ b/pipelines/azure-pipelines.yml @@ -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 From d9f2fc97cc0e1fb7877c83470132613856f7e483 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Tue, 10 Sep 2024 08:34:24 -0700 Subject: [PATCH 4/8] clean up comments --- resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 index 4ffb549f..c8737261 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 @@ -14,7 +14,6 @@ enum Ensure [DSCResource()] class VSCodeExtension { - # Key required. Do not set. [DscProperty(Key)] [string] $Name @@ -29,9 +28,6 @@ class VSCodeExtension [VSCodeExtension] Get() { - # Get all installed extensions - # Check if visual studio code is installed. - Assert-VSCode $currentState = [VSCodeExtension]::new() @@ -52,7 +48,6 @@ class VSCodeExtension { if ($extension -eq $this.Name) { - # if version parameter is provided. if ($null -ne $this.Version) { if ($this.Version -eq $currentState.InstalledExtensions[$this.Name]) @@ -106,7 +101,7 @@ class VSCodeExtension function Assert-VSCode { - # Refresh session $path value before invoking 'code' + # Refresh session $PATH value before invoking 'code.exe'. $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") try { From b649b47ef48cc1728e3b8fde7689afc1b0fce5e1 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Tue, 10 Sep 2024 15:03:24 -0700 Subject: [PATCH 5/8] address comments --- .../Microsoft.VSCode.DSC.psm1 | 72 ++++++++----------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 index c8737261..4cab2d3b 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 @@ -4,7 +4,7 @@ $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest -enum Ensure +enum VSCodeEnsure { Absent Present @@ -21,45 +21,34 @@ class VSCodeExtension [string] $Version [DscProperty()] - [Ensure] $Ensure = [Ensure]::Present - - [DscProperty(NotConfigurable)] - [hashtable] $InstalledExtensions = @{} + [VSCodeEnsure] $Ensure = [VSCodeEnsure]::Present [VSCodeExtension] Get() { - Assert-VSCode - $currentState = [VSCodeExtension]::new() - $currentState.Ensure = [Ensure]::Absent + $currentState.Ensure = [VSCodeEnsure]::Absent $currentState.Name = $this.Name $currentState.Version = $this.Version + + $installedExtensions = @{} $extensionList = (Invoke-VSCode -Command "--list-extensions --show-versions") -Split [Environment]::NewLine + # Populate hash table with installed VSCode extensions. foreach ($extension in $extensionList) { $info = $extension -Split '@' $extensionName = $info[0] $extensionVersion = $info[1] - $currentState.InstalledExtensions[$extensionName] = $extensionVersion + $installedExtensions[$extensionName] = $extensionVersion } - foreach ($extension in $currentState.InstalledExtensions.Keys) + foreach ($extension in $installedExtensions.Keys) { if ($extension -eq $this.Name) { - if ($null -ne $this.Version) - { - if ($this.Version -eq $currentState.InstalledExtensions[$this.Name]) - { - $currentState.Ensure = [Ensure]::Present - } - } - else - { - $currentState.Ensure = [Ensure]::Present - } - + $currentState.Ensure = [VSCodeEnsure]::Present + $currentState.Name = $extension + $currentState.Version = $installedExtensions[$extension] break } } @@ -70,7 +59,17 @@ class VSCodeExtension [bool] Test() { $currentState = $this.Get() - return $currentState.Ensure -eq $this.Ensure + 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() @@ -80,7 +79,7 @@ class VSCodeExtension return } - if ($this.Ensure -eq [Ensure]::Present) + if ($this.Ensure -eq [VSCodeEnsure]::Present) { $extensionArg = $this.Name @@ -95,22 +94,6 @@ class VSCodeExtension { Invoke-VSCode -Command "--uninstall-extension $($this.Name)" } - - } -} - -function Assert-VSCode -{ - # Refresh session $PATH value before invoking 'code.exe'. - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") - try - { - Invoke-VSCode -Command '--help' - return - } - catch - { - throw "VSCode is not installed" } } @@ -121,5 +104,12 @@ function Invoke-VSCode [string]$Command ) - return Invoke-Expression -Command "code $Command" + try + { + return Invoke-Expression "& `"$env:LocalAppData\Programs\Microsoft VS Code\bin\code.cmd`" $Command" + } + catch + { + throw "VSCode is not installed." + } } From 65b99683ada9ba0b7b73e7bfeaf20464e1b71544 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Wed, 11 Sep 2024 12:23:49 -0700 Subject: [PATCH 6/8] address feedback --- .../Microsoft.VSCode.DSC.psm1 | 136 ++++++++++++++---- 1 file changed, 106 insertions(+), 30 deletions(-) diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 index 4cab2d3b..0d9e1224 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 @@ -23,37 +23,44 @@ class VSCodeExtension [DscProperty()] [VSCodeEnsure] $Ensure = [VSCodeEnsure]::Present - [VSCodeExtension] Get() + static [hashtable] $InstalledExtensions + + static VSCodeExtension() { - $currentState = [VSCodeExtension]::new() - $currentState.Ensure = [VSCodeEnsure]::Absent - $currentState.Name = $this.Name - $currentState.Version = $this.Version + [VSCodeExtension]::GetInstalledExtensions() + } - $installedExtensions = @{} + static [VSCodeExtension[]] Export() + { $extensionList = (Invoke-VSCode -Command "--list-extensions --show-versions") -Split [Environment]::NewLine - # Populate hash table with installed VSCode extensions. - foreach ($extension in $extensionList) + $results = [VSCodeExtension[]]::new($extensionList.length) + + for ($i = 0; $i -lt $extensionList.length; $i++) { - $info = $extension -Split '@' - $extensionName = $info[0] - $extensionVersion = $info[1] - $installedExtensions[$extensionName] = $extensionVersion + $extensionName, $extensionVersion = $extensionList[$i] -Split '@' + $results[$i] = [VSCodeExtension]@{ + Name = $extensionName + Version = $extensionVersion + } } - foreach ($extension in $installedExtensions.Keys) + return $results + } + + [VSCodeExtension] Get() + { + $currentState = [VSCodeExtension]::InstalledExtensions[$this.Name] + if ($null -ne $currentState) { - if ($extension -eq $this.Name) - { - $currentState.Ensure = [VSCodeEnsure]::Present - $currentState.Name = $extension - $currentState.Version = $installedExtensions[$extension] - break - } + return $currentState } - return $currentState + return [VSCodeExtension]@{ + Name = $this.Name + Version = $this.Version + Ensure = [VSCodeEnsure]::Absent + } } [bool] Test() @@ -81,20 +88,89 @@ class VSCodeExtension if ($this.Ensure -eq [VSCodeEnsure]::Present) { - $extensionArg = $this.Name + $this.Install($false) + } + else + { + $this.Uninstall($false) + } + } - if ($null -ne $this.Version) - { - $extensionArg += "@$($this.Version)" - } +#region VSCodeExtension helper functions + static [void] GetInstalledExtensions() + { + [VSCodeExtension]::InstalledExtensions = @{} + foreach ($extension in [VSCodeExtension]::Export()) + { + [VSCodeExtension]::InstalledExtensions[$extension.Name] = $extension + } + } - Invoke-VSCode -Command "--install-extension $($extensionArg)" + [string] GetInstallArgument() + { + if ($null -eq $this.Version) + { + return $this.Name } - else + + return @( + $this.Name + $this.Version + ) -join '@' + } + + [void] Install([bool] $preTest) + { + if ($preTest -and $this.Test()) { - Invoke-VSCode -Command "--uninstall-extension $($this.Name)" + 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())" +} + +function Uninstall-VSCodeExtension +{ + [CmdletBinding()] + param ( + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [string]$Name + ) + + Invoke-VSCode -Command "--uninstall-extension $($this.Name)" } function Invoke-VSCode @@ -106,7 +182,7 @@ function Invoke-VSCode try { - return Invoke-Expression "& `"$env:LocalAppData\Programs\Microsoft VS Code\bin\code.cmd`" $Command" + Invoke-Expression "& `"$env:LocalAppData\Programs\Microsoft VS Code\bin\code.cmd`" $Command" } catch { From f21d1689143c01584ccec86e0b84c7397800ac86 Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Thu, 12 Sep 2024 12:55:36 -0700 Subject: [PATCH 7/8] fix cli path logic --- .../Microsoft.VSCode.DSC.psm1 | 144 +++++++++++------- 1 file changed, 90 insertions(+), 54 deletions(-) diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 index 0d9e1224..e5a4691a 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 @@ -4,11 +4,101 @@ $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest +#region Enums enum VSCodeEnsure { Absent Present } +#endregion Enums + +#region Functions +function Get-VSCodeCLIPath +{ + $codeCLIUserPath = "$env:LocalAppData\Programs\Microsoft VS Code\bin\code.cmd" + $codeCLIMachinePath = "$env:ProgramFiles\Microsoft VS Code\bin\code.cmd" + + if (Test-Path -Path $codeCLIUserPath) + { + return $codeCLIUserPath + } + elseif (Test-Path -Path $codeCLIMachinePath) + { + return $codeCLIMachinePath + } + else + { + throw "VSCode is not installed." + } +} + +function Install-VSCodeExtension +{ + [CmdletBinding()] + param ( + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [string]$Name, + [Parameter(ValueFromPipelineByPropertyName)] + [string]$Version + ) + + begin + { + function Get-VSCodeExtensionInstallArgument + { + param([string]$Name, [string]$Version) + + if ([string]::IsNullOrEmpty($Version)) + { + return $Name + } + + return @( + $Name + $Version + ) -join '@' + } + } + + process + { + $installArgument = Get-VSCodeExtensionInstallArgument @PSBoundParameters + Invoke-VSCode -Command "--install-extension $installArgument" + } +} + +function Uninstall-VSCodeExtension +{ + [CmdletBinding()] + param ( + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [string]$Name + ) + + Invoke-VSCode -Command "--uninstall-extension $($this.Name)" +} + +function Invoke-VSCode +{ + param ( + [Parameter(Mandatory = $true)] + [string]$Command + ) + + try + { + Invoke-Expression "& `"$VSCodeCLIPath`" $Command" + "$env:ProgramFiles\Microsoft VS Code\bin\code.cmd" + } + catch + { + throw "Executing code.exe with {$Command} failed." + } +} +#endregion Functions + +# Keeps the path of the code.exe CLI path. +$VSCodeCLIPath = Get-VSCodeCLIPath #region DSCResources [DSCResource()] @@ -106,19 +196,6 @@ class VSCodeExtension } } - [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()) @@ -148,44 +225,3 @@ class VSCodeExtension #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())" -} - -function Uninstall-VSCodeExtension -{ - [CmdletBinding()] - param ( - [Parameter(Mandatory, ValueFromPipelineByPropertyName)] - [string]$Name - ) - - Invoke-VSCode -Command "--uninstall-extension $($this.Name)" -} - -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." - } -} From 726a981072eae962ea34a5281ad69b6014a48fdf Mon Sep 17 00:00:00 2001 From: ryfu-msft Date: Fri, 13 Sep 2024 11:25:37 -0700 Subject: [PATCH 8/8] address comments --- .../Microsoft.VSCode.DSC.psd1 | 2 +- .../Microsoft.VSCode.DSC.psm1 | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 index 602c5b2c..b04d7a3f 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psd1 @@ -1,5 +1,5 @@ @{ - RootModule = 'Microsoft.VSCode.DSC.psm1' + RootModule = 'Microsoft.VSCode.Dsc.psm1' ModuleVersion = '0.1.0' GUID = 'baf2c585-d931-4089-8500-93a5b8de1741' Author = 'Microsoft Corporation' diff --git a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 index e5a4691a..550617d0 100644 --- a/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 +++ b/resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 @@ -4,17 +4,11 @@ $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest -#region Enums -enum VSCodeEnsure -{ - Absent - Present -} -#endregion Enums - #region Functions function Get-VSCodeCLIPath { + # Currently only supports user/machine install for VSCode on Windows. + # TODO: Update this function to handle when VSCode is installed in portable mode or on macOS/Linux. $codeCLIUserPath = "$env:LocalAppData\Programs\Microsoft VS Code\bin\code.cmd" $codeCLIMachinePath = "$env:ProgramFiles\Microsoft VS Code\bin\code.cmd" @@ -88,7 +82,6 @@ function Invoke-VSCode try { Invoke-Expression "& `"$VSCodeCLIPath`" $Command" - "$env:ProgramFiles\Microsoft VS Code\bin\code.cmd" } catch { @@ -111,7 +104,7 @@ class VSCodeExtension [string] $Version [DscProperty()] - [VSCodeEnsure] $Ensure = [VSCodeEnsure]::Present + [bool] $Exist = $true static [hashtable] $InstalledExtensions @@ -149,14 +142,14 @@ class VSCodeExtension return [VSCodeExtension]@{ Name = $this.Name Version = $this.Version - Ensure = [VSCodeEnsure]::Absent + Exist = $false } } [bool] Test() { $currentState = $this.Get() - if ($currentState.Ensure -ne $this.Ensure) + if ($currentState.Exist -ne $this.Exist) { return $false } @@ -176,7 +169,7 @@ class VSCodeExtension return } - if ($this.Ensure -eq [VSCodeEnsure]::Present) + if ($this.Exist) { $this.Install($false) }