winget-cli

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

InstallerPackageCommand.cs (9230B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="InstallerPackageCommand.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Client.Engine.Commands
      8 {
      9     using System.Management.Automation;
     10     using System.Threading.Tasks;
     11     using Microsoft.Management.Deployment;
     12     using Microsoft.WinGet.Client.Engine.Commands.Common;
     13     using Microsoft.WinGet.Client.Engine.Helpers;
     14     using Microsoft.WinGet.Client.Engine.PSObjects;
     15     using Microsoft.WinGet.Common.Command;
     16     using Microsoft.WinGet.Resources;
     17 
     18     /// <summary>
     19     /// Installs or updates a package from the pipeline or from a configured source.
     20     /// </summary>
     21     public sealed class InstallerPackageCommand : InstallCommand
     22     {
     23         /// <summary>
     24         /// Initializes a new instance of the <see cref="InstallerPackageCommand"/> class.
     25         /// </summary>
     26         /// <param name="psCmdlet">Caller cmdlet.</param>
     27         /// <param name="override">Override arguments to be passed on to the installer.</param>
     28         /// <param name="custom">Additional arguments.</param>
     29         /// <param name="location">Installation location.</param>
     30         /// <param name="allowHashMismatch">To skip the installer hash validation check.</param>
     31         /// <param name="force">To continue upon non security related failures.</param>
     32         /// <param name="header">HTTP Header to pass on to the REST Source.</param>
     33         /// <param name="psCatalogPackage">PSCatalogPackage.</param>
     34         /// <param name="version">Version to install.</param>
     35         /// <param name="log">Logging file location.</param>
     36         /// <param name="id">Package identifier.</param>
     37         /// <param name="name">Name of package.</param>
     38         /// <param name="moniker">Moniker of package.</param>
     39         /// <param name="source">Source to search. If null, all are searched.</param>
     40         /// <param name="query">Match against any field of a package.</param>
     41         /// <param name="skipDependencies">To skip package dependencies.</param>
     42         public InstallerPackageCommand(
     43             PSCmdlet psCmdlet,
     44             string @override,
     45             string custom,
     46             string location,
     47             bool allowHashMismatch,
     48             bool force,
     49             string header,
     50             PSCatalogPackage psCatalogPackage,
     51             string version,
     52             string log,
     53             string id,
     54             string name,
     55             string moniker,
     56             string source,
     57             string[] query,
     58             bool skipDependencies)
     59             : base(psCmdlet)
     60         {
     61             // InstallCommand.
     62             this.Override = @override;
     63             this.Custom = custom;
     64             this.Location = location;
     65             this.Force = force;
     66             this.Header = header;
     67             this.AllowHashMismatch = allowHashMismatch;
     68             this.SkipDependencies = skipDependencies;
     69             this.Log = log;
     70 
     71             // PackageCommand.
     72             if (psCatalogPackage != null)
     73             {
     74                 this.CatalogPackage = psCatalogPackage;
     75             }
     76 
     77             this.Version = version;
     78 
     79             // FinderCommand
     80             this.Id = id;
     81             this.Name = name;
     82             this.Moniker = moniker;
     83             this.Source = source;
     84             this.Query = query;
     85         }
     86 
     87         /// <summary>
     88         /// Process install package command.
     89         /// </summary>
     90         /// <param name="psPackageFieldMatchOption">PSPackageFieldMatchOption.</param>
     91         /// <param name="psPackageInstallScope">PSPackageInstallScope.</param>
     92         /// <param name="psProcessorArchitecture">PSProcessorArchitecture.</param>
     93         /// <param name="psPackageInstallMode">PSPackageInstallMode.</param>
     94         /// <param name="psPackageInstallerType">PSPackageInstallerType.</param>
     95         public void Install(
     96             string psPackageFieldMatchOption,
     97             string psPackageInstallScope,
     98             string psProcessorArchitecture,
     99             string psPackageInstallMode,
    100             string psPackageInstallerType)
    101         {
    102             var result = this.Execute(
    103                 async () => await this.GetPackageAndExecuteAsync(
    104                     CompositeSearchBehavior.RemotePackagesFromRemoteCatalogs,
    105                     PSEnumHelpers.ToPackageFieldMatchOption(psPackageFieldMatchOption),
    106                     async (package, version) =>
    107                     {
    108                         InstallOptions options = this.GetInstallOptions(version, psPackageInstallMode);
    109                         if (!PSEnumHelpers.IsDefaultEnum(psProcessorArchitecture))
    110                         {
    111                             options.AllowedArchitectures.Clear();
    112                             options.AllowedArchitectures.Add(PSEnumHelpers.ToProcessorArchitecture(psProcessorArchitecture));
    113                         }
    114 
    115                         if (!PSEnumHelpers.IsDefaultEnum(psPackageInstallerType))
    116                         {
    117                             options.InstallerType = PSEnumHelpers.ToPackageInstallerType(psPackageInstallerType);
    118                         }
    119 
    120                         options.PackageInstallScope = PSEnumHelpers.ToPackageInstallScope(psPackageInstallScope);
    121 
    122                         return await this.InstallPackageAsync(package, options);
    123                     }));
    124 
    125             if (result != null)
    126             {
    127                 this.Write(StreamType.Object, new PSInstallResult(result.Item1, result.Item2));
    128             }
    129         }
    130 
    131         /// <summary>
    132         /// Process update package command.
    133         /// </summary>
    134         /// <param name="includeUnknown">If updating to an unknown version is allowed.</param>
    135         /// <param name="psPackageFieldMatchOption">PSPackageFieldMatchOption.</param>
    136         /// <param name="psPackageInstallScope">PSPackageInstallScope.</param>
    137         /// <param name="psProcessorArchitecture">PSProcessorArchitecture.</param>
    138         /// <param name="psPackageInstallMode">PSPackageInstallMode.</param>
    139         /// <param name="psPackageInstallerType">PSPackageInstallerType.</param>
    140         public void Update(
    141             bool includeUnknown,
    142             string psPackageFieldMatchOption,
    143             string psPackageInstallScope,
    144             string psProcessorArchitecture,
    145             string psPackageInstallMode,
    146             string psPackageInstallerType)
    147         {
    148             var result = this.Execute(
    149                 async () => await this.GetPackageAndExecuteAsync(
    150                     CompositeSearchBehavior.LocalCatalogs,
    151                     PSEnumHelpers.ToPackageFieldMatchOption(psPackageFieldMatchOption),
    152                     async (package, version) =>
    153                     {
    154                         InstallOptions options = this.GetInstallOptions(version, psPackageInstallMode);
    155                         options.AllowUpgradeToUnknownVersion = includeUnknown;
    156 
    157                         if (!PSEnumHelpers.IsDefaultEnum(psProcessorArchitecture))
    158                         {
    159                             options.AllowedArchitectures.Clear();
    160                             options.AllowedArchitectures.Add(PSEnumHelpers.ToProcessorArchitecture(psProcessorArchitecture));
    161                         }
    162 
    163                         if (!PSEnumHelpers.IsDefaultEnum(psPackageInstallerType))
    164                         {
    165                             options.InstallerType = PSEnumHelpers.ToPackageInstallerType(psPackageInstallerType);
    166                         }
    167 
    168                         options.PackageInstallScope = PSEnumHelpers.ToPackageInstallScope(psPackageInstallScope);
    169 
    170                         return await this.UpgradePackageAsync(package, options);
    171                     }));
    172 
    173             if (result != null)
    174             {
    175                 this.Write(StreamType.Object, new PSInstallResult(result.Item1, result.Item2));
    176             }
    177         }
    178 
    179         private async Task<InstallResult> InstallPackageAsync(
    180             CatalogPackage package,
    181             InstallOptions options)
    182         {
    183             var installOperation = new InstallOperationWithProgress(
    184                 this,
    185                 string.Format(Resources.ProgressRecordActivityInstalling, package.Name));
    186             return await installOperation.ExecuteAsync(
    187                     () => PackageManagerWrapper.Instance.InstallPackageAsync(package, options));
    188         }
    189 
    190         private async Task<InstallResult> UpgradePackageAsync(
    191             CatalogPackage package,
    192             InstallOptions options)
    193         {
    194             var installOperation = new InstallOperationWithProgress(
    195                 this,
    196                 string.Format(Resources.ProgressRecordActivityUpdating, package.Name));
    197             return await installOperation.ExecuteAsync(
    198                     () => PackageManagerWrapper.Instance.UpgradePackageAsync(package, options));
    199         }
    200     }
    201 }