winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

Converters.h (11949B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include "PackageMatchFilter.g.h"
      5 #include <AppInstallerArchitecture.h>
      6 #include <winget/RepositorySource.h>
      7 #include <Workflows/WorkflowBase.h>
      8 #include <winget/Authentication.h>
      9 
     10 namespace winrt::Microsoft::Management::Deployment::implementation
     11 {
     12     winrt::Microsoft::Management::Deployment::PackageMatchField GetDeploymentMatchField(::AppInstaller::Repository::PackageMatchField field);
     13     ::AppInstaller::Repository::PackageMatchField GetRepositoryMatchField(winrt::Microsoft::Management::Deployment::PackageMatchField field);
     14     winrt::Microsoft::Management::Deployment::PackageFieldMatchOption GetDeploymentMatchOption(::AppInstaller::Repository::MatchType type);
     15     ::AppInstaller::Repository::MatchType GetRepositoryMatchType(winrt::Microsoft::Management::Deployment::PackageFieldMatchOption option);
     16     ::AppInstaller::Repository::CompositeSearchBehavior GetRepositoryCompositeSearchBehavior(winrt::Microsoft::Management::Deployment::CompositeSearchBehavior searchBehavior);
     17     ::AppInstaller::Repository::PackageVersionMetadata GetRepositoryPackageVersionMetadata(winrt::Microsoft::Management::Deployment::PackageVersionMetadataField packageVersionMetadataField);
     18     winrt::Microsoft::Management::Deployment::FindPackagesResultStatus FindPackagesResultStatus(winrt::hresult hresult);
     19     std::optional<::AppInstaller::Utility::Architecture> GetUtilityArchitecture(winrt::Windows::System::ProcessorArchitecture architecture);
     20     std::optional<winrt::Windows::System::ProcessorArchitecture> GetWindowsSystemProcessorArchitecture(::AppInstaller::Utility::Architecture architecture);
     21     std::pair<::AppInstaller::Manifest::ScopeEnum, bool> GetManifestScope(winrt::Microsoft::Management::Deployment::PackageInstallScope scope);
     22     ::AppInstaller::Manifest::InstallerTypeEnum GetManifestInstallerType(winrt::Microsoft::Management::Deployment::PackageInstallerType installerType);
     23     winrt::Microsoft::Management::Deployment::PackageInstallerType GetDeploymentInstallerType(::AppInstaller::Manifest::InstallerTypeEnum installerType);
     24     winrt::Microsoft::Management::Deployment::PackageInstallerScope GetDeploymentInstallerScope(::AppInstaller::Manifest::ScopeEnum installerScope);
     25     ::AppInstaller::Manifest::ScopeEnum GetManifestUninstallScope(winrt::Microsoft::Management::Deployment::PackageUninstallScope scope);
     26     winrt::Microsoft::Management::Deployment::ElevationRequirement GetDeploymentElevationRequirement(::AppInstaller::Manifest::ElevationRequirementEnum elevationRequirement);
     27     winrt::Microsoft::Management::Deployment::IconFileType GetDeploymentIconFileType(::AppInstaller::Manifest::IconFileTypeEnum iconFileType);
     28     winrt::Microsoft::Management::Deployment::IconResolution GetDeploymentIconResolution(::AppInstaller::Manifest::IconResolutionEnum iconResolution);
     29     winrt::Microsoft::Management::Deployment::IconTheme GetDeploymentIconTheme(::AppInstaller::Manifest::IconThemeEnum iconTheme);
     30     winrt::Microsoft::Management::Deployment::AuthenticationType GetDeploymentAuthenticationType(::AppInstaller::Authentication::AuthenticationType authType);
     31     ::AppInstaller::Authentication::AuthenticationMode GetAuthenticationMode(winrt::Microsoft::Management::Deployment::AuthenticationMode authMode);
     32     ::AppInstaller::Authentication::AuthenticationArguments GetAuthenticationArguments(winrt::Microsoft::Management::Deployment::AuthenticationArguments authArgs);
     33     ::AppInstaller::Manifest::ScopeEnum GetManifestRepairScope(winrt::Microsoft::Management::Deployment::PackageRepairScope scope);
     34     winrt::Microsoft::Management::Deployment::AddPackageCatalogStatus GetAddPackageCatalogOperationStatus(winrt::hresult hresult);
     35     winrt::Microsoft::Management::Deployment::RemovePackageCatalogStatus GetRemovePackageCatalogOperationStatus(winrt::hresult hresult);
     36     ::AppInstaller::Manifest::PlatformEnum GetPlatformEnum(winrt::Microsoft::Management::Deployment::WindowsPlatform value);
     37 
     38 #define WINGET_GET_OPERATION_RESULT_STATUS(_installResultStatus_, _uninstallResultStatus_, _downloadResultStatus_, _repairResultStatus_) \
     39     if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::InstallResultStatus>) \
     40     { \
     41         resultStatus = TStatus::_installResultStatus_; \
     42     } \
     43     else if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::UninstallResultStatus>) \
     44     { \
     45         resultStatus = TStatus::_uninstallResultStatus_; \
     46     } \
     47     else if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::DownloadResultStatus>) \
     48     { \
     49         resultStatus = TStatus::_downloadResultStatus_; \
     50     } \
     51     else if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::RepairResultStatus>) \
     52     { \
     53         resultStatus = TStatus::_repairResultStatus_; \
     54     } \
     55 
     56     template <typename TStatus>
     57     TStatus GetOperationResultStatus(::AppInstaller::CLI::Workflow::ExecutionStage executionStage, winrt::hresult hresult)
     58     {
     59         TStatus resultStatus = TStatus::Ok;
     60 
     61         // Map some known hresults to specific statuses, otherwise use the execution stage to determine the status.
     62         switch (hresult)
     63         {
     64         case S_OK:
     65             resultStatus = TStatus::Ok;
     66             break;
     67         case APPINSTALLER_CLI_ERROR_MSSTORE_BLOCKED_BY_POLICY:
     68         case APPINSTALLER_CLI_ERROR_MSSTORE_APP_BLOCKED_BY_POLICY:
     69         case APPINSTALLER_CLI_ERROR_EXPERIMENTAL_FEATURE_DISABLED:
     70         case APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY:
     71             resultStatus = TStatus::BlockedByPolicy;
     72             break;
     73         case APPINSTALLER_CLI_ERROR_INVALID_MANIFEST:
     74             resultStatus = TStatus::ManifestError;
     75             break;
     76         case E_INVALIDARG:
     77         case APPINSTALLER_CLI_ERROR_INVALID_CL_ARGUMENTS:
     78             resultStatus = TStatus::InvalidOptions;
     79             break;
     80         case APPINSTALLER_CLI_ERROR_NO_APPLICABLE_INSTALLER:
     81             WINGET_GET_OPERATION_RESULT_STATUS(NoApplicableInstallers, InternalError, NoApplicableInstallers, NoApplicableRepairer);
     82             break;
     83         case APPINSTALLER_CLI_ERROR_UPDATE_NOT_APPLICABLE:
     84         case APPINSTALLER_CLI_ERROR_UPGRADE_VERSION_UNKNOWN:
     85         case APPINSTALLER_CLI_ERROR_UPGRADE_VERSION_NOT_NEWER:
     86             WINGET_GET_OPERATION_RESULT_STATUS(NoApplicableUpgrade, InternalError, InternalError, InternalError);
     87             break;
     88         case APPINSTALLER_CLI_ERROR_NO_UNINSTALL_INFO_FOUND:
     89         case APPINSTALLER_CLI_ERROR_EXEC_UNINSTALL_COMMAND_FAILED:
     90             WINGET_GET_OPERATION_RESULT_STATUS(InstallError, UninstallError, InternalError, InternalError);
     91             break;
     92         case APPINSTALLER_CLI_ERROR_NO_REPAIR_INFO_FOUND:
     93         case APPINSTALLER_CLI_ERROR_REPAIR_NOT_APPLICABLE:
     94         case APPINSTALLER_CLI_ERROR_EXEC_REPAIR_FAILED:
     95         case APPINSTALLER_CLI_ERROR_REPAIR_NOT_SUPPORTED:
     96         case APPINSTALLER_CLI_ERROR_ADMIN_CONTEXT_REPAIR_PROHIBITED:
     97             WINGET_GET_OPERATION_RESULT_STATUS(InternalError, InternalError, InternalError, RepairError);
     98             break;
     99         case APPINSTALLER_CLI_ERROR_PACKAGE_AGREEMENTS_NOT_ACCEPTED:
    100             WINGET_GET_OPERATION_RESULT_STATUS(PackageAgreementsNotAccepted, InternalError, PackageAgreementsNotAccepted, PackageAgreementsNotAccepted);
    101             break;
    102         case APPINSTALLER_CLI_ERROR_CANNOT_WRITE_TO_UPLEVEL_INDEX:
    103         case APPINSTALLER_CLI_ERROR_INDEX_INTEGRITY_COMPROMISED:
    104         case APPINSTALLER_CLI_ERROR_YAML_INIT_FAILED:
    105         case APPINSTALLER_CLI_ERROR_YAML_INVALID_MAPPING_KEY:
    106         case APPINSTALLER_CLI_ERROR_YAML_DUPLICATE_MAPPING_KEY:
    107         case APPINSTALLER_CLI_ERROR_YAML_INVALID_OPERATION:
    108         case APPINSTALLER_CLI_ERROR_YAML_DOC_BUILD_FAILED:
    109         case APPINSTALLER_CLI_ERROR_YAML_INVALID_EMITTER_STATE:
    110         case APPINSTALLER_CLI_ERROR_YAML_INVALID_DATA:
    111         case APPINSTALLER_CLI_ERROR_LIBYAML_ERROR:
    112         case APPINSTALLER_CLI_ERROR_INTERNAL_ERROR:
    113             resultStatus = TStatus::InternalError;
    114             break;
    115         default:
    116             switch (executionStage)
    117             {
    118             case ::AppInstaller::CLI::Workflow::ExecutionStage::Initial:
    119                 resultStatus = TStatus::InternalError;
    120                 break;
    121             case ::AppInstaller::CLI::Workflow::ExecutionStage::ParseArgs:
    122                 resultStatus = TStatus::InvalidOptions;
    123                 break;
    124             case ::AppInstaller::CLI::Workflow::ExecutionStage::Discovery:
    125                 resultStatus = TStatus::CatalogError;
    126                 break;
    127             case ::AppInstaller::CLI::Workflow::ExecutionStage::Download:
    128                 WINGET_GET_OPERATION_RESULT_STATUS(DownloadError, InternalError, DownloadError, DownloadError);
    129                 break;
    130             case ::AppInstaller::CLI::Workflow::ExecutionStage::PreExecution:
    131                 resultStatus = TStatus::InternalError;
    132                 break;
    133             case ::AppInstaller::CLI::Workflow::ExecutionStage::Execution:
    134                 WINGET_GET_OPERATION_RESULT_STATUS(InstallError, UninstallError, InternalError, RepairError);
    135                 break;
    136             case ::AppInstaller::CLI::Workflow::ExecutionStage::PostExecution:
    137                 resultStatus = TStatus::InternalError;
    138                 break;
    139             default:
    140                 resultStatus = TStatus::InternalError;
    141                 break;
    142             }
    143         }
    144 
    145         return resultStatus;
    146     }
    147 
    148     template <typename TStatus>
    149     TStatus HandleCommonCatalogOperationStatus(winrt::hresult hresult)
    150     {
    151         // Common status handling for AddPackageCatalogStatus and RemovePackageCatalogStatus.
    152         if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::AddPackageCatalogStatus> || std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::RemovePackageCatalogStatus>)
    153         {
    154             switch (hresult)
    155             {
    156             case APPINSTALLER_CLI_ERROR_COMMAND_REQUIRES_ADMIN:
    157             case E_ACCESSDENIED:
    158                 return TStatus::AccessDenied;
    159             case APPINSTALLER_CLI_ERROR_INVALID_CL_ARGUMENTS:
    160             case E_INVALIDARG:
    161                 return TStatus::InvalidOptions;
    162             default:
    163                 break;
    164             }
    165         }
    166 
    167         // Common status handling for AddPackageCatalogStatus, RemovePackageCatalogStatus, and RefreshPackageCatalogStatus.
    168         switch (hresult)
    169         {
    170         case S_OK:
    171             return TStatus::Ok;
    172         case APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY:
    173             return TStatus::GroupPolicyError;
    174         case APPINSTALLER_CLI_ERROR_SOURCE_DATA_INTEGRITY_FAILURE:
    175             return TStatus::CatalogError;
    176         case APPINSTALLER_CLI_ERROR_INTERNAL_ERROR:
    177         default:
    178             return TStatus::InternalError;
    179         }
    180     }
    181 
    182     template <typename TStatus>
    183     TStatus GetPackageCatalogOperationStatus(winrt::hresult hresult)
    184     {
    185         if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::AddPackageCatalogStatus>)
    186         {
    187             return GetAddPackageCatalogOperationStatus(hresult);
    188         }
    189         else if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::RemovePackageCatalogStatus>)
    190         {
    191             return GetRemovePackageCatalogOperationStatus(hresult);
    192         }
    193         else if constexpr (std::is_same_v<TStatus, winrt::Microsoft::Management::Deployment::RefreshPackageCatalogStatus>)
    194         {
    195             return HandleCommonCatalogOperationStatus<RefreshPackageCatalogStatus>(hresult);
    196         }
    197         else
    198         {
    199             throw winrt::hresult_error(E_UNEXPECTED);
    200         }
    201     }
    202 }