UninstallPackageCommand.cs (5067B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="UninstallPackageCommand.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 /// Uninstalls a package from the local system. 20 /// </summary> 21 public sealed class UninstallPackageCommand : PackageCommand 22 { 23 /// <summary> 24 /// Initializes a new instance of the <see cref="UninstallPackageCommand"/> class. 25 /// </summary> 26 /// <param name="psCmdlet">Caller cmdlet.</param> 27 /// <param name="psCatalogPackage">PSCatalogPackage.</param> 28 /// <param name="version">Version to install.</param> 29 /// <param name="log">Logging file location.</param> 30 /// <param name="id">Package identifier.</param> 31 /// <param name="name">Name of package.</param> 32 /// <param name="moniker">Moniker of package.</param> 33 /// <param name="source">Source to search. If null, all are searched.</param> 34 /// <param name="query">Match against any field of a package.</param> 35 public UninstallPackageCommand( 36 PSCmdlet psCmdlet, 37 PSCatalogPackage psCatalogPackage, 38 string version, 39 string log, 40 string id, 41 string name, 42 string moniker, 43 string source, 44 string[] query) 45 : base(psCmdlet) 46 { 47 // PackageCommand. 48 if (psCatalogPackage != null) 49 { 50 this.CatalogPackage = psCatalogPackage; 51 } 52 53 this.Version = version; 54 55 // FinderCommand 56 this.Id = id; 57 this.Name = name; 58 this.Moniker = moniker; 59 this.Source = source; 60 this.Query = query; 61 62 // UninstallPackageCommand 63 this.Log = log; 64 } 65 66 /// <summary> 67 /// Gets or sets the path to the logging file. 68 /// </summary> 69 private string? Log { get; set; } 70 71 /// <summary> 72 /// Process uninstall package. 73 /// </summary> 74 /// <param name="psPackageFieldMatchOption">PSPackageFieldMatchOption.</param> 75 /// <param name="psPackageUninstallMode">PSPackageUninstallMode.</param> 76 /// <param name="force">Force.</param> 77 public void Uninstall( 78 string psPackageFieldMatchOption, 79 string psPackageUninstallMode, 80 bool force) 81 { 82 var result = this.Execute( 83 async () => await this.GetPackageAndExecuteAsync( 84 CompositeSearchBehavior.LocalCatalogs, 85 PSEnumHelpers.ToPackageFieldMatchOption(psPackageFieldMatchOption), 86 async (package, version) => 87 { 88 UninstallOptions options = this.GetUninstallOptions(version, PSEnumHelpers.ToPackageUninstallMode(psPackageUninstallMode), force); 89 return await this.UninstallPackageAsync(package, options); 90 })); 91 92 if (result != null) 93 { 94 this.Write(StreamType.Object, new PSUninstallResult(result.Item1, result.Item2)); 95 } 96 } 97 98 private UninstallOptions GetUninstallOptions( 99 PackageVersionId? version, 100 PackageUninstallMode packageUninstallMode, 101 bool force) 102 { 103 var options = ManagementDeploymentFactory.Instance.CreateUninstallOptions(); 104 options.Force = force; 105 if (this.Log != null) 106 { 107 options.LogOutputPath = this.Log; 108 } 109 110 options.PackageUninstallMode = packageUninstallMode; 111 112 if (version != null) 113 { 114 options.PackageVersionId = version; 115 } 116 117 return options; 118 } 119 120 private async Task<UninstallResult> UninstallPackageAsync( 121 CatalogPackage package, 122 UninstallOptions options) 123 { 124 var progressOperation = new UninstallOperationWithProgress( 125 this, 126 string.Format(Resources.ProgressRecordActivityUninstalling, package.Name)); 127 return await progressOperation.ExecuteAsync( 128 () => PackageManagerWrapper.Instance.UninstallPackageAsync(package, options)); 129 } 130 } 131 }