winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PackageListExtensions.cs (3444B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PackageListExtensions.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace WinGetMCPServer.Extensions
      8 {
      9     using Microsoft.Management.Deployment;
     10     using WinGetMCPServer.Response;
     11 
     12     /// <summary>
     13     /// Extensions for List<FindPackageResult>.
     14     /// </summary>
     15     internal static class PackageListExtensions
     16     {
     17         /// <summary>
     18         /// Adds the packages from a find to the list.
     19         /// </summary>
     20         /// <param name="list">The list to add to.</param>
     21         /// <param name="findResult">A find result.</param>
     22         /// <returns>The list.</returns>
     23         static public List<FindPackageResult> AddPackages(this List<FindPackageResult> list, FindPackagesResult findResult)
     24         {
     25             for (int i = 0; i < findResult.Matches!.Count; ++i)
     26             {
     27                 list.Add(FindPackageResultFromCatalogPackage(findResult.Matches[i].CatalogPackage));
     28             }
     29 
     30             return list;
     31         }
     32 
     33         /// <summary>
     34         /// Creates a <see cref="FindPackageResult"/> from the specified <see cref="CatalogPackage"/>.
     35         /// </summary>
     36         /// <remarks>The method populates the <see cref="FindPackageResult"/> with details such as the
     37         /// package identifier, name, installation status, and update availability. If the package is installed,
     38         /// additional information such as the catalog name and installed location is included.</remarks>
     39         /// <param name="package">The catalog package from which to create the result. Cannot be null.</param>
     40         /// <returns>A <see cref="FindPackageResult"/> representing the state of the specified catalog package, including
     41         /// installation status and available updates.</returns>
     42         static public FindPackageResult FindPackageResultFromCatalogPackage(CatalogPackage package)
     43         {
     44             FindPackageResult findPackageResult = new FindPackageResult()
     45             {
     46                 Identifier = package.Id,
     47                 Name = package.Name,
     48             };
     49 
     50             var installedVersion = package.InstalledVersion;
     51             findPackageResult.IsInstalled = installedVersion != null;
     52 
     53             if (installedVersion != null)
     54             {
     55                 findPackageResult.Source = installedVersion.PackageCatalog?.Info?.Name;
     56                 if (string.IsNullOrEmpty(findPackageResult.Source))
     57                 {
     58                     findPackageResult.Source = package.DefaultInstallVersion?.PackageCatalog?.Info?.Name;
     59                 }
     60 
     61                 findPackageResult.InstalledVersion = installedVersion.Version;
     62                 findPackageResult.IsUpdateAvailable = package.IsUpdateAvailable;
     63 
     64                 string installLocation = installedVersion.GetMetadata(PackageVersionMetadataField.InstalledLocation);
     65                 if (!string.IsNullOrEmpty(installLocation))
     66                 {
     67                     findPackageResult.InstalledLocation = installLocation;
     68                 }
     69             }
     70             else
     71             {
     72                 findPackageResult.Source = package.DefaultInstallVersion.PackageCatalog.Info.Name;
     73             }
     74 
     75             return findPackageResult;
     76         }
     77     }
     78 }