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 6 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'
}
}
}
115 changes: 115 additions & 0 deletions resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# 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

[VSCodeExtension] Get()
{
$currentState = [VSCodeExtension]::new()
$currentState.Ensure = [VSCodeEnsure]::Absent
$currentState.Name = $this.Name
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
$currentState.Version = $this.Version

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

# Populate hash table with installed VSCode extensions.
foreach ($extension in $extensionList)
{
$info = $extension -Split '@'
$extensionName = $info[0]
$extensionVersion = $info[1]
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
$installedExtensions[$extensionName] = $extensionVersion
}

foreach ($extension in $installedExtensions.Keys)
{
if ($extension -eq $this.Name)
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
{
$currentState.Ensure = [VSCodeEnsure]::Present
$currentState.Name = $extension
$currentState.Version = $installedExtensions[$extension]
break
}
}
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

return $currentState
}

[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)
{
$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)"
}
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions about this utility function:

  • What if code is installed, but not in this location?

  • What happens if the command is given invalid flags?

  • What happens if the command is given valid flags but fails for another reason (e.g. can't connect to the internet)?

  • What output does this return? Can that output be meaningfully parsed?

  • Is this utility function intended for use for other resources managing VS Code, or just this one? Can this function shift any of the logic for defining arguments into the function?

  • If you find it easier to test functions than classes in Pester, you could extract the logic for getting all extensions, installing an extension, and uninstalling an extension into functions that accept the name/version parameters and pass the instance properties or pipe the instance to the function, i.e.:

    function Install-VSCodeExtension
    {
        [CmdletBinding()]
        param (
          [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
          [string]$Name,
          [Parameter(ValueFromPipelineByPropertyName)]
          [string]$Version
        )
        
        # Implementation
    }
    
    $extension = [VSCodeExtension]@{
        Name    = 'Foo'
        Version = '1.2.3'
    }
    
    # Direct pass:
    Install-VSCodeExtension -Name $extension.Name -Version $extension.Version
    
    # Pipeline:
    $extension | Install-VSCodeExtension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code can only be installed in two locations based on scope.
C:\Users\<Your Username>\AppData\Local\Programs\Microsoft VS Code
C:\Program Files\Microsoft VS Code

Code.exe doesn't return a proper error message if you provide invalid arguments or even an invalid extension name so it is difficult to produce a reasonable error message from the output. I agree that this is another concern that I have.

The utility functions that we have here are intended to be used for other DSC resources for VSCode. This prototype that I have here is just for adding VSCode extensions, but the code.exe cli tool is very powerful so I'm sure there are more things we can add to this DSC module.

Changed to suggested functions for easier testing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are the two locations for VS Code on Windows when installed with WinGet, right? But the logic for installing/updating VS Code extensions is the same when a user installs VS Code in portable mode or on macOS/Linux - I'm not too worried about this, but it's worth keeping in mind that aside from this code snippet, everything else I see in here works fully across platforms and install methodologies, and PSDSC resources published to the gallery, especially for managing something like VS Code, are likely to draw users this code won't support.

I think both deferring this problem and/or just saying "Windows only" support for the module and resources are okay, to be clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout. I left a todo comment on the function stating that this is only supported on user/machine installs for 'Windows'. I'll need to figure out a better way to determine the install location especially to support the portable install scenario.