winget-cli

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

PSCatalogPackage.cs (4254B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PSCatalogPackage.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Client.Engine.PSObjects
      8 {
      9     using System.Linq;
     10     using Microsoft.Management.Deployment;
     11     using Microsoft.WinGet.Client.Engine.Exceptions;
     12     using Microsoft.WinGet.Client.Engine.Extensions;
     13 
     14     /// <summary>
     15     /// CatalogPackage wrapper object for displaying to PowerShell.
     16     /// </summary>
     17     public abstract class PSCatalogPackage
     18     {
     19         /// <summary>
     20         /// Initializes a new instance of the <see cref="PSCatalogPackage"/> class.
     21         /// </summary>
     22         /// <param name="catalogPackage">CatalogPackage COM object.</param>
     23         internal PSCatalogPackage(Management.Deployment.CatalogPackage catalogPackage)
     24         {
     25             this.CatalogPackageCOM = catalogPackage;
     26         }
     27 
     28         /// <summary>
     29         /// Gets the name of the catalog package.
     30         /// </summary>
     31         public string Name
     32         {
     33             get
     34             {
     35                 return this.CatalogPackageCOM.Name;
     36             }
     37         }
     38 
     39         /// <summary>
     40         /// Gets the id of the catalog package.
     41         /// </summary>
     42         public string Id
     43         {
     44             get
     45             {
     46                 return this.CatalogPackageCOM.Id;
     47             }
     48         }
     49 
     50         /// <summary>
     51         /// Gets a value indicating whether an update is available.
     52         /// </summary>
     53         public bool IsUpdateAvailable
     54         {
     55             get
     56             {
     57                 return this.CatalogPackageCOM.IsUpdateAvailable;
     58             }
     59         }
     60 
     61         /// <summary>
     62         /// Gets the source name of the catalog package.
     63         /// </summary>
     64         public string? Source
     65         {
     66             get
     67             {
     68                 return this.CatalogPackageCOM.GetSourceName();
     69             }
     70         }
     71 
     72         /// <summary>
     73         /// Gets list of strings representing the available versions.
     74         /// </summary>
     75         public string[] AvailableVersions
     76         {
     77             get
     78             {
     79                 return this.AvailablePackageVersionIds.Select(i => i.Version).ToArray();
     80             }
     81         }
     82 
     83         /// <summary>
     84         /// Gets the catalog package COM object.
     85         /// </summary>
     86         internal CatalogPackage CatalogPackageCOM { get; private set; }
     87 
     88         /// <summary>
     89         /// Gets a list of available package version ids for the package.
     90         /// </summary>
     91         private PackageVersionId[] AvailablePackageVersionIds
     92         {
     93             get
     94             {
     95                 return this.CatalogPackageCOM.AvailableVersions.ToArray();
     96             }
     97         }
     98 
     99         /// <summary>
    100         /// Checks the installed status of the catalog package.
    101         /// </summary>
    102         /// <returns>CheckInstalledStatus string.</returns>
    103         public string CheckInstalledStatus()
    104         {
    105             return this.CatalogPackageCOM.CheckInstalledStatus().Status.ToString();
    106         }
    107 
    108         /// <summary>
    109         /// Gets the PackageVersionInfo PSObject that corresponds with the version string.
    110         /// </summary>
    111         /// <param name="version">Version string.</param>
    112         /// <returns>PackageVersionInfo PSObject.</returns>
    113         /// <exception cref="NoPackageFoundException">Throws an exception if no package is found.</exception>
    114         public PSPackageVersionInfo GetPackageVersionInfo(string version)
    115         {
    116             // get specific version that matches
    117             PackageVersionId? packageVersionId = this.AvailablePackageVersionIds.FirstOrDefault(x => x.Version == version);
    118             if (packageVersionId != null)
    119             {
    120                 return new PSPackageVersionInfo(this.CatalogPackageCOM.GetPackageVersionInfo(packageVersionId));
    121             }
    122             else
    123             {
    124                 throw new NoPackageFoundException();
    125             }
    126         }
    127     }
    128 }