diff --git a/src/PowerShell/CommonFiles/PowerShellCmdlet.cs b/src/PowerShell/CommonFiles/PowerShellCmdlet.cs index bee705528a..f0755543b7 100644 --- a/src/PowerShell/CommonFiles/PowerShellCmdlet.cs +++ b/src/PowerShell/CommonFiles/PowerShellCmdlet.cs @@ -406,8 +406,8 @@ internal void CompleteProgress(int activityId, string activity, string status, b _ = this.progressRecords.TryUpdate(activityId, record.RecordType, ProgressRecordType.Processing); } - // You should only use force if you know the cmdlet that is completing this progress is an sync cmdlet that - // is running in an async context. + // You should only use force if you know the cmdlet that is completing this progress is a sync cmdlet that + // is running in an async context. A sync cmdlet is anything that doesn't start with Start-* if (force) { this.ExecuteInPowerShellThread(() => this.CmdletWrite(StreamType.Progress, record, this)); diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/InstallerPackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/InstallerPackageCommand.cs index 0736bcef82..27e5fd32e8 100644 --- a/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/InstallerPackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/InstallerPackageCommand.cs @@ -150,38 +150,22 @@ private async Task InstallPackageAsync( CatalogPackage package, InstallOptions options) { - var operation = PackageManagerWrapper.Instance.InstallPackageAsync(package, options); - var progressHandler = new InstallProgressOperation( + var installOperation = new InstallOperationWithProgress( this, - string.Format(Resources.ProgressRecordActivityInstalling, package.Name), - operation); - try - { - return await progressHandler.GetResult(); - } - finally - { - progressHandler.CompleteProgress(); - } + string.Format(Resources.ProgressRecordActivityInstalling, package.Name)); + return await installOperation.ExecuteAsync( + () => PackageManagerWrapper.Instance.InstallPackageAsync(package, options)); } private async Task UpgradePackageAsync( CatalogPackage package, InstallOptions options) { - var operation = PackageManagerWrapper.Instance.UpgradePackageAsync(package, options); - var progressHandler = new InstallProgressOperation( + var installOperation = new InstallOperationWithProgress( this, - string.Format(Resources.ProgressRecordActivityUpdating, package.Name), - operation); - try - { - return await progressHandler.GetResult(); - } - finally - { - progressHandler.CompleteProgress(); - } + string.Format(Resources.ProgressRecordActivityUpdating, package.Name)); + return await installOperation.ExecuteAsync( + () => PackageManagerWrapper.Instance.UpgradePackageAsync(package, options)); } } } diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/UninstallPackageCommand.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/UninstallPackageCommand.cs index 5bf5a4a0c0..b2f7e4be5e 100644 --- a/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/UninstallPackageCommand.cs +++ b/src/PowerShell/Microsoft.WinGet.Client.Engine/Commands/UninstallPackageCommand.cs @@ -114,19 +114,11 @@ private async Task UninstallPackageAsync( CatalogPackage package, UninstallOptions options) { - var operation = PackageManagerWrapper.Instance.UninstallPackageAsync(package, options); - var progressHandler = new UninstallProgressOperation( + var progressOperation = new UninstallOperationWithProgress( this, - string.Format(Resources.ProgressRecordActivityUninstalling, package.Name), - operation); - try - { - return await progressHandler.GetResult(); - } - finally - { - progressHandler.CompleteProgress(); - } + string.Format(Resources.ProgressRecordActivityUninstalling, package.Name)); + return await progressOperation.ExecuteAsync( + () => PackageManagerWrapper.Instance.UninstallPackageAsync(package, options)); } } } diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/HttpClientHelper.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/HttpClientHelper.cs index fae832cced..35113a6d21 100644 --- a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/HttpClientHelper.cs +++ b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/HttpClientHelper.cs @@ -77,7 +77,6 @@ public async Task DownloadUrlWithProgressAsync(string url, string fileName, Powe int percentComplete = (int)((double)totalBytes / contentLength * 100); if (percentComplete > maxPercentComplete) { - pwshCmdlet.Write(StreamType.Verbose, $"DEBUG: {percentComplete}"); maxPercentComplete = percentComplete; ProgressRecord record = new (activityId, url, Resources.DownloadingMessage) { diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/InstallProgressOperation.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/InstallOperationWithProgress.cs similarity index 77% rename from src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/InstallProgressOperation.cs rename to src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/InstallOperationWithProgress.cs index 43a5205d3c..ef75d4dad3 100644 --- a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/InstallProgressOperation.cs +++ b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/InstallOperationWithProgress.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------------- -// +// // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // // ----------------------------------------------------------------------------- @@ -13,18 +13,17 @@ namespace Microsoft.WinGet.Client.Engine.Helpers using Windows.Foundation; /// - /// Handler progress for install and update. + /// Handlers install or update operations with progress. /// - internal class InstallProgressOperation : ProgressOperationBase + internal class InstallOperationWithProgress : OperationWithProgressBase { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// A instance. /// Activity. - /// Operation. - public InstallProgressOperation(PowerShellCmdlet pwshCmdlet, string activity, IAsyncOperationWithProgress operation) - : base(pwshCmdlet, activity, operation) + public InstallOperationWithProgress(PowerShellCmdlet pwshCmdlet, string activity) + : base(pwshCmdlet, activity) { } diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/ProgressOperationBase.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/OperationWithProgressBase.cs similarity index 67% rename from src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/ProgressOperationBase.cs rename to src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/OperationWithProgressBase.cs index adc234e9d4..026b662b33 100644 --- a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/ProgressOperationBase.cs +++ b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/OperationWithProgressBase.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------------- -// +// // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // // ----------------------------------------------------------------------------- @@ -13,31 +13,24 @@ namespace Microsoft.WinGet.Client.Engine.Helpers using Windows.Foundation; /// - /// Base class to handle progress. + /// Base class for async operations with progress. /// /// The operation result. /// Progress data. - internal abstract class ProgressOperationBase + internal abstract class OperationWithProgressBase { - private IAsyncOperationWithProgress operation; - /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// A instance. /// Activity. - /// Operation. - public ProgressOperationBase( + public OperationWithProgressBase( PowerShellCmdlet pwshCmdlet, - string activity, - IAsyncOperationWithProgress operation) + string activity) { this.PwshCmdlet = pwshCmdlet; this.ActivityId = pwshCmdlet.GetNewProgressActivityId(); this.Activity = activity; - this.operation = operation; - - operation.Progress = this.Progress; } /// @@ -63,19 +56,30 @@ public ProgressOperationBase( public abstract void Progress(IAsyncOperationWithProgress operation, TProgressData progress); /// - /// Gets the result of the async operation. + /// Starts the operation and executes it as task. /// Supports cancellation. /// + /// Lambda with operation. /// TOperationReturn. - public async Task GetResult() + public async Task ExecuteAsync(Func> func) { - return await this.operation.AsTask(this.PwshCmdlet.GetCancellationToken()); + var operation = func(); + operation.Progress = this.Progress; + + try + { + return await operation.AsTask(this.PwshCmdlet.GetCancellationToken()); + } + finally + { + this.Complete(); + } } /// /// Completes progress for this activity. /// - public void CompleteProgress() + protected virtual void Complete() { this.PwshCmdlet.CompleteProgress(this.ActivityId, this.Activity, Resources.Completed, true); } diff --git a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/UninstallProgressOperation.cs b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/UninstallOperationWithProgress.cs similarity index 61% rename from src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/UninstallProgressOperation.cs rename to src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/UninstallOperationWithProgress.cs index 5b40994e72..91bd82495d 100644 --- a/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/UninstallProgressOperation.cs +++ b/src/PowerShell/Microsoft.WinGet.Client.Engine/Helpers/UninstallOperationWithProgress.cs @@ -1,5 +1,5 @@ // ----------------------------------------------------------------------------- -// +// // Copyright (c) Microsoft Corporation. Licensed under the MIT License. // // ----------------------------------------------------------------------------- @@ -15,24 +15,16 @@ namespace Microsoft.WinGet.Client.Engine.Helpers /// /// Handler progress for uninstall. /// - internal class UninstallProgressOperation : ProgressOperationBase + internal class UninstallOperationWithProgress : OperationWithProgressBase { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// A instance. /// Activity. - /// Operation. - public UninstallProgressOperation(PowerShellCmdlet pwshCmdlet, string activity, IAsyncOperationWithProgress operation) - : base(pwshCmdlet, activity, operation) + public UninstallOperationWithProgress(PowerShellCmdlet pwshCmdlet, string activity) + : base(pwshCmdlet, activity) { - ProgressRecord record = new (this.ActivityId, this.Activity, Resources.Uninstalling) - { - RecordType = ProgressRecordType.Processing, - }; - record.StatusDescription = Resources.Uninstalling; - record.PercentComplete = 1; - this.PwshCmdlet.Write(StreamType.Progress, record); } ///