forked from webgme/webgme
/
DecoratorGenerator.js
125 lines (106 loc) · 4.2 KB
/
DecoratorGenerator.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
/*globals define*/
/*jshint browser: true, node:true*/
/**
* Plugin for generating a decorator. Generating a META aware plugin is experimental and the generated code
* may need some tweaks.
* @author zhangpn / https://github.com/zhangpn
* @module CorePlugins:DecoratorGenerator
*/
define([
'plugin/PluginBase',
'text!./metadata.json',
'plugin/DecoratorGenerator/DecoratorGenerator/TemplatesInherit/Templates',
'plugin/DecoratorGenerator/DecoratorGenerator/TemplatesMinimal/Templates',
'common/util/ejs'
], function (PluginBase, pluginMetadata, TEMPLATES_INHERIT, TEMPLATES_MINIMAL, ejs) {
'use strict';
pluginMetadata = JSON.parse(pluginMetadata);
/**
* Initializes a new instance of DecoratorGenerator.
* @class
* @augments {PluginBase}
* @classdesc This class represents the plugin DecoratorGenerator.
* @constructor
*/
function DecoratorGenerator() {
// Call base class' constructor.
PluginBase.call(this);
this.pluginMetadata = pluginMetadata;
}
DecoratorGenerator.metadata = pluginMetadata;
// Prototypical inheritance from PluginBase.
DecoratorGenerator.prototype = Object.create(PluginBase.prototype);
DecoratorGenerator.prototype.constructor = DecoratorGenerator;
/**
* Main function for the plugin to execute. This will perform the execution.
* Notes:
* - Always log with the provided logger.[error,warning,info,debug].
* - Do NOT put any user interaction logic UI, etc. inside this method.
* - callback always has to be called even if error happened.
*
* @param {function(string, plugin.PluginResult)} callback - the result callback
*/
DecoratorGenerator.prototype.main = function (callback) {
// Use self to access core, project, result, logger etc from PluginBase.
// These are all instantiated at this point.
var self = this,
config = self.getCurrentConfig(),
filesToAdd = {},
success,
artifact;
if (config.inherit) {
success = self.renderInheritTemplates(filesToAdd, config);
} else {
success = self.renderMinimalTemplates(filesToAdd, config);
}
artifact = self.blobClient.createArtifact(config.decoratorName + 'Decorator');
artifact.addFiles(filesToAdd, function (err /*, hashes*/) {
if (err) {
callback(err, self.result);
return;
}
artifact.save(function (err, hash) {
if (err) {
callback(err, self.result);
return;
}
self.result.addArtifact(hash);
self.result.setSuccess(success);
callback(null, self.result);
});
});
};
DecoratorGenerator.prototype.renderMinimalTemplates = function (filesToAdd, config) {
var baseDir = config.decoratorName + 'Decorator/',
path,
dataModel = {
id: config.decoratorName
},
templateName;
for (templateName in TEMPLATES_MINIMAL) {
if (TEMPLATES_MINIMAL.hasOwnProperty(templateName)) {
path = templateName.substring(0, templateName.length - '.ejs'.length);
path = baseDir + path.replace('__ID__', config.decoratorName);
filesToAdd[path] = ejs.render(TEMPLATES_MINIMAL[templateName], dataModel);
}
}
return true;
};
DecoratorGenerator.prototype.renderInheritTemplates = function (filesToAdd, config) {
var baseDir = config.decoratorName + 'Decorator/',
path,
dataModel = {
id: config.decoratorName
},
templateName;
for (templateName in TEMPLATES_INHERIT) {
if (TEMPLATES_INHERIT.hasOwnProperty(templateName)) {
path = templateName.substring(0, templateName.length - '.ejs'.length);
path = baseDir + path.replace('__ID__', config.decoratorName);
filesToAdd[path] = ejs.render(TEMPLATES_INHERIT[templateName], dataModel);
}
}
return true;
};
return DecoratorGenerator;
});