UNPKG

@microsoft.azure/autorest.incubator

Version:
135 lines (133 loc) 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const initializer_1 = require("../../common/initializer"); const text_manipulation_1 = require("../../common/text-manipulation"); const class_1 = require("./class"); const import_1 = require("./import"); const interface_1 = require("./interface"); class Namespace extends initializer_1.Initializer { constructor(name, parent, objectInitializer) { super(); this.name = name; this.parent = parent; this.usings = new Array(); this.classes = new Array(); this.interfaces = new Array(); this.delegates = new Array(); this.namespaces = new Array(); this.header = ""; this.folder = this.fullName.replace(/\./g, '/'); this.apply(objectInitializer); } get outputFolder() { return this.folder; } addUsing(using) { if (this.usings.indexOf(using) === -1) { this.usings.push(using); } return using; } addClass(c) { if (this.classes.indexOf(c) === -1) { this.classes.push(c); } return c; } addInterface(i) { if (this.interfaces.indexOf(i) === -1) { this.interfaces.push(i); } return i; } addDelegate(delegate) { if (this.delegates.indexOf(delegate) === -1) { this.delegates.push(delegate); } return delegate; } addNamespace(n) { if (this.namespaces.indexOf(n) === -1) { this.namespaces.push(n); } return n; } add(item) { if (item instanceof class_1.Class) { this.classes.push(item); return item; } if (item instanceof Namespace) { this.namespaces.push(item); return item; } if (item instanceof interface_1.Interface) { this.interfaces.push(item); return item; } if (item instanceof import_1.Import) { this.addUsing(item); return item; } throw Error(`FATAL - UNABLE TO ADD UNKNOWN TYPE for '${JSON.stringify(item)}'`); } findClassByName(name) { return this.classes.filter(each => each.name === name); } get fullName() { if (this.parent instanceof Namespace) { return text_manipulation_1.dotCombine(this.parent.fullName, this.name); } return this.name; } toString() { return this.fullName; } async writeFiles(writer) { // write out all the files // handle nested namespaces const children = this.namespaces.map(async (namespace) => namespace.writeFiles(writer)); // combine class (XYZ) and interfaces (IXYZ) together in a single file const classes = text_manipulation_1.toMap(this.classes, c => c.fileName); const interfaces = text_manipulation_1.toMap(this.interfaces, i => i.fileName); for (const [key, classesWithSameName] of classes) { const contents = classesWithSameName.map(each => each.definition); const interfaceName = `I${key}`; const interfacesWithSameName = interfaces.get(interfaceName); if (interfacesWithSameName) { contents.push(...interfacesWithSameName.map(each => each.definition)); // remove from the list. interfaces.delete(interfaceName); } await writer(`${this.outputFolder}/${key}.cs`, this.render(contents.join(text_manipulation_1.EOL))); } for (const [key, interfacesWithSameName] of interfaces) { const contents = interfacesWithSameName.map(each => each.definition); await writer(`${this.outputFolder}/${key}.cs`, this.render(contents.join(text_manipulation_1.EOL))); } // do the delegates in a single file const delegates = this.delegates.map(v => v.implementation).join(text_manipulation_1.EOL); if (delegates) { await writer(`${this.outputFolder}/delegates.cs`, this.render(delegates)); } // wait for children to finish. await (Promise.all(children)); } render(content) { // all files get the header comment const header = text_manipulation_1.comment(this.header); // all files get imports const imports = text_manipulation_1.indent(this.usings.map((v) => v.implementation).join(text_manipulation_1.EOL), 1); const body = text_manipulation_1.indent(content, 1); return ` ${header} namespace ${this.fullName} { ${imports} ${body} } `.trim().replace(/ *$/gm, '').replace(/\n\n/g, '\n').replace(/^\s*EOL\s*$/igm, ''); } } exports.Namespace = Namespace; //# sourceMappingURL=namespace.js.map