private List<string> GetMethodParameterSets(StaticCmdletMetadata staticCmdlet) { Dictionary<string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); foreach (StaticMethodMetadata metadata in staticCmdlet.Method) { string methodParameterSet = this.GetMethodParameterSet(metadata); if (dictionary.ContainsKey(methodParameterSet)) { throw new XmlException(string.Format(CultureInfo.InvariantCulture, CmdletizationCoreResources.ScriptWriter_DuplicateParameterSetInStaticCmdlet, new object[] { this.GetCmdletName(staticCmdlet.CmdletMetadata), methodParameterSet })); } dictionary.Add(methodParameterSet, null); } return new List<string>(dictionary.Keys); }
private void WriteCmdlet(TextWriter output, StaticCmdletMetadata staticCmdlet) { Dictionary<string, ParameterMetadata> dictionary2; string str; string str2; this.WriteCmdletAliases(output, staticCmdlet.CmdletMetadata); Dictionary<string, ParameterMetadata> commonParameters = this.GetCommonParameters(); List<string> commonParameterSets = GetCommonParameterSets(commonParameters); this.GenerateMethodParametersProcessing(staticCmdlet, commonParameterSets, out str, out dictionary2, out str2); List<string> methodParameterSets = this.GetMethodParameterSets(staticCmdlet); CommandMetadata commandMetadata = this.GetCommandMetadata(staticCmdlet.CmdletMetadata); if (!string.IsNullOrEmpty(commandMetadata.DefaultParameterSetName)) { commandMetadata.DefaultParameterSetName = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { commandMetadata.DefaultParameterSetName, commonParameterSets[0] }); } MultiplyParameterSets(commonParameters, "{1}", new IEnumerable<string>[] { methodParameterSets }); MultiplyParameterSets(dictionary2, "{0}", new IEnumerable<string>[] { commonParameterSets }); EnsureOrderOfPositionalParameters(commonParameters, dictionary2); this.SetParameters(commandMetadata, new Dictionary<string, ParameterMetadata>[] { dictionary2, commonParameters }); output.WriteLine("\r\nfunction {0}\r\n{{\r\n {1}\r\n {2}\r\n param(\r\n {3})\r\n\r\n DynamicParam {{\r\n try \r\n {{\r\n if (-not $__cmdletization_exceptionHasBeenThrown)\r\n {{\r\n $__cmdletization_objectModelWrapper = Microsoft.PowerShell.Utility\\New-Object $script:ObjectModelWrapper\r\n $__cmdletization_objectModelWrapper.Initialize($PSCmdlet, $script:ClassName, $script:ClassVersion, $script:ModuleVersion, $script:PrivateData)\r\n\r\n if ($__cmdletization_objectModelWrapper -is [System.Management.Automation.IDynamicParameters])\r\n {{\r\n ([System.Management.Automation.IDynamicParameters]$__cmdletization_objectModelWrapper).GetDynamicParameters()\r\n }}\r\n }}\r\n }}\r\n catch\r\n {{\r\n $__cmdletization_exceptionHasBeenThrown = $true\r\n throw\r\n }}\r\n }}\r\n\r\n Begin {{\r\n $__cmdletization_exceptionHasBeenThrown = $false\r\n try \r\n {{\r\n __cmdletization_BindCommonParameters $__cmdletization_objectModelWrapper $PSBoundParameters\r\n $__cmdletization_objectModelWrapper.BeginProcessing()\r\n }}\r\n catch\r\n {{\r\n $__cmdletization_exceptionHasBeenThrown = $true\r\n throw\r\n }}\r\n }}\r\n ", new object[] { commandMetadata.Name, ProxyCommand.GetCmdletBindingAttribute(commandMetadata), str2, ProxyCommand.GetParamBlock(commandMetadata) }); output.WriteLine("\r\n Process {{\r\n try \r\n {{\r\n if (-not $__cmdletization_exceptionHasBeenThrown)\r\n {{\r\n{0}\r\n }}\r\n }}\r\n catch\r\n {{\r\n $__cmdletization_exceptionHasBeenThrown = $true\r\n throw\r\n }}\r\n }}\r\n ", str); output.WriteLine("\r\n End {{\r\n try\r\n {{\r\n if (-not $__cmdletization_exceptionHasBeenThrown)\r\n {{\r\n $__cmdletization_objectModelWrapper.EndProcessing()\r\n }}\r\n }}\r\n catch\r\n {{\r\n throw\r\n }}\r\n }}\r\n\r\n {0}\r\n}}\r\nMicrosoft.PowerShell.Core\\Export-ModuleMember -Function '{1}'\r\n ", this.GetHelpDirectiveForExternalHelp(), CommandMetadata.EscapeSingleQuotedString(commandMetadata.Name)); this.functionsToExport.Add(commandMetadata.Name); }
private void GenerateMethodParametersProcessing(StaticCmdletMetadata staticCmdlet, IEnumerable<string> commonParameterSets, out string scriptCode, out Dictionary<string, ParameterMetadata> methodParameters, out string outputTypeAttributeDeclaration) { methodParameters = new Dictionary<string, ParameterMetadata>(StringComparer.OrdinalIgnoreCase); StringBuilder builder = new StringBuilder(); StringWriter output = new StringWriter(CultureInfo.InvariantCulture); output.WriteLine(@" $__cmdletization_methodParameters = Microsoft.PowerShell.Utility\New-Object 'System.Collections.Generic.List[Microsoft.PowerShell.Cmdletization.MethodParameter]'"); output.WriteLine(); bool flag = staticCmdlet.Method.Length > 1; if (flag) { output.WriteLine(" switch -exact ($PSCmdlet.ParameterSetName) { "); } foreach (StaticMethodMetadata metadata in staticCmdlet.Method) { if (flag) { output.Write(" { @("); bool flag2 = true; foreach (string str in MultiplyParameterSets(this.GetMethodParameterSet(metadata), "{0}", new IEnumerable<string>[] { commonParameterSets })) { if (!flag2) { output.Write(", "); } flag2 = false; output.Write("'{0}'", CommandMetadata.EscapeSingleQuotedString(str)); } output.WriteLine(") -contains $_ } {"); } List<Type> list = new List<Type>(); List<string> list2 = new List<string>(); if (metadata.Parameters != null) { foreach (StaticMethodParameterMetadata metadata2 in metadata.Parameters) { MethodParameterBindings bindings; string cmdletParameterName = null; if (metadata2.CmdletParameterMetadata != null) { ParameterMetadata metadata4; string methodParameterSet = this.GetMethodParameterSet(metadata); ParameterMetadata metadata3 = this.GetParameter(methodParameterSet, metadata2.ParameterName, metadata2.Type, metadata2.CmdletParameterMetadata); cmdletParameterName = metadata3.Name; if (methodParameters.TryGetValue(metadata3.Name, out metadata4)) { try { metadata4.ParameterSets.Add(methodParameterSet, metadata3.ParameterSets[methodParameterSet]); goto Label_01D8; } catch (ArgumentException exception) { throw new XmlException(string.Format(CultureInfo.InvariantCulture, CmdletizationCoreResources.ScriptWriter_DuplicateQueryParameterName, new object[] { "<StaticCmdlets>...<Cmdlet>...<Method>", metadata3.Name }), exception); } } methodParameters.Add(metadata3.Name, metadata3); } Label_01D8: bindings = GetMethodParameterKind(metadata2); Type dotNetType = this.GetDotNetType(metadata2.Type); GenerateSingleMethodParameterProcessing(output, " ", cmdletParameterName, dotNetType, metadata2.Type.ETSType, metadata2.DefaultValue, metadata2.ParameterName, bindings); if (MethodParameterBindings.Out == (bindings & MethodParameterBindings.Out)) { list.Add(dotNetType); list2.Add(metadata2.Type.ETSType); } } } if (metadata.ReturnValue != null) { MethodParameterBindings methodParameterKind = GetMethodParameterKind(metadata.ReturnValue); Type item = this.GetDotNetType(metadata.ReturnValue.Type); output.WriteLine(@" $__cmdletization_returnValue = Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.Cmdletization.MethodParameter -Property @{{ Name = 'ReturnValue'; ParameterType = '{0}'; Bindings = '{1}'; Value = $null; IsValuePresent = $false }}", CommandMetadata.EscapeSingleQuotedString(item.FullName), CommandMetadata.EscapeSingleQuotedString(methodParameterKind.ToString())); if (!string.IsNullOrEmpty(metadata.ReturnValue.Type.ETSType)) { output.WriteLine(" $__cmdletization_methodParameter.ParameterTypeName = '{0}'", CommandMetadata.EscapeSingleQuotedString(metadata.ReturnValue.Type.ETSType)); } if (MethodParameterBindings.Out == (methodParameterKind & MethodParameterBindings.Out)) { list.Add(item); list2.Add(metadata.ReturnValue.Type.ETSType); } } else { output.WriteLine(" $__cmdletization_returnValue = $null"); } output.WriteLine(@" $__cmdletization_methodInvocationInfo = Microsoft.PowerShell.Utility\New-Object Microsoft.PowerShell.Cmdletization.MethodInvocationInfo @('{0}', $__cmdletization_methodParameters, $__cmdletization_returnValue)", CommandMetadata.EscapeSingleQuotedString(metadata.MethodName)); output.WriteLine(" $__cmdletization_objectModelWrapper.ProcessRecord($__cmdletization_methodInvocationInfo)"); if (flag) { output.WriteLine(" }"); } if (list.Count == 1) { builder.AppendFormat(CultureInfo.InvariantCulture, "[OutputType([{0}])]", new object[] { list[0].FullName }); if ((list2.Count == 1) && !string.IsNullOrEmpty(list2[0])) { builder.AppendFormat(CultureInfo.InvariantCulture, "[OutputType('{0}')]", new object[] { CommandMetadata.EscapeSingleQuotedString(list2[0]) }); } } } if (flag) { output.WriteLine(" }"); } scriptCode = output.ToString(); outputTypeAttributeDeclaration = builder.ToString(); }
private StaticCmdletMetadata Read33_StaticCmdletMetadata(bool isNullable, bool checkType) { XmlQualifiedName type = checkType ? base.GetXsiType() : null; bool flag = false; if (isNullable) { flag = base.ReadNull(); } if ((checkType && (type != null)) && ((type.Name != this.id23_StaticCmdletMetadata) || (type.Namespace != this.id2_Item))) { throw base.CreateUnknownTypeException(type); } if (flag) { return null; } StaticCmdletMetadata o = new StaticCmdletMetadata(); StaticMethodMetadata[] a = null; int length = 0; bool[] flagArray = new bool[2]; while (base.Reader.MoveToNextAttribute()) { if (!base.IsXmlnsAttribute(base.Reader.Name)) { base.UnknownNode(o); } } base.Reader.MoveToElement(); if (base.Reader.IsEmptyElement) { base.Reader.Skip(); o.Method = (StaticMethodMetadata[]) base.ShrinkArray(a, length, typeof(StaticMethodMetadata), true); return o; } base.Reader.ReadStartElement(); base.Reader.MoveToContent(); int whileIterations = 0; int readerCount = base.ReaderCount; while ((base.Reader.NodeType != XmlNodeType.EndElement) && (base.Reader.NodeType != XmlNodeType.None)) { if (base.Reader.NodeType == XmlNodeType.Element) { if ((!flagArray[0] && (base.Reader.LocalName == this.id81_CmdletMetadata)) && (base.Reader.NamespaceURI == this.id2_Item)) { o.CmdletMetadata = this.Read32_Item(false, true); flagArray[0] = true; } else if ((base.Reader.LocalName == this.id82_Method) && (base.Reader.NamespaceURI == this.id2_Item)) { a = (StaticMethodMetadata[]) base.EnsureArrayIndex(a, length, typeof(StaticMethodMetadata)); a[length++] = this.Read27_StaticMethodMetadata(false, true); } else { base.UnknownNode(o, "http://schemas.microsoft.com/cmdlets-over-objects/2009/11:CmdletMetadata, http://schemas.microsoft.com/cmdlets-over-objects/2009/11:Method"); } } else { base.UnknownNode(o, "http://schemas.microsoft.com/cmdlets-over-objects/2009/11:CmdletMetadata, http://schemas.microsoft.com/cmdlets-over-objects/2009/11:Method"); } base.Reader.MoveToContent(); base.CheckReaderCount(ref whileIterations, ref readerCount); } o.Method = (StaticMethodMetadata[]) base.ShrinkArray(a, length, typeof(StaticMethodMetadata), true); base.ReadEndElement(); return o; }
private void Write33_StaticCmdletMetadata(string n, string ns, StaticCmdletMetadata o, bool isNullable, bool needType) { if (o == null) { if (isNullable) { base.WriteNullTagLiteral(n, ns); } } else { if (!needType && !(o.GetType() == typeof(StaticCmdletMetadata))) { throw base.CreateUnknownTypeException(o); } base.WriteStartElement(n, ns, o, false, null); if (needType) { base.WriteXsiType("StaticCmdletMetadata", "http://schemas.microsoft.com/cmdlets-over-objects/2009/11"); } this.Write32_Item("CmdletMetadata", "http://schemas.microsoft.com/cmdlets-over-objects/2009/11", o.CmdletMetadata, false, false); StaticMethodMetadata[] method = o.Method; if (method != null) { for (int i = 0; i < method.Length; i++) { this.Write27_StaticMethodMetadata("Method", "http://schemas.microsoft.com/cmdlets-over-objects/2009/11", method[i], false, false); } } base.WriteEndElement(o); } }
private List<string> GetMethodParameterSets(StaticCmdletMetadata staticCmdlet) { Dictionary<string, object> parameterSetNames = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); foreach (StaticMethodMetadata method in staticCmdlet.Method) { string parameterSetName = GetMethodParameterSet(method); if (parameterSetNames.ContainsKey(parameterSetName)) { string message = string.Format( CultureInfo.InvariantCulture, CmdletizationCoreResources.ScriptWriter_DuplicateParameterSetInStaticCmdlet, this.GetCmdletName(staticCmdlet.CmdletMetadata), parameterSetName); throw new XmlException(message); } parameterSetNames.Add(parameterSetName, null); } return new List<string>(parameterSetNames.Keys); }
private void WriteCmdlet(TextWriter output, StaticCmdletMetadata staticCmdlet) { string attributeString = this.GetCmdletAttributes(staticCmdlet.CmdletMetadata); Dictionary<string, ParameterMetadata> commonParameters = this.GetCommonParameters(); List<string> commonParameterSets = GetCommonParameterSets(commonParameters); Dbg.Assert(commonParameterSets != null && (commonParameterSets.Count > 0), "Verifying stuff returned by GetCommonParameterSets"); Dictionary<string, ParameterMetadata> methodParameters; string methodProcessingScript; string outputTypeAttributeDeclaration; GenerateMethodParametersProcessing(staticCmdlet, commonParameterSets, out methodProcessingScript, out methodParameters, out outputTypeAttributeDeclaration); List<string> methodParameterSets = GetMethodParameterSets(staticCmdlet); CommandMetadata commandMetadata = this.GetCommandMetadata(staticCmdlet.CmdletMetadata); if (!string.IsNullOrEmpty(commandMetadata.DefaultParameterSetName)) { commandMetadata.DefaultParameterSetName = string.Format(CultureInfo.InvariantCulture, StaticMethodParameterSetTemplate, commandMetadata.DefaultParameterSetName, commonParameterSets[0]); } MultiplyParameterSets(commonParameters, StaticCommonParameterSetTemplate, methodParameterSets); MultiplyParameterSets(methodParameters, StaticMethodParameterSetTemplate, commonParameterSets); EnsureOrderOfPositionalParameters(commonParameters, methodParameters); SetParameters(commandMetadata, methodParameters, commonParameters); Dbg.Assert( Regex.IsMatch(commandMetadata.Name, @"[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Lm}]{1,100}-[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Lm}\p{Nd}]{1,100}"), "Command name doesn't need escaping - validated via xsd"); output.WriteLine( CmdletBeginBlockTemplate, /* 0 */ commandMetadata.Name, /* 1 */ ProxyCommand.GetCmdletBindingAttribute(commandMetadata), /* 2 */ attributeString, /* 3 */ outputTypeAttributeDeclaration, /* 4 */ ProxyCommand.GetParamBlock(commandMetadata)); output.WriteLine( CmdletProcessBlockTemplate, methodProcessingScript); output.WriteLine( CmdletEndBlockTemplate, /* 0 */ this.GetHelpDirectiveForExternalHelp(), /* 1 */ CodeGeneration.EscapeSingleQuotedStringContent(commandMetadata.Name)); _functionsToExport.Add(commandMetadata.Name); }
private void GenerateMethodParametersProcessing( StaticCmdletMetadata staticCmdlet, IEnumerable<string> commonParameterSets, out string scriptCode, out Dictionary<string, ParameterMetadata> methodParameters, out string outputTypeAttributeDeclaration) { methodParameters = new Dictionary<string, ParameterMetadata>(StringComparer.OrdinalIgnoreCase); StringBuilder outputTypeAttributeDeclarationBuilder = new StringBuilder(); StringWriter output = new StringWriter(CultureInfo.InvariantCulture); output.WriteLine(" $__cmdletization_methodParameters = [System.Collections.Generic.List[Microsoft.PowerShell.Cmdletization.MethodParameter]]::new()"); output.WriteLine(); bool multipleMethods = staticCmdlet.Method.Length > 1; if (multipleMethods) { output.WriteLine(" switch -exact ($PSCmdlet.ParameterSetName) { "); } foreach (StaticMethodMetadata method in staticCmdlet.Method) { if (multipleMethods) { output.Write(" { @("); bool firstParameterSet = true; foreach ( string parameterSetName in MultiplyParameterSets( GetMethodParameterSet(method), StaticMethodParameterSetTemplate, commonParameterSets)) { if (!firstParameterSet) output.Write(", "); firstParameterSet = false; output.Write("'{0}'", CodeGeneration.EscapeSingleQuotedStringContent(parameterSetName)); } output.WriteLine(") -contains $_ } {"); } List<Type> typesOfOutParameters = new List<Type>(); List<string> etsTypesOfOutParameters = new List<string>(); if (method.Parameters != null) { foreach (StaticMethodParameterMetadata methodParameter in method.Parameters) { string cmdletParameterName = null; if (methodParameter.CmdletParameterMetadata != null) { string parameterSetName = GetMethodParameterSet(method); ParameterMetadata parameterMetadata = GetParameter( parameterSetName, methodParameter.ParameterName, methodParameter.Type, methodParameter.CmdletParameterMetadata); cmdletParameterName = parameterMetadata.Name; ParameterMetadata oldParameterMetadata; if (methodParameters.TryGetValue(parameterMetadata.Name, out oldParameterMetadata)) { try { oldParameterMetadata.ParameterSets.Add(parameterSetName, parameterMetadata.ParameterSets[parameterSetName]); } catch (ArgumentException e) { string message = string.Format( CultureInfo.InvariantCulture, // xml element names and parameter names are culture-agnostic CmdletizationCoreResources.ScriptWriter_DuplicateQueryParameterName, "<StaticCmdlets>...<Cmdlet>...<Method>", parameterMetadata.Name); throw new XmlException(message, e); } } else { methodParameters.Add(parameterMetadata.Name, parameterMetadata); } } MethodParameterBindings methodParameterBindings = GetMethodParameterKind(methodParameter); Type dotNetTypeOfParameter = GetDotNetType(methodParameter.Type); GenerateSingleMethodParameterProcessing( output, " ", cmdletParameterName, dotNetTypeOfParameter, methodParameter.Type.ETSType, methodParameter.DefaultValue, methodParameter.ParameterName, methodParameterBindings); if (MethodParameterBindings.Out == (methodParameterBindings & MethodParameterBindings.Out)) { typesOfOutParameters.Add(dotNetTypeOfParameter); etsTypesOfOutParameters.Add(methodParameter.Type.ETSType); } } } if (method.ReturnValue != null) { MethodParameterBindings methodParameterBindings = GetMethodParameterKind(method.ReturnValue); Type dotNetTypeOfParameter = GetDotNetType(method.ReturnValue.Type); output.WriteLine( " $__cmdletization_returnValue = [Microsoft.PowerShell.Cmdletization.MethodParameter]@{{ Name = 'ReturnValue'; ParameterType = '{0}'; Bindings = '{1}'; Value = $null; IsValuePresent = $false }}", CodeGeneration.EscapeSingleQuotedStringContent(dotNetTypeOfParameter.FullName), CodeGeneration.EscapeSingleQuotedStringContent(methodParameterBindings.ToString())); if (!string.IsNullOrEmpty(method.ReturnValue.Type.ETSType)) { output.WriteLine( " $__cmdletization_methodParameter.ParameterTypeName = '{0}'", CodeGeneration.EscapeSingleQuotedStringContent(method.ReturnValue.Type.ETSType)); } if (MethodParameterBindings.Out == (methodParameterBindings & MethodParameterBindings.Out)) { typesOfOutParameters.Add(dotNetTypeOfParameter); etsTypesOfOutParameters.Add(method.ReturnValue.Type.ETSType); } } else { output.WriteLine(" $__cmdletization_returnValue = $null"); } output.WriteLine( " $__cmdletization_methodInvocationInfo = [Microsoft.PowerShell.Cmdletization.MethodInvocationInfo]::new('{0}', $__cmdletization_methodParameters, $__cmdletization_returnValue)", CodeGeneration.EscapeSingleQuotedStringContent(method.MethodName)); output.WriteLine(" $__cmdletization_objectModelWrapper.ProcessRecord($__cmdletization_methodInvocationInfo)"); if (multipleMethods) { output.WriteLine(" }"); } if (typesOfOutParameters.Count == 1) { outputTypeAttributeDeclarationBuilder.AppendFormat( CultureInfo.InvariantCulture, "[OutputType([{0}])]", typesOfOutParameters[0].FullName); if ((etsTypesOfOutParameters.Count == 1) && (!string.IsNullOrEmpty(etsTypesOfOutParameters[0]))) { outputTypeAttributeDeclarationBuilder.AppendFormat( CultureInfo.InvariantCulture, "[OutputType('{0}')]", CodeGeneration.EscapeSingleQuotedStringContent(etsTypesOfOutParameters[0])); } } } if (multipleMethods) { output.WriteLine(" }"); } scriptCode = output.ToString(); outputTypeAttributeDeclaration = outputTypeAttributeDeclarationBuilder.ToString(); }