Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
msftrubengu committed Nov 12, 2023
1 parent 1c770f4 commit 0f28ea6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/PowerShell/CommonFiles/PowerShellCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,38 +150,22 @@ private async Task<InstallResult> 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<InstallResult> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,11 @@ private async Task<UninstallResult> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------------
// <copyright file="InstallProgressOperation.cs" company="Microsoft Corporation">
// <copyright file="InstallOperationWithProgress.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
Expand All @@ -13,18 +13,17 @@ namespace Microsoft.WinGet.Client.Engine.Helpers
using Windows.Foundation;

/// <summary>
/// Handler progress for install and update.
/// Handlers install or update operations with progress.
/// </summary>
internal class InstallProgressOperation : ProgressOperationBase<InstallResult, InstallProgress>
internal class InstallOperationWithProgress : OperationWithProgressBase<InstallResult, InstallProgress>
{
/// <summary>
/// Initializes a new instance of the <see cref="InstallProgressOperation"/> class.
/// Initializes a new instance of the <see cref="InstallOperationWithProgress"/> class.
/// </summary>
/// <param name="pwshCmdlet">A <see cref="PowerShellCmdlet" /> instance.</param>
/// <param name="activity">Activity.</param>
/// <param name="operation">Operation.</param>
public InstallProgressOperation(PowerShellCmdlet pwshCmdlet, string activity, IAsyncOperationWithProgress<InstallResult, InstallProgress> operation)
: base(pwshCmdlet, activity, operation)
public InstallOperationWithProgress(PowerShellCmdlet pwshCmdlet, string activity)
: base(pwshCmdlet, activity)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------------
// <copyright file="ProgressOperationBase.cs" company="Microsoft Corporation">
// <copyright file="OperationWithProgressBase.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
Expand All @@ -13,31 +13,24 @@ namespace Microsoft.WinGet.Client.Engine.Helpers
using Windows.Foundation;

/// <summary>
/// Base class to handle progress.
/// Base class for async operations with progress.
/// </summary>
/// <typeparam name="TOperationResult">The operation result.</typeparam>
/// <typeparam name="TProgressData">Progress data.</typeparam>
internal abstract class ProgressOperationBase<TOperationResult, TProgressData>
internal abstract class OperationWithProgressBase<TOperationResult, TProgressData>
{
private IAsyncOperationWithProgress<TOperationResult, TProgressData> operation;

/// <summary>
/// Initializes a new instance of the <see cref="ProgressOperationBase{TOperationResult, TProgressData}"/> class.
/// Initializes a new instance of the <see cref="OperationWithProgressBase{TOperationResult, TProgressData}"/> class.
/// </summary>
/// <param name="pwshCmdlet">A <see cref="PowerShellCmdlet" /> instance.</param>
/// <param name="activity">Activity.</param>
/// <param name="operation">Operation.</param>
public ProgressOperationBase(
public OperationWithProgressBase(
PowerShellCmdlet pwshCmdlet,
string activity,
IAsyncOperationWithProgress<TOperationResult, TProgressData> operation)
string activity)
{
this.PwshCmdlet = pwshCmdlet;
this.ActivityId = pwshCmdlet.GetNewProgressActivityId();
this.Activity = activity;
this.operation = operation;

operation.Progress = this.Progress;
}

/// <summary>
Expand All @@ -63,19 +56,30 @@ public ProgressOperationBase(
public abstract void Progress(IAsyncOperationWithProgress<TOperationResult, TProgressData> operation, TProgressData progress);

/// <summary>
/// Gets the result of the async operation.
/// Starts the operation and executes it as task.
/// Supports cancellation.
/// </summary>
/// <param name="func">Lambda with operation.</param>
/// <returns>TOperationReturn.</returns>
public async Task<TOperationResult> GetResult()
public async Task<TOperationResult> ExecuteAsync(Func<IAsyncOperationWithProgress<TOperationResult, TProgressData>> 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();
}
}

/// <summary>
/// Completes progress for this activity.
/// </summary>
public void CompleteProgress()
protected virtual void Complete()
{
this.PwshCmdlet.CompleteProgress(this.ActivityId, this.Activity, Resources.Completed, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------------
// <copyright file="UninstallProgressOperation.cs" company="Microsoft Corporation">
// <copyright file="UninstallOperationWithProgress.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
Expand All @@ -15,24 +15,16 @@ namespace Microsoft.WinGet.Client.Engine.Helpers
/// <summary>
/// Handler progress for uninstall.
/// </summary>
internal class UninstallProgressOperation : ProgressOperationBase<UninstallResult, UninstallProgress>
internal class UninstallOperationWithProgress : OperationWithProgressBase<UninstallResult, UninstallProgress>
{
/// <summary>
/// Initializes a new instance of the <see cref="UninstallProgressOperation"/> class.
/// Initializes a new instance of the <see cref="UninstallOperationWithProgress"/> class.
/// </summary>
/// <param name="pwshCmdlet">A <see cref="PowerShellCmdlet" /> instance.</param>
/// <param name="activity">Activity.</param>
/// <param name="operation">Operation.</param>
public UninstallProgressOperation(PowerShellCmdlet pwshCmdlet, string activity, IAsyncOperationWithProgress<UninstallResult, UninstallProgress> 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);
}

/// <inheritdoc/>
Expand Down

0 comments on commit 0f28ea6

Please sign in to comment.