@needle-tools/needle-component-compiler 1.5.0 → 1.6.2

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 CHANGED
@@ -4,6 +4,13 @@ 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.6.2] - 2022-07-11
8
+ - fix ``@type`` for class declaration
9
+
10
+ ## [1.6.1] - 2022-07-10
11
+ - add using ``types.json`` json file that will be generated from Unity
12
+ - change ``@type`` annotiation to only work without braces to be consistent
13
+
7
14
  ## [1.5.0] - 2022-07-07
8
15
  - change ``@type`` annotation to work with and without braces (e.g. ``@type My.Type`` or ``@type (My.Type)``)
9
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.5.0",
3
+ "version": "1.6.2",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -4,6 +4,7 @@ exports.run = void 0;
4
4
  var fs_1 = require("fs");
5
5
  var ts = require("typescript");
6
6
  var fs = require("fs");
7
+ var path = require("path");
7
8
  var types = require("./types");
8
9
  var dict = types.dict;
9
10
  // add either of these two comments above a class to enforce code gen or disable it for the next class
@@ -11,7 +12,8 @@ var exportNextClassCommand = "@generate-component";
11
12
  var dontExportNextClassCommand = "@dont-generate-component";
12
13
  // add above field to add [SerializeField] attribute
13
14
  var serializeCommand = "@serializeField";
14
- var typePattern = new RegExp("@type ?\(?(?<type>[\w\.]+)\)?");
15
+ // https://regex101.com/r/ltpcKT/2
16
+ var typePattern = new RegExp("@type ?(?<type>.+)");
15
17
  var ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)");
16
18
  var CODEGEN_MARKER_START = "// NEEDLE_CODEGEN_START";
17
19
  var CODEGEN_MARKER_END = "// NEEDLE_CODEGEN_END";
@@ -23,6 +25,24 @@ function resetExportNextClass() {
23
25
  dontExportNextClass = false;
24
26
  exportNextClass = false;
25
27
  }
28
+ var typesFileContent = undefined;
29
+ function tryGetKnownType(str) {
30
+ if (typesFileContent === undefined) {
31
+ typesFileContent = null;
32
+ var filePath = path.dirname(__dirname) + "/src/types.json";
33
+ if (fs.existsSync(filePath)) {
34
+ console.log("Reading types file");
35
+ var content = fs.readFileSync(filePath, "utf8");
36
+ typesFileContent = JSON.parse(content);
37
+ }
38
+ }
39
+ if (typesFileContent) {
40
+ var fullType = typesFileContent[str];
41
+ console.log(fullType);
42
+ return fullType;
43
+ }
44
+ return null;
45
+ }
26
46
  // https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
27
47
  // const exportDir = "../dist";
28
48
  var commentStarts = [];
@@ -136,7 +156,7 @@ function run(program, outputDir, sourceFile) {
136
156
  // so we remove them
137
157
  var type = typeMatch.groups["type"];
138
158
  type = type.replace(/\(/, "").replace(/\)/, "");
139
- console.log("found type: ", type);
159
+ console.log("Found type: ", type);
140
160
  lastTypeFound = type;
141
161
  }
142
162
  var ifdefMatch = ifdefPattern.exec(comment);
@@ -254,7 +274,8 @@ function run(program, outputDir, sourceFile) {
254
274
  serializeField = false;
255
275
  var dec = node;
256
276
  // a class must inherit a component
257
- if (!dontExportNextClass && (lastTypeFound || exportNextClass || testInheritsComponent(node))) {
277
+ var inheritsComponent = testInheritsComponent(node);
278
+ if (!dontExportNextClass && (lastTypeFound || exportNextClass || inheritsComponent)) {
258
279
  resetExportNextClass();
259
280
  var name_2 = (_d = dec.name) === null || _d === void 0 ? void 0 : _d.escapedText;
260
281
  console.log("Found class: ", name_2);
@@ -269,7 +290,11 @@ function run(program, outputDir, sourceFile) {
269
290
  newContext.appendLine("{");
270
291
  newContext.indentLevel += 1;
271
292
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
272
- var typeName = lastTypeFound !== null && lastTypeFound !== void 0 ? lastTypeFound : "UnityEngine.MonoBehaviour";
293
+ var typeName = "UnityEngine.MonoBehaviour";
294
+ if (typeof inheritsComponent === "string")
295
+ typeName = inheritsComponent;
296
+ if (lastTypeFound)
297
+ typeName = lastTypeFound;
273
298
  console.log(name_2 + " inherits " + typeName);
274
299
  newContext.appendLine("public partial class " + name_2 + " : " + typeName);
275
300
  newContext.appendLine("{");
@@ -293,10 +318,14 @@ function run(program, outputDir, sourceFile) {
293
318
  var type = _c[_b];
294
319
  // const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
295
320
  // console.log(symbol);
296
- if (type.expression.getText() === "Component")
321
+ var text = type.expression.getText();
322
+ if (text === "Component")
297
323
  return true;
298
- if (type.expression.getText() === "Behaviour")
324
+ if (text === "Behaviour")
299
325
  return true;
326
+ var known = tryGetKnownType(text);
327
+ if (known)
328
+ return known;
300
329
  }
301
330
  }
302
331
  }
@@ -361,6 +390,9 @@ function run(program, outputDir, sourceFile) {
361
390
  // console.log(node);
362
391
  return res;
363
392
  }
393
+ var knownType = tryGetKnownType(typeName);
394
+ if (knownType)
395
+ return knownType;
364
396
  // console.log("Unknown type: " + typeName);
365
397
  switch (node.kind) {
366
398
  case ts.SyntaxKind.SyntaxList:
@@ -1,10 +1,9 @@
1
1
  import { readFileSync } from "fs";
2
2
  import * as ts from "typescript";
3
3
  import * as fs from "fs";
4
- import * as path from "path";
4
+ import * as path from 'path';
5
5
 
6
6
  import * as types from "./types";
7
- import { traceDeprecation } from "process";
8
7
  const dict = types.dict;
9
8
 
10
9
  // add either of these two comments above a class to enforce code gen or disable it for the next class
@@ -12,8 +11,8 @@ const exportNextClassCommand = "@generate-component";
12
11
  const dontExportNextClassCommand = "@dont-generate-component";
13
12
  // add above field to add [SerializeField] attribute
14
13
  const serializeCommand = "@serializeField";
15
- // https://regex101.com/r/ltpcKT/1
16
- const typePattern = new RegExp("@type ?\(?(?<type>[\w\.]+)\)?");
14
+ // https://regex101.com/r/ltpcKT/2
15
+ const typePattern = new RegExp("@type ?(?<type>.+)");
17
16
  const ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)")
18
17
 
19
18
  const CODEGEN_MARKER_START = "// NEEDLE_CODEGEN_START";
@@ -29,6 +28,27 @@ function resetExportNextClass() {
29
28
  }
30
29
 
31
30
 
31
+ let typesFileContent: object | undefined | null = undefined;
32
+ function tryGetKnownType(str: string): string | null {
33
+
34
+ if (typesFileContent === undefined) {
35
+ typesFileContent = null;
36
+ const filePath = path.dirname(__dirname) + "/src/types.json";
37
+ if (fs.existsSync(filePath)) {
38
+ console.log("Reading types file");
39
+ const content = fs.readFileSync(filePath, "utf8");
40
+ typesFileContent = JSON.parse(content);
41
+ }
42
+ }
43
+
44
+ if (typesFileContent) {
45
+ const fullType = typesFileContent[str];
46
+ console.log(fullType);
47
+ return fullType;
48
+ }
49
+ return null;
50
+ }
51
+
32
52
  // https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
33
53
 
34
54
  // const exportDir = "../dist";
@@ -161,7 +181,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
161
181
  // so we remove them
162
182
  let type = typeMatch.groups["type"];
163
183
  type = type.replace(/\(/, "").replace(/\)/, "");
164
- console.log("found type: ", type);
184
+ console.log("Found type: ", type);
165
185
  lastTypeFound = type;
166
186
  }
167
187
 
@@ -261,13 +281,13 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
261
281
  context.appendLine("[UnityEngine.SerializeField]");
262
282
  }
263
283
  let requireEndIf = false;
264
- if(ifdefSections.length > 0){
284
+ if (ifdefSections.length > 0) {
265
285
  requireEndIf = true;
266
286
  context.appendLine("#ifdef " + ifdefSections.pop());
267
287
  }
268
288
  context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";\n");
269
289
  lastTypeFound = null;
270
- if(requireEndIf){
290
+ if (requireEndIf) {
271
291
  context.appendLine("#endif");
272
292
  }
273
293
  break;
@@ -276,7 +296,8 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
276
296
  serializeField = false;
277
297
  const dec = <ts.ClassDeclaration>node;
278
298
  // a class must inherit a component
279
- if (!dontExportNextClass && (lastTypeFound || exportNextClass || testInheritsComponent(node))) {
299
+ const inheritsComponent = testInheritsComponent(node);
300
+ if (!dontExportNextClass && (lastTypeFound || exportNextClass || inheritsComponent)) {
280
301
  resetExportNextClass();
281
302
  const name = dec.name?.escapedText;
282
303
  console.log("Found class: ", name);
@@ -291,7 +312,9 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
291
312
  newContext.appendLine("{");
292
313
  newContext.indentLevel += 1;
293
314
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
294
- const typeName = lastTypeFound ?? "UnityEngine.MonoBehaviour";
315
+ let typeName = "UnityEngine.MonoBehaviour";
316
+ if (typeof inheritsComponent === "string") typeName = inheritsComponent;
317
+ if(lastTypeFound) typeName = lastTypeFound;
295
318
  console.log(name + " inherits " + typeName);
296
319
  newContext.appendLine("public partial class " + name + " : " + typeName);
297
320
  newContext.appendLine("{");
@@ -303,7 +326,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
303
326
  break;
304
327
  }
305
328
 
306
- function testInheritsComponent(node: ts.Node): boolean {
329
+ function testInheritsComponent(node: ts.Node): boolean | string {
307
330
  switch (node.kind) {
308
331
  case ts.SyntaxKind.ClassDeclaration:
309
332
  const dec = <ts.ClassDeclaration>node;
@@ -313,8 +336,11 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
313
336
  for (const type of h.types) {
314
337
  // const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
315
338
  // console.log(symbol);
316
- if (type.expression.getText() === "Component") return true;
317
- if (type.expression.getText() === "Behaviour") return true;
339
+ const text = type.expression.getText();
340
+ if (text === "Component") return true;
341
+ if (text === "Behaviour") return true;
342
+ const known = tryGetKnownType(text);
343
+ if (known) return known;
318
344
  }
319
345
  }
320
346
  }
@@ -381,6 +407,9 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
381
407
  // console.log(node);
382
408
  return res;
383
409
  }
410
+ const knownType = tryGetKnownType(typeName);
411
+ if (knownType)
412
+ return knownType;
384
413
  // console.log("Unknown type: " + typeName);
385
414
 
386
415
  switch (node.kind) {
package/src/test.ts CHANGED
@@ -1,21 +1,34 @@
1
+ //@type UnityEngine.MonoBehaviour
2
+ export class ButtonObject extends Interactable implements IPointerClickHandler, ISerializable {
3
+
4
+ //@type UnityEngine.Transform[]
5
+ myType?: SceneFXWindow;
6
+ }
1
7
 
2
8
  // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
3
9
  // import { RoomEntity } from "./Room";
4
10
 
11
+ // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
5
12
 
6
- namespace Hello.World
7
- {
8
- namespace Deep {
9
- export class MyClass extends Behaviour {
10
- //@ifdef TEST
11
- public myFloat :number;
12
- }
13
- }
14
- }
13
+ // export class MyNewScript extends DriveClient
14
+ // {
15
+ // //@type test
16
+ // texture : RenderTexture;
17
+ // }
18
+
19
+ // namespace Hello.World
20
+ // {
21
+ // namespace Deep {
22
+ // export class MyClass extends Behaviour {
23
+ // //@ifdef TEST
24
+ // public myFloat :number;
25
+ // }
26
+ // }
27
+ // }
15
28
 
16
- class OtherClass extends Behaviour {
29
+ // class OtherClass extends Behaviour {
17
30
 
18
- }
31
+ // }
19
32
 
20
33
  //@type (RoomEntity)
21
34
  // export class NavigationManager extends RoomEntity {
@@ -1,11 +0,0 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
3
- {
4
- // source: C:\git\needle-tiny-playground\modules\needle-tiny\component-compiler\src\test.ts
5
- public class GltfExport : UnityEngine.MonoBehaviour
6
- {
7
- public bool @binary = true;
8
- [UnityEngine.ContextMenu("enable this")]
9
- public void test(){}
10
- }
11
- }
@@ -1,11 +0,0 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
3
- {
4
- // source: C:\git\needle-tiny-playground\modules\needle-tiny\component-compiler\src\test.ts
5
- public class GltfExportBox : UnityEngine.MonoBehaviour
6
- {
7
- public UnityEngine.Transform @sceneRoot;
8
- public void start(){}
9
- public void updateGltfBox(){}
10
- }
11
- }
@@ -1,11 +0,0 @@
1
- // auto generated code - do not edit
2
-
3
- #pragma warning disable
4
-
5
- namespace Needle.Typescript.GeneratedComponents
6
- {
7
- public class MaterialColorHandler : UnityEngine.MonoBehaviour
8
- {
9
- public UnityEngine.Renderer[] @renderer;
10
- }
11
- }
package/dist/MyArray.cs DELETED
@@ -1,8 +0,0 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
3
- {
4
- public class MyArray : UnityEngine.MonoBehaviour
5
- {
6
- public float[] @arr = new float[]{ 1, 2, 3 };
7
- }
8
- }
package/dist/MyClass.cs DELETED
@@ -1,16 +0,0 @@
1
- // NEEDLE_CODEGEN_START
2
- // auto generated code - do not edit directly
3
-
4
- #pragma warning disable
5
-
6
- namespace Hello.World.Deep
7
- {
8
- public partial class MyClass : UnityEngine.MonoBehaviour
9
- {
10
- #ifdef TEST
11
- public float @myFloat;
12
- #endif
13
- }
14
- }
15
-
16
- // NEEDLE_CODEGEN_END
@@ -1,10 +0,0 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
3
- {
4
- // source: C:\git\needle-tiny-playground\modules\needle-tiny\component-compiler\src\test.ts
5
- public class MyClassWithAFloat : UnityEngine.MonoBehaviour
6
- {
7
- public float @myfloat = 0.5f;
8
- private string @myString;
9
- }
10
- }
@@ -1,13 +0,0 @@
1
- // auto generated code - do not edit directly
2
-
3
- #pragma warning disable
4
-
5
- namespace Needle.Typescript.GeneratedComponents
6
- {
7
- public partial class NavComponent : UnityEngine.MonoBehaviour
8
- {
9
- public void next(){}
10
- public void prev(){}
11
- public void isAtEnd(){}
12
- }
13
- }
@@ -1,13 +0,0 @@
1
- // auto generated code - do not edit directly
2
-
3
- #pragma warning disable
4
-
5
- namespace Needle.Typescript.GeneratedComponents
6
- {
7
- public partial class NavigationManager : RoomEntity
8
- {
9
- public float @fl = 1f;
10
- public void nav_forward(){}
11
- public void nav_backward(){}
12
- }
13
- }
@@ -1,17 +0,0 @@
1
-
2
- // I can edit this
3
-
4
-
5
- // NEEDLE_CODEGEN_START
6
- // auto generated code - do not edit directly
7
-
8
- #pragma warning disable
9
-
10
- namespace Needle.Typescript.GeneratedComponents
11
- {
12
- public partial class OtherClass : UnityEngine.MonoBehaviour
13
- {
14
- }
15
- }
16
-
17
- // NEEDLE_CODEGEN_END
@@ -1,14 +0,0 @@
1
- // auto generated code - do not edit directly
2
-
3
- #pragma warning disable
4
-
5
- namespace Needle.Typescript.GeneratedComponents
6
- {
7
- public partial class PointOfInterest : UnityEngine.MonoBehaviour
8
- {
9
- public float @myVal = 12f;
10
- public void myFunction(){}
11
- public UnityEngine.Camera @view;
12
- public string @test = "123";
13
- }
14
- }
@@ -1,9 +0,0 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
3
- {
4
- public class PrivateSerializedField : UnityEngine.MonoBehaviour
5
- {
6
- [UnityEngine.SerializeField]
7
- private UnityEngine.Color @color;
8
- }
9
- }
package/dist/Test.cs DELETED
@@ -1,8 +0,0 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
3
- {
4
- // source: C:\git\needle-tiny-playground\projects\Compiled_Export\myProject\src\scripts\SomeTestComponent.ts
5
- public class Test : UnityEngine.MonoBehaviour
6
- {
7
- }
8
- }
package/src/component.cs DELETED
@@ -1 +0,0 @@
1
- public class MyClass : MonoBehaviour {