@needle-tools/needle-component-compiler 1.5.0 → 1.6.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 CHANGED
@@ -4,6 +4,10 @@ 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.0] - 2022-07-10
8
+ - add using ``types.json`` json file that will be generated from Unity
9
+ - change ``@type`` annotiation to only work without braces to be consistent
10
+
7
11
  ## [1.5.0] - 2022-07-07
8
12
  - change ``@type`` annotation to work with and without braces (e.g. ``@type My.Type`` or ``@type (My.Type)``)
9
13
 
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.0",
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/1
16
+ var typePattern = new RegExp("@type ?(<type>[\w\.]+)");
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 = [];
@@ -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);
@@ -270,6 +291,8 @@ function run(program, outputDir, sourceFile) {
270
291
  newContext.indentLevel += 1;
271
292
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
272
293
  var typeName = lastTypeFound !== null && lastTypeFound !== void 0 ? lastTypeFound : "UnityEngine.MonoBehaviour";
294
+ if (typeof inheritsComponent === "string")
295
+ typeName = inheritsComponent;
273
296
  console.log(name_2 + " inherits " + typeName);
274
297
  newContext.appendLine("public partial class " + name_2 + " : " + typeName);
275
298
  newContext.appendLine("{");
@@ -293,10 +316,15 @@ function run(program, outputDir, sourceFile) {
293
316
  var type = _c[_b];
294
317
  // const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
295
318
  // console.log(symbol);
296
- if (type.expression.getText() === "Component")
319
+ var text = type.expression.getText();
320
+ if (text === "Component")
297
321
  return true;
298
- if (type.expression.getText() === "Behaviour")
322
+ if (text === "Behaviour")
299
323
  return true;
324
+ console.log(text);
325
+ var known = tryGetKnownType(text);
326
+ if (known)
327
+ return known;
300
328
  }
301
329
  }
302
330
  }
@@ -361,6 +389,9 @@ function run(program, outputDir, sourceFile) {
361
389
  // console.log(node);
362
390
  return res;
363
391
  }
392
+ var knownType = tryGetKnownType(typeName);
393
+ if (knownType)
394
+ return knownType;
364
395
  // console.log("Unknown type: " + typeName);
365
396
  switch (node.kind) {
366
397
  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
@@ -13,7 +12,7 @@ const dontExportNextClassCommand = "@dont-generate-component";
13
12
  // add above field to add [SerializeField] attribute
14
13
  const serializeCommand = "@serializeField";
15
14
  // https://regex101.com/r/ltpcKT/1
16
- const typePattern = new RegExp("@type ?\(?(?<type>[\w\.]+)\)?");
15
+ const typePattern = new RegExp("@type ?(<type>[\w\.]+)");
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";
@@ -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,8 @@ 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 = lastTypeFound ?? "UnityEngine.MonoBehaviour";
316
+ if (typeof inheritsComponent === "string") typeName = inheritsComponent;
295
317
  console.log(name + " inherits " + typeName);
296
318
  newContext.appendLine("public partial class " + name + " : " + typeName);
297
319
  newContext.appendLine("{");
@@ -303,7 +325,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
303
325
  break;
304
326
  }
305
327
 
306
- function testInheritsComponent(node: ts.Node): boolean {
328
+ function testInheritsComponent(node: ts.Node): boolean | string {
307
329
  switch (node.kind) {
308
330
  case ts.SyntaxKind.ClassDeclaration:
309
331
  const dec = <ts.ClassDeclaration>node;
@@ -313,8 +335,11 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
313
335
  for (const type of h.types) {
314
336
  // const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
315
337
  // console.log(symbol);
316
- if (type.expression.getText() === "Component") return true;
317
- if (type.expression.getText() === "Behaviour") return true;
338
+ const text = type.expression.getText();
339
+ if (text === "Component") return true;
340
+ if (text === "Behaviour") return true;
341
+ const known = tryGetKnownType(text);
342
+ if (known) return known;
318
343
  }
319
344
  }
320
345
  }
@@ -381,6 +406,9 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
381
406
  // console.log(node);
382
407
  return res;
383
408
  }
409
+ const knownType = tryGetKnownType(typeName);
410
+ if (knownType)
411
+ return knownType;
384
412
  // console.log("Unknown type: " + typeName);
385
413
 
386
414
  switch (node.kind) {
package/src/test.ts CHANGED
@@ -2,20 +2,26 @@
2
2
  // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
3
3
  // import { RoomEntity } from "./Room";
4
4
 
5
+ import { Behaviour } from "needle.tiny.engine/engine-components/Component";
5
6
 
6
- namespace Hello.World
7
+ export class MyNewScript extends DriveClient
7
8
  {
8
- namespace Deep {
9
- export class MyClass extends Behaviour {
10
- //@ifdef TEST
11
- public myFloat :number;
12
- }
13
- }
14
- }
9
+ texture : RenderTexture;
10
+ }
11
+
12
+ // namespace Hello.World
13
+ // {
14
+ // namespace Deep {
15
+ // export class MyClass extends Behaviour {
16
+ // //@ifdef TEST
17
+ // public myFloat :number;
18
+ // }
19
+ // }
20
+ // }
15
21
 
16
- class OtherClass extends Behaviour {
22
+ // class OtherClass extends Behaviour {
17
23
 
18
- }
24
+ // }
19
25
 
20
26
  //@type (RoomEntity)
21
27
  // 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
- }