@needle-tools/needle-component-compiler 2.0.0-pre → 2.2.0-pre

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,8 +4,12 @@ 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
- ## [2.0.0-pre] - 2022-11-12
8
- - Add first version of blender scheme compiler
7
+ ## [2.2.0-pre] - 2022-11-13
8
+ - Add: watcher now also generates the register_types file
9
+ - Add: deleting code files now also deletes the generated files and reports it
10
+
11
+ ## [2.1.0-pre] - 2022-11-12
12
+ - Add blender scheme compiler for creating and updating schemes (or removing them when the component gets deleted)
9
13
 
10
14
  ## [1.9.1] - 2022-11-05
11
15
  - Fix ``new Vector2(1, .5)`` generating invalid C# where number arguments were emitted as double instead of float
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "2.0.0-pre",
3
+ "version": "2.2.0-pre",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,7 +28,8 @@
28
28
  "src/base-compiler.ts",
29
29
  "src/blender-compiler.ts",
30
30
  "src/component-compiler.ts",
31
- "src/watcher.ts"
31
+ "src/watcher.ts",
32
+ "src/register-types.ts"
32
33
  ],
33
34
  "ignore": "src/test.ts",
34
35
  "extensions": "ts",
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
- exports.__esModule = true;
3
- exports.Compiler = exports.runFromFile = exports.CodeTextWriter = exports.Visibility = void 0;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Compiler = exports.runFromFile = exports.handleDeletedFile = exports.CodeTextWriter = exports.Visibility = void 0;
4
4
  var fs_1 = require("fs");
5
5
  var ts = require("typescript");
6
6
  var fs = require("fs");
7
+ var commands_1 = require("./commands");
7
8
  var Visibility;
8
9
  (function (Visibility) {
9
10
  Visibility[Visibility["Public"] = 0] = "Public";
@@ -35,11 +36,15 @@ var CodeTextWriter = /** @class */ (function () {
35
36
  for (var i = 0; i < this.indent; i++) {
36
37
  indent += " ";
37
38
  }
38
- // remove whitespace so we can ensure we correctly end with a comma
39
- this.lines.push(indent + (line === null || line === void 0 ? void 0 : line.trim()));
39
+ var lines = line.split(/\r?\n/);
40
+ for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
41
+ var line_1 = lines_1[_i];
42
+ // remove whitespace so we can ensure we correctly end with a comma
43
+ this.lines.push(indent + (line_1 === null || line_1 === void 0 ? void 0 : line_1.trim()));
44
+ }
40
45
  };
41
46
  CodeTextWriter.prototype.write = function (text) {
42
- var lines = text.split("\r");
47
+ var lines = text.split(/\r?\n/);
43
48
  if (lines.length > 1) {
44
49
  for (var i = 0; i < lines.length; i++) {
45
50
  this.writeLine(lines[i]);
@@ -71,15 +76,53 @@ var CodeTextWriter = /** @class */ (function () {
71
76
  fs.mkdirSync(directory, { recursive: true });
72
77
  }
73
78
  console.log("Writing " + filename + " to " + directory);
74
- var content = this.lines.join("\r");
79
+ var content = this.toString();
75
80
  this.lines.length = 0;
76
81
  var fullPath = directory + "/" + filename;
77
82
  fs.writeFileSync(fullPath, content);
78
83
  return fs.realpathSync(fullPath);
79
84
  };
85
+ CodeTextWriter.prototype.clear = function () {
86
+ this.lines.length = 0;
87
+ };
88
+ CodeTextWriter.prototype.toString = function () {
89
+ return this.lines.join("\r");
90
+ };
80
91
  return CodeTextWriter;
81
92
  }());
82
93
  exports.CodeTextWriter = CodeTextWriter;
94
+ function handleDeletedFile(filePath, types) {
95
+ if (!fs.existsSync(filePath)) {
96
+ var infos = types[filePath];
97
+ if (!infos)
98
+ return;
99
+ for (var i = 0; i < infos.length; i++) {
100
+ var info = infos[i];
101
+ // check if the source file is in any other file
102
+ var found = false;
103
+ for (var otherSourceFile in types) {
104
+ if (otherSourceFile == filePath)
105
+ continue;
106
+ for (var _i = 0, _a = types[otherSourceFile]; _i < _a.length; _i++) {
107
+ var otherInfo = _a[_i];
108
+ if (otherInfo.filePath == info.filePath) {
109
+ found = true;
110
+ break;
111
+ }
112
+ }
113
+ if (found)
114
+ break;
115
+ if (fs.existsSync(info.filePath)) {
116
+ fs.rmSync(info.filePath);
117
+ (0, commands_1.sendFileDeletedCommand)(info.filePath);
118
+ infos.splice(i, 1);
119
+ i--;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ exports.handleDeletedFile = handleDeletedFile;
83
126
  function runFromFile(writer, path) {
84
127
  if (!fs.existsSync(path)) {
85
128
  console.error("File not found", path);
@@ -1,17 +1,31 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BlenderWriter = void 0;
4
4
  var fs_1 = require("fs");
5
5
  var base_compiler_1 = require("./base-compiler");
6
6
  var commands_1 = require("./commands");
7
- var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn"];
7
+ var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn", "vec2", "vec3"];
8
8
  var BlenderWriter = /** @class */ (function () {
9
9
  function BlenderWriter(outputDirectory) {
10
10
  this.writer = new base_compiler_1.CodeTextWriter();
11
- this.createdSchemesPerFile = {};
12
- this.currentlyProcessingFiles = {};
13
- this.outputDirectory = outputDirectory;
11
+ this._createdSchemesPerFile = {};
12
+ this._currentlyProcessingFiles = {};
13
+ this._outputDirectory = outputDirectory;
14
14
  }
15
+ Object.defineProperty(BlenderWriter.prototype, "outputInfo", {
16
+ get: function () {
17
+ return this._createdSchemesPerFile;
18
+ },
19
+ enumerable: false,
20
+ configurable: true
21
+ });
22
+ Object.defineProperty(BlenderWriter.prototype, "outputDirectory", {
23
+ get: function () {
24
+ return this._outputDirectory;
25
+ },
26
+ enumerable: false,
27
+ configurable: true
28
+ });
15
29
  BlenderWriter.prototype.resolveTypeName = function (typeName, comments) {
16
30
  typeName = typeName.toLocaleLowerCase();
17
31
  if (typeName.startsWith("three."))
@@ -27,56 +41,67 @@ var BlenderWriter = /** @class */ (function () {
27
41
  return "string";
28
42
  case "void":
29
43
  return "void";
30
- case "any":
31
- return "any";
32
44
  case "vector2":
33
45
  return "vec2";
46
+ case "vector3":
47
+ return "vec3";
48
+ case "vector4":
49
+ return "vec4";
50
+ case "quaternion":
51
+ return "quat";
52
+ case "euler":
53
+ return "vec3";
34
54
  case "color":
35
55
  return "col";
36
- case "renderer":
37
- return "comp";
38
56
  case "eventlist":
39
57
  return "evt";
58
+ case "renderer":
59
+ return "comp";
40
60
  }
41
61
  };
42
62
  BlenderWriter.prototype.begin = function (filePath) {
43
- if (!this.currentlyProcessingFiles[filePath]) {
44
- this.currentlyProcessingFiles[filePath] = [];
63
+ if (!this._currentlyProcessingFiles[filePath]) {
64
+ this._currentlyProcessingFiles[filePath] = [];
45
65
  }
46
- // this.writer.beginBlock("{")
47
66
  };
48
67
  BlenderWriter.prototype.end = function (filePath) {
49
- var results = this.currentlyProcessingFiles[filePath];
68
+ var results = this._currentlyProcessingFiles[filePath];
50
69
  console.log("Writing schemes for", filePath, results);
51
70
  if (results) {
52
- var previousResultsFromThisFile = this.createdSchemesPerFile[filePath];
71
+ var previousResultsFromThisFile = this._createdSchemesPerFile[filePath];
53
72
  if (previousResultsFromThisFile) {
54
73
  for (var _i = 0, previousResultsFromThisFile_1 = previousResultsFromThisFile; _i < previousResultsFromThisFile_1.length; _i++) {
55
74
  var previouslyCreated = previousResultsFromThisFile_1[_i];
56
- var foundInAny = results.includes(previouslyCreated);
75
+ var foundInAny = false;
76
+ for (var _a = 0, results_1 = results; _a < results_1.length; _a++) {
77
+ var res = results_1[_a];
78
+ if (res.filePath === previouslyCreated.filePath) {
79
+ foundInAny = true;
80
+ break;
81
+ }
82
+ }
57
83
  if (!foundInAny) {
58
- for (var sourcePath in this.createdSchemesPerFile) {
84
+ for (var sourcePath in this._createdSchemesPerFile) {
59
85
  if (foundInAny)
60
86
  break;
61
87
  if (sourcePath === filePath)
62
88
  continue;
63
- var otherSourceSchemes = this.createdSchemesPerFile[sourcePath];
89
+ var otherSourceSchemes = this._createdSchemesPerFile[sourcePath];
64
90
  if (otherSourceSchemes.includes(previouslyCreated)) {
65
91
  // the file was moved to another source file
66
92
  foundInAny = true;
67
93
  }
68
94
  }
69
95
  }
70
- if (!foundInAny) {
71
- (0, fs_1.unlinkSync)(previouslyCreated);
96
+ if (!foundInAny && (0, fs_1.existsSync)(previouslyCreated.filePath)) {
97
+ (0, fs_1.unlinkSync)(previouslyCreated.filePath);
98
+ (0, commands_1.sendFileDeletedCommand)(previouslyCreated.filePath);
72
99
  }
73
100
  }
74
101
  }
75
- this.createdSchemesPerFile[filePath] = results;
102
+ this._createdSchemesPerFile[filePath] = results;
76
103
  }
77
- delete this.currentlyProcessingFiles[filePath];
78
- // this.writer.endBlock("}");
79
- // this.writer.flush(this.outputFilePath);
104
+ delete this._currentlyProcessingFiles[filePath];
80
105
  };
81
106
  BlenderWriter.prototype.startNewType = function (filePath, typeName, baseType, comments) {
82
107
  if (baseType !== "Behaviour")
@@ -92,6 +117,8 @@ var BlenderWriter = /** @class */ (function () {
92
117
  this.writeScheme(filePath, typeName);
93
118
  };
94
119
  BlenderWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
120
+ if (visibility !== base_compiler_1.Visibility.Public)
121
+ return;
95
122
  if (!supportedTypes.includes(type)) {
96
123
  console.log("-- unsupported type:", type);
97
124
  return;
@@ -117,6 +144,8 @@ var BlenderWriter = /** @class */ (function () {
117
144
  this.writer.endBlock("},");
118
145
  };
119
146
  BlenderWriter.prototype.writeMethod = function (visibility, name, returnType, args, comments) {
147
+ if (visibility !== base_compiler_1.Visibility.Public)
148
+ return;
120
149
  this.writer.beginBlock("\"" + name + "\": {");
121
150
  this.writer.writeLine("\"type\": \"fn\",");
122
151
  this.writer.endBlock("},");
@@ -125,13 +154,13 @@ var BlenderWriter = /** @class */ (function () {
125
154
  console.log("new type:", typeName);
126
155
  };
127
156
  BlenderWriter.prototype.writeScheme = function (processingFilePath, component) {
128
- var outputPath = this.writer.flush(this.outputDirectory, component + ".component.json");
157
+ var outputPath = this.writer.flush(this._outputDirectory, component + ".component.json");
129
158
  // if an output path is returned it means a file has been written to that path
130
159
  if (outputPath) {
131
160
  (0, commands_1.sendFileWrittenCommand)(outputPath);
132
161
  // add the scheme to the list of created schemes
133
- if (processingFilePath && this.currentlyProcessingFiles[processingFilePath]) {
134
- this.currentlyProcessingFiles[processingFilePath].push(outputPath);
162
+ if (processingFilePath && this._currentlyProcessingFiles[processingFilePath]) {
163
+ this._currentlyProcessingFiles[processingFilePath].push({ componentName: component, filePath: outputPath });
135
164
  }
136
165
  }
137
166
  };
package/src/commands.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
- exports.__esModule = true;
3
- exports.sendFileWrittenCommand = exports.sendReloadCommand = void 0;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sendFileDeletedCommand = exports.sendFileWrittenCommand = exports.sendReloadCommand = void 0;
4
4
  function sendReloadCommand() {
5
5
  process.stdout.write("CMD:reload\r");
6
6
  }
@@ -9,3 +9,7 @@ function sendFileWrittenCommand(path) {
9
9
  process.stdout.write("CMD:writefile " + path + "\r");
10
10
  }
11
11
  exports.sendFileWrittenCommand = sendFileWrittenCommand;
12
+ function sendFileDeletedCommand(path) {
13
+ process.stdout.write("CMD:deletedfile " + path + "\r");
14
+ }
15
+ exports.sendFileDeletedCommand = sendFileDeletedCommand;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.run = exports.compile = void 0;
4
4
  var fs_1 = require("fs");
5
5
  var ts = require("typescript");
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeTypeRegistry = void 0;
4
+ var fs_1 = require("fs");
5
+ var path = require("path");
6
+ var base_compiler_1 = require("./base-compiler");
7
+ function writeTypeRegistry(types, registerTypesPath) {
8
+ console.log("Writing type registry to", registerTypesPath);
9
+ var directory = path.dirname(registerTypesPath);
10
+ if (!(0, fs_1.existsSync)(directory)) {
11
+ (0, fs_1.mkdirSync)(directory);
12
+ }
13
+ var file = (0, fs_1.createWriteStream)(registerTypesPath);
14
+ var writer = new base_compiler_1.CodeTextWriter();
15
+ writer.writeLine("// This is an auto-generated file. Do not edit!\n");
16
+ writer.writeLine("import { TypeStore } from \"@needle-tools/engine\";\n");
17
+ for (var sourceFile in types) {
18
+ if (!(0, fs_1.existsSync)(sourceFile))
19
+ continue;
20
+ var generatedFile = types[sourceFile];
21
+ var relativePath = path.relative(directory, sourceFile).replace(/\\/g, "/");
22
+ for (var _i = 0, generatedFile_1 = generatedFile; _i < generatedFile_1.length; _i++) {
23
+ var fileInfo = generatedFile_1[_i];
24
+ writer.writeLine("import { ".concat(fileInfo.componentName, " } from \"").concat(relativePath, "\";"));
25
+ writer.writeLine("TypeStore.add(\"".concat(fileInfo.componentName, "\", ").concat(fileInfo.componentName, ");"));
26
+ }
27
+ }
28
+ writer.writeLine("\n// Made with 🌵 Needle");
29
+ writer.writeLine("\n// www.needle.tools");
30
+ var code = writer.toString();
31
+ file.write(code);
32
+ file.end();
33
+ }
34
+ exports.writeTypeRegistry = writeTypeRegistry;
package/src/test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //@type("MyComponent")
4
4
  // export class CameraView extends Behaviour {
5
5
  // static views: CameraView[] = [];
package/src/watcher.js CHANGED
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DirectoryWatcher = void 0;
4
4
  var fs = require("fs");
5
5
  var base_compiler_1 = require("./base-compiler");
6
6
  var blender_compiler_1 = require("./blender-compiler");
7
7
  var chokidar = require("chokidar");
8
8
  var commands_1 = require("./commands");
9
+ var register_types_1 = require("./register-types");
9
10
  // https://github.com/paulmillr/chokidar
10
11
  var DirectoryWatcher = /** @class */ (function () {
11
12
  function DirectoryWatcher() {
12
13
  this.compiler = new base_compiler_1.Compiler();
13
- this._needReload = false;
14
+ this._typesChanged = false;
14
15
  }
15
- DirectoryWatcher.prototype.startWatching = function (dir, writer) {
16
+ DirectoryWatcher.prototype.startWatching = function (dir, writer, registerTypesPath) {
16
17
  var _this = this;
17
18
  // console.log("Start watching", dir);
18
19
  chokidar.watch(dir + "/**/*.ts").on('all', function (event, path) {
@@ -21,15 +22,17 @@ var DirectoryWatcher = /** @class */ (function () {
21
22
  case "add":
22
23
  console.log("File", path, "has been added");
23
24
  _this.compiler.compile(writer, fs.readFileSync(path).toString(), path);
24
- _this._needReload = true;
25
+ _this._typesChanged = true;
25
26
  break;
26
27
  case "change":
27
28
  console.log("File", path, "has been changed");
28
29
  _this.compiler.compile(writer, fs.readFileSync(path).toString(), path);
29
- _this._needReload = true;
30
+ _this._typesChanged = true;
30
31
  break;
31
32
  case "unlink":
32
33
  console.log("File", path, "has been removed");
34
+ (0, base_compiler_1.handleDeletedFile)(path, writer.outputInfo);
35
+ _this._typesChanged = true;
33
36
  break;
34
37
  default:
35
38
  console.log("File unhandled event", event, path);
@@ -41,9 +44,10 @@ var DirectoryWatcher = /** @class */ (function () {
41
44
  });
42
45
  // call reload cmd
43
46
  setInterval(function () {
44
- if (_this._needReload) {
45
- _this._needReload = false;
47
+ if (_this._typesChanged) {
48
+ _this._typesChanged = false;
46
49
  (0, commands_1.sendReloadCommand)();
50
+ (0, register_types_1.writeTypeRegistry)(writer.outputInfo, registerTypesPath);
47
51
  }
48
52
  }, 2000);
49
53
  };
@@ -55,18 +59,19 @@ process.on('exit', function () {
55
59
  // console.log('Bye node');
56
60
  });
57
61
  var args = process.argv.slice(2);
58
- if (args.length < 2) {
59
- console.error("Missing arguments, usage: node watcher.js <directory> <output>");
62
+ if (args.length < 3) {
63
+ console.error("Missing arguments, usage: node watcher.js <directory> <output> <registerTypesPath>");
60
64
  process.exit(1);
61
65
  }
62
66
  var directoryToWatch = args[0].replace("\"", "");
63
67
  var outputDirectory = args[1].replace("\"", "");
68
+ var registerTypesPath = args[2].replace("\"", "");
64
69
  if (!fs.existsSync(directoryToWatch)) {
65
70
  console.error("Directory to watch does not exist");
66
71
  process.exit(1);
67
72
  }
68
73
  if (fs.existsSync(outputDirectory))
69
- fs.rmdirSync(outputDirectory, { recursive: true });
74
+ fs.rmSync(outputDirectory, { recursive: true });
70
75
  console.log("Watch: " + directoryToWatch + " and output to: " + outputDirectory);
71
76
  var watcher = new DirectoryWatcher();
72
- watcher.startWatching(directoryToWatch, new blender_compiler_1.BlenderWriter(outputDirectory));
77
+ watcher.startWatching(directoryToWatch, new blender_compiler_1.BlenderWriter(outputDirectory), registerTypesPath);
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var helpers_1 = require("./helpers");
4
4
  describe('Basic typescript', function () {
5
5
  it('should generate component', function () {
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var helpers_1 = require("./helpers");
4
4
  describe('Typescript with public methods', function () {
5
5
  it('should generate component with public method', function () {
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var helpers_1 = require("./helpers");
4
4
  describe('Typescript with nonSerialized', function () {
5
5
  it('should ignore field', function () {
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var helpers_1 = require("./helpers");
4
4
  describe('Typescript with fields', function () {
5
5
  it('should generate component with number', function () {
package/test/helpers.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.compareCodegenWithDefaultContext = exports.compareCodegen = exports.testCompile = void 0;
4
4
  var component_compiler_1 = require("../src/component-compiler");
5
5
  var chai_1 = require("chai");
package/tsconfig.json CHANGED
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "target": "es5",
3
4
  "types": [
4
5
  "mocha",
5
6
  "node"