winget-cli

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

InstallOperationWithProgress.cs (2405B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="InstallOperationWithProgress.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Client.Engine.Helpers
      8 {
      9     using System.Management.Automation;
     10     using Microsoft.Management.Deployment;
     11     using Microsoft.WinGet.Client.Engine.Common;
     12     using Microsoft.WinGet.Common.Command;
     13     using Windows.Foundation;
     14 
     15     /// <summary>
     16     /// Handlers install or update operations with progress.
     17     /// </summary>
     18     internal class InstallOperationWithProgress : OperationWithProgressBase<InstallResult, InstallProgress>
     19     {
     20         /// <summary>
     21         /// Initializes a new instance of the <see cref="InstallOperationWithProgress"/> class.
     22         /// </summary>
     23         /// <param name="pwshCmdlet">A <see cref="PowerShellCmdlet" /> instance.</param>
     24         /// <param name="activity">Activity.</param>
     25         public InstallOperationWithProgress(PowerShellCmdlet pwshCmdlet, string activity)
     26             : base(pwshCmdlet, activity)
     27         {
     28         }
     29 
     30         /// <inheritdoc/>
     31         public override void Progress(IAsyncOperationWithProgress<InstallResult, InstallProgress> operation, InstallProgress progress)
     32         {
     33             ProgressRecord record = new (this.ActivityId, this.Activity, progress.State.ToString())
     34             {
     35                 RecordType = ProgressRecordType.Processing,
     36             };
     37 
     38             if (progress.State == PackageInstallProgressState.Downloading && progress.BytesRequired != 0)
     39             {
     40                 double downloaded = (double)progress.BytesDownloaded / Constants.OneMB;
     41                 double total = (double)progress.BytesRequired / Constants.OneMB;
     42                 record.StatusDescription = $"{downloaded:0.0} MB / {total:0.0} MB";
     43                 record.PercentComplete = (int)(progress.DownloadProgress * 100);
     44             }
     45             else if (progress.State == PackageInstallProgressState.Installing)
     46             {
     47                 record.PercentComplete = (int)(progress.InstallationProgress * 100);
     48             }
     49 
     50             this.PwshCmdlet.Write(StreamType.Progress, record);
     51         }
     52     }
     53 }