@needle-tools/needle-component-compiler 1.4.0 → 1.6.1

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,7 +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.3.0] - 2022-07-06
7
+ ## [1.6.1] - 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
+
11
+ ## [1.5.0] - 2022-07-07
12
+ - change ``@type`` annotation to work with and without braces (e.g. ``@type My.Type`` or ``@type (My.Type)``)
13
+
14
+ ## [1.4.0] - 2022-07-06
8
15
  - add CODEGEN_START and END sections to allow adding code before or after a generated componnet
9
16
  - add ``@ifdef`` to ifdef fields
10
17
 
package/Readme.md CHANGED
@@ -11,4 +11,5 @@ Please run ``npm install`` first before using.
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
13
  - ``@serializeField`` field decorator, similar to ``[SerializeField]``
14
- - ``@type(MyNamespace.MyType)`` decorator for fields or classes, specifiy C# type of field or class
14
+ - ``@type MyNamespace.MyType`` decorator for fields or classes, specifiy C# type of field or class
15
+ - ``@ifdef MY_IFDEF`` field decorator only at the moment
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.4.0",
3
+ "version": "1.6.1",
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>.+)\)");
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);
@@ -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,14 @@ 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
+ var known = tryGetKnownType(text);
325
+ if (known)
326
+ return known;
300
327
  }
301
328
  }
302
329
  }
@@ -361,6 +388,9 @@ function run(program, outputDir, sourceFile) {
361
388
  // console.log(node);
362
389
  return res;
363
390
  }
391
+ var knownType = tryGetKnownType(typeName);
392
+ if (knownType)
393
+ return knownType;
364
394
  // console.log("Unknown type: " + typeName);
365
395
  switch (node.kind) {
366
396
  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,7 +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
- const typePattern = new RegExp("@type ?\((?<type>.+)\)");
14
+ // https://regex101.com/r/ltpcKT/2
15
+ const typePattern = new RegExp("@type ?(?<type>.+)");
16
16
  const ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)")
17
17
 
18
18
  const CODEGEN_MARKER_START = "// NEEDLE_CODEGEN_START";
@@ -28,6 +28,27 @@ function resetExportNextClass() {
28
28
  }
29
29
 
30
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
+
31
52
  // https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
32
53
 
33
54
  // const exportDir = "../dist";
@@ -160,7 +181,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
160
181
  // so we remove them
161
182
  let type = typeMatch.groups["type"];
162
183
  type = type.replace(/\(/, "").replace(/\)/, "");
163
- console.log("found type: ", type);
184
+ console.log("Found type: ", type);
164
185
  lastTypeFound = type;
165
186
  }
166
187
 
@@ -260,13 +281,13 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
260
281
  context.appendLine("[UnityEngine.SerializeField]");
261
282
  }
262
283
  let requireEndIf = false;
263
- if(ifdefSections.length > 0){
284
+ if (ifdefSections.length > 0) {
264
285
  requireEndIf = true;
265
286
  context.appendLine("#ifdef " + ifdefSections.pop());
266
287
  }
267
288
  context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";\n");
268
289
  lastTypeFound = null;
269
- if(requireEndIf){
290
+ if (requireEndIf) {
270
291
  context.appendLine("#endif");
271
292
  }
272
293
  break;
@@ -275,7 +296,8 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
275
296
  serializeField = false;
276
297
  const dec = <ts.ClassDeclaration>node;
277
298
  // a class must inherit a component
278
- if (!dontExportNextClass && (lastTypeFound || exportNextClass || testInheritsComponent(node))) {
299
+ const inheritsComponent = testInheritsComponent(node);
300
+ if (!dontExportNextClass && (lastTypeFound || exportNextClass || inheritsComponent)) {
279
301
  resetExportNextClass();
280
302
  const name = dec.name?.escapedText;
281
303
  console.log("Found class: ", name);
@@ -290,7 +312,8 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
290
312
  newContext.appendLine("{");
291
313
  newContext.indentLevel += 1;
292
314
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
293
- const typeName = lastTypeFound ?? "UnityEngine.MonoBehaviour";
315
+ let typeName = lastTypeFound ?? "UnityEngine.MonoBehaviour";
316
+ if (typeof inheritsComponent === "string") typeName = inheritsComponent;
294
317
  console.log(name + " inherits " + typeName);
295
318
  newContext.appendLine("public partial class " + name + " : " + typeName);
296
319
  newContext.appendLine("{");
@@ -302,7 +325,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
302
325
  break;
303
326
  }
304
327
 
305
- function testInheritsComponent(node: ts.Node): boolean {
328
+ function testInheritsComponent(node: ts.Node): boolean | string {
306
329
  switch (node.kind) {
307
330
  case ts.SyntaxKind.ClassDeclaration:
308
331
  const dec = <ts.ClassDeclaration>node;
@@ -312,8 +335,11 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
312
335
  for (const type of h.types) {
313
336
  // const symbol = program.getTypeChecker().getSymbolAtLocation(type.expression);
314
337
  // console.log(symbol);
315
- if (type.expression.getText() === "Component") return true;
316
- 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;
317
343
  }
318
344
  }
319
345
  }
@@ -380,6 +406,9 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
380
406
  // console.log(node);
381
407
  return res;
382
408
  }
409
+ const knownType = tryGetKnownType(typeName);
410
+ if (knownType)
411
+ return knownType;
383
412
  // console.log("Unknown type: " + typeName);
384
413
 
385
414
  switch (node.kind) {
package/src/test.ts CHANGED
@@ -2,20 +2,27 @@
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
+ //@type test
10
+ texture : RenderTexture;
11
+ }
12
+
13
+ // namespace Hello.World
14
+ // {
15
+ // namespace Deep {
16
+ // export class MyClass extends Behaviour {
17
+ // //@ifdef TEST
18
+ // public myFloat :number;
19
+ // }
20
+ // }
21
+ // }
15
22
 
16
- class OtherClass extends Behaviour {
23
+ // class OtherClass extends Behaviour {
17
24
 
18
- }
25
+ // }
19
26
 
20
27
  //@type (RoomEntity)
21
28
  // 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 {