-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathgenerate-wrappers.js
1166 lines (921 loc) · 37.1 KB
/
generate-wrappers.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const _ = require('underscore');
const path = require('path');
const fse = require('fs-extra');
const Glob = require('glob').Glob;
const Handlebars = require('handlebars');
const classConfigs = require('./three-class-config');
const Types = require('./prop-types.js');
const scriptDir = __dirname;
const baseDir = path.resolve(scriptDir, '..');
const jsSrcDir = path.resolve(baseDir, 'src/');
const pySrcDir = path.resolve(baseDir, '..', 'pythreejs');
const docSrcDir = path.resolve(baseDir, '..', 'docs', 'source', 'api');
const templateDir = path.resolve(scriptDir, 'templates');
const threeSrcDir = path.resolve(baseDir, 'node_modules', 'three', 'src');
const AUTOGEN_EXT = 'autogen';
const JS_AUTOGEN_EXT = '.' + AUTOGEN_EXT + '.js';
/**
* Custom classes, i.e. classes that should be included in the
* autogen routine but which has no *direct* counterpart in the
* three.js library.
*/
const CUSTOM_CLASSES = [
'assets/GLTFAsset.js',
'textures/ImageTexture.js',
'textures/TextTexture.js',
'cameras/CombinedCamera.js',
'controls/Controls.js',
'controls/OrbitControls.js',
'controls/TrackballControls.js',
'controls/FlyControls.js',
'controls/Picker.js',
'core/BaseGeometry.js',
'core/BaseBufferGeometry.js',
'objects/CloneArray.js',
'objects/Blackbox.js',
'objects/Line2.js',
'objects/LineSegments2.js',
'geometries/LineGeometry.js',
'geometries/LineSegmentsGeometry.js',
'materials/LineMaterial.js',
];
const IGNORE_FILES = [
'**/Three.Legacy.js', // Don't support legacy interface (deprecation should be done python side)
'**/Three.js', // Don't process aggregrate file
'**/polyfills.js', // Polyfill of JS methods, nothing to export
'**/utils.js', // Utility functions, no objects to export
'**/constants.js', // Processed into enums in separate script
'**/audio/AudioContext.js', // JS API for audio, nothing to expose
'**/core/Face3.js', // Implemented as trait only, not widget model
'**/core/Uniform.js', // Implemented as trait only, not widget model
'**/geometries/Geometries.js', // index.js like file, nothing new here
'**/materials/Materials.js', // index.js like file, nothing new here
'**/materials/MeshDistanceMaterial.js', // TODO: Undocumented as of yet
'**/math/Vector2.js', // Implemented as trait only, not widget model
'**/math/Vector3.js', // Implemented as trait only, not widget model
'**/math/Vector4.js', // Implemented as trait only, not widget model
'**/math/Matrix3.js', // Implemented as trait only, not widget model
'**/math/Matrix4.js', // Implemented as trait only, not widget model
'**/math/Color.js', // Implemented as trait only, not widget model
'**/math/Euler.js', // Implemented as trait only, not widget model
'**/renderers/WebGLRenderer.js', // For now, the internals of the webgl
'**/renderers/WebGL2Renderer.js', // render is not exposed.
//'**/renderers/webgl/**',
'**/renderers/webgl/WebGLAttributes.js',
'**/renderers/webgl/WebGLAnimation.js',
'**/renderers/webgl/WebGLBackground.js',
'**/renderers/webgl/WebGLClipping.js',
'**/renderers/webgl/WebGLFlareRenderer.js',
'**/renderers/webgl/WebGLInfo.js',
'**/renderers/webgl/WebGLMorphtargets.js',
'**/renderers/webgl/WebGLRenderLists.js',
'**/renderers/webgl/WebGLRenderStates.js',
'**/renderers/webgl/WebGLSpriteRenderer.js',
'**/renderers/webgl/WebGLTextures.js',
'**/renderers/webgl/WebGLUniforms.js',
'**/renderers/webgl/WebGLUtils.js',
'**/renderers/webvr/**',
'**/renderers/shaders/**',
'**/extras/curves/Curves.js', // index.js like file, nothing new here
'**/loaders/LoaderUtils.js', // Only functions, nothing to export
'**/extras/Earcut.js', // Only functions, nothing to export
'**/extras/ShapeUtils.js', // Only functions, nothing to export
'**/extras/ImageUtils.js', // Only functions, nothing to export
'**/extras/core/Interpolations.js', // Only functions, nothing to export
'**/textures/CanvasTexture.js' // Canvases are not referenceable from python
];
//
// Templates
//
function compileTemplate(templateName) {
templateName = path.basename(templateName, '.mustache');
const templatePath = path.resolve(templateDir, templateName + '.mustache');
return Handlebars.compile(fse.readFileSync(templatePath, {
encoding: 'utf-8'
}));
}
const jsWrapperTemplate = compileTemplate('js_wrapper');
const jsIndexTemplate = compileTemplate('js_index');
const pyWrapperTemplate = compileTemplate('py_wrapper');
const pyTopLevelInitTemplate = compileTemplate('py_top_level_init');
const docTemplate = compileTemplate('autodoc');
const docIndexTemplate = compileTemplate('autodoc_index');
const pathSep = /\\|\//;
Handlebars.registerHelper('indent', function (data, indent) {
const out = data.replace(/\n/g, '\n' + indent);
return new Handlebars.SafeString(out);
});
Handlebars.registerHelper('rst', function (data, indent) {
let out = data
.replace(/\*/g, '\\*')
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]');
if (indent) {
out = out.replace(/\n/g, '\n' + indent);
}
return new Handlebars.SafeString(out);
});
Handlebars.registerHelper('notSuper', function (v1, options) {
if (v1 === (
options.data.root.superClass.modelName ||
options.data.root.superClass.className
)) {
return options.inverse(this);
}
return options.fn(this);
});
//
// Helper Functions
//
function getClassConfig(className) {
// console.log('getClassConfig: ' + className);
className = className.replace(/\./g, '_');
if (!(className in classConfigs)) {
throw new Error('invalid class name: ' + className);
}
const curClass = classConfigs[className];
const result = Object.assign({}, curClass);
result.propsDefinedByThree = [];
result.propsDefinedByThree = result.propsDefinedByThree.concat(curClass.propsDefinedByThree || []);
// combine cur props with superclass properties for allProperties
result.allProperties = {};
result.properties = result.properties || {};
if (curClass.superClass && curClass.superClass !== classConfigs._defaults.superClass) {
const superClassConfig = getClassConfig(curClass.superClass);
Object.assign(result.allProperties, superClassConfig.allProperties);
result.propsDefinedByThree = result.propsDefinedByThree.concat(superClassConfig.propsDefinedByThree || []);
}
Object.assign(result.allProperties, curClass.properties);
// we want to inherit all propsDefinedByThree
// add defaults
_.defaults(
result,
classConfigs._defaults
);
if ('type' in result.allProperties && result.allProperties.type instanceof Types.String) {
if ('type' in result.properties) {
result.properties.type.defaultValue = className;
} else {
result.properties.type = new Types.String(className);
result.allProperties.type = result.properties.type;
}
}
return result;
}
/**
* Finds any extra class definitions in addition to the default one.
*
* E.g. SphereGeometry.js defines both SphereGeometry and SphereBufferGeometry.
* Given that both are defined in the class config with the same relativePath,
* this function returns an array of extra definitions for that file, i.e.
* ['SphereBufferGeometry'] for an input of 'SphereGeometry' in the example.
*/
function getExtraDefines(className) {
className = className.replace(/\./g, '_');
if (!(className in classConfigs)) {
throw new Error('invalid class name: ' + className);
}
const relativePath = classConfigs[className].relativePath;
const shared = [];
Object.keys(classConfigs).forEach(function(key) {
if (key[0] === '_') {
return; // continue
}
const config = classConfigs[key];
if (config.relativePath === relativePath && key !== className) {
shared.push(key);
}
});
if (shared.length > 0) {
console.log('extra defines found: ' + shared);
}
return shared;
}
function relativePathToPythonImportPath(relativePath) {
let tokens = relativePath.split(pathSep);
const firstToken = tokens[0];
let sawFolderToken = false;
if (tokens.length <= 0) { return '.'; }
let result = '';
if (firstToken === '.') {
tokens = tokens.slice(1);
result = '';
} else if (firstToken === '..') {
tokens = tokens.slice(1);
result = '.';
}
tokens.forEach(function(token) {
if (token === '.') {
return;
} else if (token === '..') {
result += '.';
} else {
result += '.' + token;
sawFolderToken = true;
}
});
if (!sawFolderToken) { result += '.'; }
return result;
}
// Execute a function for each match to a glob query
//
// Parameters:
// globPattern: String glob pattern for node-glob
// mapFn: Function function(pathRelativeToCwd), should return a promise or list of promises
// globOptions: Object of options passed directly to node-glob
//
// Returns: Promise that resolves with array of results from mapFn applies to all glob matches
function mapPromiseFnOverGlob(globPattern, mapFn, globOptions) {
return new Promise(function(resolve, reject) {
let promises = [];
let result;
// trailing slash will match only directories
new Glob(globPattern, globOptions)
.on('match', function(match) {
result = mapFn(match);
if (result instanceof Array) {
promises = promises.concat(result);
} else {
promises.push(result);
}
})
.on('end', function() {
// wait for all file ops to finish
Promise.all(promises).then(resolve).catch(reject);
})
.on('error', function(err) {
reject(err);
})
.on('abort', function() {
reject(new Error('Aborted'));
});
});
}
function mapPromiseFnOverFileList(fileList, mapFn) {
let promises = [];
fileList.forEach(function(filePath) {
const result = mapFn(filePath);
if (result instanceof Array) {
promises = promises.concat(result);
} else {
promises.push(result);
}
}, this);
return Promise.all(promises);
}
function mapPromiseFnOverThreeModules(mapFn) {
return mapPromiseFnOverGlob('**/*.js', mapFn, {
cwd: threeSrcDir,
nodir: true ,
ignore: IGNORE_FILES,
});
}
//
// Javascript wrapper writer
//
class JavascriptWrapper {
constructor(modulePath, className) {
this.jsDestPath = path.resolve(jsSrcDir, modulePath);
this.destDir = path.dirname(this.jsDestPath);
this.relativePathToBase = path.relative(this.destDir, jsSrcDir).replace(pathSep, '/');
this.jsAutoDestPath = path.resolve(
this.destDir,
path.basename(this.jsDestPath, '.js') + '.' + AUTOGEN_EXT + '.js');
if (className) {
this.className = className;
this.jsAutoDestPath = path.resolve(
path.dirname(this.jsAutoDestPath),
className + '.' + AUTOGEN_EXT + '.js');
} else {
this.className = path.basename(modulePath, '.js').replace(/\./g, '_');
const extraDefines = getExtraDefines(this.className);
extraDefines.forEach(function(extraClassName) {
createJavascriptWrapper(modulePath, extraClassName);
});
}
this.config = getClassConfig(this.className);
this.modelName = this.className + 'Model';
// check if manual file exists
const customSrcPath = path.join(path.dirname(this.jsDestPath), path.basename(this.jsDestPath, '.js') + '.js');
this.hasOverride = fse.existsSync(customSrcPath);
this.processSuperClass();
this.processDependencies();
this.processProperties();
this.processConstructorArgs();
this.processOverrideClass();
// Template and context
this.template = jsWrapperTemplate;
this.context = {
now: new Date(),
generatorScriptName: path.basename(__filename),
relativePathToBase: this.relativePathToBase,
className: this.className,
viewName: this.viewName,
modelName: this.modelName,
superClass: this.superClass,
constructor: {
args: this.constructorArgs,
},
properties: this.properties,
dependencies: this.dependencies,
props_created_by_three: this.config.propsDefinedByThree,
serialized_props: this.serializedProps,
enum_properties: this.enum_properties,
override_class: this.overrideClass, // { relativePath }
};
// Render template
this.output = this.template(this.context);
}
getRequireInfoFromClassDescriptor(classDescriptor) {
const result = {};
if (typeof classDescriptor === 'string') {
if (classDescriptor in classConfigs) {
const config = getClassConfig(classDescriptor);
result.className = classDescriptor;
result.relativePath = config.relativePath;
} else {
result.className = path.basename(classDescriptor, '.js');
result.relativePath = classDescriptor;
}
} else {
throw new Error('invalid classDescriptor: ' + classDescriptor);
}
result.modelName = result.className + 'Model';
result.absolutePath = path.resolve(jsSrcDir, result.relativePath);
let absPath = result.absolutePath;
if (fse.existsSync(absPath + '.js')) {
absPath += '.js';
} else {
absPath += JS_AUTOGEN_EXT;
}
result.requirePath = path.relative(this.destDir, absPath).replace(/\\/g, '/');
if (result.requirePath.charAt(0) !== '.') {
result.requirePath = './' + result.requirePath;
}
return result;
}
processSuperClass() {
this.superClass = this.getRequireInfoFromClassDescriptor(this.config.superClass);
}
processDependencies() {
const dependencies = {};
// process explicitly listed dependencies
_.reduce(this.config.dependencies, function(result, depName) {
result[depName] = this.getRequireInfoFromClassDescriptor(depName);
return result;
}, dependencies, this);
// infer dependencies from any properties that reference other Three types
_.reduce(this.config.properties, function(result, prop) {
if (prop instanceof Types.ThreeType || prop instanceof Types.InitializedThreeType ||
prop instanceof Types.ThreeTypeArray || prop instanceof Types.ThreeTypeDict) {
if (prop.typeName !== 'this') {
if (typeof prop.typeName === 'string') {
let typeName = prop.typeName || './_base/Three';
result[typeName] = this.getRequireInfoFromClassDescriptor(typeName);
} else if (prop.typeName instanceof Array) {
prop.typeName.forEach(function(typeName) {
result[typeName] = this.getRequireInfoFromClassDescriptor(typeName);
}, this);
}
}
}
return result;
}, dependencies, this);
this.dependencies = dependencies;
}
processProperties() {
this.properties = _.mapObject(this.config.properties, function(prop) {
return {
defaultJson: prop.getJSPropertyValue(),
property_array_name: prop.getPropArrayName(),
property_converter: prop.getPropertyConverterFn(),
property_assigner: prop.getPropertyAssignmentFn(),
};
}, this);
this.serializedProps = _.mapObject(
_.pick(this.config.properties,
function(prop) {
return !!prop.serializer;
}),
function(prop) {
return prop.serializer;
}, {});
this.enum_properties = _.reduce(this.config.properties, function(result, prop, propName) {
if (prop.enumTypeName) {
result[propName] = prop.enumTypeName;
}
return result;
}, {});
}
processConstructorArgs() {
function getConstructorParametersObject() {
let result = [ '{' ];
result = result.concat(_.keys(this.config.properties).map(function(propName) {
return ' ' + propName + ': ' + this.getModelToThreeGetter(propName) + ',';
}, this));
result.push(' }');
return result;
}
const constructorArgs = this.config.constructorArgs.map(function(propName) {
if (propName === 'parameters') {
return getConstructorParametersObject.bind(this)().join('\n');
} else {
return this.getModelToThreeGetter(propName);
}
}, this);
this.constructorArgs = constructorArgs;
}
processOverrideClass() {
if (!this.hasOverride) {
return;
}
console.log('JS override exists for ' + this.className);
const overrideModule = 'Override';
const overrideModel = overrideModule + '.' + this.modelClass;
this.overrideClass = {
relativePath: './' + this.className + '.js',
modelName: overrideModel,
};
}
getModelToThreeGetter(propName) {
const prop = this.config.allProperties[propName];
if (!prop) {
throw new Error('invalid propName: ' + propName);
}
const converter = prop.getPropertyConverterFn();
if (converter) {
return 'this.' + converter + 'ModelToThree(this.get(\'' + propName + '\'), \'' + propName +'\')';
} else {
return 'this.get(\'' + propName + '\')';
}
}
getOutputFilename() {
return this.jsAutoDestPath;
}
}
function createJavascriptWrapper(modulePath, className) {
let wrapper;
try {
wrapper = new JavascriptWrapper(modulePath, className);
} catch (e) {
console.log('error creating wrapper: ');
console.log(e);
console.log('skipping: ' + modulePath + (className ? ':' + className : ''));
return Promise.resolve(false);
}
return fse.outputFile(wrapper.getOutputFilename(), wrapper.output);
// NOTE: Old implementation
// const wrapper = new JavascriptWrapper(modulePath);
// return wrapper.writeOutFile();
}
function writeJavascriptIndexFiles() {
console.log('Writing javascript indices...');
const excludes = [
/\.swp$/,
/\.DS_Store$/,
/index\.js$/,
'./embed.js',
'./extension.js',
];
// Regexp's
const RE_AUTOGEN_EXT = /\.autogen\.js$/;
async function writeIndexForDir(dirPath, isTopLevel) {
const dirAbsPath = path.resolve(jsSrcDir, dirPath);
// Generate list of files in dir to include in index.js as require lines
let dirFiles = await fse.readdir(dirAbsPath);
// get proper relative path for file
dirFiles = dirFiles.map(filename => {
return './' + path.join(dirPath, filename);
});
// filter excluded files
dirFiles = dirFiles.filter(filePath => {
// ignore autogen files in _base dir
if (/_base/.test(dirPath) && RE_AUTOGEN_EXT.test(filePath)) {
return false;
}
// compare filePath to each exclude pattern
const shouldExclude = _.any(excludes, function(testPattern) {
if (testPattern instanceof RegExp) {
return testPattern.test(filePath);
} else if (typeof testPattern === 'string') {
return testPattern === filePath;
}
});
if (shouldExclude) {
return false;
}
// if override class exists, load it in favor of the autogen file
// e.g. for WebGLRenderer.js, Object3D.js, DataTexture.js
// override classes should extend the autogen versions
if (RE_AUTOGEN_EXT.test(filePath)) {
const dirname = path.dirname(filePath);
const basename = path.basename(filePath, JS_AUTOGEN_EXT);
const overrideName = basename + '.js';
const overridePath = './' + path.join(dirname, overrideName);
// override file present, so don't include autogen file in index
if (dirFiles.indexOf(overridePath) > -1) {
console.log('override exists for: ' + filePath);
return false;
}
}
return true;
});
// convert file paths relative to js src dir to paths relative to dirPath
dirFiles = dirFiles.map(function(filePath) {
return './' + path.basename(filePath);
});
// render template
const context = {
now: new Date(),
generatorScriptName: path.basename(__filename),
top_level: isTopLevel,
submodules: dirFiles,
};
const output = jsIndexTemplate(context);
const outputPath = path.resolve(jsSrcDir, dirPath, 'index.js');
return fse.outputFile(outputPath, output);
}
// map over all directories in js src dir
return mapPromiseFnOverGlob(
'**/', // trailing slash globs for dirs only
function(dirPath) {
return writeIndexForDir(dirPath, false);
},
{ cwd: jsSrcDir, }
).then(function() {
// write top-level index (not included in above glob)
return writeIndexForDir('.', true);
});
}
//
// Python wrapper writer
//
class PythonWrapper {
constructor(modulePath, className) {
this.modulePath = modulePath;
this.dirRelativePath = path.dirname(modulePath);
this.destDirAbsolutePath = path.resolve(pySrcDir, this.dirRelativePath);
this.destDirRelativeToBase = path.relative(this.destDirAbsolutePath, pySrcDir);
this.basename = path.basename(modulePath, '.js');
if (className) {
this.className = className;
} else {
this.className = this.basename.replace(/\./g, '_');
const extraDefines = getExtraDefines(this.className);
extraDefines.forEach(function(extraClassName) {
createPythonWrapper(modulePath, extraClassName);
});
}
this.pyDestPath = path.resolve(this.destDirAbsolutePath, this.className + '.py');
this.pyAutoDestPath = path.resolve(this.destDirAbsolutePath, this.className + '_' + AUTOGEN_EXT + '.py');
this.pyBaseRelativePath = path.relative(this.destDirAbsolutePath, pySrcDir);
this.pyBaseRelativePath = relativePathToPythonImportPath(this.pyBaseRelativePath);
// check if manual file exists
this.hasOverride = fse.existsSync(this.pyDestPath);
this.isCustom = CUSTOM_CLASSES.indexOf(modulePath) !== -1;
this.hasParameters = false;
this.config = getClassConfig(this.className);
this.processSuperClass();
this.processDependencies();
this.processProperties();
this.processDocsUrl();
this.processConstructorArgs();
// Template and context
this.context = {
now: new Date(),
generatorScriptName: path.basename(__filename),
threejs_docs_url: this.docsUrl,
py_base_relative_path: this.pyBaseRelativePath,
constructor: {
args: this.constructorArgs,
hasParameters: this.hasParameters,
},
className: this.className,
modelName: this.className + 'Model',
superClass: this.superClass,
properties: this.properties,
dependencies: this.dependencies,
hasOverride: this.hasOverride,
isCustom: this.isCustom,
todo: this.config.todo || false,
};
// Render template
this.output = pyWrapperTemplate(this.context);
}
getRequireInfoFromClassDescriptor(classDescriptor) {
const result = {};
if (typeof classDescriptor === 'string') {
if (classDescriptor in classConfigs) {
const config = getClassConfig(classDescriptor);
result.className = classDescriptor;
result.relativePath = config.relativePath;
} else {
result.className = path.basename(classDescriptor, '.js');
result.relativePath = classDescriptor;
}
} else {
throw new Error('invalid classDescriptor: ' + classDescriptor);
}
// get path of dependency relative to module dir
result.absolutePath = path.resolve(pySrcDir, result.relativePath);
if (!fse.existsSync(result.absolutePath + '.py')) {
result.absolutePath += '_' + AUTOGEN_EXT;
}
result.requirePath = path.relative(this.destDirAbsolutePath, result.absolutePath);
result.pyRelativePath = relativePathToPythonImportPath(result.requirePath);
return result;
}
processSuperClass() {
this.superClass = this.getRequireInfoFromClassDescriptor(this.config.superClass);
if (this.superClass.className === 'Three') {
this.superClass.className = 'ThreeWidget';
}
}
processDependencies() {
const dependencies = {};
// process explicitly listed dependencies
_.reduce(this.config.dependencies, function(result, depName) {
result[depName] = this.getRequireInfoFromClassDescriptor(depName);
return result;
}, dependencies, this);
// infer dependencies from any properties that reference other Three types
_.reduce(this.config.properties, function(result, prop) {
if (prop instanceof Types.ThreeType || prop instanceof Types.InitializedThreeType ||
prop instanceof Types.ThreeTypeArray || prop instanceof Types.ThreeTypeDict) {
if (prop.typeName !== 'this') {
if (typeof prop.typeName === 'string') {
let typeName = prop.typeName || './_base/Three';
result[typeName] = this.getRequireInfoFromClassDescriptor(typeName);
if (result[typeName].className === 'Three') {
result[typeName].className = 'ThreeWidget';
}
} else if (prop.typeName instanceof Array) {
prop.typeName.forEach(function(typeName) {
result[typeName] = this.getRequireInfoFromClassDescriptor(typeName);
}, this);
}
}
}
return result;
}, dependencies, this);
this.dependencies = dependencies;
}
processProperties() {
this.properties = _.mapObject(this.config.properties, function(prop) {
return {
help: prop.options.help,
trait_declaration: prop.getTraitlet(),
defaultJson: prop.getPythonDefaultValue(),
};
}, this);
}
processConstructorArgs() {
this.constructorArgs = this.config.constructorArgs.map(function(propName) {
// Currently, we don't generate an __init__ method for classes that use the parameters
// constructor arg
if (propName === 'parameters') {
this.hasParameters = true;
return {
name: propName,
prop: {
defaultJson: '{}',
}
};
}
return {
name: propName,
prop: {
defaultJson: this.config.allProperties[propName].getPythonDefaultValue(),
}
};
}, this);
}
processDocsUrl() {
if (this.isCustom) {
this.docsUrl = null;
}
const refTokens = this.modulePath.split(pathSep);
// strip extension off filename
refTokens[refTokens.length - 1] = path.basename(refTokens[refTokens.length - 1], '.js');
let refUrl = 'https://threejs.org/docs/#api/' + refTokens.join('/');
// combine middle elements of url with dot
refUrl = refUrl.replace('Renderers/WebGL/Plugins/', 'Renderers.WebGL.Plugins/');
refUrl = refUrl.replace('Renderers/WebGL/', 'Renderers.WebGL/');
refUrl = refUrl.replace('Renderers/Shaders/', 'Renderers.Shaders/');
refUrl = refUrl.replace('Extras/Animation/', 'Extras.Animation/');
refUrl = refUrl.replace('Extras/Core/', 'Extras.Core/');
refUrl = refUrl.replace('Extras/Curves/', 'Extras.Curves/');
refUrl = refUrl.replace('Extras/Geometries/', 'Extras.Geometries/');
refUrl = refUrl.replace('Extras/Helpers/', 'Extras.Helpers/');
refUrl = refUrl.replace('Extras/Objects/', 'Extras.Objects/');
this.docsUrl = refUrl;
}
getOutputFilename() {
return this.pyAutoDestPath;
}
getDocFilename() {
return path.resolve(docSrcDir, this.dirRelativePath, `${this.className}_autogen.rst`);
}
getDocOutput() {
return docTemplate(this.context);
}
}
function createPythonWrapper(modulePath, className) {
let wrapper;
try {
wrapper = new PythonWrapper(modulePath, className);
} catch (e) {
console.log(e);
console.log('skipping: ' + modulePath + (className ? ':' + className : ''));
return Promise.resolve(false);
}
let fname = wrapper.getOutputFilename();
let pyPromise = fse.outputFile(fname, wrapper.output);
// Also output documentation for the Python API
let docfname = wrapper.getDocFilename();
//console.log(docfname);
//let docPromise = Promise.resolve();
let docPromise = fse.outputFile(docfname, wrapper.getDocOutput());
return Promise.all([pyPromise, docPromise]);
}
function createPythonModuleInitFile(modulePath) {
let dirname = path.dirname(path.resolve(pySrcDir, modulePath));
let pyInitFilePath;
const promises = [];
while (dirname !== pySrcDir) {
pyInitFilePath = path.join(dirname, '__init__.py');
promises.push(fse.ensureFile(pyInitFilePath));
if (dirname === path.dirname(dirname)) {
throw new Error(`${dirname}, ${path.dirname(dirname)}`);
}
dirname = path.dirname(dirname);
}
return Promise.all(promises);
}
function writeDocModuleFiles() {
console.log('Writing document indices...');
const RE_AUTOGEN = /index.rst/g;
function writeIndexForDir(dirPath, isTopLevel) {
const dirAbsPath = path.resolve(docSrcDir, dirPath);
let moduleName;
if (dirPath === '.') {
moduleName = 'pythreejs';
} else {
moduleName = path.basename(dirPath);
}
// Generate list of files in dir to include in module as toc entries
return fse.readdir(dirAbsPath).then(function(dirFiles) {
// sort directories first:
dirFiles = _.sortBy(dirFiles, filePath => {
return fse.statSync(path.join(dirAbsPath, filePath)).isDirectory() ? 0 : 1;
});
dirFiles = dirFiles.filter(filePath => {
return !filePath.match(RE_AUTOGEN);
});
// convert file paths to paths relative to dirPath
dirFiles = dirFiles.map(filePath => {
if (fse.statSync(path.join(dirAbsPath, filePath)).isDirectory()) {
return `./${filePath}/index`;
}
// Need to use forward slash for RST:
return `./${path.basename(filePath)}`;
});
// render template
const context = {
now: new Date(),