Skip to content

Commit

Permalink
#17 Code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
faizanvahevaria committed Jun 25, 2019
1 parent bce8afd commit b7c3163
Show file tree
Hide file tree
Showing 5 changed files with 1,285 additions and 1,311 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
90 changes: 40 additions & 50 deletions code-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,65 @@ const openApiGen = require('./openApi-generator');

class CodeGenerator {

/**
* @constructor
*
* @param {type.UMLPackage} baseModel
* @param {string} basePath generated files and directories to be placed
*/
constructor (baseModel, basePath) {
/** @member {type.Model} */
this.baseModel = baseModel

/** @member {string} */
this.basePath = basePath
constructor(baseModel, basePath,options) {
this.baseModel = baseModel;
this.basePath = basePath;
this.options = options;
}

/**
* Return Indent String based on options
* @param {Object} options
* @return {string}
*/
getIndentString (options) {
if (options.useTab) {
return '\t'
} else {
var i
var len
var indent = []
for (i = 0, len = options.indentSpaces; i < len; i++) {
indent.push(' ')
}
return indent.join('')
}
getIndentString() {
return this.options.useTab ?
"\t" :
this.options.indentSpaces.map(() => " ").join("");

}

generate(elem, basePath, options,fileType){
try{
if (elem instanceof type.UMLPackage) {
fs.mkdirSync(basePath)
let schemaWriter = new openApiGen.OpenApiGenerator();
schemaWriter.generate(basePath,elem,options,fileType);
}
else{
fs.mkdirSync(basePath);
let i,len

for (i = 0, len = elem.ownedElements.length; i < len; i++) {
let def = elem.ownedElements[i];
if (def instanceof type.UMLPackage) {
let schemaWriter = new openApiGen.OpenApiGenerator();
schemaWriter.generate(basePath,def,options,fileType);
}
}
}
// app.toast.info("OpenAPI generation completed");
} catch (e) {
app.toast.error("Generation Failed!");
}
generate(fileType) {
try {
fs.mkdirSync(this.basePath);
const schemaWriter = new openApiGen.OpenApiGenerator();
if (this.baseModel instanceof type.UMLPackage) {
schemaWriter.generate(
this.basePath,
this.baseModel,
this.options,
fileType
);
} else {
this.baseModel.ownedElements.forEach(element => {
if (element instanceof type.UMLPackage) {
schemaWriter.generate(
this.basePath,
element,
this.options,
fileType
);
}
});
}
} catch (_e) {
app.toast.error("Generation Failed!");
}
}

}

/**
/**
* Generate
* @param {type.Model} baseModel
* @param {string} basePath
* @param {Object} options
*/
function generate (baseModel, basePath, options,fileType) {
var codeGenerator = new CodeGenerator(baseModel, basePath);
codeGenerator.generate(baseModel, basePath, options,fileType)
function generate(baseModel, basePath, options, fileType) {
const codeGenerator = new CodeGenerator(baseModel, basePath, options);
codeGenerator.generate(fileType);
}

exports.generate = generate
110 changes: 59 additions & 51 deletions codegen-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,62 @@
* CodeWriter
*/
class CodeWriter {
/**
* @constructor
*/
constructor (indentString) {
/** @member {Array.<string>} lines */
this.lines = []

/** @member {string} indentString */
this.indentString = indentString || ' ' // default 2 spaces

/** @member {Array.<string>} indentations */
this.indentations = []
}

/**
* Indent
*/
indent () {
this.indentations.push(this.indentString)
}

/**
* Outdent
*/
outdent () {
this.indentations.splice(this.indentations.length - 1, 1)
}

/**
* Write a line
* @param {string} line
*/
writeLine (line) {
if (line) {
this.lines.push(this.indentations.join('') + line)
} else {
this.lines.push('')
}
}

/**
* Return as all string data
* @return {string}
*/
getData () {
return this.lines.join('\n')
}

}

exports.CodeWriter = CodeWriter;
/**
* @constructor
*/
constructor (indentString) {
/** @member {Array.<string>} lines */
this.lines = []

/** @member {string} indentString */
this.indentString = indentString || ' ' // default 2 spaces

/** @member {Array.<string>} indentations */
this.indentations = []
}

/**
* Indent
*/
indent () {
this.indentations.push(this.indentString)
}

/**
* Outdent
*/
outdent () {
this.indentations.splice(this.indentations.length - 1, 1)
}

/**
* Write a line
* @param {string} line
*/
writeLine(line, mIndent, mOutdent) {
var i = 0;
for (i = 0; i < mIndent; i++) {
this.indent();
}
if (line) {
this.lines.push(this.indentations.join('') + line)
} else {
this.lines.push('')
}
for (i = 0; i < mOutdent; i++) {
this.outdent();
}
}

/**
* Return as all string data
* @return {string}
*/
getData () {
return this.lines.join('\n')
}

}

exports.CodeWriter = CodeWriter;

2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const PREF_INDENTSPC = "openapi.gen.indentSpaces";
function getGenOptions() {
return {
idlDoc: app.preferences.get(PREF_GENDOC),
indentSpaces: app.preferences.get(PREF_INDENTSPC),
indentSpaces: [],
debug: app.preferences.get(PREF_DEBUG_KEY)
};
}
Expand Down
Loading

0 comments on commit b7c3163

Please sign in to comment.