@needle-tools/needle-component-compiler 1.7.3 → 1.9.0
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 +8 -0
- package/DEV.bat +1 -0
- package/{RUNTEST.bat → RUN_MANUAL_TEST.bat} +0 -0
- package/RUN_TESTS.bat +1 -0
- package/Readme.md +2 -1
- package/package.json +21 -3
- package/src/component-compiler.js +146 -71
- package/src/component-compiler.ts +147 -67
- package/src/test.ts +27 -3
- package/test/codegen/abstract method/expected.cs +14 -0
- package/test/codegen/abstract method/input.js +27 -0
- package/test/codegen/abstract method/input.ts +4 -0
- package/test/codegen/abstract method/output.cs +14 -0
- package/test/codegen/basic/expected.cs +13 -0
- package/test/codegen/basic/input.js +26 -0
- package/test/codegen/basic/input.ts +2 -0
- package/test/codegen/basic/output.cs +13 -0
- package/test/codegen/basic-no-export/expected.cs +0 -0
- package/test/codegen/basic-no-export/input.js +27 -0
- package/test/codegen/basic-no-export/input.ts +4 -0
- package/test/codegen/basic-no-export/output.cs +0 -0
- package/test/codegen/bool/expected.cs +14 -0
- package/test/codegen/bool/input.js +26 -0
- package/test/codegen/bool/input.ts +3 -0
- package/test/codegen/bool/output.cs +14 -0
- package/test/codegen/fields with default values/expected.cs +17 -0
- package/test/codegen/fields with default values/input.js +31 -0
- package/test/codegen/fields with default values/input.ts +6 -0
- package/test/codegen/fields with default values/output.cs +17 -0
- package/test/codegen/method ignore inline type declaration/expected.cs +14 -0
- package/test/codegen/method ignore inline type declaration/input.js +27 -0
- package/test/codegen/method ignore inline type declaration/input.ts +3 -0
- package/test/codegen/method ignore inline type declaration/output.cs +14 -0
- package/test/codegen/nonserialized ignore field/expected.cs +13 -0
- package/test/codegen/nonserialized ignore field/input.js +26 -0
- package/test/codegen/nonserialized ignore field/input.ts +4 -0
- package/test/codegen/nonserialized ignore field/output.cs +13 -0
- package/test/codegen/nonserialized on property does not affect method/expected.cs +14 -0
- package/test/codegen/nonserialized on property does not affect method/input.js +34 -0
- package/test/codegen/nonserialized on property does not affect method/input.ts +8 -0
- package/test/codegen/nonserialized on property does not affect method/output.cs +13 -0
- package/test/codegen/number/expected.cs +14 -0
- package/test/codegen/number/input.js +26 -0
- package/test/codegen/number/input.ts +3 -0
- package/test/codegen/number/output.cs +14 -0
- package/test/codegen/object/expected.cs +14 -0
- package/test/codegen/object/input.js +26 -0
- package/test/codegen/object/input.ts +3 -0
- package/test/codegen/object/output.cs +14 -0
- package/test/codegen/private.methods/expected.cs +13 -0
- package/test/codegen/private.methods/input.js +27 -0
- package/test/codegen/private.methods/input.ts +3 -0
- package/test/codegen/private.methods/output.cs +13 -0
- package/test/codegen/protected.methods/expected.cs +13 -0
- package/test/codegen/protected.methods/input.js +27 -0
- package/test/codegen/protected.methods/input.ts +3 -0
- package/test/codegen/protected.methods/output.cs +13 -0
- package/test/codegen/public method with args/expected.cs +14 -0
- package/test/codegen/public method with args/input.js +27 -0
- package/test/codegen/public method with args/input.ts +3 -0
- package/test/codegen/public method with args/output.cs +14 -0
- package/test/codegen/public method with multiple args/expected.cs +14 -0
- package/test/codegen/public method with multiple args/input.js +27 -0
- package/test/codegen/public method with multiple args/input.ts +3 -0
- package/test/codegen/public method with multiple args/output.cs +14 -0
- package/test/codegen/public.methods/expected.cs +14 -0
- package/test/codegen/public.methods/input.js +27 -0
- package/test/codegen/public.methods/input.ts +3 -0
- package/test/codegen/public.methods/output.cs +14 -0
- package/test/codegen/static method/expected.cs +13 -0
- package/test/codegen/static method/input.js +27 -0
- package/test/codegen/static method/input.ts +3 -0
- package/test/codegen/static method/output.cs +14 -0
- package/test/codegen/string/expected.cs +14 -0
- package/test/codegen/string/input.js +26 -0
- package/test/codegen/string/input.ts +3 -0
- package/test/codegen/string/output.cs +14 -0
- package/test/codegen/stringArray/expected.cs +14 -0
- package/test/codegen/stringArray/input.js +26 -0
- package/test/codegen/stringArray/input.ts +3 -0
- package/test/codegen/stringArray/output.cs +14 -0
- package/test/component.basic.test.ts +52 -0
- package/test/component.methods.test.ts +185 -0
- package/test/component.nonserialized.test.ts +45 -0
- package/test/component.primitives.test.ts +121 -0
- package/test/helpers.ts +72 -0
- package/tsconfig.json +8 -0
- package/workspace.code-workspace +7 -7
package/Changelog.md
CHANGED
|
@@ -4,6 +4,14 @@ All notable changes to this package will be documented in this file.
|
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [1.9.0] - 2022-10-18
|
|
8
|
+
- Change: emit ``onEnable`` as ``OnEnable`` and ``onDisable`` as ``OnDisable`` so Unity creates component checkboxes
|
|
9
|
+
- Change: ignore private and protected methods
|
|
10
|
+
|
|
11
|
+
## [1.8.0] - 2022-09-02
|
|
12
|
+
- Add ``@nonSerialized`` decorator for fields and methods
|
|
13
|
+
- Fix comment gen for unknown types
|
|
14
|
+
|
|
7
15
|
## [1.7.3] - 2022-09-02
|
|
8
16
|
- Fix codegen for unknown array types
|
|
9
17
|
- Fix codegen for abstract classes
|
package/DEV.bat
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npm install && npm run dev
|
|
File without changes
|
package/RUN_TESTS.bat
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npm run test & pause
|
package/Readme.md
CHANGED
|
@@ -10,6 +10,7 @@ Please run ``npm install`` first before using.
|
|
|
10
10
|
### Command decorators
|
|
11
11
|
- ``@dont-generate-component`` add before class to skip generating a component
|
|
12
12
|
- ``@generate-component`` to enforce generating a component (not required)
|
|
13
|
-
- ``@serializeField`` field decorator, similar to ``[SerializeField]``
|
|
13
|
+
- ``@serializeField`` field decorator, similar to ``[SerializeField]`` in Unity
|
|
14
|
+
- ``@nonSerialized`` field or method decorator to skip generating c# code for a field or a method, similar to ``[NonSerialized]`` in Unity
|
|
14
15
|
- ``@type MyNamespace.MyType`` decorator for fields or classes, specifiy C# type of field or class
|
|
15
16
|
- ``@ifdef MY_IFDEF`` field decorator only at the moment
|
package/package.json
CHANGED
|
@@ -1,16 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@needle-tools/needle-component-compiler",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Compile mock unity components from typescript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"tsc": "tsc"
|
|
7
|
+
"tsc": "tsc",
|
|
8
|
+
"dev": "npm-watch compile",
|
|
9
|
+
"compile" : "tsc",
|
|
10
|
+
"test": "mocha -r ts-node/register test/**.test.ts"
|
|
8
11
|
},
|
|
9
12
|
"dependencies": {
|
|
10
13
|
"typescript": "^4.5.5"
|
|
11
14
|
},
|
|
12
15
|
"devDependencies": {
|
|
13
|
-
"@types/
|
|
16
|
+
"@types/chai": "^4.3.3",
|
|
17
|
+
"@types/mocha": "^9.1.1",
|
|
18
|
+
"@types/node": "^18.7.18",
|
|
19
|
+
"chai": "^4.3.6",
|
|
20
|
+
"mocha": "^10.0.0",
|
|
21
|
+
"ts-node": "^10.9.1",
|
|
22
|
+
"npm-watch": "^0.11.0"
|
|
23
|
+
},
|
|
24
|
+
"watch": {
|
|
25
|
+
"compile": {
|
|
26
|
+
"patterns": [
|
|
27
|
+
"src/component-compiler.ts"
|
|
28
|
+
],
|
|
29
|
+
"extensions": "ts",
|
|
30
|
+
"quiet": false
|
|
31
|
+
}
|
|
14
32
|
},
|
|
15
33
|
"author": {
|
|
16
34
|
"name": "Needle",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
exports.__esModule = true;
|
|
3
|
-
exports.run = void 0;
|
|
3
|
+
exports.run = exports.compile = void 0;
|
|
4
4
|
var fs_1 = require("fs");
|
|
5
5
|
var ts = require("typescript");
|
|
6
6
|
var fs = require("fs");
|
|
@@ -12,41 +12,59 @@ var exportNextClassCommand = "@generate-component";
|
|
|
12
12
|
var dontExportNextClassCommand = "@dont-generate-component";
|
|
13
13
|
// add above field to add [SerializeField] attribute
|
|
14
14
|
var serializeCommand = "@serializeField";
|
|
15
|
+
var dontSerializeCommand = "@nonSerialized";
|
|
15
16
|
// https://regex101.com/r/ltpcKT/2
|
|
16
17
|
var typePattern = new RegExp("@type ?(?<type>.+)");
|
|
17
18
|
var ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)");
|
|
18
19
|
var CODEGEN_MARKER_START = "// NEEDLE_CODEGEN_START";
|
|
19
20
|
var CODEGEN_MARKER_END = "// NEEDLE_CODEGEN_END";
|
|
21
|
+
var allowDebugLogs = true;
|
|
20
22
|
// will be set to true when e.g. a comment for export is found
|
|
21
23
|
var exportNextClass = false;
|
|
22
24
|
var dontExportNextClass = false;
|
|
23
25
|
var serializeField = false;
|
|
26
|
+
var dontSerialize = false;
|
|
27
|
+
var typesFileContent = undefined;
|
|
28
|
+
// const exportDir = "../dist";
|
|
29
|
+
var commentStarts = [];
|
|
30
|
+
var contexts = [];
|
|
31
|
+
var lastTypeFound = null;
|
|
32
|
+
var ifdefSections = [];
|
|
33
|
+
function resetAllState() {
|
|
34
|
+
exportNextClass = false;
|
|
35
|
+
dontExportNextClass = false;
|
|
36
|
+
serializeField = false;
|
|
37
|
+
dontSerialize = false;
|
|
38
|
+
typesFileContent = undefined;
|
|
39
|
+
commentStarts.length = 0;
|
|
40
|
+
contexts.length = 0;
|
|
41
|
+
lastTypeFound = null;
|
|
42
|
+
ifdefSections.length = 0;
|
|
43
|
+
}
|
|
24
44
|
function resetExportNextClass() {
|
|
25
45
|
dontExportNextClass = false;
|
|
26
46
|
exportNextClass = false;
|
|
27
47
|
}
|
|
28
|
-
var typesFileContent = undefined;
|
|
29
48
|
function tryGetKnownType(str) {
|
|
30
49
|
if (typesFileContent === undefined) {
|
|
31
50
|
typesFileContent = null;
|
|
32
51
|
var filePath = path.dirname(__dirname) + "/src/types.json";
|
|
33
52
|
if (fs.existsSync(filePath)) {
|
|
34
|
-
|
|
53
|
+
if (allowDebugLogs)
|
|
54
|
+
console.log("Reading types file");
|
|
35
55
|
var content = fs.readFileSync(filePath, "utf8");
|
|
36
56
|
typesFileContent = JSON.parse(content);
|
|
37
57
|
}
|
|
38
58
|
}
|
|
39
59
|
if (typesFileContent) {
|
|
40
60
|
var fullType = typesFileContent[str];
|
|
41
|
-
if (fullType)
|
|
61
|
+
if (fullType && allowDebugLogs)
|
|
42
62
|
console.log(fullType);
|
|
43
63
|
return fullType;
|
|
44
64
|
}
|
|
45
65
|
return null;
|
|
46
66
|
}
|
|
47
67
|
// https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
|
|
48
|
-
// const exportDir = "../dist";
|
|
49
|
-
var commentStarts = [];
|
|
50
68
|
var ExportContext = /** @class */ (function () {
|
|
51
69
|
function ExportContext(outputDir, fileName) {
|
|
52
70
|
this.classEnd = -1;
|
|
@@ -74,46 +92,62 @@ var ExportContext = /** @class */ (function () {
|
|
|
74
92
|
ExportContext.prototype.flush = function () {
|
|
75
93
|
if (this.textBuffer.length <= 0)
|
|
76
94
|
return;
|
|
77
|
-
|
|
78
|
-
var
|
|
79
|
-
this.
|
|
80
|
-
|
|
81
|
-
|
|
95
|
+
this.textBuffer = CODEGEN_MARKER_START + "\n" + this.textBuffer + "\n" + CODEGEN_MARKER_END;
|
|
96
|
+
var code = this.textBuffer;
|
|
97
|
+
if (this.outputDir !== null) {
|
|
98
|
+
var dir = this.outputDir + "/";
|
|
99
|
+
var path_1 = dir + this.fileName;
|
|
100
|
+
this.replaceGeneratedCodeSection(path_1);
|
|
101
|
+
if (allowDebugLogs)
|
|
102
|
+
console.log("Write to " + path_1);
|
|
103
|
+
fs.writeFileSync(path_1, code);
|
|
104
|
+
}
|
|
82
105
|
this.reset();
|
|
106
|
+
return code;
|
|
83
107
|
};
|
|
84
108
|
ExportContext.prototype.reset = function () {
|
|
85
109
|
this.textBuffer = "";
|
|
86
110
|
this.classEnd = -1;
|
|
87
111
|
};
|
|
88
112
|
ExportContext.prototype.replaceGeneratedCodeSection = function (path) {
|
|
89
|
-
this.textBuffer = CODEGEN_MARKER_START + "\n" + this.textBuffer + "\n" + CODEGEN_MARKER_END;
|
|
90
113
|
if (fs.existsSync(path)) {
|
|
91
114
|
var existing = fs.readFileSync(path, "utf8");
|
|
92
115
|
var regex = new RegExp("(?<before>.*?)\/\/ ?NEEDLE_CODEGEN_START.+\/\/ ?NEEDLE_CODEGEN_END(?<after>.*)", "s");
|
|
93
116
|
var matches = regex.exec(existing);
|
|
94
117
|
if (matches === null || matches === void 0 ? void 0 : matches.groups) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
var
|
|
98
|
-
|
|
118
|
+
if (allowDebugLogs)
|
|
119
|
+
console.log("Found codegen sections");
|
|
120
|
+
var before_1 = matches.groups.before;
|
|
121
|
+
var after_1 = matches.groups.after;
|
|
122
|
+
this.textBuffer = before_1 + this.textBuffer + after_1;
|
|
99
123
|
}
|
|
100
124
|
}
|
|
101
125
|
};
|
|
102
126
|
return ExportContext;
|
|
103
127
|
}());
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
128
|
+
function compile(code, fileName, outputDir, debugLogs) {
|
|
129
|
+
if (debugLogs === void 0) { debugLogs = true; }
|
|
130
|
+
resetAllState();
|
|
131
|
+
allowDebugLogs = debugLogs;
|
|
132
|
+
// Parse a file
|
|
133
|
+
var sourceFile = ts.createSourceFile(fileName, code, ts.ScriptTarget.ES2015, true);
|
|
134
|
+
var prog = ts.createProgram([fileName], {});
|
|
135
|
+
// delint it
|
|
136
|
+
return run(prog, outputDir, sourceFile);
|
|
137
|
+
}
|
|
138
|
+
exports.compile = compile;
|
|
107
139
|
function run(program, outputDir, sourceFile) {
|
|
108
|
-
if (!fs.existsSync(outputDir)) {
|
|
140
|
+
if (outputDir !== null && !fs.existsSync(outputDir)) {
|
|
109
141
|
console.error("Output directory does not exist: \"" + outputDir + "\"");
|
|
110
142
|
return;
|
|
111
143
|
}
|
|
144
|
+
var results = [];
|
|
112
145
|
traverseFile(sourceFile);
|
|
113
146
|
function traverseFile(node) {
|
|
114
147
|
visit(node);
|
|
115
148
|
ts.forEachChild(node, traverseFile);
|
|
116
149
|
}
|
|
150
|
+
return results;
|
|
117
151
|
function visit(node) {
|
|
118
152
|
var _a, _b, _c, _d, _e, _f;
|
|
119
153
|
var context = contexts.length > 0 ? contexts[contexts.length - 1] : null;
|
|
@@ -123,12 +157,14 @@ function run(program, outputDir, sourceFile) {
|
|
|
123
157
|
context.indentLevel -= 1;
|
|
124
158
|
context.append("}\n");
|
|
125
159
|
}
|
|
126
|
-
context.flush();
|
|
160
|
+
var code = context.flush();
|
|
161
|
+
results.push(code);
|
|
127
162
|
context = null;
|
|
128
163
|
contexts.pop();
|
|
129
164
|
}
|
|
130
165
|
}
|
|
131
|
-
|
|
166
|
+
if (allowDebugLogs)
|
|
167
|
+
console.log("\t", ts.SyntaxKind[node.kind]);
|
|
132
168
|
var commentRanges = ts.getLeadingCommentRanges(sourceFile.getFullText(), node.getFullStart());
|
|
133
169
|
if (commentRanges === null || commentRanges === void 0 ? void 0 : commentRanges.length) {
|
|
134
170
|
for (var _i = 0, commentRanges_1 = commentRanges; _i < commentRanges_1.length; _i++) {
|
|
@@ -146,6 +182,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
146
182
|
}
|
|
147
183
|
else if (comment.includes(serializeCommand))
|
|
148
184
|
serializeField = true;
|
|
185
|
+
else if (comment.includes(dontSerializeCommand))
|
|
186
|
+
dontSerialize = true;
|
|
149
187
|
}
|
|
150
188
|
if (comment.includes(exportNextClassCommand))
|
|
151
189
|
exportNextClass = true;
|
|
@@ -155,10 +193,11 @@ function run(program, outputDir, sourceFile) {
|
|
|
155
193
|
if (typeMatch && typeMatch.groups) {
|
|
156
194
|
// for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
|
|
157
195
|
// so we remove them
|
|
158
|
-
var
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
196
|
+
var type = typeMatch.groups["type"];
|
|
197
|
+
type = type.replace(/\(/, "").replace(/\)/, "");
|
|
198
|
+
if (allowDebugLogs)
|
|
199
|
+
console.log("Found type: ", type);
|
|
200
|
+
lastTypeFound = type;
|
|
162
201
|
}
|
|
163
202
|
var ifdefMatch = ifdefPattern.exec(comment);
|
|
164
203
|
if (ifdefMatch && ifdefMatch.groups) {
|
|
@@ -168,6 +207,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
168
207
|
}
|
|
169
208
|
}
|
|
170
209
|
}
|
|
210
|
+
var skip = dontSerialize;
|
|
171
211
|
switch (node.kind) {
|
|
172
212
|
// Namespace
|
|
173
213
|
// case ts.SyntaxKind.ModuleDeclaration:
|
|
@@ -187,13 +227,17 @@ function run(program, outputDir, sourceFile) {
|
|
|
187
227
|
case ts.SyntaxKind.MethodDeclaration:
|
|
188
228
|
lastTypeFound = null;
|
|
189
229
|
serializeField = false;
|
|
230
|
+
dontSerialize = false;
|
|
190
231
|
resetExportNextClass();
|
|
191
232
|
if (!context)
|
|
192
233
|
break;
|
|
193
234
|
// TODO: always emit at least OnEnable method per class so generated components have toggle in editor
|
|
194
235
|
var meth = node;
|
|
195
236
|
// const isCoroutine = func.asteriskToken;
|
|
196
|
-
if (meth.name) {
|
|
237
|
+
if (!skip && meth.name) {
|
|
238
|
+
var pub_1 = isPublic(meth);
|
|
239
|
+
if (!pub_1)
|
|
240
|
+
return;
|
|
197
241
|
var paramsStr = "";
|
|
198
242
|
for (var _g = 0, _h = meth.parameters; _g < _h.length; _g++) {
|
|
199
243
|
var param = _h[_g];
|
|
@@ -201,13 +245,22 @@ function run(program, outputDir, sourceFile) {
|
|
|
201
245
|
continue;
|
|
202
246
|
if (paramsStr.length > 0)
|
|
203
247
|
paramsStr += ", ";
|
|
204
|
-
var
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
paramsStr +=
|
|
248
|
+
var type = tryResolveTypeRecursive(param);
|
|
249
|
+
if (type === undefined)
|
|
250
|
+
type = "object";
|
|
251
|
+
paramsStr += type + " @" + param.name.getText();
|
|
208
252
|
}
|
|
209
253
|
var methodName = meth.name.getText();
|
|
254
|
+
switch (methodName) {
|
|
255
|
+
case "onEnable":
|
|
256
|
+
methodName = "OnEnable";
|
|
257
|
+
break;
|
|
258
|
+
case "onDisable":
|
|
259
|
+
methodName = "OnDisable";
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
210
262
|
context.onBeforeMethod(methodName);
|
|
263
|
+
// let visibility = pub ? "public" : "private";
|
|
211
264
|
context.append("public void " + methodName + "(" + paramsStr + "){}\n");
|
|
212
265
|
}
|
|
213
266
|
break;
|
|
@@ -216,28 +269,36 @@ function run(program, outputDir, sourceFile) {
|
|
|
216
269
|
resetExportNextClass();
|
|
217
270
|
if (!context)
|
|
218
271
|
break;
|
|
219
|
-
|
|
272
|
+
if (allowDebugLogs)
|
|
273
|
+
console.log("Found variable", node.getText());
|
|
220
274
|
var vardec = node;
|
|
221
275
|
var varName = "@" + vardec.name.getText();
|
|
222
276
|
var pub = isPublic(vardec);
|
|
223
277
|
var visibility = pub ? "public" : "private";
|
|
224
278
|
var isAccessible = pub;
|
|
225
|
-
|
|
226
|
-
|
|
279
|
+
dontSerialize = false;
|
|
280
|
+
if (serializeField) {
|
|
281
|
+
if (allowDebugLogs)
|
|
282
|
+
console.log("[SerializeField]");
|
|
227
283
|
context.appendLine("[UnityEngine.SerializeField]");
|
|
228
284
|
isAccessible = true;
|
|
229
285
|
}
|
|
286
|
+
else if (skip)
|
|
287
|
+
isAccessible = false;
|
|
230
288
|
if (!isAccessible) {
|
|
231
|
-
|
|
289
|
+
if (allowDebugLogs)
|
|
290
|
+
console.log("Skip because not public or serializeable");
|
|
232
291
|
break;
|
|
233
292
|
}
|
|
234
293
|
var name_1 = vardec.name.getText();
|
|
235
|
-
|
|
294
|
+
if (allowDebugLogs)
|
|
295
|
+
console.log("Variable:", name_1);
|
|
236
296
|
if (name_1.startsWith("\"@") || name_1.startsWith("\"$") || name_1.startsWith("$"))
|
|
237
297
|
break;
|
|
238
298
|
var typeString = lastTypeFound !== null && lastTypeFound !== void 0 ? lastTypeFound : tryResolveTypeRecursive(node);
|
|
239
299
|
var postFix = "";
|
|
240
300
|
var typeName = (_c = vardec.type) === null || _c === void 0 ? void 0 : _c.getText();
|
|
301
|
+
var shouldCommentTheLine = typeString === undefined;
|
|
241
302
|
if (typeString === undefined) {
|
|
242
303
|
postFix = " → Could not resolve C# type";
|
|
243
304
|
}
|
|
@@ -284,12 +345,15 @@ function run(program, outputDir, sourceFile) {
|
|
|
284
345
|
if (typeString === undefined)
|
|
285
346
|
typeString = typeName;
|
|
286
347
|
if (typeString === "[]") {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
348
|
+
if (allowDebugLogs)
|
|
349
|
+
console.log("Unknown array type for \"" + varName + " " + typeName + "\" - your type is probably not known (did you just create it this session?) and you might need to regenerate the Typemap in Unity. Go to \"Needle Engine/Internal/Generate Type Map for component compiler");
|
|
350
|
+
// typeString = "object[]";
|
|
351
|
+
// assignment = " = new object[0]";
|
|
352
|
+
shouldCommentTheLine = true;
|
|
290
353
|
}
|
|
291
|
-
|
|
292
|
-
|
|
354
|
+
if (allowDebugLogs)
|
|
355
|
+
console.log("EMIT member: \"" + typeString + "\" " + varName, assignment, "Last type found:", lastTypeFound);
|
|
356
|
+
var prefix = shouldCommentTheLine ? "// " : "";
|
|
293
357
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";" + postFix + "\n");
|
|
294
358
|
lastTypeFound = null;
|
|
295
359
|
if (requireEndIf) {
|
|
@@ -304,9 +368,11 @@ function run(program, outputDir, sourceFile) {
|
|
|
304
368
|
if (!dontExportNextClass && (lastTypeFound || exportNextClass || inheritsComponent)) {
|
|
305
369
|
resetExportNextClass();
|
|
306
370
|
var name_2 = (_d = dec.name) === null || _d === void 0 ? void 0 : _d.escapedText;
|
|
307
|
-
|
|
371
|
+
if (allowDebugLogs)
|
|
372
|
+
console.log("Found class: ", name_2);
|
|
308
373
|
var namespace = (_e = tryParseNamespace(node)) !== null && _e !== void 0 ? _e : "Needle.Typescript.GeneratedComponents";
|
|
309
|
-
|
|
374
|
+
if (allowDebugLogs)
|
|
375
|
+
console.log("NAMESPACE", namespace);
|
|
310
376
|
var newContext = new ExportContext(outputDir, name_2 + ".cs");
|
|
311
377
|
newContext.appendLine("// auto generated code - do not edit directly");
|
|
312
378
|
newContext.appendLine("");
|
|
@@ -321,7 +387,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
321
387
|
typeName_1 = inheritsComponent;
|
|
322
388
|
if (lastTypeFound)
|
|
323
389
|
typeName_1 = lastTypeFound;
|
|
324
|
-
|
|
390
|
+
if (allowDebugLogs)
|
|
391
|
+
console.log(name_2 + " inherits " + typeName_1);
|
|
325
392
|
var modifiers = "";
|
|
326
393
|
if (dec.modifiers) {
|
|
327
394
|
for (var _l = 0, _m = dec.modifiers; _l < _m.length; _l++) {
|
|
@@ -329,7 +396,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
329
396
|
switch (mod.getText()) {
|
|
330
397
|
case "abstract":
|
|
331
398
|
modifiers += " abstract";
|
|
332
|
-
|
|
399
|
+
if (allowDebugLogs)
|
|
400
|
+
console.log(name_2 + " is abstract");
|
|
333
401
|
break;
|
|
334
402
|
}
|
|
335
403
|
}
|
|
@@ -342,7 +410,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
342
410
|
contexts.push(newContext);
|
|
343
411
|
}
|
|
344
412
|
else {
|
|
345
|
-
|
|
413
|
+
if (allowDebugLogs)
|
|
414
|
+
console.log("Class type is unknown and will not generate a component: ", (_f = dec.name) === null || _f === void 0 ? void 0 : _f.escapedText);
|
|
346
415
|
}
|
|
347
416
|
lastTypeFound = null;
|
|
348
417
|
break;
|
|
@@ -357,10 +426,10 @@ function run(program, outputDir, sourceFile) {
|
|
|
357
426
|
if (h.types.length <= 0)
|
|
358
427
|
continue;
|
|
359
428
|
for (var _b = 0, _c = h.types; _b < _c.length; _b++) {
|
|
360
|
-
var
|
|
429
|
+
var type = _c[_b];
|
|
361
430
|
// const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
|
|
362
431
|
// console.log(symbol);
|
|
363
|
-
var text =
|
|
432
|
+
var text = type.expression.getText();
|
|
364
433
|
if (text === "Component")
|
|
365
434
|
return true;
|
|
366
435
|
if (text === "Behaviour")
|
|
@@ -381,7 +450,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
381
450
|
case ts.SyntaxKind.FirstLiteralToken:
|
|
382
451
|
return node.getText();
|
|
383
452
|
case ts.SyntaxKind.NewExpression:
|
|
384
|
-
var
|
|
453
|
+
var type = undefined;
|
|
385
454
|
var args = undefined;
|
|
386
455
|
for (var _i = 0, _a = node.getChildren(); _i < _a.length; _i++) {
|
|
387
456
|
var ch = _a[_i];
|
|
@@ -390,7 +459,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
390
459
|
switch (ch.kind) {
|
|
391
460
|
case ts.SyntaxKind.Identifier:
|
|
392
461
|
case ts.SyntaxKind.PropertyAccessExpression:
|
|
393
|
-
|
|
462
|
+
type = tryGetTypeFromText(text);
|
|
394
463
|
break;
|
|
395
464
|
case ts.SyntaxKind.SyntaxList:
|
|
396
465
|
args = text;
|
|
@@ -399,20 +468,21 @@ function run(program, outputDir, sourceFile) {
|
|
|
399
468
|
}
|
|
400
469
|
if (!args)
|
|
401
470
|
args = "";
|
|
402
|
-
if (
|
|
403
|
-
return "new " +
|
|
471
|
+
if (type)
|
|
472
|
+
return "new " + type + "(" + args + ")";
|
|
404
473
|
// const expType = node.getChildren().find(c => c.kind === ts.SyntaxKind.Identifier);
|
|
405
474
|
break;
|
|
406
475
|
}
|
|
407
476
|
var str = node.getText();
|
|
408
|
-
|
|
477
|
+
if (allowDebugLogs)
|
|
478
|
+
console.log("Unknown assignment:", str, ts.SyntaxKind[node.kind]);
|
|
409
479
|
return str;
|
|
410
480
|
}
|
|
411
481
|
function isPublic(node) {
|
|
412
482
|
if (node.kind === ts.SyntaxKind.PublicKeyword) {
|
|
413
483
|
return true;
|
|
414
484
|
}
|
|
415
|
-
else if (node.kind === ts.SyntaxKind.PrivateKeyword) {
|
|
485
|
+
else if (node.kind === ts.SyntaxKind.PrivateKeyword || node.kind === ts.SyntaxKind.ProtectedKeyword) {
|
|
416
486
|
return false;
|
|
417
487
|
}
|
|
418
488
|
for (var _i = 0, _a = node.getChildren(); _i < _a.length; _i++) {
|
|
@@ -453,7 +523,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
453
523
|
var separatorIndex = typeName.lastIndexOf(".");
|
|
454
524
|
if (separatorIndex > 0) {
|
|
455
525
|
var newName = typeName.substring(separatorIndex + 1);
|
|
456
|
-
|
|
526
|
+
if (allowDebugLogs)
|
|
527
|
+
console.log("Remove import name from type: \"" + typeName + "\" → \"" + newName + "\"");
|
|
457
528
|
typeName = newName;
|
|
458
529
|
}
|
|
459
530
|
var res = dict[typeName];
|
|
@@ -505,7 +576,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
505
576
|
case ts.SyntaxKind.TypeReference:
|
|
506
577
|
var typeRef = node;
|
|
507
578
|
var typeName_2 = typeRef.typeName.getText();
|
|
508
|
-
|
|
579
|
+
if (allowDebugLogs)
|
|
580
|
+
console.log("TypeReference:", typeName_2);
|
|
509
581
|
switch (typeName_2) {
|
|
510
582
|
case "Array":
|
|
511
583
|
break;
|
|
@@ -563,7 +635,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
563
635
|
if (childResult !== undefined) {
|
|
564
636
|
if (res === undefined)
|
|
565
637
|
res = "";
|
|
566
|
-
|
|
638
|
+
if (allowDebugLogs)
|
|
639
|
+
console.log("Child: " + ts.SyntaxKind[child.kind] + " → " + childResult);
|
|
567
640
|
// if the thing is a generic return as generic result
|
|
568
641
|
if (isInGenericDeclaration && !res.includes("[]")) {
|
|
569
642
|
res = "<" + childResult + ">";
|
|
@@ -591,18 +664,20 @@ function run(program, outputDir, sourceFile) {
|
|
|
591
664
|
}
|
|
592
665
|
}
|
|
593
666
|
exports.run = run;
|
|
594
|
-
if (process
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
667
|
+
if (process) {
|
|
668
|
+
if (process.argv.length < 4) {
|
|
669
|
+
console.error("Missing args, call with: <output_dir> <input_files>");
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
var outputDir_1 = process.argv[2];
|
|
673
|
+
var fileNames = process.argv.slice(3);
|
|
674
|
+
fileNames.forEach(function (fileName) {
|
|
675
|
+
if (!fs.existsSync(fileName)) {
|
|
676
|
+
}
|
|
677
|
+
else {
|
|
678
|
+
var code = (0, fs_1.readFileSync)(fileName).toString();
|
|
679
|
+
compile(code, fileName, outputDir_1);
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
}
|
|
608
683
|
}
|