UNPKG

@microsoft.azure/autorest.incubator

Version:
125 lines (124 loc) 8.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const schema_1 = require("../common/code-model/schema"); const dictionary_1 = require("../common/dictionary"); const text_manipulation_1 = require("../common/text-manipulation"); const access_modifier_1 = require("../csharp/code-dom/access-modifier"); const attribute_1 = require("../csharp/code-dom/attribute"); const class_1 = require("../csharp/code-dom/class"); const expression_1 = require("../csharp/code-dom/expression"); const field_1 = require("../csharp/code-dom/field"); const method_1 = require("../csharp/code-dom/method"); const property_1 = require("../csharp/code-dom/property"); const statement_1 = require("../csharp/code-dom/statements/statement"); const variable_1 = require("../csharp/code-dom/variable"); const cmdlet_class_1 = require("../powershell/cmdlet-class"); const powershell_declarations_1 = require("../powershell/powershell-declarations"); class ModelCmdlet extends class_1.Class { // protected processRecord: Method; constructor(namespace, schema, state, objectInitializer) { const name = `New${schema.details.csharp.name}Object`; super(namespace, name, cmdlet_class_1.PSCmdlet); this.state = state; this.apply(objectInitializer); addClassAttributes(this, schema, name); const td = this.state.project.schemaDefinitionResolver.resolveTypeDeclaration(schema, true, this.state); const prop = this.add(new field_1.InitializedField(`_${schema.details.csharp.name.uncapitalize()}`, td, `new ${schema.details.csharp.namespace}.${schema.details.csharp.name}()`, { access: access_modifier_1.Access.Private })); const processRecord = this.add(new method_1.Method('ProcessRecord', undefined, { access: access_modifier_1.Access.Protected, override: access_modifier_1.Modifier.Override })).add(`WriteObject(${prop});`); // this.processRecord.add(`WriteObject(new ${schema.details.csharp.namespace}.${schema.details.csharp.name} {`); // adds the parameters to the cmdlet and adds to the method to set the value from the parameter. addPowershellParameters(this, schema, prop); // this.processRecord.add(`});`); } } exports.ModelCmdlet = ModelCmdlet; function addClassAttributes($class, schema, name) { const td = $class.state.project.schemaDefinitionResolver.resolveTypeDeclaration(schema, true, $class.state); $class.add(new attribute_1.Attribute(powershell_declarations_1.CmdletAttribute, { parameters: [`System.Management.Automation.VerbsCommon.New`, new expression_1.StringExpression(`${schema.details.csharp.name || ''}Object`)] })); $class.add(new attribute_1.Attribute(powershell_declarations_1.OutputTypeAttribute, { parameters: [{ value: `typeof(${td.declaration})` }] })); } function addPowershellParameters($class, schema, prop, ensureMemberIsCreated = undefined, expandName = false) { // next: // -- inline parameters from member property models. // polymorphic models? if (schema.details.csharp.isPolymorphic) { console.error(`\n\nPOLYMORPHIC TYPE! ${schema.details.csharp.name}`); } for (const a of dictionary_1.values(schema.allOf)) { addPowershellParameters($class, a, prop); } // add a parameter for each property for (const { key: name, value: property } of dictionary_1.items(schema.properties)) { const td = $class.state.project.schemaDefinitionResolver.resolveTypeDeclaration(property.schema, true, $class.state); if (property.schema.type === schema_1.JsonType.Object) { // properties property get inlining without hassle const member = new variable_1.MemberVariable(prop, property.details.csharp.name); if (name === 'properties') { // inline these properties instead. const ensure = new statement_1.Statements(ensureMemberIsCreated); ensure.add(`${expression_1.valueOf(member)} = ${expression_1.valueOf(member)} ?? new ${property.schema.details.csharp.fullname}();`); addPowershellParameters($class, property.schema, member, ensure); continue; } if (!property.schema.additionalProperties) { if (dictionary_1.length(property.schema.properties) <= $class.state.project.maxInlinedParameters) { // inline these properties instead. const ensure = new statement_1.Statements(ensureMemberIsCreated); ensure.add(`${expression_1.valueOf(member)} = ${expression_1.valueOf(member)} ?? new ${property.schema.details.csharp.fullname}();`); addPowershellParameters($class, property.schema, member, ensure, true); continue; } } } // other properties if (property.schema.type === schema_1.JsonType.Object) { // console.error(`\n\nLARGE OBJECT NOT INLINED ${property.details.csharp.name}`); } if (!property.schema.readOnly) { // add a cmdlet parameter let cmdletParameter; const pname = expandName ? text_manipulation_1.pascalCase([schema.details.csharp.name, property.details.csharp.name]) : property.details.csharp.name; if (property.schema.type === schema_1.JsonType.Boolean) { // use a switch instead cmdletParameter = $class.add(new property_1.ImplementedProperty(pname, powershell_declarations_1.SwitchParameter, { /* getterStatements: new Statements(function* () { if (ensureMemberIsCreated) { yield ensureMemberIsCreated; } yield Return(`new System.Management.Automation.SwitchParameter(true == ${prop}.${property.details.csharp.name});`); }), */ setterStatements: new statement_1.Statements(function* () { if (ensureMemberIsCreated) { yield ensureMemberIsCreated; } yield `${prop}.${property.details.csharp.name} = value.ToBool();`; }), })); // statements.add(indent(`${property.details.csharp.name} = this.MyInvocation.BoundParameters.ContainsKey("${property.details.csharp.name}") ? this.${property.details.csharp.name}.ToBool() : default(${td.declaration}),`)); } else { let propname = pname; let n = 1; while ($class.properties.find(p => p.name === propname)) { // property exists with that name // let's hack this a smidgen propname = `${pname}${n++}`; } cmdletParameter = $class.add(new property_1.ImplementedProperty(propname, td, { setterStatements: new statement_1.Statements(function* () { if (ensureMemberIsCreated) { yield ensureMemberIsCreated; } yield `${prop}.${property.details.csharp.name} = value;`; }), })); // statements.add(indent(`${property.details.csharp.name} = this.MyInvocation.BoundParameters.ContainsKey("${property.details.csharp.name}") ? this.${property.details.csharp.name} : default(${td.declaration}),`)); } const desc = (property.details.csharp.description || 'HELP MESSAGE MISSING').replace(/[\r?\n]/gm, ''); cmdletParameter.add(new attribute_1.Attribute(powershell_declarations_1.ParameterAttribute, { parameters: [new expression_1.LiteralExpression(`Mandatory = ${property.details.default.required ? 'true' : 'false'}`), new expression_1.LiteralExpression(`HelpMessage = "${text_manipulation_1.escapeString(desc)}"`)] })); } } // if } exports.addPowershellParameters = addPowershellParameters; //# sourceMappingURL=model-cmdlet.js.map