12
12
# limitations under the License.
13
13
# ----------------------------------------------------------------------------------
14
14
15
- param ([ string []] $Models )
15
+ function CreateModelCmdlet {
16
16
17
- if ($Models.Count -eq 0 )
18
- {
19
- return
20
- }
17
+ param ([string []]$Models )
21
18
22
- $ModelCsPath = Join-Path ( Join-Path $PSScriptRoot ' generated\api ' ) ' Models '
23
- $ModuleName = ' Az.Functions ' .Split( " . " )[ 1 ]
24
- $OutputDir = Join-Path $PSScriptRoot ' custom\autogen-model-cmdlets '
25
- $null = New-Item - ItemType Directory - Force - Path $OutputDir
19
+ if ( $Models .Count -eq 0 )
20
+ {
21
+ return
22
+ }
26
23
27
- $CsFiles = Get-ChildItem - Path $ModelCsPath - Recurse - Filter * .cs
28
- $Content = ' '
29
- $null = $CsFiles | ForEach-Object - Process { if ($_.Name.Split (' .' ).count -eq 2 )
30
- { $Content += get-content $_.fullname - raw
31
- } }
24
+ $ModelCsPath = Join-Path (Join-Path $PSScriptRoot ' generated\api' ) ' Models'
25
+ $ModuleName = ' Az.Functions' .Split(" ." )[1 ]
26
+ $OutputDir = Join-Path $PSScriptRoot ' custom\autogen-model-cmdlets'
27
+ $null = New-Item - ItemType Directory - Force - Path $OutputDir
32
28
33
- $Tree = [Microsoft.CodeAnalysis.CSharp.SyntaxFactory ]::ParseCompilationUnit($Content )
34
- $Nodes = $Tree.ChildNodes ().ChildNodes()
35
- foreach ($Model in $Models )
36
- {
37
- $InterfaceNode = $Nodes | Where-Object { ($_.Keyword.value -eq ' interface' ) -and ($_.Identifier.value -eq " I$Model " ) }
38
- if ($InterfaceNode.count -eq 0 ) {
39
- continue
40
- }
41
- # through a queue, we iterate all the parent models.
42
- $Queue = @ ($InterfaceNode )
43
- $visited = @ (" I$Model " )
44
- $AllInterfaceNodes = @ ()
45
- while ($Queue.count -ne 0 )
29
+ $CsFiles = Get-ChildItem - Path $ModelCsPath - Recurse - Filter * .cs
30
+ $Content = ' '
31
+ $null = $CsFiles | ForEach-Object - Process { if ($_.Name.Split (' .' ).count -eq 2 )
32
+ { $Content += get-content $_.fullname - raw
33
+ } }
34
+
35
+ $Tree = [Microsoft.CodeAnalysis.CSharp.SyntaxFactory ]::ParseCompilationUnit($Content )
36
+ $Nodes = $Tree.ChildNodes ().ChildNodes()
37
+ foreach ($Model in $Models )
46
38
{
47
- $AllInterfaceNodes += $Queue [0 ]
48
- # Baselist contains the direct parent models.
49
- foreach ($parent in $Queue [0 ].BaseList.Types)
39
+ $InterfaceNode = $Nodes | Where-Object { ($_.Keyword.value -eq ' interface' ) -and ($_.Identifier.value -eq " I$Model " ) }
40
+ if ($InterfaceNode.count -eq 0 ) {
41
+ continue
42
+ }
43
+ # through a queue, we iterate all the parent models.
44
+ $Queue = @ ($InterfaceNode )
45
+ $visited = @ (" I$Model " )
46
+ $AllInterfaceNodes = @ ()
47
+ while ($Queue.count -ne 0 )
50
48
{
51
- if (($parent.Type.Right.Identifier.Value -ne ' IJsonSerializable' ) -and (-not $visited.Contains ($parent.Type.Right.Identifier.Value )))
49
+ $AllInterfaceNodes += $Queue [0 ]
50
+ # Baselist contains the direct parent models.
51
+ foreach ($parent in $Queue [0 ].BaseList.Types)
52
52
{
53
- $Queue = [Array ]$Queue + ($Nodes | Where-Object { ($_.Keyword.value -eq ' interface' ) -and ($_.Identifier.value -eq $parent.Type.Right.Identifier.Value ) })
54
- $visited = [Array ]$visited + $parent.Type.Right.Identifier.Value
53
+ if (($parent.Type.Right.Identifier.Value -ne ' IJsonSerializable' ) -and (-not $visited.Contains ($parent.Type.Right.Identifier.Value )))
54
+ {
55
+ $Queue = [Array ]$Queue + ($Nodes | Where-Object { ($_.Keyword.value -eq ' interface' ) -and ($_.Identifier.value -eq $parent.Type.Right.Identifier.Value ) })
56
+ $visited = [Array ]$visited + $parent.Type.Right.Identifier.Value
57
+ }
55
58
}
59
+ $first , $Queue = $Queue
56
60
}
57
- $first , $Queue = $Queue
58
- }
59
61
60
- $Namespace = $InterfaceNode.Parent.Name
61
- $ObjectType = $Model
62
- $ObjectTypeWithNamespace = " ${Namespace} .${ObjectType} "
63
- # remove duplicated module name
64
- if ($ObjectType.StartsWith ($ModuleName )) {
65
- $ModulePrefix = ' '
66
- } else {
67
- $ModulePrefix = $ModuleName
68
- }
69
- $OutputPath = Join-Path - ChildPath " New-Az${ModulePrefix}${ObjectType} Object.ps1" - Path $OutputDir
62
+ $Namespace = $InterfaceNode.Parent.Name
63
+ $ObjectType = $Model
64
+ $ObjectTypeWithNamespace = " ${Namespace} .${ObjectType} "
65
+ # remove duplicated module name
66
+ if ($ObjectType.StartsWith ($ModuleName )) {
67
+ $ModulePrefix = ' '
68
+ } else {
69
+ $ModulePrefix = $ModuleName
70
+ }
71
+ $OutputPath = Join-Path - ChildPath " New-Az${ModulePrefix}${ObjectType} Object.ps1" - Path $OutputDir
70
72
71
- $ParameterDefineScriptList = New-Object System.Collections.Generic.List[string ]
72
- $ParameterAssignScriptList = New-Object System.Collections.Generic.List[string ]
73
- foreach ($Node in $AllInterfaceNodes )
74
- {
75
- foreach ($Member in $Node.Members )
73
+ $ParameterDefineScriptList = New-Object System.Collections.Generic.List[string ]
74
+ $ParameterAssignScriptList = New-Object System.Collections.Generic.List[string ]
75
+ foreach ($Node in $AllInterfaceNodes )
76
76
{
77
- $Arguments = $Member.AttributeLists.Attributes.ArgumentList.Arguments
78
- $Required = $false
79
- $Description = " "
80
- $Readonly = $False
81
- foreach ($Argument in $Arguments )
77
+ foreach ($Member in $Node.Members )
82
78
{
83
- if ($Argument.NameEquals.Name.Identifier.Value -eq " Required" )
79
+ $Arguments = $Member.AttributeLists.Attributes.ArgumentList.Arguments
80
+ $Required = $false
81
+ $Description = " "
82
+ $Readonly = $False
83
+ foreach ($Argument in $Arguments )
84
84
{
85
- $Required = $Argument.Expression.Token.Value
85
+ if ($Argument.NameEquals.Name.Identifier.Value -eq " Required" )
86
+ {
87
+ $Required = $Argument.Expression.Token.Value
88
+ }
89
+ if ($Argument.NameEquals.Name.Identifier.Value -eq " Description" )
90
+ {
91
+ $Description = $Argument.Expression.Token.Value.Trim (' .' ).replace(' "' , ' `"' )
92
+ }
93
+ if ($Argument.NameEquals.Name.Identifier.Value -eq " Readonly" )
94
+ {
95
+ $Readonly = $Argument.Expression.Token.Value
96
+ }
86
97
}
87
- if ($Argument .NameEquals.Name.Identifier.Value -eq " Description " )
98
+ if ($Readonly )
88
99
{
89
- $Description = $Argument .Expression.Token.Value.Trim ( ' . ' ).replace( ' " ' , ' `" ' )
100
+ continue
90
101
}
91
- if ($Argument.NameEquals.Name.Identifier.Value -eq " Readonly" )
102
+ $Identifier = $Member.Identifier.Value
103
+ $Type = $Member.Type.ToString ().replace(' ?' , ' ' ).Split(" ::" )[-1 ]
104
+ $ParameterDefinePropertyList = New-Object System.Collections.Generic.List[string ]
105
+ if ($Required )
92
106
{
93
- $Readonly = $Argument .Expression.Token.Value
107
+ $ParameterDefinePropertyList .Add ( " Mandatory " )
94
108
}
109
+ if ($Description -ne " " )
110
+ {
111
+ $ParameterDefinePropertyList.Add (" HelpMessage=`" ${Description} .`" " )
112
+ }
113
+ $ParameterDefineProperty = [System.String ]::Join(" , " , $ParameterDefinePropertyList )
114
+ $ParameterDefineScript = "
115
+ [Parameter($ParameterDefineProperty )]
116
+ [${Type} ]
117
+ `$ ${Identifier} "
118
+ $ParameterDefineScriptList.Add ($ParameterDefineScript )
119
+ $ParameterAssignScriptList.Add ("
120
+ `$ Object.${Identifier} = `$ ${Identifier} " )
95
121
}
96
- if ($Readonly )
97
- {
98
- continue
99
- }
100
- $Identifier = $Member.Identifier.Value
101
- $Type = $Member.Type.ToString ().replace(' ?' , ' ' ).Split(" ::" )[-1 ]
102
- $ParameterDefinePropertyList = New-Object System.Collections.Generic.List[string ]
103
- if ($Required )
104
- {
105
- $ParameterDefinePropertyList.Add (" Mandatory" )
106
- }
107
- if ($Description -ne " " )
108
- {
109
- $ParameterDefinePropertyList.Add (" HelpMessage=`" ${Description} .`" " )
110
- }
111
- $ParameterDefineProperty = [System.String ]::Join(" , " , $ParameterDefinePropertyList )
112
- $ParameterDefineScript = "
113
- [Parameter($ParameterDefineProperty )]
114
- [${Type} ]
115
- `$ ${Identifier} "
116
- $ParameterDefineScriptList.Add ($ParameterDefineScript )
117
- $ParameterAssignScriptList.Add ("
118
- `$ Object.${Identifier} = `$ ${Identifier} " )
119
122
}
120
- }
121
- $ParameterDefineScript = $ParameterDefineScriptList | Join-String - Separator " ,"
122
- $ParameterAssignScript = $ParameterAssignScriptList | Join-String - Separator " "
123
+ $ParameterDefineScript = $ParameterDefineScriptList | Join-String - Separator " ,"
124
+ $ParameterAssignScript = $ParameterAssignScriptList | Join-String - Separator " "
123
125
124
- $Script = "
125
- # ----------------------------------------------------------------------------------
126
- #
127
- # Copyright Microsoft Corporation
128
- # Licensed under the Apache License, Version 2.0 (the \`" License\`" );
129
- # you may not use this file except in compliance with the License.
130
- # You may obtain a copy of the License at
131
- # http://www.apache.org/licenses/LICENSE-2.0
132
- # Unless required by applicable law or agreed to in writing, software
133
- # distributed under the License is distributed on an \`" AS IS\`" BASIS,
134
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135
- # See the License for the specific language governing permissions and
136
- # limitations under the License.
137
- # ----------------------------------------------------------------------------------
126
+ $Script = "
127
+ # ----------------------------------------------------------------------------------
128
+ #
129
+ # Copyright Microsoft Corporation
130
+ # Licensed under the Apache License, Version 2.0 (the \`" License\`" );
131
+ # you may not use this file except in compliance with the License.
132
+ # You may obtain a copy of the License at
133
+ # http://www.apache.org/licenses/LICENSE-2.0
134
+ # Unless required by applicable law or agreed to in writing, software
135
+ # distributed under the License is distributed on an \`" AS IS\`" BASIS,
136
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137
+ # See the License for the specific language governing permissions and
138
+ # limitations under the License.
139
+ # ----------------------------------------------------------------------------------
138
140
139
- <#
140
- .Synopsis
141
- Create a in-memory object for ${ObjectType}
142
- .Description
143
- Create a in-memory object for ${ObjectType}
141
+ <#
142
+ .Synopsis
143
+ Create a in-memory object for ${ObjectType}
144
+ .Description
145
+ Create a in-memory object for ${ObjectType}
144
146
145
- .Outputs
146
- ${ObjectTypeWithNamespace}
147
- .Link
148
- https://docs.microsoft.com/powershell/module/az.${ModuleName} /new-Az${ModulePrefix}${ObjectType} Object
149
- #>
150
- function New-Az${ModulePrefix}${ObjectType} Object {
151
- [OutputType('${ObjectTypeWithNamespace} ')]
152
- [CmdletBinding(PositionalBinding=`$ false)]
153
- Param(
154
- ${ParameterDefineScript}
155
- )
147
+ .Outputs
148
+ ${ObjectTypeWithNamespace}
149
+ .Link
150
+ https://docs.microsoft.com/powershell/module/az.${ModuleName} /new-Az${ModulePrefix}${ObjectType} Object
151
+ #>
152
+ function New-Az${ModulePrefix}${ObjectType} Object {
153
+ [OutputType('${ObjectTypeWithNamespace} ')]
154
+ [CmdletBinding(PositionalBinding=`$ false)]
155
+ Param(
156
+ ${ParameterDefineScript}
157
+ )
156
158
157
- process {
158
- `$ Object = [${ObjectTypeWithNamespace} ]::New()
159
- ${ParameterAssignScript}
160
- return `$ Object
159
+ process {
160
+ `$ Object = [${ObjectTypeWithNamespace} ]::New()
161
+ ${ParameterAssignScript}
162
+ return `$ Object
163
+ }
164
+ }
165
+ "
166
+ Set-Content - Path $OutputPath - Value $Script
161
167
}
162
- }
163
- "
164
- Set-Content - Path $OutputPath - Value $Script
165
- }
168
+ }
0 commit comments