Utilities.cs (5255B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="Utilities.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Configuration.Engine.Helpers 8 { 9 using System; 10 using System.Linq; 11 using System.Management.Automation; 12 using System.Management.Automation.Host; 13 using System.Security.Principal; 14 using Microsoft.Management.Configuration; 15 using Microsoft.WinGet.Configuration.Engine.PSObjects; 16 17 /// <summary> 18 /// Helper methods. 19 /// </summary> 20 internal static class Utilities 21 { 22 /// <summary> 23 /// Gets a value indicating whether the current assembly is executing in an administrative context. 24 /// </summary> 25 [System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Windows only API")] 26 public static bool ExecutingAsAdministrator 27 { 28 get 29 { 30 WindowsIdentity identity = WindowsIdentity.GetCurrent(); 31 WindowsPrincipal principal = new (identity); 32 return principal.IsInRole(WindowsBuiltInRole.Administrator); 33 } 34 } 35 36 /// <summary> 37 /// Helper for StreamType.Information. Creates a HostInformationMessage with 38 /// the specified information. 39 /// </summary> 40 /// <param name="message">Message.</param> 41 /// <param name="noNewLine">Add not to add a new line.</param> 42 /// <param name="foregroundColor">Optional foreground color.</param> 43 /// <param name="backgroundColor">Optional background color.</param> 44 /// <returns>The information message.</returns> 45 public static HostInformationMessage CreateInformationMessage( 46 string message, 47 bool noNewLine = false, 48 ConsoleColor? foregroundColor = null, 49 ConsoleColor? backgroundColor = null) 50 { 51 var infoMessage = new HostInformationMessage 52 { 53 Message = message, 54 NoNewLine = noNewLine, 55 }; 56 57 try 58 { 59 infoMessage.ForegroundColor = foregroundColor; 60 infoMessage.BackgroundColor = backgroundColor; 61 } 62 catch (HostException) 63 { 64 // Expected if the host is not interactive, or doesn't have Foreground / Background colors. 65 } 66 67 return infoMessage; 68 } 69 70 /// <summary> 71 /// Splits the message into lines. 72 /// </summary> 73 /// <param name="message">Message.</param> 74 /// <param name="maxLines">Max lines.</param> 75 /// <returns>Lines.</returns> 76 public static string[] SplitIntoLines(string message, int maxLines) 77 { 78 var lines = message.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); 79 if (lines.Length > maxLines) 80 { 81 return lines.Take(maxLines).ToArray(); 82 } 83 84 return lines; 85 } 86 87 /// <summary> 88 /// Converts ConfigurationTestResult value to PSConfigurationTestResult. 89 /// </summary> 90 /// <param name="value">ConfigurationTestResult value.</param> 91 /// <returns>PSConfigurationTestResult.</returns> 92 public static PSConfigurationTestResult ToPSConfigurationTestResult(ConfigurationTestResult value) 93 { 94 return value switch 95 { 96 ConfigurationTestResult.Unknown => PSConfigurationTestResult.Unknown, 97 ConfigurationTestResult.Positive => PSConfigurationTestResult.Positive, 98 ConfigurationTestResult.Negative => PSConfigurationTestResult.Negative, 99 ConfigurationTestResult.Failed => PSConfigurationTestResult.Failed, 100 ConfigurationTestResult.NotRun => PSConfigurationTestResult.NotRun, 101 _ => throw new InvalidOperationException(), 102 }; 103 } 104 105 /// <summary> 106 /// Converts ConfigurationUnitState value to PSConfigurationUnitState. 107 /// </summary> 108 /// <param name="value">ConfigurationUnitState value.</param> 109 /// <returns>PSConfigurationUnitState.</returns> 110 public static PSConfigurationUnitState ToPSConfigurationUnitState(ConfigurationUnitState value) 111 { 112 return value switch 113 { 114 ConfigurationUnitState.Unknown => PSConfigurationUnitState.Unknown, 115 ConfigurationUnitState.Pending => PSConfigurationUnitState.Pending, 116 ConfigurationUnitState.InProgress => PSConfigurationUnitState.InProgress, 117 ConfigurationUnitState.Completed => PSConfigurationUnitState.Completed, 118 ConfigurationUnitState.Skipped => PSConfigurationUnitState.Skipped, 119 _ => throw new InvalidOperationException(), 120 }; 121 } 122 } 123 }