winget-cli

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

Factory.cs (10739B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="Factory.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.Processor.PowerShell.Helpers
      8 {
      9     using System;
     10     using System.Collections.Generic;
     11     using System.Linq;
     12     using System.Management.Automation;
     13     using Microsoft.Management.Configuration.Processor.PowerShell.DscResourcesInfo;
     14     using Microsoft.Management.Configuration.Processor.Unit;
     15     using Windows.Security.Cryptography.Certificates;
     16 
     17     /// <summary>
     18     /// Enables creation of PowerShell specific instances of configuration interfaces.
     19     /// </summary>
     20     internal static class Factory
     21     {
     22         private static readonly IEnumerable<string> PublicRepositories = new string[]
     23         {
     24             "https://www.powershellgallery.com/api/v2",
     25         };
     26 
     27         /// <summary>
     28         /// Initializes a new instance of the <see cref="ConfigurationUnitProcessorDetails"/> class.
     29         /// </summary>
     30         /// <param name="unitName">Unit name.</param>
     31         /// <param name="dscResourceInfo">DSC Resource Info.</param>
     32         /// <param name="psModuleInfo">PSModuleInfo.</param>
     33         /// <param name="getModuleInfo">GetModuleInfo.</param>
     34         /// <param name="certs">List of certificates.</param>
     35         /// <returns>An initialized instance of <see cref="ConfigurationUnitProcessorDetails"/>.</returns>
     36         public static ConfigurationUnitProcessorDetails CreateUnitProcessorDetails(
     37             string unitName,
     38             DscResourceInfoInternal? dscResourceInfo,
     39             PSModuleInfo? psModuleInfo,
     40             PSObject? getModuleInfo,
     41             List<Certificate>? certs)
     42         {
     43             if (dscResourceInfo is null &&
     44                 psModuleInfo is null &&
     45                 getModuleInfo is null)
     46             {
     47                 throw new ArgumentException();
     48             }
     49 
     50             ConfigurationUnitProcessorDetails result = new ConfigurationUnitProcessorDetails
     51             {
     52                 UnitType = unitName,
     53             };
     54 
     55             if (dscResourceInfo is not null)
     56             {
     57                 result.IsLocal = true;
     58 
     59                 var settings = new List<IConfigurationUnitSettingDetails>();
     60                 foreach (var properties in dscResourceInfo.Properties)
     61                 {
     62                     settings.Add(CreateUnitSettingDetails(properties));
     63                 }
     64 
     65                 result.Settings = settings;
     66 
     67                 if (psModuleInfo is null)
     68                 {
     69                     result.ModuleName = dscResourceInfo.ModuleName;
     70                     result.Version = dscResourceInfo.Version is not null ? dscResourceInfo.Version.ToString() : null;
     71                 }
     72             }
     73 
     74             if (psModuleInfo is not null)
     75             {
     76                 result.ModuleDocumentationUri = psModuleInfo.HelpInfoUri is not null ? new Uri(psModuleInfo.HelpInfoUri) : null;
     77                 result.UnitIconUri = psModuleInfo.IconUri;
     78                 result.ModuleName = psModuleInfo.Name;
     79                 result.ModuleType = psModuleInfo.ModuleType.ToString();
     80                 result.ModuleDescription = psModuleInfo.Description;
     81                 result.PublishedModuleUri = psModuleInfo.ProjectUri;
     82                 result.Version = psModuleInfo.Version.ToString();
     83                 result.Author = psModuleInfo.Author;
     84                 result.Publisher = psModuleInfo.CompanyName;
     85             }
     86 
     87             if (getModuleInfo is not null)
     88             {
     89                 result.ModuleSource = TryGetPropertyAsString(getModuleInfo, "Repository");
     90                 result.PublishedDate = TryGetPropertyFromDateTimeToDateTimeOffset(getModuleInfo, "PublishedDate");
     91 
     92                 var repoSourceLocation = getModuleInfo.Properties["RepositorySourceLocation"];
     93                 if (repoSourceLocation is not null)
     94                 {
     95                     string? repoSourceLocationValue = repoSourceLocation.Value as string;
     96                     if (repoSourceLocationValue is not null)
     97                     {
     98                         result.IsPublic = PublicRepositories.Any(r => r == repoSourceLocationValue);
     99                     }
    100                 }
    101 
    102                 if (psModuleInfo is null)
    103                 {
    104                     // Type is not the same as this PSModuleType.
    105                     result.UnitIconUri = TryGetPropertyAsUri(getModuleInfo, "IconUri");
    106                     result.ModuleName = TryGetPropertyAsString(getModuleInfo, "Name");
    107                     result.ModuleDescription = TryGetPropertyAsString(getModuleInfo, "Description");
    108                     result.Version = TryGetPropertyAsString(getModuleInfo, "Version");
    109                     result.Author = TryGetPropertyAsString(getModuleInfo, "Author");
    110                     result.Publisher = TryGetPropertyAsString(getModuleInfo, "CompanyName");
    111                 }
    112             }
    113 
    114             result.SigningInformation = certs;
    115 
    116             return result;
    117         }
    118 
    119         /// <summary>
    120         /// Initializes a new instance of the <see cref="Factory"/> class.
    121         /// </summary>
    122         /// <param name="dscResourceInfo">DSC Resource info.</param>
    123         /// <returns>An initialized instance of <see cref="ConfigurationUnitSettingDetails"/>.</returns>
    124         public static ConfigurationUnitSettingDetails CreateUnitSettingDetails(DscResourcePropertyInfoInternal dscResourceInfo)
    125         {
    126             return new ConfigurationUnitSettingDetails
    127             {
    128                 Identifier = dscResourceInfo.Name,
    129                 IsRequired = dscResourceInfo.IsMandatory,
    130                 Type = GetPropertyType(dscResourceInfo.PropertyType),
    131             };
    132         }
    133 
    134         private static string? TryGetPropertyAsString(PSObject getModuleInfo, string getModuleInfoProperty)
    135         {
    136             var moduleProperty = getModuleInfo.Properties[getModuleInfoProperty];
    137             if (moduleProperty is not null)
    138             {
    139                 return moduleProperty.Value as string;
    140             }
    141 
    142             return null;
    143         }
    144 
    145         private static Uri? TryGetPropertyAsUri(PSObject getModuleInfo, string getModuleInfoProperty)
    146         {
    147             var moduleProperty = getModuleInfo.Properties[getModuleInfoProperty];
    148             if (moduleProperty is not null)
    149             {
    150                 string? modulePropertyString = moduleProperty.Value as string;
    151                 if (modulePropertyString is not null)
    152                 {
    153                     return new Uri(modulePropertyString);
    154                 }
    155             }
    156 
    157             return null;
    158         }
    159 
    160         private static DateTimeOffset TryGetPropertyFromDateTimeToDateTimeOffset(PSObject getModuleInfo, string getModuleInfoProperty)
    161         {
    162             var moduleProperty = getModuleInfo.Properties[getModuleInfoProperty];
    163             if (moduleProperty is not null)
    164             {
    165                 DateTime propertyAsDateTime;
    166 
    167                 try
    168                 {
    169                     propertyAsDateTime = (DateTime)moduleProperty.Value;
    170                     return new DateTimeOffset(propertyAsDateTime);
    171                 }
    172                 catch
    173                 {
    174                 }
    175             }
    176 
    177             return default;
    178         }
    179 
    180         private static Windows.Foundation.PropertyType GetPropertyType(string propertyType)
    181         {
    182             switch (propertyType.ToLowerInvariant())
    183             {
    184                 case "[byte]": return Windows.Foundation.PropertyType.UInt8;
    185                 case "[int16]": return Windows.Foundation.PropertyType.Int16;
    186                 case "[uint16]": return Windows.Foundation.PropertyType.UInt16;
    187                 case "[int32]": return Windows.Foundation.PropertyType.Int32;
    188                 case "[uint32]": return Windows.Foundation.PropertyType.UInt32;
    189                 case "[int64]": return Windows.Foundation.PropertyType.Int64;
    190                 case "[uint64]": return Windows.Foundation.PropertyType.UInt64;
    191                 case "[single]": return Windows.Foundation.PropertyType.Single;
    192                 case "[double]": return Windows.Foundation.PropertyType.Double;
    193                 case "[char]": return Windows.Foundation.PropertyType.Char16;
    194                 case "[bool]": return Windows.Foundation.PropertyType.Boolean;
    195                 case "[string]": return Windows.Foundation.PropertyType.String;
    196                 case "[datetime]": return Windows.Foundation.PropertyType.DateTime;
    197                 case "[datetimeoffset]": return Windows.Foundation.PropertyType.DateTime;
    198                 case "[timespan]": return Windows.Foundation.PropertyType.TimeSpan;
    199                 case "[guid]": return Windows.Foundation.PropertyType.Guid;
    200                 case "[byte[]]": return Windows.Foundation.PropertyType.UInt8Array;
    201                 case "[int16[]]": return Windows.Foundation.PropertyType.Int16Array;
    202                 case "[uint16[]]": return Windows.Foundation.PropertyType.UInt16Array;
    203                 case "[int32[]]": return Windows.Foundation.PropertyType.Int32Array;
    204                 case "[uint32[]]": return Windows.Foundation.PropertyType.UInt32Array;
    205                 case "[int64[]]": return Windows.Foundation.PropertyType.Int64Array;
    206                 case "[uint64[]]": return Windows.Foundation.PropertyType.UInt64Array;
    207                 case "[single[]]": return Windows.Foundation.PropertyType.SingleArray;
    208                 case "[double[]]": return Windows.Foundation.PropertyType.DoubleArray;
    209                 case "[char[]]": return Windows.Foundation.PropertyType.Char16Array;
    210                 case "[bool[]]": return Windows.Foundation.PropertyType.BooleanArray;
    211                 case "[string[]]": return Windows.Foundation.PropertyType.StringArray;
    212                 case "[object[]]": return Windows.Foundation.PropertyType.InspectableArray;
    213                 case "[datetime[]]": return Windows.Foundation.PropertyType.DateTimeArray;
    214                 case "[datetimeoffset[]]": return Windows.Foundation.PropertyType.DateTimeArray;
    215                 case "[timespan[]]": return Windows.Foundation.PropertyType.TimeSpanArray;
    216                 case "[guid[]]": return Windows.Foundation.PropertyType.GuidArray;
    217 
    218                 // Everything else will just be an object...
    219                 default: return Windows.Foundation.PropertyType.Inspectable;
    220             }
    221         }
    222     }
    223 }