|
1 | 1 | // Copyright (c) Microsoft Corporation and Contributors |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
| 4 | +using DevHome.Logging; |
4 | 5 | using DevHome.SetupFlow.Common.Extensions; |
5 | 6 | using DevHome.SetupFlow.Common.Helpers; |
6 | 7 | using DevHome.SetupFlow.Common.WindowsPackageManager; |
@@ -38,53 +39,63 @@ public IAsyncOperation<ElevatedInstallTaskResult> InstallPackage(string packageI |
38 | 39 | { |
39 | 40 | return Task.Run(async () => |
40 | 41 | { |
41 | | - Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Elevated install requested for package [{packageId}] from catalog [{catalogName}]"); |
42 | 42 | var result = new ElevatedInstallTaskResult(); |
43 | | - |
44 | | - var packageManager = _wingetFactory.CreatePackageManager(); |
45 | | - |
46 | | - Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Connecting to catalog [{catalogName}]"); |
47 | | - var catalogReference = packageManager.GetPackageCatalogByName(catalogName); |
48 | | - var connectResult = await catalogReference.ConnectAsync(); |
49 | | - if (connectResult.Status != ConnectResultStatus.Ok) |
| 43 | + try |
50 | 44 | { |
51 | | - Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to connect to the catalog [{catalogName}] with status {connectResult.Status}"); |
52 | | - result.TaskAttempted = false; |
| 45 | + Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Elevated install requested for package [{packageId}] from catalog [{catalogName}]"); |
| 46 | + |
| 47 | + var packageManager = _wingetFactory.CreatePackageManager(); |
| 48 | + |
| 49 | + Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Connecting to catalog [{catalogName}]"); |
| 50 | + var catalogReference = packageManager.GetPackageCatalogByName(catalogName); |
| 51 | + var connectResult = await catalogReference.ConnectAsync(); |
| 52 | + if (connectResult.Status != ConnectResultStatus.Ok) |
| 53 | + { |
| 54 | + Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to connect to the catalog [{catalogName}] with status {connectResult.Status}"); |
| 55 | + result.TaskAttempted = false; |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Finding package [{packageId}] in catalog"); |
| 60 | + var findOptions = CreateFindOptionsForPackageId(packageId); |
| 61 | + var findResult = connectResult.PackageCatalog.FindPackages(findOptions); |
| 62 | + if (findResult.Status != FindPackagesResultStatus.Ok |
| 63 | + || findResult.Matches.Count < 1 |
| 64 | + || findResult.WasLimitExceeded) |
| 65 | + { |
| 66 | + Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to find package. Status={findResult.Status}, Matches Count={findResult.Matches.Count}, LimitReached={findResult.WasLimitExceeded}"); |
| 67 | + result.TaskAttempted = false; |
| 68 | + return result; |
| 69 | + } |
| 70 | + |
| 71 | + var packageToInstall = findResult.Matches[0].CatalogPackage; |
| 72 | + |
| 73 | + var installOptions = _wingetFactory.CreateInstallOptions(); |
| 74 | + installOptions.PackageInstallMode = PackageInstallMode.Silent; |
| 75 | + |
| 76 | + Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Initiating install of package {packageId}"); |
| 77 | + var installResult = await packageManager.InstallPackageAsync(packageToInstall, installOptions); |
| 78 | + var extendedErrorCode = installResult.ExtendedErrorCode?.HResult ?? HRESULT.S_OK; |
| 79 | + |
| 80 | + // Contract version 4 |
| 81 | + var installErrorCode = installResult.GetValueOrDefault(res => res.InstallerErrorCode, HRESULT.S_OK); |
| 82 | + |
| 83 | + Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Install finished. Status={installResult.Status}, InstallerErrorCode={installErrorCode}, ExtendedErrorCode={extendedErrorCode}, RebootRequired={installResult.RebootRequired}"); |
| 84 | + result.TaskAttempted = true; |
| 85 | + result.TaskSucceeded = installResult.Status == InstallResultStatus.Ok; |
| 86 | + result.RebootRequired = installResult.RebootRequired; |
| 87 | + result.Status = (int)installResult.Status; |
| 88 | + result.ExtendedErrorCode = extendedErrorCode; |
| 89 | + result.InstallerErrorCode = installErrorCode; |
| 90 | + |
53 | 91 | return result; |
54 | 92 | } |
55 | | - |
56 | | - Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Finding package [{packageId}] in catalog"); |
57 | | - var findOptions = CreateFindOptionsForPackageId(packageId); |
58 | | - var findResult = connectResult.PackageCatalog.FindPackages(findOptions); |
59 | | - if (findResult.Status != FindPackagesResultStatus.Ok |
60 | | - || findResult.Matches.Count < 1 |
61 | | - || findResult.WasLimitExceeded) |
| 93 | + catch (Exception e) |
62 | 94 | { |
63 | | - Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to find package. Status={findResult.Status}, Matches Count={findResult.Matches.Count}, LimitReached={findResult.WasLimitExceeded}"); |
64 | | - result.TaskAttempted = false; |
65 | | - return result; |
| 95 | + Log.Logger?.ReportError(Log.Component.AppManagement, "Elevated app install failed.", e); |
| 96 | + result.TaskSucceeded = false; |
66 | 97 | } |
67 | 98 |
|
68 | | - var packageToInstall = findResult.Matches[0].CatalogPackage; |
69 | | - |
70 | | - var installOptions = _wingetFactory.CreateInstallOptions(); |
71 | | - installOptions.PackageInstallMode = PackageInstallMode.Silent; |
72 | | - |
73 | | - Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Initiating install of package {packageId}"); |
74 | | - var installResult = await packageManager.InstallPackageAsync(packageToInstall, installOptions); |
75 | | - var extendedErrorCode = installResult.ExtendedErrorCode?.HResult ?? HRESULT.S_OK; |
76 | | - |
77 | | - // Contract version 4 |
78 | | - var installErrorCode = installResult.GetValueOrDefault(res => res.InstallerErrorCode, HRESULT.S_OK); |
79 | | - |
80 | | - Log.Logger?.ReportInfo(Log.Component.AppManagement, $"Install finished. Status={installResult.Status}, InstallerErrorCode={installErrorCode}, ExtendedErrorCode={extendedErrorCode}, RebootRequired={installResult.RebootRequired}"); |
81 | | - result.TaskAttempted = true; |
82 | | - result.TaskSucceeded = installResult.Status == InstallResultStatus.Ok; |
83 | | - result.RebootRequired = installResult.RebootRequired; |
84 | | - result.Status = (int)installResult.Status; |
85 | | - result.ExtendedErrorCode = extendedErrorCode; |
86 | | - result.InstallerErrorCode = installErrorCode; |
87 | | - |
88 | 99 | return result; |
89 | 100 | }).AsAsyncOperation(); |
90 | 101 | } |
|
0 commit comments