@needle-tools/needle-component-compiler 2.4.1-pre → 3.0.0-alpha

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.
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runFromFile = exports.handleDeletedFile = exports.CodeTextWriter = exports.MemorySink = exports.FileSink = exports.Visibility = void 0;
4
+ var fs_1 = require("fs");
5
+ var fs = require("fs");
6
+ var commands_1 = require("./commands");
7
+ var Compiler_1 = require("./Compiler");
8
+ var Visibility;
9
+ (function (Visibility) {
10
+ Visibility[Visibility["Public"] = 0] = "Public";
11
+ Visibility[Visibility["Protected"] = 1] = "Protected";
12
+ Visibility[Visibility["Private"] = 2] = "Private";
13
+ })(Visibility = exports.Visibility || (exports.Visibility = {}));
14
+ var FileSink = /** @class */ (function () {
15
+ function FileSink(directory) {
16
+ this.directory = directory;
17
+ }
18
+ FileSink.prototype.flush = function (id, str) {
19
+ if (!fs.existsSync(this.directory)) {
20
+ fs.mkdirSync(this.directory, { recursive: true });
21
+ }
22
+ console.log("Writing " + id + " to " + this.directory);
23
+ var fullPath = this.directory + "/" + id;
24
+ fs.writeFileSync(fullPath, str);
25
+ return fs.realpathSync(fullPath);
26
+ };
27
+ return FileSink;
28
+ }());
29
+ exports.FileSink = FileSink;
30
+ var MemorySink = /** @class */ (function () {
31
+ function MemorySink() {
32
+ this.results = [];
33
+ }
34
+ MemorySink.prototype.clear = function () {
35
+ this.results.length = 0;
36
+ };
37
+ MemorySink.prototype.flush = function (id, str) {
38
+ this.results.push(str);
39
+ return "";
40
+ };
41
+ return MemorySink;
42
+ }());
43
+ exports.MemorySink = MemorySink;
44
+ var CodeTextWriter = /** @class */ (function () {
45
+ function CodeTextWriter() {
46
+ this.indent = 0;
47
+ this.lines = [];
48
+ }
49
+ CodeTextWriter.prototype.beginBlock = function (line) {
50
+ this.writeLine(line);
51
+ this.indent++;
52
+ };
53
+ CodeTextWriter.prototype.endBlock = function (line) {
54
+ // remove the comma from the last line
55
+ if (this.lines.length > 0) {
56
+ var prevLine = this.lines[this.lines.length - 1];
57
+ if (prevLine.endsWith(",")) {
58
+ this.lines[this.lines.length - 1] = prevLine.substring(0, prevLine.length - 1);
59
+ }
60
+ }
61
+ this.indent--;
62
+ this.writeLine(line);
63
+ };
64
+ CodeTextWriter.prototype.writeLine = function (line) {
65
+ var indent = "";
66
+ for (var i = 0; i < this.indent; i++) {
67
+ indent += " ";
68
+ }
69
+ var lines = line.split(/\r?\n/);
70
+ for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
71
+ var line_1 = lines_1[_i];
72
+ // remove whitespace so we can ensure we correctly end with a comma
73
+ this.lines.push(indent + (line_1 === null || line_1 === void 0 ? void 0 : line_1.trim()));
74
+ }
75
+ };
76
+ CodeTextWriter.prototype.write = function (text) {
77
+ var lines = text.split(/\r?\n/);
78
+ if (lines.length > 1) {
79
+ for (var i = 0; i < lines.length; i++) {
80
+ this.writeLine(lines[i]);
81
+ }
82
+ }
83
+ else {
84
+ if (this.lines.length == 0) {
85
+ this.writeLine(text);
86
+ }
87
+ else {
88
+ var lastLine = this.lines[this.lines.length - 1];
89
+ this.lines[this.lines.length - 1] = lastLine + text;
90
+ }
91
+ }
92
+ };
93
+ // beginArray() {
94
+ // this.writeLine("[");
95
+ // this.indent++;
96
+ // }
97
+ // insertArrayItem(item: string) {
98
+ // this.writeLine(item + ",");
99
+ // }
100
+ // endArray() {
101
+ // }
102
+ CodeTextWriter.prototype.flush = function () {
103
+ var str = this.toString();
104
+ this.lines.length = 0;
105
+ return str;
106
+ // if (this.lines.length === 0) return;
107
+ // if (!fs.existsSync(directory)) {
108
+ // fs.mkdirSync(directory, { recursive: true });
109
+ // }
110
+ // console.log("Writing " + filename + " to " + directory);
111
+ // const content = this.toString();
112
+ // this.lines.length = 0;
113
+ // const fullPath = directory + "/" + filename;
114
+ // fs.writeFileSync(fullPath, content);
115
+ // return fs.realpathSync(fullPath);
116
+ };
117
+ CodeTextWriter.prototype.clear = function () {
118
+ this.lines.length = 0;
119
+ };
120
+ CodeTextWriter.prototype.toString = function () {
121
+ return this.lines.join("\r");
122
+ };
123
+ return CodeTextWriter;
124
+ }());
125
+ exports.CodeTextWriter = CodeTextWriter;
126
+ function handleDeletedFile(filePath, types) {
127
+ if (!fs.existsSync(filePath)) {
128
+ var infos = types[filePath];
129
+ if (!infos)
130
+ return;
131
+ for (var i = 0; i < infos.length; i++) {
132
+ var info = infos[i];
133
+ // check if the source file is in any other file
134
+ var found = false;
135
+ for (var otherSourceFile in types) {
136
+ if (otherSourceFile == filePath)
137
+ continue;
138
+ for (var _i = 0, _a = types[otherSourceFile]; _i < _a.length; _i++) {
139
+ var otherInfo = _a[_i];
140
+ if (otherInfo.filePath == info.filePath) {
141
+ found = true;
142
+ break;
143
+ }
144
+ }
145
+ if (found)
146
+ break;
147
+ if (fs.existsSync(info.filePath)) {
148
+ fs.rmSync(info.filePath);
149
+ (0, commands_1.sendFileDeletedCommand)(info.filePath);
150
+ infos.splice(i, 1);
151
+ i--;
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+ exports.handleDeletedFile = handleDeletedFile;
158
+ function runFromFile(writer, path) {
159
+ if (!fs.existsSync(path)) {
160
+ console.error("File not found", path);
161
+ return;
162
+ }
163
+ var code = (0, fs_1.readFileSync)(path).toString();
164
+ var compiler = new Compiler_1.Compiler();
165
+ compiler.compile(writer, code, path);
166
+ return compiler;
167
+ }
168
+ exports.runFromFile = runFromFile;
@@ -0,0 +1,3 @@
1
+ export declare function sendReloadCommand(): void;
2
+ export declare function sendFileWrittenCommand(path: string): void;
3
+ export declare function sendFileDeletedCommand(path: string): void;
@@ -0,0 +1,3 @@
1
+ import * as ts from "typescript";
2
+ export declare function compile(code: string, fileName: string, outputDir: string | null, debugLogs?: boolean): string[];
3
+ export declare function run(program: ts.Program, outputDir: string | null, sourceFile: ts.SourceFile): string[];
@@ -1,4 +1,9 @@
1
1
  "use strict";
2
+ /*
3
+
4
+ V1 csharp compiler
5
+
6
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
8
  exports.run = exports.compile = void 0;
4
9
  var fs_1 = require("fs");
@@ -0,0 +1,14 @@
1
+ import { Visibility } from "../base-compiler";
2
+ import { BaseWriter } from "../BaseWriter";
3
+ export declare class BlenderWriter extends BaseWriter {
4
+ resolveCSharpTypeName(typeName: string, comments?: string[]): string | void;
5
+ startNewType(filePath: string | null, typeName: string, baseTypes: string[], comments?: string[]): void | boolean;
6
+ endNewType(filePath: string | null, typeName: string): void;
7
+ writeMember(visibility: Visibility, name: string, isArray: boolean, type: string, initialValue?: string, comments?: string[]): void;
8
+ writeMethod(visibility: Visibility, name: string, returnType: string | undefined, args: {
9
+ name: string;
10
+ type: string;
11
+ defaultValue?: string;
12
+ }[], comments?: string[]): void;
13
+ writeNewTypeExpression(typeName: string, args?: string[]): void;
14
+ }
@@ -1,33 +1,31 @@
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
+ })();
2
17
  Object.defineProperty(exports, "__esModule", { value: true });
3
18
  exports.BlenderWriter = void 0;
4
- var fs_1 = require("fs");
5
- var base_compiler_1 = require("./base-compiler");
6
- var commands_1 = require("./commands");
19
+ var base_compiler_1 = require("../base-compiler");
20
+ var BaseWriter_1 = require("../BaseWriter");
7
21
  var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn", "vec2", "vec3", "comp"];
8
22
  var knownBaseTypes = ["Behaviour", "IComponent"];
9
- var BlenderWriter = /** @class */ (function () {
10
- function BlenderWriter(_sink) {
11
- this._sink = _sink;
12
- this.writer = new base_compiler_1.CodeTextWriter();
13
- this._createdSchemesPerFile = {};
14
- this._currentlyProcessingFiles = {};
23
+ var BlenderWriter = /** @class */ (function (_super) {
24
+ __extends(BlenderWriter, _super);
25
+ function BlenderWriter() {
26
+ return _super !== null && _super.apply(this, arguments) || this;
15
27
  }
16
- Object.defineProperty(BlenderWriter.prototype, "outputInfo", {
17
- get: function () {
18
- return this._createdSchemesPerFile;
19
- },
20
- enumerable: false,
21
- configurable: true
22
- });
23
- Object.defineProperty(BlenderWriter.prototype, "sink", {
24
- get: function () {
25
- return this._sink;
26
- },
27
- enumerable: false,
28
- configurable: true
29
- });
30
- BlenderWriter.prototype.resolveTypeName = function (typeName, comments) {
28
+ BlenderWriter.prototype.resolveCSharpTypeName = function (typeName, comments) {
31
29
  typeName = typeName.toLocaleLowerCase();
32
30
  if (typeName.startsWith("three."))
33
31
  typeName = typeName.substring("three.".length);
@@ -57,64 +55,47 @@ var BlenderWriter = /** @class */ (function () {
57
55
  case "eventlist":
58
56
  return "evt";
59
57
  case "behaviour":
58
+ case "animation": // Animation component ref
59
+ case "animator": // Animator component ref
60
+ case "camera":
61
+ case "light":
62
+ case "rigidbody":
63
+ case "renderer": // Renderer component ref
64
+ case "canvas": // Canvas component ref
65
+ case "charactercontroller": // CharacterController component ref
66
+ case "videoplayer": // VideoPlayer component ref
67
+ case "lookatconstraint": // LookAtConstraint component ref
60
68
  return "comp";
61
69
  case "texture":
62
70
  return "tex";
63
71
  case "image":
64
72
  return "img";
65
73
  case "object3d":
74
+ case "gameobject": // GameObject = scene object reference
66
75
  return "obj";
76
+ case "vec2":
77
+ return "vec2";
67
78
  case "material":
68
79
  return "mat";
69
80
  case "animationclip":
70
81
  return "anim";
82
+ case "animatorcontroller":
83
+ return "animatorcontroller";
84
+ case "audioclip":
85
+ return "audioclip";
86
+ case "videoclip":
87
+ case "video":
88
+ return "videoclip";
89
+ case "assetreference":
90
+ case "asset":
91
+ return "assetreference";
92
+ case "mesh":
93
+ return "mesh";
94
+ case "timelineasset":
95
+ return "timeline";
71
96
  }
72
97
  return typeName;
73
98
  };
74
- BlenderWriter.prototype.begin = function (filePath) {
75
- if (!this._currentlyProcessingFiles[filePath]) {
76
- this._currentlyProcessingFiles[filePath] = [];
77
- }
78
- };
79
- BlenderWriter.prototype.end = function (filePath) {
80
- var results = this._currentlyProcessingFiles[filePath];
81
- console.log("Writing schemes for", filePath, results);
82
- if (results) {
83
- var previousResultsFromThisFile = this._createdSchemesPerFile[filePath];
84
- if (previousResultsFromThisFile) {
85
- for (var _i = 0, previousResultsFromThisFile_1 = previousResultsFromThisFile; _i < previousResultsFromThisFile_1.length; _i++) {
86
- var previouslyCreated = previousResultsFromThisFile_1[_i];
87
- var foundInAny = false;
88
- for (var _a = 0, results_1 = results; _a < results_1.length; _a++) {
89
- var res = results_1[_a];
90
- if (res.filePath === previouslyCreated.filePath) {
91
- foundInAny = true;
92
- break;
93
- }
94
- }
95
- if (!foundInAny) {
96
- for (var sourcePath in this._createdSchemesPerFile) {
97
- if (foundInAny)
98
- break;
99
- if (sourcePath === filePath)
100
- continue;
101
- var otherSourceSchemes = this._createdSchemesPerFile[sourcePath];
102
- if (otherSourceSchemes.includes(previouslyCreated)) {
103
- // the file was moved to another source file
104
- foundInAny = true;
105
- }
106
- }
107
- }
108
- if (!foundInAny && (0, fs_1.existsSync)(previouslyCreated.filePath)) {
109
- (0, fs_1.unlinkSync)(previouslyCreated.filePath);
110
- (0, commands_1.sendFileDeletedCommand)(previouslyCreated.filePath);
111
- }
112
- }
113
- }
114
- this._createdSchemesPerFile[filePath] = results;
115
- }
116
- delete this._currentlyProcessingFiles[filePath];
117
- };
118
99
  BlenderWriter.prototype.startNewType = function (filePath, typeName, baseTypes, comments) {
119
100
  var isSupportedType = false;
120
101
  for (var i = 0; i < knownBaseTypes.length; i++) {
@@ -139,6 +120,25 @@ var BlenderWriter = /** @class */ (function () {
139
120
  BlenderWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
140
121
  if (visibility !== base_compiler_1.Visibility.Public)
141
122
  return;
123
+ // Check if type is a registered TypeScript enum
124
+ var enumMembers = this._enumRegistry.get(type);
125
+ if (enumMembers && enumMembers.length > 0) {
126
+ this.writer.beginBlock("\"" + name + "\": {");
127
+ this.writer.writeLine("\"type\": \"enum\",");
128
+ // Resolve default value: try to match explicit initializer (e.g. "lighttype.spot")
129
+ var enumValue = enumMembers[0].value;
130
+ if (initialValue && initialValue.includes(".")) {
131
+ var memberKey_1 = initialValue.split(".").pop();
132
+ var found = enumMembers.find(function (m) { return m.name.toLowerCase() === memberKey_1; });
133
+ if (found !== undefined)
134
+ enumValue = found.value;
135
+ }
136
+ this.writer.writeLine("\"value\": " + JSON.stringify(enumValue) + ",");
137
+ var options = enumMembers.map(function (m) { return m.name; });
138
+ this.writer.writeLine("\"options\": " + JSON.stringify(options));
139
+ this.writer.endBlock("},");
140
+ return;
141
+ }
142
142
  // if (!supportedTypes.includes(type)) {
143
143
  // console.log("-- unsupported type:", type);
144
144
  // return;
@@ -173,18 +173,7 @@ var BlenderWriter = /** @class */ (function () {
173
173
  BlenderWriter.prototype.writeNewTypeExpression = function (typeName, args) {
174
174
  console.log("new type:", typeName);
175
175
  };
176
- BlenderWriter.prototype.writeScheme = function (processingFilePath, component) {
177
- var res = this.sink.flush(component + ".component.json", this.writer.flush());
178
- // if an output path is returned it means a file has been written to that path
179
- if (res && (0, fs_1.existsSync)(res)) {
180
- (0, commands_1.sendFileWrittenCommand)(res);
181
- // add the scheme to the list of created schemes
182
- if (processingFilePath && this._currentlyProcessingFiles[processingFilePath]) {
183
- this._currentlyProcessingFiles[processingFilePath].push({ componentName: component, filePath: res });
184
- }
185
- }
186
- };
187
176
  return BlenderWriter;
188
- }());
177
+ }(BaseWriter_1.BaseWriter));
189
178
  exports.BlenderWriter = BlenderWriter;
190
179
  // runFromFile(new BlenderWriter("dist"), "src\\test.ts");
@@ -0,0 +1,16 @@
1
+ import { Argument, Visibility } from "../base-compiler";
2
+ import { BaseWriter } from "../BaseWriter";
3
+ export declare class CSharpWriter extends BaseWriter {
4
+ resolveCSharpTypeName(typescriptTypeName: string): string | void;
5
+ startNewType(filePath: string, typeName: string, baseTypes: string[], comments?: string[]): boolean | void;
6
+ endNewType(filePath: string, typeName: string): void;
7
+ writeMember(visibility: Visibility, name: string, isArray: boolean, type: string, initialValue?: string, comments?: string[]): void;
8
+ writeMethod(visibility: Visibility, name: string, returnType: string, args: Argument[], comments: string[]): void;
9
+ writeNewTypeExpression(typeName: string, args?: string[]): void;
10
+ /** Converts camelCase to PascalCase for method names */
11
+ private toPascalCase;
12
+ /** Resolve a "new Type(args)" expression: map TS types to C# qualified types */
13
+ private resolveNewExpression;
14
+ /** Override writeScheme to use .cs extension instead of .component.json */
15
+ protected writeCSharpScheme(processingFilePath: string | null, component: string): void;
16
+ }