Skip to content

Commit 004ed97

Browse files
committed
Update
1 parent 5a27a99 commit 004ed97

File tree

6 files changed

+1862
-147
lines changed

6 files changed

+1862
-147
lines changed

src/ResourceManager/Compute/Commands.Compute/Generated/cli.js

Lines changed: 1439 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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(
16+
[Parameter(Mandatory = $true)]
17+
[System.Type]$TypeInfo,
18+
19+
[Parameter(Mandatory = $true)]
20+
[string]$NameSpace,
21+
22+
[Parameter(Mandatory = $false)]
23+
[string]$ParameterName = $null,
24+
25+
[Parameter(Mandatory = $false)]
26+
[string]$CmdletNounPrefix = "Azure"
27+
)
28+
29+
function New-ParameterCmdletTreeNode()
30+
{
31+
param ([string]$Name, [System.Type]$TypeInfo, $Parent)
32+
33+
$node = New-Object PSObject;
34+
$node | Add-Member -Type NoteProperty -Name Name -Value $Name;
35+
$node | Add-Member -Type NoteProperty -Name Parent -Value $Parent;
36+
$node | Add-Member -Type NoteProperty -Name Properties -Value @();
37+
$node | Add-Member -Type NoteProperty -Name SubNodes -Value @();
38+
39+
return $node;
40+
}
41+
42+
function Create-ParameterCmdletTreeImpl
43+
{
44+
param(
45+
[Parameter(Mandatory = $true)]
46+
[System.Type]$TypeInfo,
47+
48+
[Parameter(Mandatory = $false)]
49+
$Parent = $null,
50+
51+
[Parameter(Mandatory = $false)]
52+
[string]$ParameterName = $null,
53+
54+
[Parameter(Mandatory = $false)]
55+
[int]$Depth = 0
56+
)
57+
58+
if ([string]::IsNullOrEmpty($typeInfo.FullName))
59+
{
60+
return $null;
61+
}
62+
elseif (-not $typeInfo.FullName.StartsWith($NameSpace + "."))
63+
{
64+
return $null;
65+
}
66+
67+
$treeNode = New-ParameterCmdletTreeNode $ParameterName $TypeInfo $Parent;
68+
69+
$padding = ($Depth.ToString() + (' ' * (4 * ($Depth + 1))));
70+
Write-Verbose ($padding + $treeNode.Name);
71+
Write-Verbose ($padding + "T: " + $treeNode);
72+
Write-Verbose ($padding + "P: " + $Parent);
73+
74+
foreach ($item in $TypeInfo.GetProperties())
75+
{
76+
$itemProp = [System.Reflection.PropertyInfo]$item;
77+
78+
if ($itemProp.PropertyType.FullName.StartsWith($NameSpace + "."))
79+
{
80+
$subTreeNode = Create-ParameterCmdletTreeImpl $itemProp.PropertyType $treeNode $itemProp.Name ($Depth + 1);
81+
if ($subTreeNode -ne $null)
82+
{
83+
$treeNode.SubNodes += $subTreeNode;
84+
}
85+
}
86+
elseif ($itemProp.PropertyType.FullName.StartsWith("System.Collections.Generic.IList"))
87+
{
88+
$listItemType = $itemProp.PropertyType.GenericTypeArguments[0];
89+
90+
Write-Verbose ($padding + '-' + $listItemType.Name + "List");
91+
92+
$subTreeNode = Create-ParameterCmdletTreeImpl $listItemType $treeNode $itemProp.Name ($Depth + 1)
93+
if ($subTreeNode -ne $null)
94+
{
95+
$treeNode.SubNodes += $subTreeNode;
96+
}
97+
}
98+
else
99+
{
100+
$nodeProp = @{ Name = $itemProp.Name; Type = $itemProp.PropertyType };
101+
$treeNode.Properties += $nodeProp;
102+
Write-Verbose ($padding + '-' + $nodeProp["Name"] + " : " + $nodeProp["Type"]);
103+
}
104+
}
105+
106+
return $treeNode;
107+
}
108+
109+
Write-Output (Create-ParameterCmdletTreeImpl $TypeInfo $null $ParameterName 0);
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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(
16+
[Parameter(Mandatory = $true)]
17+
$CmdletTreeNode,
18+
19+
# VirtualMachine, VirtualMachineScaleSet, etc.
20+
[Parameter(Mandatory = $true)]
21+
[string]$OperationName,
22+
23+
# CLI commands or PS cmdlets
24+
[Parameter(Mandatory = $false)]
25+
[string]$ToolType = "CLI",
26+
27+
[Parameter(Mandatory = $false)]
28+
[string]$CmdletNounPrefix = "Azure"
29+
)
30+
31+
$NEW_LINE = "`r`n";
32+
. "$PSScriptRoot\StringProcessingHelper.ps1";
33+
34+
function Generate-ParameterCommandImpl
35+
{
36+
param(
37+
[Parameter(Mandatory = $true)]
38+
$TreeNode
39+
)
40+
41+
$cli_method_option_name = Get-CliOptionName $TreeNode.Name;
42+
$cli_op_description = Get-CliOptionName $OperationName;
43+
$category_name = Get-CliCategoryName $OperationName;
44+
$params_category_name = 'parameters';
45+
$params_generate_category_name = 'set';
46+
$cli_param_name = $params_category_name;
47+
48+
# Path to Node
49+
$pathToTreeNode = "";
50+
$parentNode = $TreeNode;
51+
while ($parentNode -ne $null)
52+
{
53+
$nodeName = Get-CliNormalizedName $parentNode.Name.Trim();
54+
55+
if ($parentNode.Parent -ne $null)
56+
{
57+
$pathToTreeNode = "/$nodeName" + $pathToTreeNode;
58+
}
59+
60+
$parentNode = $parentNode.Parent;
61+
}
62+
63+
# 1. Parameter Set Command
64+
$code = " //$params_category_name parameters set ${cli_method_option_name}" + $NEW_LINE;
65+
$code += " var ${params_category_name} = ${category_name}.category('${params_category_name}')" + $NEW_LINE;
66+
$code += " .description(`$('Commands to manage parameter for your ${cli_op_description}.'));" + $NEW_LINE;
67+
$code += " var ${params_generate_category_name} = ${params_category_name}.category('${params_generate_category_name}')" + $NEW_LINE;
68+
$code += " .description(`$('Commands to set parameter file for your ${cli_op_description}.'));" + $NEW_LINE;
69+
$code += " ${params_generate_category_name}.command('${cli_method_option_name}')" + $NEW_LINE;
70+
$code += " .description(`$('Set ${category_name} parameter string or files.'))" + $NEW_LINE;
71+
$code += " .usage('[options]')" + $NEW_LINE;
72+
$code += " .option('--parameter-file <parameter-file>', `$('The parameter file path.'))" + $NEW_LINE;
73+
$code += " .option('--value <value>', `$('The JSON value.'))" + $new_line_str;
74+
$code += " .option('--parse', `$('Parse the JSON value to object.'))" + $new_line_str;
75+
76+
# For Each Property, Set the Option
77+
foreach ($propertyItem in $TreeNode.Properties)
78+
{
79+
$code += " .option('--" + (Get-CliOptionName $propertyItem["Name"]);
80+
$code += " <" + (Get-CliNormalizedName $propertyItem["Name"]);
81+
$code += ">', `$('Set the " + (Get-CliOptionName $propertyItem["Name"]);
82+
$code += "value.'))" + $new_line_str;
83+
}
84+
85+
$code += " .execute(function (";
86+
$code += " parameterFile";
87+
$code += " , options, _) {" + $NEW_LINE;
88+
89+
$code += " console.log(options.parameterFile);" + $new_line_str;
90+
$code += " console.log(options.value);" + $new_line_str;
91+
$code += " console.log(options.parse);" + $new_line_str;
92+
$code += " if (options.parse) {" + $new_line_str;
93+
$code += " options.value = JSON.parse(options.value);" + $new_line_str;
94+
$code += " }" + $new_line_str;
95+
$code += " console.log(options.value);" + $new_line_str;
96+
$code += " console.log(`"=====================================`");" + $new_line_str;
97+
$code += " console.log(`"Reading file content from: \`"`" + options.parameterFile + `"\`"`");" + $new_line_str;
98+
$code += " console.log(`"=====================================`");" + $new_line_str;
99+
$code += " var fileContent = fs.readFileSync(options.parameterFile, 'utf8');" + $new_line_str;
100+
$code += " var ${cli_param_name}Obj = JSON.parse(fileContent);" + $new_line_str;
101+
$code += " console.log(`"JSON object:`");" + $new_line_str;
102+
$code += " console.log(JSON.stringify(${cli_param_name}Obj));" + $new_line_str;
103+
104+
$code += " options.operation = 'replace';" + $new_line_str;
105+
$code += " options.path = `"${pathToTreeNode}`";" + $new_line_str;
106+
# $code += " jsonpatch.apply(${cli_param_name}Obj, [{op: options.operation, path: options.path, value: options.value}]);" + $new_line_str;
107+
108+
# For Each Property, Apply the Change if Any
109+
foreach ($propertyItem in $TreeNode.Properties)
110+
{
111+
$paramName = (Get-CliNormalizedName $propertyItem["Name"]);
112+
$code += " var paramPath = " + "options.path" + " + `"/`" + " + "`"" + ${paramName} + "`";" + $new_line_str;
113+
$code += " if (options.${paramName}) {" + $new_line_str;
114+
$code += " jsonpatch.apply(${cli_param_name}Obj, [{op: options.operation, path: paramPath, value: options.${paramName}}]);" + $new_line_str;
115+
$code += " }" + $new_line_str;
116+
}
117+
118+
$code += " var updatedContent = JSON.stringify(${cli_param_name}Obj);" + $new_line_str;
119+
$code += " console.log(`"=====================================`");" + $new_line_str;
120+
$code += " console.log(`"JSON object (updated):`");" + $new_line_str;
121+
$code += " console.log(JSON.stringify(${cli_param_name}Obj));" + $new_line_str;
122+
$code += " console.log(`"=====================================`");" + $new_line_str;
123+
$code += " fs.writeFileSync(options.parameterFile, beautify(updatedContent));" + $new_line_str;
124+
$code += " console.log(`"=====================================`");" + $new_line_str;
125+
$code += " console.log(`"Parameter file updated at: `" + options.parameterFile);" + $new_line_str;
126+
$code += " console.log(`"=====================================`");" + $new_line_str;
127+
128+
$code += " });" + $NEW_LINE;
129+
$code += "" + $NEW_LINE;
130+
131+
foreach ($subNode in $TreeNode.SubNodes)
132+
{
133+
if ($null -ne $subNode)
134+
{
135+
$code += Generate-ParameterCommandImpl $subNode;
136+
}
137+
}
138+
139+
return $code;
140+
}
141+
142+
Write-Output (Generate-ParameterCommandImpl $CmdletTreeNode);

0 commit comments

Comments
 (0)