Skip to content

Commit 92c9fc2

Browse files
committed
metaclass
1 parent 949d479 commit 92c9fc2

File tree

2 files changed

+91
-53
lines changed

2 files changed

+91
-53
lines changed

MetaClass.js

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@ const JUMP_LINE = '\n';
22
const FORMAT_CHAR = '\t';
33

44
class Function {
5-
constructor() {
6-
this.name = '';
7-
this.type = '';
8-
this.parameters = [];
9-
this.privacy = '';
5+
constructor({
6+
name = '',
7+
type = '',
8+
parameters = [],
9+
privacy = ''
10+
}) {
11+
this.name = name;
12+
const typeParts = type.split('.');
13+
this.type = typeParts[typeParts.length - 1];
14+
15+
this.parameters = parameters;
16+
this.dependencies = new Set([typeParts[0]].concat(this.parameters.map(parameter => parameter.split('.')[0])))
17+
this.privacy = privacy;
18+
}
19+
20+
buildParameters() {
21+
return this.parameters
22+
.map(parameter => `${parameter.name}: ${parameter.type}`)
23+
.join(', ');
24+
}
25+
26+
build() {
27+
let builder = '';
28+
const privacyIfExists = this.privacy ? `${this.privacy} ` : '';
29+
builder += `${FORMAT_CHAR}${privacyIfExists}${this.name} (${this.buildParameters()}): ${this.type} {${JUMP_LINE}`;
30+
builder += `${FORMAT_CHAR}${FORMAT_CHAR}//TODO Implement${JUMP_LINE}`;
31+
builder += `${FORMAT_CHAR}}${JUMP_LINE}`;
32+
return builder;
1033
}
1134
}
1235

@@ -16,22 +39,48 @@ class Propertie {
1639
}
1740
}
1841

42+
class MetaImport {
43+
constructor(fullObjectPath) {
44+
this.imports = {};
45+
}
46+
47+
addImport(fullObjectPath) {
48+
const fullObjectPathParts = fullObjectPath.split('.');
49+
const fromObj = fullObjectPathParts[0];
50+
const importObj = fullObjectPathParts[1];
51+
52+
const allImportsFromObject = this.imports[fromObj] || [];
53+
allImportsFromObject.push(importObj);
54+
this.imports[fromObj] = allImportsFromObject
55+
}
56+
57+
build() {
58+
return Object
59+
.keys(this.imports)
60+
.map((from => `import { ${this.imports[from].join(', ')} } from ${from}`))
61+
.join(JUMP_LINE)
62+
}
63+
}
64+
1965
class MetaClass {
2066
constructor() {
2167
this.properties = [];
2268
this.functions = [];
2369
this.className = '';
70+
this.imports = new MetaImports();
2471
}
2572

2673
buildImports() {
2774
let builder = '';
28-
return builder;
75+
// return this.functions
76+
// .map(func => func.dependencies)
77+
// .reduce((prev, cur) => new Set([...prev, ...cur]), new Set())
78+
// .map(import;
2979
}
3080

3181
buildClass() {
3282
let builder = '';
33-
builder += `class ${this.className} {'`;
34-
builder += JUMP_LINE;
83+
builder += `class ${this.className} {`;
3584
return builder;
3685
}
3786

@@ -46,24 +95,15 @@ class MetaClass {
4695
}
4796

4897
buildFunctions() {
49-
let builder = '';
50-
this.functions.forEach(func => {
51-
let parametersBuilder = ''
52-
if (func.parameters) {
53-
func.parameters.forEach(parameter => {
54-
parametersBuilder += `${parameter.name}: ${parameter.type}, `;
55-
});
56-
parametersBuilder = parametersBuilder.substring(0, parametersBuilder.length - 2)
57-
}
58-
metaClass += `${FORMAT_CHAR}static ${func.name} (${parametersBuilder}): ${func.type} {${JUMP_LINE}`;
59-
metaClass += `${FORMAT_CHAR}${FORMAT_CHAR}//TODO Implement${JUMP_LINE}`;
60-
metaClass += `${FORMAT_CHAR}}${JUMP_LINE}`;
61-
})
62-
return builder;
98+
return this.functions
99+
.map(func => func.build())
100+
.reduce((prev, cur) => {
101+
return `${prev}${JUMP_LINE}${cur}`;
102+
}, '');
63103
}
64104

65105
buildEndClass() {
66-
let builder = '';
106+
let builder = JUMP_LINE;
67107
builder += '};'
68108
builder += JUMP_LINE;
69109
builder += `export default ${this.className};`;
@@ -72,13 +112,17 @@ class MetaClass {
72112

73113
build() {
74114
let metaClass = '';
75-
metaClass += buildImports();
76-
metaClass += buildClass();
77-
metaClass += buildProperties();
78-
metaClass += buildFunctions();
79-
metaClass += buildEndClass();
115+
metaClass += this.buildImports();
116+
metaClass += this.buildClass();
117+
// metaClass += this.buildProperties();
118+
metaClass += this.buildFunctions();
119+
metaClass += this.buildEndClass();
80120
return metaClass;
81121
}
82122
}
83123

84-
export default MetaClass;
124+
module.exports = {
125+
MetaClass,
126+
Function,
127+
Propertie
128+
}

getSpreadsheetDocs.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const fs = require('fs');
22
const axios = require('axios');
3-
const MetaClass = require('./MetaClass');
3+
const Meta = require('./MetaClass');
4+
const MetaClass = Meta.MetaClass;
5+
const Function = Meta.Function;
46

57
const API_JSON_DIR = 'api-json'
68
const CLASSES_DIR = 'classes'
@@ -29,42 +31,34 @@ const processObj = (obj, treeName) => {
2931
return;
3032
}
3133

34+
const metaClass = new MetaClass();
3235
const fullName = obj['1'];
3336
const name = fullName.indexOf('.') > -1 ? obj['1'].split('.')[1] : obj['1'];
34-
const dirToSave = CLASSES_DIR + '/' + treeName;
37+
const dirToSave = CLASSES_DIR + '/' + (treeName || name);
3538
const filePath = dirToSave + '/' + (!treeName ? 'index.js' : name + '.js' )
36-
let metaClass = 'class ' + name + ' {\n';
39+
metaClass.className = name;
3740

3841
const props = obj['2']
3942
const funcs = obj['3']
4043
if (!treeName && props && Array.isArray(props)) {
41-
props.forEach(item => {
42-
const propName = item['1']
43-
metaClass += '\n' + propName + ','
44-
});
45-
metaClass += ';\n';
44+
metaClass.properties = props.map(item => ({ name: item['1'] }));
4645
}
4746

4847
if (funcs && Array.isArray(funcs)) {
49-
funcs.forEach(func => {
50-
const funcName = func['1']
51-
const resultType = func['2']
52-
const params = func['3']
53-
54-
let strParams = ''
55-
if (params) {
56-
params.forEach(param => {
57-
strParams += param['1'] + ': ' + param['2'] + ', '
58-
})
59-
strParams = strParams.substring(0, strParams.length - 2)
60-
}
61-
metaClass += '\tstatic ' + funcName + '(' + strParams + '): ' + resultType + ' {\n\t\t//TODO Implement\n\t}\n';
48+
metaClass.functions = funcs.map(func => {
49+
return new Function({
50+
name: func['1'],
51+
type: func['2'],
52+
parameters: (func['3'] || []).map(parameter => ({
53+
name: parameter['1'],
54+
type: parameter['2']
55+
})),
56+
privacy: treeName ? '' : 'static'
57+
})
6258
})
6359
}
6460

65-
metaClass += '};\n\nexport default ' + name + ';';
66-
67-
fs.writeFile(filePath, metaClass, function(err) {
61+
fs.writeFile(filePath, metaClass.build(), function(err) {
6862
console.log('+->Saved class ' + filePath);
6963
resolve(name)
7064
})

0 commit comments

Comments
 (0)