CatalogPackageExtensions.cs (2594B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="CatalogPackageExtensions.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Engine.Extensions 8 { 9 using Microsoft.Management.Deployment; 10 11 /// <summary> 12 /// Extensions for the <see cref="CatalogPackage" /> class. 13 /// </summary> 14 internal static class CatalogPackageExtensions 15 { 16 /// <summary> 17 /// Converts a <see cref="CatalogPackage" /> to a string previewing the specified version. 18 /// </summary> 19 /// <param name="package">A <see cref="CatalogPackage" /> instance.</param> 20 /// <param name="version">A <see cref="PackageVersionId" /> instance. If null, the latest available version is used.</param> 21 /// <returns>A <see cref="string" /> instance.</returns> 22 public static string ToString( 23 this CatalogPackage package, 24 PackageVersionId? version) 25 { 26 if ((version != null) || (package.AvailableVersions.Count > 0)) 27 { 28 string versionString = (version is null) 29 ? package.AvailableVersions[0].Version 30 : version.Version; 31 return $"{package.Name} [{package.Id}] Version {versionString}"; 32 } 33 else 34 { 35 // There were no available versions! 36 return $"{package.Name} [{package.Id}]"; 37 } 38 } 39 40 /// <summary> 41 /// Gets the best effort source name of a <see cref="CatalogPackage" /> that matches its Id. 42 /// This source name is used together with Id in operation output classes for display purposes. 43 /// </summary> 44 /// <param name="package">A <see cref="CatalogPackage" /> instance.</param> 45 /// <returns>The best effort source name of the package.</returns> 46 public static string? GetSourceName(this CatalogPackage package) 47 { 48 for (int i = 0; i < package.AvailableVersions.Count; ++i) 49 { 50 var versionInfo = package.GetPackageVersionInfo(package.AvailableVersions[i]); 51 if (versionInfo.Id == package.Id) 52 { 53 return versionInfo.PackageCatalog.Info.Name; 54 } 55 } 56 57 return null; 58 } 59 } 60 }