forked from sdiemert/StarUMLJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSCodeGenerator.js
150 lines (105 loc) · 4.11 KB
/
JSCodeGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Created by sdiemert on 15-07-10.
*/
define(function (require, exports, module) {
"use strict";
var fs = app.getModule("filesystem/FileSystem");
var fsUtils = app.getModule("file/FileUtils");
var async = app.getModule("utils/Async");
var PrototypeCodeGenerator = require("CodeGenerators/PrototypeCodeGenerator").PrototypeCodeGenerator;
var FunctionalCodeGenerator = require("CodeGenerators/FunctionalCodeGenerator").FunctionalCodeGenerator;
var MongooseCodeGenerator = require("CodeGenerators/MongooseCodeGenerator").MongooseCodeGenerator;
var EmberDSCodeGenerator = require("CodeGenerators/EmberDSCodeGenerator").EmberDSCodeGenerator;
/**
*
* @param {type.UMLPackage} baseModel
* @param {string} basePath
* @constructor
*/
function JSCodeGenerator(baseModel, basePath, opts) {
this.baseModel = baseModel;
this.basePath = basePath;
this.opts = opts;
this.generator = this.getGenerator(opts);
}
JSCodeGenerator.prototype.generate = function (elem, path, opts) {
var result = new $.Deferred();
var self = this;
var fullPath = "";
var directory = "";
var file = "";
if (elem instanceof type.UMLModel) {
fullPath = path;
async.doSequentially(
elem.ownedElements,
function (child) {
return self.generate(child, fullPath, opts);
},
false
).then(result.resolve, result.reject);
}
else if (elem instanceof type.UMLPackage) {
fullPath = path + "/" + elem.name;
//handle creating a new directory.
directory = fs.getDirectoryForPath(fullPath);
directory.create(function (err, stat) {
if (!err) {
async.doSequentially(
elem.ownedElements,
function (child) {
return self.generate(child, fullPath, opts);
},
false
).then(result.resolve, result.reject);
} else {
console.log(err);
result.reject(err);
}
});
} else if (elem instanceof type.UMLClass) {
if (elem.stereotype === "annotationType") {
//do annotation type
} else {
//generate the class.
fullPath = path + "/" + self.generator.getFileName(elem.name,true);
console.log(elem);
file = fs.getFileForPath(fullPath);
fsUtils.writeText(file, self.generateClassCode(elem, opts), true).then(result.resolve, result.reject);
}
} else {
return result.resolve();
}
return result.promise();
};
JSCodeGenerator.prototype.generateClassCode = function (elem, opts) {
return this.generator.generate(elem);
};
/**
* Get the appropriate generator class based on the options set in the configuration
* @param options the options
* @return the generator
*/
JSCodeGenerator.prototype.getGenerator = function (options) {
var generator = null;
switch (options.classType) {
case "functional":
generator = new FunctionalCodeGenerator(options.indentSpaces);
break;
case "prototype":
generator = new PrototypeCodeGenerator(options.indentSpaces);
break;
case "mongoose":
generator = new MongooseCodeGenerator(options);
break;
case "ember":
generator = new EmberDSCodeGenerator(options);
break;
}
console.log(generator);
return generator;
};
exports.generate = function (baseModel, basePath, opts) {
var jsgen = new JSCodeGenerator(baseModel, basePath, opts);
return jsgen.generate(baseModel, basePath, opts);
};
});