@needle-tools/needle-component-compiler 1.3.0 → 1.4.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 +4 -0
- package/dist/MyClass.cs +6 -0
- package/dist/OtherClass.cs +7 -0
- package/package.json +1 -1
- package/src/component-compiler.js +36 -6
- package/src/component-compiler.ts +40 -7
- package/src/test.ts +2 -1
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.3.0] - 2022-07-06
|
|
8
|
+
- add CODEGEN_START and END sections to allow adding code before or after a generated componnet
|
|
9
|
+
- add ``@ifdef`` to ifdef fields
|
|
10
|
+
|
|
7
11
|
## [1.3.0] - 2022-06-27
|
|
8
12
|
- add support for typescript namespace
|
|
9
13
|
|
package/dist/MyClass.cs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// NEEDLE_CODEGEN_START
|
|
1
2
|
// auto generated code - do not edit directly
|
|
2
3
|
|
|
3
4
|
#pragma warning disable
|
|
@@ -6,5 +7,10 @@ namespace Hello.World.Deep
|
|
|
6
7
|
{
|
|
7
8
|
public partial class MyClass : UnityEngine.MonoBehaviour
|
|
8
9
|
{
|
|
10
|
+
#ifdef TEST
|
|
11
|
+
public float @myFloat;
|
|
12
|
+
#endif
|
|
9
13
|
}
|
|
10
14
|
}
|
|
15
|
+
|
|
16
|
+
// NEEDLE_CODEGEN_END
|
package/dist/OtherClass.cs
CHANGED
package/package.json
CHANGED
|
@@ -12,6 +12,9 @@ var dontExportNextClassCommand = "@dont-generate-component";
|
|
|
12
12
|
// add above field to add [SerializeField] attribute
|
|
13
13
|
var serializeCommand = "@serializeField";
|
|
14
14
|
var typePattern = new RegExp("@type ?\((?<type>.+)\)");
|
|
15
|
+
var ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)");
|
|
16
|
+
var CODEGEN_MARKER_START = "// NEEDLE_CODEGEN_START";
|
|
17
|
+
var CODEGEN_MARKER_END = "// NEEDLE_CODEGEN_END";
|
|
15
18
|
// will be set to true when e.g. a comment for export is found
|
|
16
19
|
var exportNextClass = false;
|
|
17
20
|
var dontExportNextClass = false;
|
|
@@ -51,10 +54,8 @@ var ExportContext = /** @class */ (function () {
|
|
|
51
54
|
if (this.textBuffer.length <= 0)
|
|
52
55
|
return;
|
|
53
56
|
var dir = this.outputDir + "/";
|
|
54
|
-
if (!fs.existsSync(dir)) {
|
|
55
|
-
fs.mkdirSync(dir);
|
|
56
|
-
}
|
|
57
57
|
var path = dir + this.fileName;
|
|
58
|
+
this.replaceGeneratedCodeSection(path);
|
|
58
59
|
console.log("Write to " + path);
|
|
59
60
|
fs.writeFileSync(path, this.textBuffer);
|
|
60
61
|
this.reset();
|
|
@@ -63,10 +64,25 @@ var ExportContext = /** @class */ (function () {
|
|
|
63
64
|
this.textBuffer = "";
|
|
64
65
|
this.classEnd = -1;
|
|
65
66
|
};
|
|
67
|
+
ExportContext.prototype.replaceGeneratedCodeSection = function (path) {
|
|
68
|
+
this.textBuffer = CODEGEN_MARKER_START + "\n" + this.textBuffer + "\n" + CODEGEN_MARKER_END;
|
|
69
|
+
if (fs.existsSync(path)) {
|
|
70
|
+
var existing = fs.readFileSync(path, "utf8");
|
|
71
|
+
var regex = new RegExp("(?<before>.*?)\/\/ ?NEEDLE_CODEGEN_START.+\/\/ ?NEEDLE_CODEGEN_END(?<after>.*)", "s");
|
|
72
|
+
var matches = regex.exec(existing);
|
|
73
|
+
if (matches === null || matches === void 0 ? void 0 : matches.groups) {
|
|
74
|
+
console.log("Found codegen sections");
|
|
75
|
+
var before = matches.groups.before;
|
|
76
|
+
var after = matches.groups.after;
|
|
77
|
+
this.textBuffer = before + this.textBuffer + after;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
66
81
|
return ExportContext;
|
|
67
82
|
}());
|
|
68
83
|
var contexts = [];
|
|
69
84
|
var lastTypeFound = null;
|
|
85
|
+
var ifdefSections = [];
|
|
70
86
|
function run(program, outputDir, sourceFile) {
|
|
71
87
|
if (!fs.existsSync(outputDir)) {
|
|
72
88
|
console.error("Output directory does not exist: \"" + outputDir + "\"");
|
|
@@ -114,15 +130,21 @@ function run(program, outputDir, sourceFile) {
|
|
|
114
130
|
exportNextClass = true;
|
|
115
131
|
if (comment.includes(dontExportNextClassCommand))
|
|
116
132
|
dontExportNextClass = true;
|
|
117
|
-
var
|
|
118
|
-
if (
|
|
133
|
+
var typeMatch = typePattern.exec(comment);
|
|
134
|
+
if (typeMatch && typeMatch.groups) {
|
|
119
135
|
// for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
|
|
120
136
|
// so we remove them
|
|
121
|
-
var type =
|
|
137
|
+
var type = typeMatch.groups["type"];
|
|
122
138
|
type = type.replace(/\(/, "").replace(/\)/, "");
|
|
123
139
|
console.log("found type: ", type);
|
|
124
140
|
lastTypeFound = type;
|
|
125
141
|
}
|
|
142
|
+
var ifdefMatch = ifdefPattern.exec(comment);
|
|
143
|
+
if (ifdefMatch && ifdefMatch.groups) {
|
|
144
|
+
var ifdef = ifdefMatch.groups["ifdef"];
|
|
145
|
+
if (ifdef)
|
|
146
|
+
ifdefSections.push(ifdef);
|
|
147
|
+
}
|
|
126
148
|
}
|
|
127
149
|
}
|
|
128
150
|
switch (node.kind) {
|
|
@@ -217,8 +239,16 @@ function run(program, outputDir, sourceFile) {
|
|
|
217
239
|
console.log("SERIALIZE");
|
|
218
240
|
context.appendLine("[UnityEngine.SerializeField]");
|
|
219
241
|
}
|
|
242
|
+
var requireEndIf = false;
|
|
243
|
+
if (ifdefSections.length > 0) {
|
|
244
|
+
requireEndIf = true;
|
|
245
|
+
context.appendLine("#ifdef " + ifdefSections.pop());
|
|
246
|
+
}
|
|
220
247
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";\n");
|
|
221
248
|
lastTypeFound = null;
|
|
249
|
+
if (requireEndIf) {
|
|
250
|
+
context.appendLine("#endif");
|
|
251
|
+
}
|
|
222
252
|
break;
|
|
223
253
|
case ts.SyntaxKind.ClassDeclaration:
|
|
224
254
|
serializeField = false;
|
|
@@ -13,6 +13,10 @@ const dontExportNextClassCommand = "@dont-generate-component";
|
|
|
13
13
|
// add above field to add [SerializeField] attribute
|
|
14
14
|
const serializeCommand = "@serializeField";
|
|
15
15
|
const typePattern = new RegExp("@type ?\((?<type>.+)\)");
|
|
16
|
+
const ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)")
|
|
17
|
+
|
|
18
|
+
const CODEGEN_MARKER_START = "// NEEDLE_CODEGEN_START";
|
|
19
|
+
const CODEGEN_MARKER_END = "// NEEDLE_CODEGEN_END";
|
|
16
20
|
|
|
17
21
|
// will be set to true when e.g. a comment for export is found
|
|
18
22
|
let exportNextClass: boolean = false;
|
|
@@ -65,10 +69,8 @@ class ExportContext {
|
|
|
65
69
|
flush() {
|
|
66
70
|
if (this.textBuffer.length <= 0) return;
|
|
67
71
|
const dir = this.outputDir + "/";
|
|
68
|
-
if (!fs.existsSync(dir)) {
|
|
69
|
-
fs.mkdirSync(dir);
|
|
70
|
-
}
|
|
71
72
|
const path = dir + this.fileName;
|
|
73
|
+
this.replaceGeneratedCodeSection(path);
|
|
72
74
|
console.log("Write to " + path);
|
|
73
75
|
fs.writeFileSync(path, this.textBuffer);
|
|
74
76
|
this.reset();
|
|
@@ -78,11 +80,27 @@ class ExportContext {
|
|
|
78
80
|
this.textBuffer = "";
|
|
79
81
|
this.classEnd = -1;
|
|
80
82
|
}
|
|
83
|
+
|
|
84
|
+
private replaceGeneratedCodeSection(path: string) {
|
|
85
|
+
this.textBuffer = CODEGEN_MARKER_START + "\n" + this.textBuffer + "\n" + CODEGEN_MARKER_END;
|
|
86
|
+
if (fs.existsSync(path)) {
|
|
87
|
+
const existing = fs.readFileSync(path, "utf8");
|
|
88
|
+
const regex = new RegExp("(?<before>.*?)\/\/ ?NEEDLE_CODEGEN_START.+\/\/ ?NEEDLE_CODEGEN_END(?<after>.*)", "s");
|
|
89
|
+
const matches = regex.exec(existing);
|
|
90
|
+
if (matches?.groups) {
|
|
91
|
+
console.log("Found codegen sections")
|
|
92
|
+
const before = matches.groups.before;
|
|
93
|
+
const after = matches.groups.after;
|
|
94
|
+
this.textBuffer = before + this.textBuffer + after;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
81
98
|
}
|
|
82
99
|
|
|
83
100
|
const contexts: ExportContext[] = [];
|
|
84
101
|
|
|
85
102
|
let lastTypeFound: string | null = null;
|
|
103
|
+
let ifdefSections: string[] = [];
|
|
86
104
|
|
|
87
105
|
export function run(program: ts.Program, outputDir: string, sourceFile: ts.SourceFile) {
|
|
88
106
|
|
|
@@ -136,15 +154,22 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
136
154
|
exportNextClass = true;
|
|
137
155
|
if (comment.includes(dontExportNextClassCommand))
|
|
138
156
|
dontExportNextClass = true;
|
|
139
|
-
const
|
|
140
|
-
if (
|
|
157
|
+
const typeMatch = typePattern.exec(comment);
|
|
158
|
+
if (typeMatch && typeMatch.groups) {
|
|
141
159
|
// for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
|
|
142
160
|
// so we remove them
|
|
143
|
-
let type =
|
|
161
|
+
let type = typeMatch.groups["type"];
|
|
144
162
|
type = type.replace(/\(/, "").replace(/\)/, "");
|
|
145
163
|
console.log("found type: ", type);
|
|
146
164
|
lastTypeFound = type;
|
|
147
165
|
}
|
|
166
|
+
|
|
167
|
+
const ifdefMatch = ifdefPattern.exec(comment);
|
|
168
|
+
if (ifdefMatch && ifdefMatch.groups) {
|
|
169
|
+
const ifdef = ifdefMatch.groups["ifdef"];
|
|
170
|
+
if (ifdef)
|
|
171
|
+
ifdefSections.push(ifdef);
|
|
172
|
+
}
|
|
148
173
|
}
|
|
149
174
|
}
|
|
150
175
|
|
|
@@ -234,8 +259,16 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
234
259
|
console.log("SERIALIZE");
|
|
235
260
|
context.appendLine("[UnityEngine.SerializeField]");
|
|
236
261
|
}
|
|
262
|
+
let requireEndIf = false;
|
|
263
|
+
if(ifdefSections.length > 0){
|
|
264
|
+
requireEndIf = true;
|
|
265
|
+
context.appendLine("#ifdef " + ifdefSections.pop());
|
|
266
|
+
}
|
|
237
267
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";\n");
|
|
238
268
|
lastTypeFound = null;
|
|
269
|
+
if(requireEndIf){
|
|
270
|
+
context.appendLine("#endif");
|
|
271
|
+
}
|
|
239
272
|
break;
|
|
240
273
|
|
|
241
274
|
case ts.SyntaxKind.ClassDeclaration:
|
|
@@ -313,7 +346,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
313
346
|
case ts.SyntaxKind.Identifier:
|
|
314
347
|
const id = ch as ts.Identifier;
|
|
315
348
|
if (id.text) {
|
|
316
|
-
if(!namespace) namespace = "";
|
|
349
|
+
if (!namespace) namespace = "";
|
|
317
350
|
namespace = id.text + (namespace ? "." : "") + namespace;
|
|
318
351
|
}
|
|
319
352
|
break;
|