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

Conversation

ryfu-msft
Copy link
Contributor

Adds a new DSC resource for VSCode that utilizes the code.exe executable to install a specified VSCode extension.

Accepts 2 parameters

  • Name of the extension (required)
  • Version of the extension (optional)

Tested manually with various VSCode extensions.

@ryfu-msft ryfu-msft requested a review from JohnMcPMS September 10, 2024 22:14
Copy link

@michaeltlombardi michaeltlombardi left a comment

Choose a reason for hiding this comment

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

Added a few notes on possible areas for extraction/extension/performance/testability, but overall this seems to be a very functional implementation.

My largest concern is around surfacing useful error messages when an install operation fails - in particular, the resource should be able to indicate:

  • That the install failed because the machine can't connect to the internet.
  • That the install failed because the specified extension name doesn't exist.
  • That the install failed because the specified extension version doesn't exist.
  • That the install failed for another reason, and surface the output from VS Code's command (and exit code).

resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 Outdated Show resolved Hide resolved
resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 Outdated Show resolved Hide resolved
resources/Microsoft.VSCode.DSC/Microsoft.VSCode.DSC.psm1 Outdated Show resolved Hide resolved
Comment on lines 100 to 115
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.

@ryfu-msft
Copy link
Contributor Author

Will address tests in a separate PR as this is just a prototype.

@ryfu-msft ryfu-msft merged commit 8ad836a into microsoft:main Sep 13, 2024
4 checks passed
@ryfu-msft ryfu-msft deleted the VSCodeDSC branch September 13, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants