winget-cli

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

PSEnumHelpers.cs (7372B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PSEnumHelpers.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Client.Engine.Helpers
      8 {
      9     using System;
     10     using Microsoft.Management.Deployment;
     11     using Newtonsoft.Json.Linq;
     12     using Windows.System;
     13 
     14     /// <summary>
     15     /// Extension methods for PS Enum wrappers for Microsoft.Management.Deployment enums.
     16     /// </summary>
     17     internal static class PSEnumHelpers
     18     {
     19         /// <summary>
     20         /// Checks if the provided enum string value matches the 'Default' value for PS Enums.
     21         /// </summary>
     22         /// <param name="value">Enum string value.</param>
     23         /// <returns>Boolean value.</returns>
     24         public static bool IsDefaultEnum(string value)
     25         {
     26             return string.Equals(value, "Default", StringComparison.OrdinalIgnoreCase);
     27         }
     28 
     29         /// <summary>
     30         /// Converts PSPackageInstallMode string value to PackageInstallMode.
     31         /// </summary>
     32         /// <param name="value">PSPackageInstallMode to string value.</param>
     33         /// <returns>PackageInstallMode.</returns>
     34         public static PackageInstallMode ToPackageInstallMode(string value)
     35         {
     36             return value switch
     37             {
     38                 "Default" => PackageInstallMode.Default,
     39                 "Silent" => PackageInstallMode.Silent,
     40                 "Interactive" => PackageInstallMode.Interactive,
     41                 _ => throw new InvalidOperationException(),
     42             };
     43         }
     44 
     45         /// <summary>
     46         /// Converts PSPackageInstallScope string value to PackageInstallScope.
     47         /// </summary>
     48         /// <param name="value">PSPackageInstallScope to string value.</param>
     49         /// <returns>PackageInstallScope.</returns>
     50         public static PackageInstallScope ToPackageInstallScope(string value)
     51         {
     52             return value switch
     53             {
     54                 "Any" => PackageInstallScope.Any,
     55                 "User" => PackageInstallScope.User,
     56                 "System" => PackageInstallScope.System,
     57                 "UserOrUnknown" => PackageInstallScope.UserOrUnknown,
     58                 "SystemOrUnknown" => PackageInstallScope.SystemOrUnknown,
     59                 _ => throw new InvalidOperationException(),
     60             };
     61         }
     62 
     63         /// <summary>
     64         /// Converts PSProcessorArchitecture string value to ProcessorArchitecture.
     65         /// </summary>
     66         /// <param name="value">PSProcessorArchitecture to string value.</param>
     67         /// <returns>ProcessorArchitecture.</returns>
     68         public static ProcessorArchitecture ToProcessorArchitecture(string value)
     69         {
     70             return value switch
     71             {
     72                 "X86" => ProcessorArchitecture.X86,
     73                 "Arm" => ProcessorArchitecture.Arm,
     74                 "X64" => ProcessorArchitecture.X64,
     75                 "Arm64" => ProcessorArchitecture.Arm64,
     76                 _ => throw new InvalidOperationException(),
     77             };
     78         }
     79 
     80         /// <summary>
     81         /// Converts PSPackageUninstallMode string value to PackageUninstallMode.
     82         /// </summary>
     83         /// <param name="value">PSPackageUninstallMode string value.</param>
     84         /// <returns>PackageUninstallMode.</returns>
     85         public static PackageUninstallMode ToPackageUninstallMode(string value)
     86         {
     87             return value switch
     88             {
     89                 "Default" => PackageUninstallMode.Default,
     90                 "Silent" => PackageUninstallMode.Silent,
     91                 "Interactive" => PackageUninstallMode.Interactive,
     92                 _ => throw new InvalidOperationException(),
     93             };
     94         }
     95 
     96         /// <summary>
     97         /// Converts PSPackageFieldMatchOption string value to PackageFieldMatchOption.
     98         /// </summary>
     99         /// <param name="value">PSPackageFieldMatchOption string value.</param>
    100         /// <returns>PackageFieldMatchOption.</returns>
    101         public static PackageFieldMatchOption ToPackageFieldMatchOption(string value)
    102         {
    103             return value switch
    104             {
    105                 "Equals" => PackageFieldMatchOption.Equals,
    106                 "EqualsCaseInsensitive" => PackageFieldMatchOption.EqualsCaseInsensitive,
    107                 "StartsWithCaseInsensitive" => PackageFieldMatchOption.StartsWithCaseInsensitive,
    108                 "ContainsCaseInsensitive" => PackageFieldMatchOption.ContainsCaseInsensitive,
    109                 _ => throw new InvalidOperationException(),
    110             };
    111         }
    112 
    113         /// <summary>
    114         /// Converts PSPackageInstallerType string value to PackageInstallerType.
    115         /// </summary>
    116         /// <param name="value">PSPackageInstallerType string value.</param>
    117         /// <returns>PackageInstallerType.</returns>
    118         public static PackageInstallerType ToPackageInstallerType(string value)
    119         {
    120             return value switch
    121             {
    122                 "Unknown" => PackageInstallerType.Unknown,
    123                 "Inno" => PackageInstallerType.Inno,
    124                 "Wix" => PackageInstallerType.Wix,
    125                 "Msi" => PackageInstallerType.Msi,
    126                 "Nullsoft" => PackageInstallerType.Nullsoft,
    127                 "Zip" => PackageInstallerType.Zip,
    128                 "Msix" => PackageInstallerType.Msix,
    129                 "Exe" => PackageInstallerType.Exe,
    130                 "Burn" => PackageInstallerType.Burn,
    131                 "MSStore" => PackageInstallerType.MSStore,
    132                 "Portable" => PackageInstallerType.Portable,
    133                 _ => throw new InvalidOperationException(),
    134             };
    135         }
    136 
    137         /// <summary>
    138         /// Converts PSPackageRepairMode string value to PackageRepairMode.
    139         /// </summary>
    140         /// <param name="value">PSPackageRepairMode string value.</param>
    141         /// <returns>PackageRepairMode.</returns>
    142         public static PackageRepairMode ToPackageRepairMode(string value)
    143         {
    144             return value switch
    145             {
    146                 "Default" => PackageRepairMode.Default,
    147                 "Silent" => PackageRepairMode.Silent,
    148                 "Interactive" => PackageRepairMode.Interactive,
    149                 _ => throw new InvalidOperationException(),
    150             };
    151         }
    152 
    153         /// <summary>
    154         /// Converts PSWindowsPlatform string value to WindowsPlatform.
    155         /// </summary>
    156         /// <param name="value">PSWindowsPlatform string value.</param>
    157         /// <returns>WindowsPlatform.</returns>
    158         public static WindowsPlatform ToWindowsPlatform(string value)
    159         {
    160             return value switch
    161             {
    162                 "Default" => WindowsPlatform.Unknown,
    163                 "Universal" => WindowsPlatform.Universal,
    164                 "Desktop" => WindowsPlatform.Desktop,
    165                 "IoT" => WindowsPlatform.IoT,
    166                 "Team" => WindowsPlatform.Team,
    167                 "Holographic" => WindowsPlatform.Holographic,
    168                 _ => throw new InvalidOperationException(),
    169             };
    170         }
    171     }
    172 }