meta-package-manager icon indicating copy to clipboard operation
meta-package-manager copied to clipboard

`winget` support

Open dinhanhx opened this issue 2 years ago • 11 comments

WinGet is a new native tool in Windows 11.

dinhanhx avatar Mar 12 '22 02:03 dinhanhx

I looked at it and the parsing would currently be a bloody mess. The tool is currently only designed to be used interactively in a terminal.

There are some issues already in discussion to allow machine-readable outputs, but until that the parsing is too complicated, because winget creates a table with dynamic-sized columns, which is even with sed etc. not too trivial.

https://github.com/microsoft/winget-cli/issues/221 https://github.com/microsoft/winget-cli/issues/2032

I would put it on hold until winget offers a better experiance for parsing.

autinerd avatar Jun 15 '22 08:06 autinerd

Alright let's wait then

dinhanhx avatar Jun 15 '22 08:06 dinhanhx

Parsing table with variable columns is doable. Look for instance how other package managers does it:

  • emerge: https://github.com/kdeldycke/meta-package-manager/blob/3cf0599a9b35d78b61676e5fdf475e0333f27fb6/meta_package_manager/managers/emerge.py#L100-L107
  • flatpak: https://github.com/kdeldycke/meta-package-manager/blob/3cf0599a9b35d78b61676e5fdf475e0333f27fb6/meta_package_manager/managers/flatpak.py#L47-L54

I can try to implement a first version and we can collectively iterate on it. The thing is I do not have any Windows instance available at the moment. But if you provide me with terminal captures of some commands I can make an attempt.

Does WinGet allows you to list installed/outdated packages? That might be a start.

kdeldycke avatar Jun 26 '22 11:06 kdeldycke

~~might be relevant, 3rd party module adding winget support to powershell https://github.com/ethanbergstrom/Cobalt output of list is the same though so don't think it solves the parsing~~

hannesdelbeke avatar Feb 26 '23 16:02 hannesdelbeke

seems there is now official powershell support that supports custom output

@halr9000 we've just released a native PowerShell module. It provides structured output.

however when looking at the repo, the list command doesn't seem to be in there

Get-WinGetVersion
Enable-WinGetSetting
Disable-WinGetSetting
Add-WinGetSource
Remove-WinGetSource
Reset-WinGetSource
Find-WinGetPackage
Get-WinGetPackage
Get-WinGetSource
Install-WinGetPackage
Uninstall-WinGetPackage
Update-WinGetPackage

hannesdelbeke avatar Feb 26 '23 16:02 hannesdelbeke

@hannesdelbeke Not sure precisely what you mean but I would expect a list action to correspond to the get verb.

So, Get-WingetPackage

Does that help?

halr9000 avatar Feb 26 '23 17:02 halr9000

@hannesdelbeke Not sure precisely what you mean but I would expect a list action to correspond to the get verb. So, Get-WingetPackage Does that help?

yes it seems that Get-WingetPackage lists all installed packages, just like winget list. I was confused since I expected the commands to be named the same.

Do you perhaps also know if there is an option to get machine readable output from Get-WingetPackage? e.g. output comma separated, or a json or yaml.

hannesdelbeke avatar Feb 26 '23 17:02 hannesdelbeke

yes it seems that Get-WingetPackage lists all installed packages, just like winget list. I was confused since I expected the commands to be named the same.

Oh! Actually, what they did is very powershelly of them, I like it. :) PowerShell has a strongly encouraged (but ultimately. optional) set of approved verbs. You can also list them, along with definitions by executing Get-Verb. Same is true for parameters. For this reason, anyone who understands PowreShell well, would never expect a "straight port", as that would ruin one of the key principles of the language, which is consistency. For more, I would read the Monad Manifesto, or search for interviews of Jeffrey Snover on why he and the team created the language. (There's at least one of me interviewing him on PowerScripting Podcast many years ago.)

Do you perhaps also know if there is an option to get machine readable output from Get-WingetPackage? e.g. output comma separated, or a json or yaml.

Yup, checked just now, and the winget team did good here as well. Another principle: "everything is an object". Because the cmdlet returns an object and not an array of strings, you can do cool stuff like convert it to JSON in a consistent fashion like you would with any other object:

PS > Get-WinGetPackage python | ConvertTo-Json
[
  {
    "ID": "Miniconda3 py39_4.10.3 (Python 3.9.5 64-bit)",
    "Version": "394.10.3",
    "Source": ""
  },
  {
    "ID": "Python.Python.3.10",
    "Version": "3.10.9",
    "Source": "winget"
  },
  {
    "ID": "SomePythonThings.WingetUIStore",
    "Version": "1.6.1",
    "Source": "winget"
  }
]

As far as YAML goes, looks like there's a module for that. CSV is built-in like JSON, try convertto-csv.

halr9000 avatar Feb 27 '23 03:02 halr9000

Wow, thanks @hannesdelbeke and @halr9000 for keeping track of these new developments! I'm not a Windows user so I'm slow picking up new development. I'll try to take a look at it at one point, but if you think there's enough material to attempt an implementation, feel free to start a PR. Even if it's janky or hackish, it can serve as a good starting point to iterate on.

At the moment I'm deep into refactoring and stabilizing the test suite of mpm and that's my only focus so far. And fixing my deeply broken blog. After all of that I might revisit this issue.

kdeldycke avatar Feb 27 '23 11:02 kdeldycke

Ah, perfect, thanks @hannesdelbeke and @halr9000! I could do an implementation in the next days.

autinerd avatar Feb 27 '23 13:02 autinerd

@autinerd If you try an implementation go ahead! I'm not going to nitpick your code anyway, there lots of automatic fix implemented right now by the way of GitHub actions.

kdeldycke avatar Feb 27 '23 14:02 kdeldycke