| |
| |
|
|
| using System.Collections.Generic; |
| using System.Management.Automation.Help; |
|
|
| namespace System.Management.Automation |
| { |
| |
| |
| |
| internal static class MamlUtil |
| { |
| |
| |
| |
| |
| |
| internal static void OverrideName(PSObject maml1, PSObject maml2) |
| { |
| PrependPropertyValue(maml1, maml2, new string[] { "Name" }, true); |
| PrependPropertyValue(maml1, maml2, new string[] { "Details", "Name" }, true); |
| } |
|
|
| |
| |
| |
| |
| |
| internal static void OverridePSTypeNames(PSObject maml1, PSObject maml2) |
| { |
| foreach (var typename in maml2.TypeNames) |
| { |
| if (typename.StartsWith(DefaultCommandHelpObjectBuilder.TypeNameForDefaultHelp, StringComparison.OrdinalIgnoreCase)) |
| { |
| |
| return; |
| } |
| } |
|
|
| maml1.TypeNames.Clear(); |
| |
| foreach (string typeName in maml2.TypeNames) |
| { |
| maml1.TypeNames.Add(typeName); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| internal static void AddCommonProperties(PSObject maml1, PSObject maml2) |
| { |
| if (maml1.Properties["PSSnapIn"] == null) |
| { |
| PSPropertyInfo snapInProperty = maml2.Properties["PSSnapIn"]; |
| if (snapInProperty != null) |
| { |
| maml1.Properties.Add(new PSNoteProperty("PSSnapIn", snapInProperty.Value)); |
| } |
| } |
|
|
| if (maml1.Properties["ModuleName"] == null) |
| { |
| PSPropertyInfo moduleNameProperty = maml2.Properties["ModuleName"]; |
| if (moduleNameProperty != null) |
| { |
| maml1.Properties.Add(new PSNoteProperty("ModuleName", moduleNameProperty.Value)); |
| } |
| } |
| } |
|
|
| |
| |
| |
| internal static void PrependSyntax(PSObject maml1, PSObject maml2) |
| { |
| PrependPropertyValue(maml1, maml2, new string[] { "Syntax", "SyntaxItem" }, false); |
| } |
|
|
| |
| |
| |
| internal static void PrependDetailedDescription(PSObject maml1, PSObject maml2) |
| { |
| PrependPropertyValue(maml1, maml2, new string[] { "Description" }, false); |
| } |
|
|
| |
| |
| |
| |
| internal static void OverrideParameters(PSObject maml1, PSObject maml2) |
| { |
| string[] parametersPath = new string[] { "Parameters", "Parameter" }; |
| |
| List<object> maml2items = new List<object>(); |
|
|
| |
|
|
| |
| PSPropertyInfo propertyInfo2 = GetPropertyInfo(maml2, parametersPath); |
| var array = propertyInfo2.Value as Array; |
| if (array != null) |
| { |
| maml2items.AddRange(array as IEnumerable<object>); |
| } |
| else |
| { |
| maml2items.Add(PSObject.AsPSObject(propertyInfo2.Value)); |
| } |
|
|
| |
| EnsurePropertyInfoPathExists(maml1, parametersPath); |
| |
| PSPropertyInfo propertyInfo1 = GetPropertyInfo(maml1, parametersPath); |
| List<object> maml1items = new List<object>(); |
| array = propertyInfo1.Value as Array; |
| if (array != null) |
| { |
| maml1items.AddRange(array as IEnumerable<object>); |
| } |
| else |
| { |
| maml1items.Add(PSObject.AsPSObject(propertyInfo1.Value)); |
| } |
|
|
| |
| for (int index = 0; index < maml2items.Count; index++) |
| { |
| PSObject m2paramObj = PSObject.AsPSObject(maml2items[index]); |
| string param2Name = string.Empty; |
| PSPropertyInfo m2propertyInfo = m2paramObj.Properties["Name"]; |
|
|
| if (m2propertyInfo != null) |
| { |
| if (!LanguagePrimitives.TryConvertTo<string>(m2propertyInfo.Value, out param2Name)) |
| { |
| continue; |
| } |
| } |
|
|
| bool isParamFoundInMaml1 = false; |
| foreach (PSObject m1ParamObj in maml1items) |
| { |
| string param1Name = string.Empty; |
| PSPropertyInfo m1PropertyInfo = m1ParamObj.Properties["Name"]; |
|
|
| if (m1PropertyInfo != null) |
| { |
| if (!LanguagePrimitives.TryConvertTo<string>(m1PropertyInfo.Value, out param1Name)) |
| { |
| continue; |
| } |
| } |
|
|
| if (param1Name.Equals(param2Name, StringComparison.OrdinalIgnoreCase)) |
| { |
| isParamFoundInMaml1 = true; |
| } |
| } |
|
|
| if (!isParamFoundInMaml1) |
| { |
| maml1items.Add(maml2items[index]); |
| } |
| } |
|
|
| |
| if (maml1items.Count == 1) |
| { |
| propertyInfo1.Value = maml1items[0]; |
| } |
| else if (maml1items.Count >= 2) |
| { |
| propertyInfo1.Value = maml1items.ToArray(); |
| } |
| } |
|
|
| |
| |
| |
| internal static void PrependNotes(PSObject maml1, PSObject maml2) |
| { |
| PrependPropertyValue(maml1, maml2, new string[] { "AlertSet", "Alert" }, false); |
| } |
|
|
| |
| |
| |
| internal static PSPropertyInfo GetPropertyInfo(PSObject psObject, string[] path) |
| { |
| if (path.Length == 0) |
| { |
| return null; |
| } |
|
|
| for (int i = 0; i < path.Length; ++i) |
| { |
| string propertyName = path[i]; |
| PSPropertyInfo propertyInfo = psObject.Properties[propertyName]; |
| if (i == path.Length - 1) |
| { |
| return propertyInfo; |
| } |
|
|
| if (propertyInfo == null || propertyInfo.Value is not PSObject) |
| { |
| return null; |
| } |
|
|
| psObject = (PSObject)propertyInfo.Value; |
| } |
|
|
| |
| return null; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| internal static void PrependPropertyValue(PSObject maml1, PSObject maml2, string[] path, bool shouldOverride) |
| { |
| |
| List<object> items = new List<object>(); |
|
|
| |
|
|
| |
| PSPropertyInfo propertyInfo2 = GetPropertyInfo(maml2, path); |
|
|
| if (propertyInfo2 != null) |
| { |
| var array = propertyInfo2.Value as Array; |
| if (array != null) |
| { |
| items.AddRange(propertyInfo2.Value as IEnumerable<object>); |
| } |
| else |
| { |
| items.Add(propertyInfo2.Value); |
| } |
| } |
|
|
| |
| EnsurePropertyInfoPathExists(maml1, path); |
| |
| PSPropertyInfo propertyInfo1 = GetPropertyInfo(maml1, path); |
|
|
| if (propertyInfo1 != null) |
| { |
| if (!shouldOverride) |
| { |
| var array = propertyInfo1.Value as Array; |
| if (array != null) |
| { |
| items.AddRange(propertyInfo1.Value as IEnumerable<object>); |
| } |
| else |
| { |
| items.Add(propertyInfo1.Value); |
| } |
| } |
|
|
| |
| if (items.Count == 1) |
| { |
| propertyInfo1.Value = items[0]; |
| } |
| else if (items.Count >= 2) |
| { |
| propertyInfo1.Value = items.ToArray(); |
| } |
| } |
| } |
|
|
| |
| |
| |
| internal static void EnsurePropertyInfoPathExists(PSObject psObject, string[] path) |
| { |
| if (path.Length == 0) |
| { |
| return; |
| } |
|
|
| |
| for (int i = 0; i < path.Length; ++i) |
| { |
| string propertyName = path[i]; |
| PSPropertyInfo propertyInfo = psObject.Properties[propertyName]; |
|
|
| |
| if (propertyInfo == null) |
| { |
| |
| object propertyValue = (i < path.Length - 1) ? new PSObject() : null; |
| propertyInfo = new PSNoteProperty(propertyName, propertyValue); |
| psObject.Properties.Add(propertyInfo); |
| } |
|
|
| |
| if (i == path.Length - 1) |
| { |
| return; |
| } |
|
|
| |
| if (propertyInfo.Value == null || propertyInfo.Value is not PSObject) |
| { |
| propertyInfo.Value = new PSObject(); |
| } |
|
|
| |
| psObject = (PSObject)propertyInfo.Value; |
| } |
| } |
| } |
| } |
|
|