|
| 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