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

WinGetPackage DSC Resource #2863

Merged
merged 9 commits into from
Apr 27, 2023

Conversation

msftrubengu
Copy link
Contributor

@msftrubengu msftrubengu commented Jan 17, 2023

This is a preview implementation of a DSC resource that installs packages on winget.

For now, ensures a package is installed or not. Its install behavior depends if the package is already installed for the user or not.

This resource is not final and it just provides a simple implementation.

Microsoft Reviewers: Open in CodeFlow

@msftrubengu msftrubengu requested a review from a team as a code owner January 17, 2023 23:11
@github-actions

This comment has been minimized.

[DSCResource()]
class WinGetPackage
{
[DscProperty(Key)]
Copy link
Member

Choose a reason for hiding this comment

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

Is there not additionally a way to specify that this value is required?

{
$wingetPackageResource.Installed = $true
$wingetPackageResource.Version = $catalogPackage.InstalledVersion.Version
$wingetPackageResource.Source = $catalogPackage.DefaultInstallVersion.PackageCatalog.Info.Name
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure what you wanted the Source here to mean, but this likely isn't the correct value to pull.

If you want "the place that this came from", then $catalogPackage.InstalledVersion.PackageCatalog is more correct, but you would need to then determine how to handle the case that it was installed outside of winget.


$catalogPackage = Get-WinGetPackage -Id $this.Id -Exact

# If no version is given, see if the latests is installed.
Copy link
Member

Choose a reason for hiding this comment

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

I would think that if no version is given, then it doesn't matter what version is present on the system. If you want to have Latest semantics, add a Latest property to set.

# We can't have CatalogPackage as member of this class because it doesn't have a default constructor.
# A call to Invoke-DscResource will fail with
# 'ImportClassResourcesFromModule: Exception calling "ImportClassResourcesFromModule" with "4" argument(s): "The DSC resource 'CatalogPackage' has no default constructor."'
$catalogPackage = Get-WinGetPackage -Id $this.Id -Exact
Copy link
Member

Choose a reason for hiding this comment

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

Exact is not the correct matching strategy; it requires the case to match. What we want is EqualsCaseInsensitive, but that isn't exposed via PS currently. We alternate between that and ContainsCaseInsensitive in winget commands based on whether the command is targeting a single package or multiple, but the PS commands don't carry the actions that are intended during the search phase.

The matching strategy parameter needs to be changed to expose all of the options, probably as a parameter that takes an enumeration value.


if ($this.Installed)
{
$catalogPackage = Get-WinGetPackage -Id $this.Id -Exact
Copy link
Member

Choose a reason for hiding this comment

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

This seems extremely inefficient. Couldn't you make an internal type/hashtable that contains all of the relevant information and get it just once?


if ([string]::IsNullOrWhiteSpace($this.Version))
{
Update-WinGetPackage @hashArgs
Copy link
Member

Choose a reason for hiding this comment

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

Again, I don't see how not specifying a version means "the latest version" rather than "I don't care what the version is".

{'Greater' -or 'Unknown'}
{
# The installed package has a greated version or unknown. Uninstall and install.
Uninstall-WinGetPackage -Id $this.Id
Copy link
Member

Choose a reason for hiding this comment

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

We don't have a pipelineable parameter to just take in the package object? This call is not safe as written (ignores source).

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback Issue needs attention from issue or PR author label Feb 10, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Author-Feedback Issue needs attention from issue or PR author label Apr 20, 2023
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@msftrubengu
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

# DSC only validates keys and mandatories in a Set call.
if ([string]::IsNullOrWhiteSpace($this.Id))
{
throw "WinGetPackage: Id is required"
Copy link
Member

Choose a reason for hiding this comment

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

//TODO: Localize this and other error messages

@msftrubengu msftrubengu merged commit b197961 into microsoft:master Apr 27, 2023
8 checks passed
@msftrubengu msftrubengu deleted the wingetpackageresource branch August 14, 2023 22:27
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.

None yet

2 participants