Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5477e0

Browse files
fix: new classes to manage all the information of the class to generate
1 parent c4c001c commit a5477e0

File tree

3 files changed

+194
-73
lines changed

3 files changed

+194
-73
lines changed

class-generator.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2018 @cesiztel All rights reserved.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
* DEALINGS IN THE SOFTWARE.
21+
*
22+
*/
23+
24+
class ClassMethodGenerator {
25+
26+
constructor(name, scope, description) {
27+
this.name = name;
28+
29+
this.scope = scope;
30+
31+
this.description = description;
32+
33+
this.params = [];
34+
35+
this.returns = [];
36+
37+
this.body = null;
38+
}
39+
40+
getName() {
41+
return this.name;
42+
}
43+
44+
getScope() {
45+
return this.scope;
46+
}
47+
48+
getDescription() {
49+
return this.description;
50+
}
51+
52+
addParam(param) {
53+
this.params.push(param);
54+
}
55+
56+
getParams() {
57+
return this.params;
58+
}
59+
60+
addReturn(singleReturn) {
61+
this.returns.push(singleReturn);
62+
}
63+
64+
getReturns() {
65+
return this.returns;
66+
}
67+
68+
setBody(body) {
69+
this.body = body;
70+
}
71+
72+
getBody() {
73+
return this.body;
74+
}
75+
}
76+
77+
class ClassGenerator {
78+
79+
constructor(name) {
80+
this.name = name;
81+
82+
this.imports = [];
83+
84+
this.methods = [];
85+
86+
this.extends = [];
87+
88+
this.implements = [];
89+
}
90+
91+
getName() {
92+
return this.name;
93+
}
94+
95+
addImport(singleImport) {
96+
this.imports.push(singleImport);
97+
}
98+
99+
getImports() {
100+
return this.imports;
101+
}
102+
103+
addMethodGenerator(singleMethodGenerator) {
104+
this.methods.push(singleMethodGenerator)
105+
}
106+
107+
addExtend(singleExtend) {
108+
this.extends.push(singleExtend);
109+
}
110+
111+
addImplement(singleImplement) {
112+
this.implements.push(singleImplement);
113+
}
114+
115+
getExtends() {
116+
return this.extends;
117+
}
118+
119+
getImplements() {
120+
return this.implements;
121+
}
122+
123+
getMethodGenerators() {
124+
return this.methods;
125+
}
126+
}
127+
128+
exports.ClassGenerator = ClassGenerator;
129+
exports.ClassMethodGenerator = ClassMethodGenerator;

code-class-generator.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ class CodeBaseClassGenerator {
3232
/**
3333
* @constructor
3434
*/
35-
constructor (classConfiguration, extension, writer) {
36-
/** @member {string} classConfiguration */
37-
this.classConfiguration = classConfiguration;
38-
39-
/** @member {string} extension */
40-
this.extension = extension || null;
35+
constructor (schema, writer) {
36+
/** @member {string} schema */
37+
this.schema = schema;
4138

4239
/** @member {type.CodeWriter} */
4340
this.writer = writer;
@@ -55,9 +52,11 @@ class CodeBaseClassGenerator {
5552
}
5653

5754
imports() {
58-
for (var i in this.classConfiguration.uses) {
59-
this.writer.writeLine('use ' + this.classConfiguration.uses[i]);
60-
}
55+
let writer = this.writer;
56+
this.schema.getImports().forEach(function (singleImport) {
57+
writer.writeLine('use ' + singleImport);
58+
});
59+
6160
this.writer.writeLine('');
6261
}
6362

@@ -71,49 +70,54 @@ class CodeBaseClassGenerator {
7170
}
7271

7372
classSignature() {
74-
this.writer.writeLine('class ' + this.classConfiguration.name + ' extends ' + this.extension);
73+
let classExtends = this.schema.getExtends().join(',');
74+
let classImplementation = this.schema.getImplements().join(',');
75+
76+
let signatureExtras = '';
77+
if (classExtends.length > 0) {
78+
signatureExtras += ' extends ' + classExtends;
79+
}
80+
81+
if (classImplementation.length > 0) {
82+
signatureExtras += ' implements ' + classImplementation;
83+
}
84+
85+
this.writer.writeLine('class ' + this.schema.getName() + signatureExtras);
7586
}
7687

77-
blockDocs(methodMetaData) {
88+
mainClassCodeBody() {
89+
let writer = this.writer;
90+
this.schema.getMethodGenerators().forEach(function (singleMethodGenerator) {
91+
this.blockDocs(singleMethodGenerator);
92+
this.writeMethod(singleMethodGenerator);
93+
94+
writer.writeLine('');
95+
});
96+
}
97+
98+
blockDocs(methodGenerator) {
7899
this.writer.writeLine ('/**');
79-
this.writer.writeLine (' * ' + methodMetaData.description);
100+
this.writer.writeLine (' * ' + methodGenerator.getDescription());
80101
this.writer.writeLine (' *');
81-
for (var k in methodMetaData.returnValues) {
82-
this.writer.writeLine (' * @return ' + methodMetaData.returnValues[k].type);
83-
}
102+
let writer = this.writer;
103+
methodGenerator.getReturns().forEach(function (singleReturn) {
104+
writer.writeLine (' * @return ' + singleReturn.type);
105+
});
84106
this.writer.writeLine ( " */" );
85107
}
86108

87-
writeMethod(methodMetaData) {
88-
let scope = methodMetaData.scope;
89-
let methodName = methodMetaData.name;
90-
91-
this.writer.writeLine(scope + ' function ' + methodName +'()');
109+
writeMethod(methodGenerator) {
110+
this.writer.writeLine(methodGenerator.getScope() + ' function ' + methodGenerator.getName() +'()');
92111
this.writer.writeLine('{');
93-
if (methodMetaData.body === null) {
112+
if (methodGenerator.getBody() === null) {
94113
this.writer.indent();
95114
this.writer.writeLine("// Your code goes here...");
96115
this.writer.outdent();
97116
} else {
98-
methodMetaData.body();
117+
methodGenerator.getBody()();
99118
}
100119
this.writer.writeLine('}');
101120
}
102-
103-
mainClassCodeBody() {
104-
let genClass = this.classConfiguration;
105-
106-
let methodLength = genClass.methods.length - 1;
107-
let methodCounter = 0;
108-
for (var j in genClass.methods) {
109-
this.blockDocs(genClass.methods[j]);
110-
this.writeMethod(genClass.methods[j]);
111-
112-
if (methodCounter < methodLength) this.writer.writeLine('');
113-
114-
methodCounter++;
115-
}
116-
}
117121
}
118122

119123
exports.CodeBaseClassGenerator = CodeBaseClassGenerator;

migrations/code-generator.js

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const path = require('path');
2626
const codegen = require('../utils/codegen-utils');
2727
const fileUtils = require('../utils/file-utils');
2828
const codeClassGen = require('../code-class-generator');
29+
const classGenerator = require('../class-generator');
2930

3031
/**
3132
* Code Generator
@@ -95,43 +96,30 @@ class MigrationCodeGenerator {
9596
let tableName = elem.model.name;
9697
var classCodeGenerator = this;
9798

98-
var migrationClass = {
99-
name: 'Create' + (tableName.charAt(0).toUpperCase() + tableName.slice(1)) + 'Table',
100-
uses: [
101-
'Illuminate\\Support\\Facades\\Schema;',
102-
'Illuminate\\Database\\Schema\\Blueprint;',
103-
'Illuminate\\Database\\Migrations\\Migration;'
104-
],
105-
methods: [
106-
{
107-
'name': 'up',
108-
'scope': 'public',
109-
'description': 'Run the migrations.',
110-
'params': [],
111-
'returns': [{
112-
"type": "void"
113-
}],
114-
body: function () {
115-
classCodeGenerator.generateUpBody(tableName, elem);
116-
}
117-
},
118-
{
119-
'name': 'down',
120-
'scope': 'public',
121-
'description': 'Reverse the migrations.',
122-
'params': [],
123-
'returns': [{
124-
"type": "void"
125-
}],
126-
body: function () {
127-
classCodeGenerator.generateDownBody(tableName);
128-
}
129-
}
130-
]
131-
};
99+
let className = 'Create' + (tableName.charAt(0).toUpperCase() + tableName.slice(1)) + 'Table';
100+
101+
let classGenerator = classGenerator.ClassGenerator(className);
102+
classGenerator.addImport('Illuminate\\Support\\Facades\\Schema;');
103+
classGenerator.addImport('Illuminate\\Database\\Schema\\Blueprint;');
104+
classGenerator.addImport('Illuminate\\Database\\Migrations\\Migration;');
105+
classGenerator.addExtend('Migration');
106+
107+
let upMethodGenerator = classGenerator.ClassMethodGenerator('up', 'public', 'Run the migrations.');
108+
upMethodGenerator.addReturn({ "type": "void" });
109+
upMethodGenerator.setBody(function () {
110+
classCodeGenerator.generateUpBody(tableName, elem);
111+
});
112+
113+
let downMethodGenerator = classGenerator.ClassMethodGenerator('down', 'public', 'Reverse the migrations.');
114+
downMethodGenerator.addReturn({ "type": "void" });
115+
downMethodGenerator.setBody(function () {
116+
classCodeGenerator.generateDownBody(tableName);
117+
});
118+
119+
classGenerator.addMethodGenerator(upMethodGenerator);
120+
classGenerator.addMethodGenerator(downMethodGenerator);
132121

133-
let codeBaseClassGenerator = new codeClassGen.CodeBaseClassGenerator(migrationClass, 'Migration', this.writer);
134-
codeBaseClassGenerator.generate();
122+
(new codeClassGen.CodeBaseClassGenerator(classGenerator, this.writer)).generate();
135123
}
136124

137125
generateUpBody (tableName, elem) {

0 commit comments

Comments
 (0)