1+
2+ function New-InMemoryObjectScriptCreator {
3+ Param (
4+ [Parameter (Mandatory , HelpMessage = " Path of the generated cs file." )]
5+ [string ]
6+ $CsPath ,
7+ [Parameter (Mandatory )]
8+ [string ]
9+ $OutputDir ,
10+ [Parameter (Mandatory )]
11+ [string ]
12+ $ModuleName
13+ )
14+
15+ $Content = Get-Content $CsPath - Raw
16+ $Tree = [Microsoft.CodeAnalysis.CSharp.SyntaxFactory ]::ParseCompilationUnit($Content )
17+ $Nodes = $Tree.ChildNodes ().ChildNodes()
18+
19+ $InterfaceNode = $Nodes | Where-Object { ($_.Keyword.value -eq ' interface' ) -and (-not $_.Identifier.value.Contains (" Internal" )) }
20+ $ClassNode = $Nodes | Where-Object { ($_.Keyword.value -eq ' class' ) }
21+
22+ $Namespace = $Tree.Members.Name
23+ $ObjectType = $ClassNode.Identifier.Value
24+ $ObjectTypeWithNamespace = " ${Namespace} .${ObjectType} "
25+ $OutputPath = Join-Path - ChildPath " New-Az${ModuleName}${ObjectType} Object.ps1" - Path $OutputDir
26+
27+ $ParameterDefineScriptList = New-Object System.Collections.Generic.List[string ]
28+ $ParameterAssignScriptList = New-Object System.Collections.Generic.List[string ]
29+
30+ foreach ($Member in $InterfaceNode.Members ) {
31+ $Arguments = $Member.AttributeLists.Attributes.ArgumentList.Arguments
32+ $Required = $false
33+ $Description = " "
34+ foreach ($Argument in $Arguments ) {
35+ if ($Argument.NameEquals.Name.Identifier.Value -eq " Required" ) {
36+ $Required = $Argument.Expression.Token.Value
37+ }
38+ if ($Argument.NameEquals.Name.Identifier.Value -eq " Description" ) {
39+ $Description = $Argument.Expression.Token.Value.replace (' "' , ' `"' )
40+ }
41+ if ($Argument.NameEquals.Name.Identifier.Value -eq " Readonly" ) {
42+ if ($Argument.Expression.Token.Value ) {
43+ continue
44+ }
45+ }
46+ }
47+ $Identifier = $Member.Identifier.Value
48+ $Type = $Member.Type.ToString ()
49+ if ($Required ) {
50+ $ParameterDefineScriptList.Add ("
51+ [Parameter(Mandatory, HelpMessage=`" ${Description} .`" )]
52+ [${Type} ]
53+ `$ ${Identifier} " )
54+ } else {
55+ $ParameterDefineScriptList.Add ("
56+ [Parameter(HelpMessage=`" ${Description} .`" )]
57+ [${Type} ]
58+ `$ ${Identifier} " )
59+ }
60+ $ParameterAssignScriptList.Add ("
61+ `$ Object.${Identifier} = `$ ${Identifier} " )
62+ }
63+
64+ $ParameterDefineScript = $ParameterDefineScriptList | Join-String - Separator " ,"
65+ $ParameterAssignScript = $ParameterAssignScriptList | Join-String - Separator " "
66+
67+ $Script = "
68+ # ----------------------------------------------------------------------------------
69+ #
70+ # Copyright Microsoft Corporation
71+ # Licensed under the Apache License, Version 2.0 (the \`" License\`" );
72+ # you may not use this file except in compliance with the License.
73+ # You may obtain a copy of the License at
74+ # http://www.apache.org/licenses/LICENSE-2.0
75+ # Unless required by applicable law or agreed to in writing, software
76+ # distributed under the License is distributed on an \`" AS IS\`" BASIS,
77+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
78+ # See the License for the specific language governing permissions and
79+ # limitations under the License.
80+ # ----------------------------------------------------------------------------------
81+
82+ <#
83+ .Synopsis
84+ Create a in-memory object for ${ObjectType}
85+ .Description
86+ Create a in-memory object for ${ObjectType}
87+
88+ .Outputs
89+ ${ObjectTypeWithNamespace}
90+ .Link
91+ https://docs.microsoft.com/en-us/powershell/module/az.${ModuleName} /new-Az${ModuleName}${ObjectType} Object
92+ #>
93+ function New-Az${ModuleName}${ObjectType} Object {
94+ [OutputType('${ObjectTypeWithNamespace} ')]
95+ [CmdletBinding(PositionalBinding=`$ false)]
96+ Param(
97+ ${ParameterDefineScript}
98+ )
99+
100+ process {
101+ `$ Object = [${ObjectTypeWithNamespace} ]::New()
102+ ${ParameterAssignScript}
103+ return `$ Object
104+ }
105+ }
106+ "
107+ Set-Content - Path $OutputPath - Value $Script
108+ }
109+
110+ # Export-ModuleMember New-InMemoryObjectScriptCreator
0 commit comments