-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathPackageManagerWrapper.cs
More file actions
189 lines (170 loc) · 7.16 KB
/
PackageManagerWrapper.cs
File metadata and controls
189 lines (170 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// -----------------------------------------------------------------------------
// <copyright file="PackageManagerWrapper.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace Microsoft.WinGet.Client.Engine.Helpers
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Management.Deployment;
using Microsoft.WinGet.Client.Engine.Common;
using Microsoft.WinGet.Client.Engine.Exceptions;
using Windows.Foundation;
/// <summary>
/// Wrapper for PackageManager that handles rpc disconnections.
/// The object is disconnected when the server is killed or on an update.
/// </summary>
internal sealed class PackageManagerWrapper
{
private static readonly Lazy<PackageManagerWrapper> Lazy = new (() => new PackageManagerWrapper());
private PackageManager packageManager = null!;
private PackageManagerWrapper()
{
}
/// <summary>
/// Gets the instance object.
/// </summary>
public static PackageManagerWrapper Instance
{
get { return Lazy.Value; }
}
/// <summary>
/// Wrapper for InstallPackageAsync.
/// </summary>
/// <param name="package">The package to install.</param>
/// <param name="options">The install options.</param>
/// <returns>An async operation with progress.</returns>
public IAsyncOperationWithProgress<InstallResult, InstallProgress> InstallPackageAsync(CatalogPackage package, InstallOptions options)
{
return this.Execute(
() => this.packageManager.InstallPackageAsync(package, options),
false);
}
/// <summary>
/// Wrapper for UpgradePackageAsync.
/// </summary>
/// <param name="package">The package to upgrade.</param>
/// <param name="options">The install options.</param>
/// <returns>An async operation with progress.</returns>
public IAsyncOperationWithProgress<InstallResult, InstallProgress> UpgradePackageAsync(CatalogPackage package, InstallOptions options)
{
return this.Execute(
() => this.packageManager.UpgradePackageAsync(package, options),
false);
}
/// <summary>
/// Wrapper for UninstallPackageAsync.
/// </summary>
/// <param name="package">The package to uninstall.</param>
/// <param name="options">The uninstall options.</param>
/// <returns>An async operation with progress.</returns>
public IAsyncOperationWithProgress<UninstallResult, UninstallProgress> UninstallPackageAsync(CatalogPackage package, UninstallOptions options)
{
return this.Execute(
() => this.packageManager.UninstallPackageAsync(package, options),
false);
}
/// <summary>
/// Wrapper for DownloadPackageAsync.
/// </summary>
/// <param name="package">The package to download.</param>
/// <param name="options">The download options.</param>
/// <returns>An async operation with progress.</returns>
public IAsyncOperationWithProgress<DownloadResult, PackageDownloadProgress> DownloadPackageAsync(CatalogPackage package, DownloadOptions options)
{
return this.Execute(
() => this.packageManager.DownloadPackageAsync(package, options),
false);
}
/// <summary>
/// Wrapper for RepairPackagesAsync.
/// </summary>
/// <param name="package">The package to repair.</param>
/// <param name="options">The repair options.</param>
/// <returns>An async operation with progress.</returns>
public IAsyncOperationWithProgress<RepairResult, RepairProgress> RepairPackageAsync(CatalogPackage package, RepairOptions options)
{
return this.Execute(
() => this.packageManager.RepairPackageAsync(package, options),
false);
}
/// <summary>
/// Wrapper for GetPackageCatalogs.
/// </summary>
/// <returns>A list of PackageCatalogReferences.</returns>
public IReadOnlyList<PackageCatalogReference> GetPackageCatalogs()
{
return this.Execute(
() => this.packageManager.GetPackageCatalogs(),
true);
}
/// <summary>
/// Wrapper for GetPackageCatalogByName.
/// </summary>
/// <param name="source">The name of the source.</param>
/// <returns>A PackageCatalogReference.</returns>
public PackageCatalogReference GetPackageCatalogByName(string source)
{
return this.Execute(
() => this.packageManager.GetPackageCatalogByName(source),
true);
}
/// <summary>
/// Wrapper for CreateCompositePackageCatalog.
/// </summary>
/// <param name="options">CreateCompositePackageCatalogOptions.</param>
/// <returns>A PackageCatalogReference.</returns>
public PackageCatalogReference CreateCompositePackageCatalog(CreateCompositePackageCatalogOptions options)
{
return this.Execute(
() => this.packageManager.CreateCompositePackageCatalog(options),
false);
}
/// <summary>
/// Gets the version of the package manager that is running.
/// </summary>
/// <returns>The version string.</returns>
public string? GetVersion()
{
try
{
return this.Execute(() => this.packageManager.Version, true);
}
catch
{
return null;
}
}
private TReturn Execute<TReturn>(Func<TReturn> func, bool canRetry)
{
if (Utilities.UsesInProcWinget && Utilities.ThreadIsSTA)
{
// If you failed here, then you didn't wrap your call in ManagementDeploymentCommand.Execute
throw new SingleThreadedApartmentException();
}
bool stopRetry = false;
while (true)
{
if (this.packageManager == null)
{
this.packageManager = ManagementDeploymentFactory.Instance.CreatePackageManager();
}
try
{
return func();
}
catch (COMException ex) when (ex.HResult == ErrorCode.RpcServerUnavailable || ex.HResult == ErrorCode.RpcCallFailed)
{
this.packageManager = null!;
if (stopRetry || !canRetry)
{
throw;
}
stopRetry = true;
}
}
}
}
}