|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +param([string[]]$Models) |
| 16 | + |
| 17 | +if ($Models.Count -eq 0) |
| 18 | +{ |
| 19 | + return |
| 20 | +} |
| 21 | + |
| 22 | +$ModelCsPath = Join-Path (Join-Path $PSScriptRoot 'generated\api') 'Models' |
| 23 | +$ModuleName = 'Az.Migrate'.Split(".")[1] |
| 24 | +$OutputDir = Join-Path $PSScriptRoot 'custom\autogen-model-cmdlets' |
| 25 | +$null = New-Item -ItemType Directory -Force -Path $OutputDir |
| 26 | + |
| 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 | + } } |
| 32 | + |
| 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) |
| 46 | + { |
| 47 | + $AllInterfaceNodes += $Queue[0] |
| 48 | + # Baselist contains the direct parent models. |
| 49 | + foreach ($parent in $Queue[0].BaseList.Types) |
| 50 | + { |
| 51 | + if (($parent.Type.Right.Identifier.Value -ne 'IJsonSerializable') -and (-not $visited.Contains($parent.Type.Right.Identifier.Value))) |
| 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 |
| 55 | + } |
| 56 | + } |
| 57 | + $first, $Queue = $Queue |
| 58 | + } |
| 59 | + |
| 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 |
| 70 | + |
| 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) |
| 76 | + { |
| 77 | + $Arguments = $Member.AttributeLists.Attributes.ArgumentList.Arguments |
| 78 | + $Required = $false |
| 79 | + $Description = "" |
| 80 | + $Readonly = $False |
| 81 | + foreach ($Argument in $Arguments) |
| 82 | + { |
| 83 | + if ($Argument.NameEquals.Name.Identifier.Value -eq "Required") |
| 84 | + { |
| 85 | + $Required = $Argument.Expression.Token.Value |
| 86 | + } |
| 87 | + if ($Argument.NameEquals.Name.Identifier.Value -eq "Description") |
| 88 | + { |
| 89 | + $Description = $Argument.Expression.Token.Value.Trim('.').replace('"', '`"') |
| 90 | + } |
| 91 | + if ($Argument.NameEquals.Name.Identifier.Value -eq "Readonly") |
| 92 | + { |
| 93 | + $Readonly = $Argument.Expression.Token.Value |
| 94 | + } |
| 95 | + } |
| 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 | + } |
| 120 | + } |
| 121 | + $ParameterDefineScript = $ParameterDefineScriptList | Join-String -Separator "," |
| 122 | + $ParameterAssignScript = $ParameterAssignScriptList | Join-String -Separator "" |
| 123 | + |
| 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 | +# ---------------------------------------------------------------------------------- |
| 138 | +
|
| 139 | +<# |
| 140 | +.Synopsis |
| 141 | +Create a in-memory object for ${ObjectType} |
| 142 | +.Description |
| 143 | +Create a in-memory object for ${ObjectType} |
| 144 | +
|
| 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 | + ) |
| 156 | +
|
| 157 | + process { |
| 158 | + `$Object = [${ObjectTypeWithNamespace}]::New() |
| 159 | +${ParameterAssignScript} |
| 160 | + return `$Object |
| 161 | + } |
| 162 | +} |
| 163 | +" |
| 164 | + Set-Content -Path $OutputPath -Value $Script |
| 165 | +} |
0 commit comments