@needle-tools/needle-component-compiler 2.4.0-pre → 3.0.0-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Changelog.md +5 -1
- package/dist/BaseWriter.d.ts +23 -0
- package/dist/BaseWriter.js +90 -0
- package/dist/Compiler.d.ts +20 -0
- package/{src/base-compiler.js → dist/Compiler.js} +99 -194
- package/dist/base-compiler.d.ts +54 -0
- package/dist/base-compiler.js +168 -0
- package/dist/commands.d.ts +3 -0
- package/dist/component-compiler.d.ts +3 -0
- package/{src → dist}/component-compiler.js +5 -0
- package/dist/impl/blender-compiler.d.ts +14 -0
- package/dist/impl/blender-compiler.js +179 -0
- package/dist/impl/csharp-compiler.d.ts +16 -0
- package/dist/impl/csharp-compiler.js +306 -0
- package/dist/impl/react-three-fiber-compiler.d.ts +20 -0
- package/{src → dist/impl}/react-three-fiber-compiler.js +3 -1
- package/dist/register-types.d.ts +8 -0
- package/dist/watcher.d.ts +6 -0
- package/{src → dist}/watcher.js +3 -2
- package/package.json +13 -7
- package/src/blender-compiler.js +0 -180
- package/src/csharp-compiler.js +0 -58
- package/src/test.js +0 -194
- package/src/types.js +0 -51
- package/test/component.basic.test.js +0 -26
- package/test/component.methods.test.js +0 -82
- package/test/component.nonserialized.test.js +0 -20
- package/test/component.primitives.test.js +0 -47
- package/test/helpers.js +0 -54
- /package/{src → dist}/commands.js +0 -0
- /package/{src → dist}/register-types.js +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runFromFile = exports.handleDeletedFile = exports.CodeTextWriter = exports.MemorySink = exports.FileSink = exports.Visibility = void 0;
|
|
4
|
+
var fs_1 = require("fs");
|
|
5
|
+
var fs = require("fs");
|
|
6
|
+
var commands_1 = require("./commands");
|
|
7
|
+
var Compiler_1 = require("./Compiler");
|
|
8
|
+
var Visibility;
|
|
9
|
+
(function (Visibility) {
|
|
10
|
+
Visibility[Visibility["Public"] = 0] = "Public";
|
|
11
|
+
Visibility[Visibility["Protected"] = 1] = "Protected";
|
|
12
|
+
Visibility[Visibility["Private"] = 2] = "Private";
|
|
13
|
+
})(Visibility = exports.Visibility || (exports.Visibility = {}));
|
|
14
|
+
var FileSink = /** @class */ (function () {
|
|
15
|
+
function FileSink(directory) {
|
|
16
|
+
this.directory = directory;
|
|
17
|
+
}
|
|
18
|
+
FileSink.prototype.flush = function (id, str) {
|
|
19
|
+
if (!fs.existsSync(this.directory)) {
|
|
20
|
+
fs.mkdirSync(this.directory, { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
console.log("Writing " + id + " to " + this.directory);
|
|
23
|
+
var fullPath = this.directory + "/" + id;
|
|
24
|
+
fs.writeFileSync(fullPath, str);
|
|
25
|
+
return fs.realpathSync(fullPath);
|
|
26
|
+
};
|
|
27
|
+
return FileSink;
|
|
28
|
+
}());
|
|
29
|
+
exports.FileSink = FileSink;
|
|
30
|
+
var MemorySink = /** @class */ (function () {
|
|
31
|
+
function MemorySink() {
|
|
32
|
+
this.results = [];
|
|
33
|
+
}
|
|
34
|
+
MemorySink.prototype.clear = function () {
|
|
35
|
+
this.results.length = 0;
|
|
36
|
+
};
|
|
37
|
+
MemorySink.prototype.flush = function (id, str) {
|
|
38
|
+
this.results.push(str);
|
|
39
|
+
return "";
|
|
40
|
+
};
|
|
41
|
+
return MemorySink;
|
|
42
|
+
}());
|
|
43
|
+
exports.MemorySink = MemorySink;
|
|
44
|
+
var CodeTextWriter = /** @class */ (function () {
|
|
45
|
+
function CodeTextWriter() {
|
|
46
|
+
this.indent = 0;
|
|
47
|
+
this.lines = [];
|
|
48
|
+
}
|
|
49
|
+
CodeTextWriter.prototype.beginBlock = function (line) {
|
|
50
|
+
this.writeLine(line);
|
|
51
|
+
this.indent++;
|
|
52
|
+
};
|
|
53
|
+
CodeTextWriter.prototype.endBlock = function (line) {
|
|
54
|
+
// remove the comma from the last line
|
|
55
|
+
if (this.lines.length > 0) {
|
|
56
|
+
var prevLine = this.lines[this.lines.length - 1];
|
|
57
|
+
if (prevLine.endsWith(",")) {
|
|
58
|
+
this.lines[this.lines.length - 1] = prevLine.substring(0, prevLine.length - 1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.indent--;
|
|
62
|
+
this.writeLine(line);
|
|
63
|
+
};
|
|
64
|
+
CodeTextWriter.prototype.writeLine = function (line) {
|
|
65
|
+
var indent = "";
|
|
66
|
+
for (var i = 0; i < this.indent; i++) {
|
|
67
|
+
indent += " ";
|
|
68
|
+
}
|
|
69
|
+
var lines = line.split(/\r?\n/);
|
|
70
|
+
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
|
|
71
|
+
var line_1 = lines_1[_i];
|
|
72
|
+
// remove whitespace so we can ensure we correctly end with a comma
|
|
73
|
+
this.lines.push(indent + (line_1 === null || line_1 === void 0 ? void 0 : line_1.trim()));
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
CodeTextWriter.prototype.write = function (text) {
|
|
77
|
+
var lines = text.split(/\r?\n/);
|
|
78
|
+
if (lines.length > 1) {
|
|
79
|
+
for (var i = 0; i < lines.length; i++) {
|
|
80
|
+
this.writeLine(lines[i]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
if (this.lines.length == 0) {
|
|
85
|
+
this.writeLine(text);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
var lastLine = this.lines[this.lines.length - 1];
|
|
89
|
+
this.lines[this.lines.length - 1] = lastLine + text;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
// beginArray() {
|
|
94
|
+
// this.writeLine("[");
|
|
95
|
+
// this.indent++;
|
|
96
|
+
// }
|
|
97
|
+
// insertArrayItem(item: string) {
|
|
98
|
+
// this.writeLine(item + ",");
|
|
99
|
+
// }
|
|
100
|
+
// endArray() {
|
|
101
|
+
// }
|
|
102
|
+
CodeTextWriter.prototype.flush = function () {
|
|
103
|
+
var str = this.toString();
|
|
104
|
+
this.lines.length = 0;
|
|
105
|
+
return str;
|
|
106
|
+
// if (this.lines.length === 0) return;
|
|
107
|
+
// if (!fs.existsSync(directory)) {
|
|
108
|
+
// fs.mkdirSync(directory, { recursive: true });
|
|
109
|
+
// }
|
|
110
|
+
// console.log("Writing " + filename + " to " + directory);
|
|
111
|
+
// const content = this.toString();
|
|
112
|
+
// this.lines.length = 0;
|
|
113
|
+
// const fullPath = directory + "/" + filename;
|
|
114
|
+
// fs.writeFileSync(fullPath, content);
|
|
115
|
+
// return fs.realpathSync(fullPath);
|
|
116
|
+
};
|
|
117
|
+
CodeTextWriter.prototype.clear = function () {
|
|
118
|
+
this.lines.length = 0;
|
|
119
|
+
};
|
|
120
|
+
CodeTextWriter.prototype.toString = function () {
|
|
121
|
+
return this.lines.join("\r");
|
|
122
|
+
};
|
|
123
|
+
return CodeTextWriter;
|
|
124
|
+
}());
|
|
125
|
+
exports.CodeTextWriter = CodeTextWriter;
|
|
126
|
+
function handleDeletedFile(filePath, types) {
|
|
127
|
+
if (!fs.existsSync(filePath)) {
|
|
128
|
+
var infos = types[filePath];
|
|
129
|
+
if (!infos)
|
|
130
|
+
return;
|
|
131
|
+
for (var i = 0; i < infos.length; i++) {
|
|
132
|
+
var info = infos[i];
|
|
133
|
+
// check if the source file is in any other file
|
|
134
|
+
var found = false;
|
|
135
|
+
for (var otherSourceFile in types) {
|
|
136
|
+
if (otherSourceFile == filePath)
|
|
137
|
+
continue;
|
|
138
|
+
for (var _i = 0, _a = types[otherSourceFile]; _i < _a.length; _i++) {
|
|
139
|
+
var otherInfo = _a[_i];
|
|
140
|
+
if (otherInfo.filePath == info.filePath) {
|
|
141
|
+
found = true;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (found)
|
|
146
|
+
break;
|
|
147
|
+
if (fs.existsSync(info.filePath)) {
|
|
148
|
+
fs.rmSync(info.filePath);
|
|
149
|
+
(0, commands_1.sendFileDeletedCommand)(info.filePath);
|
|
150
|
+
infos.splice(i, 1);
|
|
151
|
+
i--;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
exports.handleDeletedFile = handleDeletedFile;
|
|
158
|
+
function runFromFile(writer, path) {
|
|
159
|
+
if (!fs.existsSync(path)) {
|
|
160
|
+
console.error("File not found", path);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
var code = (0, fs_1.readFileSync)(path).toString();
|
|
164
|
+
var compiler = new Compiler_1.Compiler();
|
|
165
|
+
compiler.compile(writer, code, path);
|
|
166
|
+
return compiler;
|
|
167
|
+
}
|
|
168
|
+
exports.runFromFile = runFromFile;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Visibility } from "../base-compiler";
|
|
2
|
+
import { BaseWriter } from "../BaseWriter";
|
|
3
|
+
export declare class BlenderWriter extends BaseWriter {
|
|
4
|
+
resolveCSharpTypeName(typeName: string, comments?: string[]): string | void;
|
|
5
|
+
startNewType(filePath: string | null, typeName: string, baseTypes: string[], comments?: string[]): void | boolean;
|
|
6
|
+
endNewType(filePath: string | null, typeName: string): void;
|
|
7
|
+
writeMember(visibility: Visibility, name: string, isArray: boolean, type: string, initialValue?: string, comments?: string[]): void;
|
|
8
|
+
writeMethod(visibility: Visibility, name: string, returnType: string | undefined, args: {
|
|
9
|
+
name: string;
|
|
10
|
+
type: string;
|
|
11
|
+
defaultValue?: string;
|
|
12
|
+
}[], comments?: string[]): void;
|
|
13
|
+
writeNewTypeExpression(typeName: string, args?: string[]): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BlenderWriter = void 0;
|
|
19
|
+
var base_compiler_1 = require("../base-compiler");
|
|
20
|
+
var BaseWriter_1 = require("../BaseWriter");
|
|
21
|
+
var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn", "vec2", "vec3", "comp"];
|
|
22
|
+
var knownBaseTypes = ["Behaviour", "IComponent"];
|
|
23
|
+
var BlenderWriter = /** @class */ (function (_super) {
|
|
24
|
+
__extends(BlenderWriter, _super);
|
|
25
|
+
function BlenderWriter() {
|
|
26
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
27
|
+
}
|
|
28
|
+
BlenderWriter.prototype.resolveCSharpTypeName = function (typeName, comments) {
|
|
29
|
+
typeName = typeName.toLocaleLowerCase();
|
|
30
|
+
if (typeName.startsWith("three."))
|
|
31
|
+
typeName = typeName.substring("three.".length);
|
|
32
|
+
switch (typeName) {
|
|
33
|
+
case "number":
|
|
34
|
+
return "float";
|
|
35
|
+
case "int":
|
|
36
|
+
return "int";
|
|
37
|
+
case "boolean":
|
|
38
|
+
return "bool";
|
|
39
|
+
case "string":
|
|
40
|
+
return "string";
|
|
41
|
+
case "void":
|
|
42
|
+
return "void";
|
|
43
|
+
case "vector2":
|
|
44
|
+
return "vec2";
|
|
45
|
+
case "vector3":
|
|
46
|
+
return "vec3";
|
|
47
|
+
case "vector4":
|
|
48
|
+
return "vec4";
|
|
49
|
+
case "quaternion":
|
|
50
|
+
return "quat";
|
|
51
|
+
case "euler":
|
|
52
|
+
return "vec3";
|
|
53
|
+
case "color":
|
|
54
|
+
return "col";
|
|
55
|
+
case "eventlist":
|
|
56
|
+
return "evt";
|
|
57
|
+
case "behaviour":
|
|
58
|
+
case "animation": // Animation component ref
|
|
59
|
+
case "animator": // Animator component ref
|
|
60
|
+
case "camera":
|
|
61
|
+
case "light":
|
|
62
|
+
case "rigidbody":
|
|
63
|
+
case "renderer": // Renderer component ref
|
|
64
|
+
case "canvas": // Canvas component ref
|
|
65
|
+
case "charactercontroller": // CharacterController component ref
|
|
66
|
+
case "videoplayer": // VideoPlayer component ref
|
|
67
|
+
case "lookatconstraint": // LookAtConstraint component ref
|
|
68
|
+
return "comp";
|
|
69
|
+
case "texture":
|
|
70
|
+
return "tex";
|
|
71
|
+
case "image":
|
|
72
|
+
return "img";
|
|
73
|
+
case "object3d":
|
|
74
|
+
case "gameobject": // GameObject = scene object reference
|
|
75
|
+
return "obj";
|
|
76
|
+
case "vec2":
|
|
77
|
+
return "vec2";
|
|
78
|
+
case "material":
|
|
79
|
+
return "mat";
|
|
80
|
+
case "animationclip":
|
|
81
|
+
return "anim";
|
|
82
|
+
case "animatorcontroller":
|
|
83
|
+
return "animatorcontroller";
|
|
84
|
+
case "audioclip":
|
|
85
|
+
return "audioclip";
|
|
86
|
+
case "videoclip":
|
|
87
|
+
case "video":
|
|
88
|
+
return "videoclip";
|
|
89
|
+
case "assetreference":
|
|
90
|
+
case "asset":
|
|
91
|
+
return "assetreference";
|
|
92
|
+
case "mesh":
|
|
93
|
+
return "mesh";
|
|
94
|
+
case "timelineasset":
|
|
95
|
+
return "timeline";
|
|
96
|
+
}
|
|
97
|
+
return typeName;
|
|
98
|
+
};
|
|
99
|
+
BlenderWriter.prototype.startNewType = function (filePath, typeName, baseTypes, comments) {
|
|
100
|
+
var isSupportedType = false;
|
|
101
|
+
for (var i = 0; i < knownBaseTypes.length; i++) {
|
|
102
|
+
if (baseTypes.includes(knownBaseTypes[i])) {
|
|
103
|
+
isSupportedType = true;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// console.log(baseTypes, knownBaseTypes, isSupportedType)
|
|
108
|
+
if (!isSupportedType)
|
|
109
|
+
return false;
|
|
110
|
+
this.writer.beginBlock("{");
|
|
111
|
+
if (comments)
|
|
112
|
+
console.log(comments);
|
|
113
|
+
this.writer.beginBlock("\"" + typeName + "\": {");
|
|
114
|
+
};
|
|
115
|
+
BlenderWriter.prototype.endNewType = function (filePath, typeName) {
|
|
116
|
+
this.writer.endBlock("},");
|
|
117
|
+
this.writer.endBlock("}");
|
|
118
|
+
this.writeScheme(filePath, typeName);
|
|
119
|
+
};
|
|
120
|
+
BlenderWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
|
|
121
|
+
if (visibility !== base_compiler_1.Visibility.Public)
|
|
122
|
+
return;
|
|
123
|
+
// Check if type is a registered TypeScript enum
|
|
124
|
+
var enumMembers = this._enumRegistry.get(type);
|
|
125
|
+
if (enumMembers && enumMembers.length > 0) {
|
|
126
|
+
this.writer.beginBlock("\"" + name + "\": {");
|
|
127
|
+
this.writer.writeLine("\"type\": \"enum\",");
|
|
128
|
+
// Resolve default value: try to match explicit initializer (e.g. "lighttype.spot")
|
|
129
|
+
var enumValue = enumMembers[0].value;
|
|
130
|
+
if (initialValue && initialValue.includes(".")) {
|
|
131
|
+
var memberKey_1 = initialValue.split(".").pop();
|
|
132
|
+
var found = enumMembers.find(function (m) { return m.name.toLowerCase() === memberKey_1; });
|
|
133
|
+
if (found !== undefined)
|
|
134
|
+
enumValue = found.value;
|
|
135
|
+
}
|
|
136
|
+
this.writer.writeLine("\"value\": " + JSON.stringify(enumValue) + ",");
|
|
137
|
+
var options = enumMembers.map(function (m) { return m.name; });
|
|
138
|
+
this.writer.writeLine("\"options\": " + JSON.stringify(options));
|
|
139
|
+
this.writer.endBlock("},");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// if (!supportedTypes.includes(type)) {
|
|
143
|
+
// console.log("-- unsupported type:", type);
|
|
144
|
+
// return;
|
|
145
|
+
// }
|
|
146
|
+
this.writer.beginBlock("\"" + name + "\": {");
|
|
147
|
+
this.writer.writeLine("\"type\": \"" + type + "\",");
|
|
148
|
+
if (initialValue && !isArray) {
|
|
149
|
+
if (initialValue.startsWith("new "))
|
|
150
|
+
initialValue = undefined;
|
|
151
|
+
switch (type) {
|
|
152
|
+
case "bool":
|
|
153
|
+
initialValue = initialValue.toLowerCase();
|
|
154
|
+
break;
|
|
155
|
+
case "string":
|
|
156
|
+
initialValue = "\"" + initialValue + "\"";
|
|
157
|
+
}
|
|
158
|
+
if (initialValue)
|
|
159
|
+
this.writer.writeLine("\"value\": " + initialValue);
|
|
160
|
+
}
|
|
161
|
+
if (isArray) {
|
|
162
|
+
this.writer.writeLine("\"value\": []");
|
|
163
|
+
}
|
|
164
|
+
this.writer.endBlock("},");
|
|
165
|
+
};
|
|
166
|
+
BlenderWriter.prototype.writeMethod = function (visibility, name, returnType, args, comments) {
|
|
167
|
+
if (visibility !== base_compiler_1.Visibility.Public)
|
|
168
|
+
return;
|
|
169
|
+
this.writer.beginBlock("\"" + name + "\": {");
|
|
170
|
+
this.writer.writeLine("\"type\": \"fn\",");
|
|
171
|
+
this.writer.endBlock("},");
|
|
172
|
+
};
|
|
173
|
+
BlenderWriter.prototype.writeNewTypeExpression = function (typeName, args) {
|
|
174
|
+
console.log("new type:", typeName);
|
|
175
|
+
};
|
|
176
|
+
return BlenderWriter;
|
|
177
|
+
}(BaseWriter_1.BaseWriter));
|
|
178
|
+
exports.BlenderWriter = BlenderWriter;
|
|
179
|
+
// runFromFile(new BlenderWriter("dist"), "src\\test.ts");
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Argument, Visibility } from "../base-compiler";
|
|
2
|
+
import { BaseWriter } from "../BaseWriter";
|
|
3
|
+
export declare class CSharpWriter extends BaseWriter {
|
|
4
|
+
resolveCSharpTypeName(typescriptTypeName: string): string | void;
|
|
5
|
+
startNewType(filePath: string, typeName: string, baseTypes: string[], comments?: string[]): boolean | void;
|
|
6
|
+
endNewType(filePath: string, typeName: string): void;
|
|
7
|
+
writeMember(visibility: Visibility, name: string, isArray: boolean, type: string, initialValue?: string, comments?: string[]): void;
|
|
8
|
+
writeMethod(visibility: Visibility, name: string, returnType: string, args: Argument[], comments: string[]): void;
|
|
9
|
+
writeNewTypeExpression(typeName: string, args?: string[]): void;
|
|
10
|
+
/** Converts camelCase to PascalCase for method names */
|
|
11
|
+
private toPascalCase;
|
|
12
|
+
/** Resolve a "new Type(args)" expression: map TS types to C# qualified types */
|
|
13
|
+
private resolveNewExpression;
|
|
14
|
+
/** Override writeScheme to use .cs extension instead of .component.json */
|
|
15
|
+
protected writeCSharpScheme(processingFilePath: string | null, component: string): void;
|
|
16
|
+
}
|