-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate-sources-task.js
More file actions
287 lines (261 loc) · 8.95 KB
/
generate-sources-task.js
File metadata and controls
287 lines (261 loc) · 8.95 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
const fs = require('fs-extra');
const IncrementalFileTask = require('appc-tasks').IncrementalFileTask;
const metabase = require('../metabase');
const path = require('path');
/**
* Generates Hyperloop wrapper files for all Java classes being referenced
*/
class GenerateSourcesTask extends IncrementalFileTask {
constructor(taskInfo) {
super(taskInfo);
this._metabase = null;
this._references = null;
this._classListPathAndFilename = path.join(this.incrementalDirectory, 'classes.json');
this._generatedClasses = new Set();
}
/**
* Gets the output directory where all wrappers will be generate to
*
* @return {String} Full path to the output directory
*/
get outputDirectory() {
return this._outputDirectory;
}
/**
* Sets the out directory where all wrappers will be generate to
*
* @param {String} outputPath Full path to the output directory
*/
set outputDirectory(outputPath) {
this._outputDirectory = outputPath;
this._hyperloopOutputDirectory = path.join(outputPath, 'hyperloop');
fs.ensureDirSync(this._hyperloopOutputDirectory);
this.registerOutputPath(outputPath);
}
/**
* Metabase that will be used to generate the wrapper files
*
* @return {Object} Metabase object
*/
get metabase() {
return this._metabase;
}
/**
* Sets the metabase object
*
* @param {Object} metabase
*/
set metabase(metabase) {
this._metabase = metabase;
}
/**
* Mapping of source files and their referenced Java types
*
* @return {Map}
*/
get references() {
return this._references;
}
/**
* Sets the Java type reference map
*
* @param {Map} references
*/
set references(references) {
this._references = references;
}
/**
* @inheritdoc
*/
get incrementalOutputs() {
return [this._outputDirectory, this._classListPathAndFilename];
}
/**
* Does a full task run, which will generate the Hyperloop wrapper for every
* referenced Java class
*
* @return {Promise}
*/
async doFullTaskRun() {
await fs.emptyDir(this._outputDirectory);
await fs.ensureDir(this._hyperloopOutputDirectory);
if (this.references.size === 0) {
this._logger.info('Skipping Hyperloop wrapper generation, no usage found ...');
return;
}
const classesToGenerate = metabase.generate.expandDependencies(this.metabase, this.getAllReferencedClasses());
await this.generateSources(classesToGenerate, []);
this._generatedClasses = new Set(classesToGenerate);
await this.generateBootstrap();
await this.writeClassList();
}
/**
* Does an incremental task run, which will generate the Hyperloop wrappers
* for new and changed files and delete the wrapper for removed references.
*
* To properly detect removed references we store a list with the name of all
* previously generated wrapper files and compare that with the ones that were
* generated in the current run. Any wrapper file that is not used
* anymore will be deleted.
*
* @param {Map.<String, String>} changedFiles Map of changed files and their state (created, changed, deleted)
* @return {Promise}
*/
async doIncrementalTaskRun(changedFiles) {
const fullBuild = !(await this.loadClassList());
if (fullBuild) {
return await this.doFullTaskRun();
}
const expandedClassList = metabase.generate.expandDependencies(this.metabase, this.getAllReferencedClasses());
const classesToGenerate = expandedClassList.filter(className => !this._generatedClasses.has(className));
const classesToRemove = Array.from(this._generatedClasses).filter(className => expandedClassList.indexOf(className) === -1);
await this.removeUnusedClasses(classesToRemove);
await this.generateSources(classesToGenerate, classesToRemove);
classesToGenerate.forEach(className => this._generatedClasses.add(className));
classesToRemove.forEach(className => this._generatedClasses.delete(className));
await this.generateBootstrap();
await this.writeClassList();
}
/**
* Gets a list of all referenced native types
*
* @return {Array.<String>} Array of referenced native types
*/
getAllReferencedClasses() {
let referencedClasses = [];
this.references.forEach(fileInfo => {
referencedClasses = referencedClasses.concat(fileInfo.usedClasses);
});
return referencedClasses;
}
/**
* Removes any unused class wrappers from the output directory
*
* @param {Array.<String>} classesToRemove Array of class names
* @return {Promise}
*/
async removeUnusedClasses(classesToRemove) {
await Promise.all(classesToRemove.map(async className => {
try {
const classPathAndFilename = path.join(this._hyperloopOutputDirectory, className + '.js');
if (await fs.exists(classPathAndFilename)) {
await fs.unlink(classPathAndFilename);
}
} catch (err) {
this.logger.error(`Failed to delete file: ${classPathAndFilename}`);
}
}));
}
/**
* Generates the wrapper source files by delegating to the generator inside
* the metabase module
*
* @param {Array.<String>} classesToGenerate Array of classes to generate sources for
* @param {Array.<String>} removedClasses Array of classes that were removed
* @return {Promise}
*/
async generateSources(classesToGenerate, removedClasses) {
if (classesToGenerate.length === 0 && removedClasses.length === 0) {
this.logger.trace('All class wrappers are up-to-date.');
return;
}
await new Promise((resolve, reject) => {
const options = {
classesToGenerate: classesToGenerate,
removedClasses: removedClasses,
existingClasses: Array.from(this._generatedClasses)
};
metabase.generate.generateFromJSON(this._hyperloopOutputDirectory, this.metabase, options, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
/**
* Generate a hyperloop bootstrap script to be loaded on app startup, but before the "app.js" gets loaded.
* Provides JS require/import alias names matching Java class names which maps them to their equivalent JS files.
*
* This method is expected to be called after the generateSources() method and after updating
* member variable "_generatedClasses" with all Java class name references.
* @return {Promise}
*/
async generateBootstrap() {
const bootstrapFileLines = [];
const bootstrapFileName = 'hyperloop.bootstrap.js';
const bootstrapFilePath = path.join(this._hyperloopOutputDirectory, bootstrapFileName);
if (this._generatedClasses.size > 0) {
bootstrapFileLines.push('var binding = global.binding;');
const outputFileNames = await fs.readdir(this._hyperloopOutputDirectory);
outputFileNames.sort();
for (const fileName of outputFileNames) {
if ((fileName !== bootstrapFileName) && (path.extname(fileName).toLowerCase() === '.js')) {
const requireName = fileName.substring(0, fileName.length - 3);
if (this._generatedClasses.has(requireName)) {
// Bind to a Java class. (Use dot notation when referencing inner classes.)
const aliasName = requireName.replace(/\$/g, '.');
bootstrapFileLines.push(`binding.redirect('${aliasName}', '/hyperloop/${requireName}');`);
} else {
// Bind to a Java package. (Uses wildcard notation such as "java.io.*".)
bootstrapFileLines.push(`binding.redirect('${requireName}.*', '/hyperloop/${requireName}');`);
}
}
}
}
if (bootstrapFileLines.length > 0) {
await fs.writeFile(bootstrapFilePath, bootstrapFileLines.join('\n') + '\n');
} else if (await fs.exists(bootstrapFilePath)) {
await fs.unlink(bootstrapFilePath);
}
}
/**
* Fetches an array of all JS-to-Java proxy files generated by this task.
* Intended to be called after the task has been ran.
*
* Each element in the array provides a file path relative to task's assigned "outputDirectory",
* such as "hyperloop/java.io.File.js".
*
* @return {Promise<String[]>}
* Returns an array of JS proxy file paths generated by this task.
* Returns an empty array if no JS files have been generated yet.
*/
async fetchGeneratedJsProxyPaths() {
const proxyFilePaths = [];
const outputFileNames = await fs.readdir(this._hyperloopOutputDirectory);
for (const fileName of outputFileNames) {
if (!fileName.endsWith('.bootstrap.js') && (path.extname(fileName).toLowerCase() === '.js')) {
proxyFilePaths.push(`hyperloop/${fileName}`)
}
}
return proxyFilePaths;
}
/**
* Loads the class list used in incremental task runs
*
* @return {Promise<Boolean>} True if the files was loaded successfully, false if not
*/
async loadClassList() {
try {
if (await fs.exists(this._classListPathAndFilename)) {
const fileContent = await fs.readFile(this._classListPathAndFilename);
this._generatedClasses = new Set(JSON.parse(fileContent.toString()));
return true
}
} catch (e) {
this.logger.trace('Loading class list failed: ' + e);
}
return false;
}
/**
* Stores the list of all currently generated Hyperloop class wrappers
*
* @return {Promise}
*/
async writeClassList() {
await fs.writeFile(this._classListPathAndFilename, JSON.stringify(Array.from(this._generatedClasses)));
}
}
module.exports = GenerateSourcesTask;