@needle-tools/needle-component-compiler 1.9.2-exp → 2.1.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,8 @@ 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.2-exp] - 2022-11-12
8
- - Add first version of blender scheme compiler
7
+ ## [2.1.0-pre] - 2022-11-12
8
+ - Add blender scheme compiler for creating and updating schemes (or removing them when the component gets deleted)
9
9
 
10
10
  ## [1.9.1] - 2022-11-05
11
11
  - 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": "1.9.2-exp",
3
+ "version": "2.1.0-pre",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -55,15 +55,15 @@ var CodeTextWriter = /** @class */ (function () {
55
55
  }
56
56
  }
57
57
  };
58
- CodeTextWriter.prototype.beginArray = function () {
59
- this.writeLine("[");
60
- this.indent++;
61
- };
62
- CodeTextWriter.prototype.insertArrayItem = function (item) {
63
- this.writeLine(item + ",");
64
- };
65
- CodeTextWriter.prototype.endArray = function () {
66
- };
58
+ // beginArray() {
59
+ // this.writeLine("[");
60
+ // this.indent++;
61
+ // }
62
+ // insertArrayItem(item: string) {
63
+ // this.writeLine(item + ",");
64
+ // }
65
+ // endArray() {
66
+ // }
67
67
  CodeTextWriter.prototype.flush = function (directory, filename) {
68
68
  if (this.lines.length === 0)
69
69
  return;
@@ -87,31 +87,32 @@ function runFromFile(writer, path) {
87
87
  }
88
88
  var code = (0, fs_1.readFileSync)(path).toString();
89
89
  var compiler = new Compiler();
90
- compiler.compile(writer, code);
90
+ compiler.compile(writer, code, path);
91
91
  return compiler;
92
92
  }
93
93
  exports.runFromFile = runFromFile;
94
94
  var Compiler = /** @class */ (function () {
95
95
  function Compiler() {
96
96
  }
97
- Compiler.prototype.compile = function (writer, code) {
97
+ Compiler.prototype.compile = function (writer, code, sourceFilePath) {
98
98
  var file = "needle_compiled.ts";
99
99
  var sourceFile = ts.createSourceFile(file, code, ts.ScriptTarget.ES2015, true, /*setParentNodes */ ts.ScriptKind.TS);
100
100
  var prog = ts.createProgram([file], {});
101
- this.run(prog, writer, sourceFile);
101
+ this.run(prog, writer, sourceFile, sourceFilePath);
102
102
  };
103
- Compiler.prototype.run = function (prog, writer, sourceFile) {
103
+ Compiler.prototype.run = function (prog, writer, sourceFile, filePath) {
104
104
  var _this = this;
105
- writer.begin();
105
+ console.log("Starting compilation of " + filePath);
106
+ writer.begin(filePath);
106
107
  ts.forEachChild(sourceFile, function (node) {
107
- _this.visit(node, writer);
108
+ _this.visit(filePath, node, writer);
108
109
  });
109
- writer.end();
110
+ writer.end(filePath);
110
111
  };
111
- Compiler.prototype.visit = function (node, writer) {
112
+ Compiler.prototype.visit = function (filePath, node, writer) {
112
113
  var _this = this;
113
114
  if (ts.isClassDeclaration(node)) {
114
- this.visitClassDeclaration(node, writer);
115
+ this.visitClassDeclaration(filePath, node, writer);
115
116
  }
116
117
  else if (ts.isPropertyDeclaration(node)) {
117
118
  this.visitPropertyDeclaration(node, writer);
@@ -121,24 +122,24 @@ var Compiler = /** @class */ (function () {
121
122
  }
122
123
  else {
123
124
  ts.forEachChild(node, function (node) {
124
- _this.visit(node, writer);
125
+ _this.visit(filePath, node, writer);
125
126
  });
126
127
  }
127
128
  };
128
- Compiler.prototype.visitClassDeclaration = function (node, writer) {
129
+ Compiler.prototype.visitClassDeclaration = function (filePath, node, writer) {
129
130
  var _this = this;
130
131
  var name = node.name.text;
131
132
  var base = node.heritageClauses && node.heritageClauses[0].types[0].expression.getText();
132
133
  this.debugLog("CLASS START", name, base);
133
- var res = writer.startNewType(name, base, this.getComments(node));
134
+ var res = writer.startNewType(filePath, name, base, this.getComments(node));
134
135
  if (res === false) {
135
136
  this.debugLog("CLASS SKIPPED", name);
136
137
  return;
137
138
  }
138
139
  ts.forEachChild(node, function (node) {
139
- _this.visit(node, writer);
140
+ _this.visit(filePath, node, writer);
140
141
  });
141
- writer.endNewType(name);
142
+ writer.endNewType(filePath, name);
142
143
  this.debugLog("CLASS END", name, "\n");
143
144
  };
144
145
  Compiler.prototype.visitPropertyDeclaration = function (node, writer) {
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  exports.__esModule = true;
3
3
  exports.BlenderWriter = void 0;
4
+ var fs_1 = require("fs");
4
5
  var base_compiler_1 = require("./base-compiler");
5
6
  var commands_1 = require("./commands");
6
7
  var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn"];
7
8
  var BlenderWriter = /** @class */ (function () {
8
9
  function BlenderWriter(outputDirectory) {
9
10
  this.writer = new base_compiler_1.CodeTextWriter();
11
+ this.createdSchemesPerFile = {};
12
+ this.currentlyProcessingFiles = {};
10
13
  this.outputDirectory = outputDirectory;
11
14
  }
12
15
  BlenderWriter.prototype.resolveTypeName = function (typeName, comments) {
@@ -36,14 +39,47 @@ var BlenderWriter = /** @class */ (function () {
36
39
  return "evt";
37
40
  }
38
41
  };
39
- BlenderWriter.prototype.begin = function () {
42
+ BlenderWriter.prototype.begin = function (filePath) {
43
+ if (!this.currentlyProcessingFiles[filePath]) {
44
+ this.currentlyProcessingFiles[filePath] = [];
45
+ }
40
46
  // this.writer.beginBlock("{")
41
47
  };
42
- BlenderWriter.prototype.end = function () {
48
+ BlenderWriter.prototype.end = function (filePath) {
49
+ var results = this.currentlyProcessingFiles[filePath];
50
+ console.log("Writing schemes for", filePath, results);
51
+ if (results) {
52
+ var previousResultsFromThisFile = this.createdSchemesPerFile[filePath];
53
+ if (previousResultsFromThisFile) {
54
+ for (var _i = 0, previousResultsFromThisFile_1 = previousResultsFromThisFile; _i < previousResultsFromThisFile_1.length; _i++) {
55
+ var previouslyCreated = previousResultsFromThisFile_1[_i];
56
+ var foundInAny = results.includes(previouslyCreated);
57
+ if (!foundInAny) {
58
+ for (var sourcePath in this.createdSchemesPerFile) {
59
+ if (foundInAny)
60
+ break;
61
+ if (sourcePath === filePath)
62
+ continue;
63
+ var otherSourceSchemes = this.createdSchemesPerFile[sourcePath];
64
+ if (otherSourceSchemes.includes(previouslyCreated)) {
65
+ // the file was moved to another source file
66
+ foundInAny = true;
67
+ }
68
+ }
69
+ }
70
+ if (!foundInAny && (0, fs_1.existsSync)(previouslyCreated)) {
71
+ (0, fs_1.unlinkSync)(previouslyCreated);
72
+ (0, commands_1.sendFileDeletedCommand)(previouslyCreated);
73
+ }
74
+ }
75
+ }
76
+ this.createdSchemesPerFile[filePath] = results;
77
+ }
78
+ delete this.currentlyProcessingFiles[filePath];
43
79
  // this.writer.endBlock("}");
44
80
  // this.writer.flush(this.outputFilePath);
45
81
  };
46
- BlenderWriter.prototype.startNewType = function (typeName, baseType, comments) {
82
+ BlenderWriter.prototype.startNewType = function (filePath, typeName, baseType, comments) {
47
83
  if (baseType !== "Behaviour")
48
84
  return false;
49
85
  this.writer.beginBlock("{");
@@ -51,10 +87,10 @@ var BlenderWriter = /** @class */ (function () {
51
87
  console.log(comments);
52
88
  this.writer.beginBlock("\"" + typeName + "\": {");
53
89
  };
54
- BlenderWriter.prototype.endNewType = function (typeName) {
90
+ BlenderWriter.prototype.endNewType = function (filePath, typeName) {
55
91
  this.writer.endBlock("},");
56
92
  this.writer.endBlock("}");
57
- this.writeScheme(typeName);
93
+ this.writeScheme(filePath, typeName);
58
94
  };
59
95
  BlenderWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
60
96
  if (!supportedTypes.includes(type)) {
@@ -89,10 +125,16 @@ var BlenderWriter = /** @class */ (function () {
89
125
  BlenderWriter.prototype.writeNewTypeExpression = function (typeName, args) {
90
126
  console.log("new type:", typeName);
91
127
  };
92
- BlenderWriter.prototype.writeScheme = function (component) {
128
+ BlenderWriter.prototype.writeScheme = function (processingFilePath, component) {
93
129
  var outputPath = this.writer.flush(this.outputDirectory, component + ".component.json");
94
- if (outputPath)
130
+ // if an output path is returned it means a file has been written to that path
131
+ if (outputPath) {
95
132
  (0, commands_1.sendFileWrittenCommand)(outputPath);
133
+ // add the scheme to the list of created schemes
134
+ if (processingFilePath && this.currentlyProcessingFiles[processingFilePath]) {
135
+ this.currentlyProcessingFiles[processingFilePath].push(outputPath);
136
+ }
137
+ }
96
138
  };
97
139
  return BlenderWriter;
98
140
  }());
package/src/commands.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  exports.__esModule = true;
3
- exports.sendFileWrittenCommand = exports.sendReloadCommand = void 0;
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;
package/src/test.js CHANGED
@@ -1,34 +1,5 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  exports.__esModule = true;
18
- exports.MyTestComponent = void 0;
19
- var MyTestComponent = /** @class */ (function (_super) {
20
- __extends(MyTestComponent, _super);
21
- function MyTestComponent() {
22
- var _this = _super !== null && _super.apply(this, arguments) || this;
23
- _this.myVector2 = new Vector2(1, .5);
24
- _this.myNumber = 1;
25
- return _this;
26
- }
27
- MyTestComponent.prototype.myMethod = function () {
28
- };
29
- return MyTestComponent;
30
- }(Behaviour));
31
- exports.MyTestComponent = MyTestComponent;
32
3
  //@type("MyComponent")
33
4
  // export class CameraView extends Behaviour {
34
5
  // static views: CameraView[] = [];
package/src/watcher.js CHANGED
@@ -20,10 +20,12 @@ var DirectoryWatcher = /** @class */ (function () {
20
20
  switch (event) {
21
21
  case "add":
22
22
  console.log("File", path, "has been added");
23
+ _this.compiler.compile(writer, fs.readFileSync(path).toString(), path);
24
+ _this._needReload = true;
23
25
  break;
24
26
  case "change":
25
27
  console.log("File", path, "has been changed");
26
- _this.compiler.compile(writer, fs.readFileSync(path).toString());
28
+ _this.compiler.compile(writer, fs.readFileSync(path).toString(), path);
27
29
  _this._needReload = true;
28
30
  break;
29
31
  case "unlink":
@@ -50,7 +52,7 @@ var DirectoryWatcher = /** @class */ (function () {
50
52
  exports.DirectoryWatcher = DirectoryWatcher;
51
53
  // listen to process exit
52
54
  process.on('exit', function () {
53
- console.log('Bye node');
55
+ // console.log('Bye node');
54
56
  });
55
57
  var args = process.argv.slice(2);
56
58
  if (args.length < 2) {
@@ -59,6 +61,12 @@ if (args.length < 2) {
59
61
  }
60
62
  var directoryToWatch = args[0].replace("\"", "");
61
63
  var outputDirectory = args[1].replace("\"", "");
64
+ if (!fs.existsSync(directoryToWatch)) {
65
+ console.error("Directory to watch does not exist");
66
+ process.exit(1);
67
+ }
68
+ if (fs.existsSync(outputDirectory))
69
+ fs.rmdirSync(outputDirectory, { recursive: true });
62
70
  console.log("Watch: " + directoryToWatch + " and output to: " + outputDirectory);
63
71
  var watcher = new DirectoryWatcher();
64
72
  watcher.startWatching(directoryToWatch, new blender_compiler_1.BlenderWriter(outputDirectory));