forked from webgme/webgme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVisualizerGenerator.js
166 lines (144 loc) · 5.85 KB
/
VisualizerGenerator.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*globals define*/
/*jshint node:true, browser:true*/
/**
* Plugin for generating visualizers.
* @author brollb / https://github.com/brollb
* @module CorePlugins:VisualizerGenerator
*/
define([
'plugin/PluginConfig',
'plugin/PluginBase',
'text!./metadata.json',
'common/util/ejs',
'plugin/VisualizerGenerator/VisualizerGenerator/Templates/Templates'
], function (
PluginConfig,
PluginBase,
pluginMetadata,
ejs,
TEMPLATES) {
'use strict';
pluginMetadata = JSON.parse(pluginMetadata);
/**
* Initializes a new instance of VisualizerGenerator.
* @class
* @augments {PluginBase}
* @classdesc This class represents the plugin VisualizerGenerator.
* @constructor
*/
function VisualizerGenerator() {
// Call base class' constructor.
PluginBase.call(this);
this.pluginMetadata = pluginMetadata;
}
VisualizerGenerator.metadata = pluginMetadata;
// Prototypical inheritance from PluginBase.
VisualizerGenerator.prototype = Object.create(PluginBase.prototype);
VisualizerGenerator.prototype.constructor = VisualizerGenerator;
/**
* 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
*/
VisualizerGenerator.prototype.main = function (callback) {
var self = this,
content,
artifact,
count,
files,
file,
filePath,
getFilePath,
onAddFile = function (err) {
if (err) {
callback(err, self.result);
return;
}
if (--count === 0) {
self.blobClient.saveAllArtifacts(function (err, hashes) {
if (err) {
callback(err, self.result);
return;
}
// Visualizer instructions
self.createMessage(null, 'Extract the visualizerFiles.zip' +
' in your repository root.');
self.createMessage(null, 'Add the visualizer entry to ' +
'src/client/js/Visualizers.json');
self.createMessage(null, 'Set the path of the visualizer ' +
'entry to "js/Panels/' + content.visualizerID + '/' +
content.visualizerID + 'Panel"');
self.createMessage(self.rootNode, 'Select the root-node and add ' +
content.visualizerID + ' to the validVisualizers attribute '+
'(separate with spaces) to enable the visualizer for the current ' +
'project');
self.createMessage(null, 'Refresh the browser to update the ' +
'available visualizers');
self.result.addArtifact(hashes[0]);
self.logger.info('Artifacts are saved here: ' + hashes.toString());
self.result.setSuccess(true);
callback(null, self.result);
});
}
};
// Create the content used in the template files
content = self.getCurrentConfig();
content.date = new Date();
content.version = this.getVersion();
content.widgetClass = VisualizerGenerator.toDashed(content.visualizerID);
getFilePath = VisualizerGenerator.getFilePath.bind(null, content.visualizerID);
files = Object.keys(TEMPLATES);
count = files.length;
artifact = self.blobClient.createArtifact('visualizerFiles');
for (var i = files.length; i--;) {
file = ejs.render(TEMPLATES[files[i]], content);
filePath = getFilePath(files[i]);
artifact.addFile(filePath, file, onAddFile);
}
};
VisualizerGenerator.getFilePath = function (visName, fileName) {
var fileToDir = {
Control: 'Panels',
Panel: 'Panels',
Widget: 'Widgets'
},
baseUrl = 'src/client/js',
baseName,
ext,
extRegex = /\.[\w]+$/,
path;
// Get the original extension (second to last)
ext = fileName.replace(extRegex, '').match(extRegex);
// Get the base name and capitalize it
baseName = fileName.match(/^[\w\d_]*/)[0];
baseName = baseName.substring(0,1).toUpperCase()+baseName.substring(1);
// Put it in a visName directory then in Panels or Widgets
if (baseName === 'Styles') {
fileName = visName+'Widget'+ext;
path = [baseUrl, 'Widgets', visName, 'styles', fileName].join('/');
} else {
fileName = visName+baseName+ext;
path = [baseUrl, fileToDir[baseName], visName, fileName].join('/');
}
return path;
};
VisualizerGenerator.toDashed = function(camelCase) {
var index;
// Convert first character to lowercase
camelCase = camelCase.substring(0,1).toLowerCase()+camelCase.substring(1);
// Convert each capital letter to '-'+\L
index = camelCase.search(/[A-Z]+/);
while (index > -1) {
camelCase = camelCase.substring(0, index) + '-' +
camelCase.substring(index, index+1).toLowerCase() +
camelCase.substring(index+1);
index = camelCase.search(/[A-Z]+/);
}
return camelCase;
};
return VisualizerGenerator;
});