winget-cli

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

ConfigurationSetProgressOutputBase.cs (4169B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="ConfigurationSetProgressOutputBase.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Configuration.Engine.Helpers
      8 {
      9     using System;
     10     using System.Collections.Generic;
     11     using Microsoft.Management.Configuration;
     12     using Microsoft.WinGet.Common.Command;
     13     using Windows.Foundation;
     14 
     15     /// <summary>
     16     /// Helper to handle progress callbacks.
     17     /// </summary>
     18     /// <typeparam name="TOperationResult">The operation result.</typeparam>
     19     /// <typeparam name="TProgressData">Progress data.</typeparam>
     20     internal abstract class ConfigurationSetProgressOutputBase<TOperationResult, TProgressData>
     21     {
     22         private readonly PowerShellCmdlet cmd;
     23         private readonly int activityId;
     24         private readonly string activity;
     25         private readonly string inProgressMessage;
     26         private readonly string completeMessage;
     27         private readonly int totalUnitsExpected;
     28 
     29         /// <summary>
     30         /// Initializes a new instance of the <see cref="ConfigurationSetProgressOutputBase{TOperationResult, TProgressData}"/> class.
     31         /// </summary>
     32         /// <param name="cmd">Command that outputs the messages.</param>
     33         /// <param name="activityId">The activity id of the progress bar.</param>
     34         /// <param name="activity">The activity.</param>
     35         /// <param name="inProgressMessage">The message in the progress bar.</param>
     36         /// <param name="completeMessage">The activity complete message.</param>
     37         /// <param name="totalUnitsExpected">Total of units expected.</param>
     38         public ConfigurationSetProgressOutputBase(PowerShellCmdlet cmd, int activityId, string activity, string inProgressMessage, string completeMessage, int totalUnitsExpected)
     39         {
     40             this.cmd = cmd;
     41             this.activityId = activityId;
     42             this.activity = activity;
     43             this.inProgressMessage = inProgressMessage;
     44             this.completeMessage = completeMessage;
     45             this.totalUnitsExpected = totalUnitsExpected;
     46 
     47             // Write initial progress record.
     48             // For some reason, if this is 0 the progress bar is shown full. Start with 1%
     49             this.cmd.WriteProgressWithPercentage(activityId, activity, $"{this.inProgressMessage} 0/{this.totalUnitsExpected}", 1, 100);
     50         }
     51 
     52         /// <summary>
     53         /// Gets or sets a hash set with the completed units.
     54         /// </summary>
     55         protected HashSet<Guid> UnitsCompleted { get; set; } = new ();
     56 
     57         /// <summary>
     58         /// Progress callback.
     59         /// </summary>
     60         /// <param name="operation">Async operation in progress.</param>
     61         /// <param name="data">Change data.</param>
     62         public abstract void Progress(IAsyncOperationWithProgress<TOperationResult, TProgressData> operation, TProgressData data);
     63 
     64         /// <summary>
     65         /// Handle progress.
     66         /// </summary>
     67         /// <param name="result">Set result.</param>
     68         public abstract void HandleProgress(TOperationResult result);
     69 
     70         /// <summary>
     71         /// Completes the progress bar.
     72         /// </summary>
     73         public void CompleteProgress()
     74         {
     75             this.cmd.CompleteProgress(this.activityId, this.activity, this.completeMessage);
     76         }
     77 
     78         /// <summary>
     79         /// Marks a unit as completed and increase progress.
     80         /// </summary>
     81         /// <param name="unit">Unit.</param>
     82         protected void CompleteUnit(ConfigurationUnit unit)
     83         {
     84             if (this.UnitsCompleted.Add(unit.InstanceIdentifier))
     85             {
     86                 this.cmd.WriteProgressWithPercentage(this.activityId, this.activity, $"{this.inProgressMessage} {this.UnitsCompleted.Count}/{this.totalUnitsExpected}", this.UnitsCompleted.Count, this.totalUnitsExpected);
     87             }
     88         }
     89     }
     90 }