mushroom-cli 1.1.0-alpha.11 → 1.1.0-alpha.14
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/common.js +3 -3
- package/gen/gen-csharp.js +450 -27
- package/mr.js +4 -4
- package/package.json +5 -5
package/common.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
|
|
3
|
-
function ensureFolder(path) {
|
|
3
|
+
function ensureFolder(path, isFolder) {
|
|
4
4
|
const index = path.lastIndexOf("/");
|
|
5
|
-
if (index <= 0){
|
|
5
|
+
if (!isFolder && index <= 0){
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
|
-
path = path.substring(0, index);
|
|
8
|
+
if (!isFolder) path = path.substring(0, index);
|
|
9
9
|
if (path === "." || path === "..")
|
|
10
10
|
return;
|
|
11
11
|
|
package/gen/gen-csharp.js
CHANGED
|
@@ -3,6 +3,31 @@ const common = require("../common.js");
|
|
|
3
3
|
const package = require("./../package.json");
|
|
4
4
|
|
|
5
5
|
//#region shared function
|
|
6
|
+
const newLine = `
|
|
7
|
+
`;
|
|
8
|
+
const header = `// NOTE: Please do not modify this file
|
|
9
|
+
// NOTE: Version and compatibility information: please see in Info.cs file
|
|
10
|
+
|
|
11
|
+
// ReSharper disable CheckNamespace
|
|
12
|
+
// ReSharper disable CommentTypo
|
|
13
|
+
// ReSharper disable EmptyNamespace
|
|
14
|
+
// ReSharper disable IdentifierTypo
|
|
15
|
+
// ReSharper disable InconsistentNaming
|
|
16
|
+
// ReSharper disable PartialTypeWithSinglePart
|
|
17
|
+
// ReSharper disable RedundantUsingDirective
|
|
18
|
+
// ReSharper disable StringLiteralTypo
|
|
19
|
+
// ReSharper disable UnusedMember.Global
|
|
20
|
+
// ReSharper disable UnusedMember.Local
|
|
21
|
+
// ReSharper disable UnusedParameter.Local
|
|
22
|
+
// ReSharper disable UnusedTypeParameter
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
using MongoDB.Bson;
|
|
26
|
+
using MongoDB.Bson.Serialization.Attributes;
|
|
27
|
+
using MongoDB.Driver;
|
|
28
|
+
using JsonPropertyAttribute = Newtonsoft.Json.JsonPropertyAttribute;
|
|
29
|
+
using NullValueHandling = Newtonsoft.Json.NullValueHandling;`;
|
|
30
|
+
|
|
6
31
|
function singleLine(str) {
|
|
7
32
|
if (!str)
|
|
8
33
|
return "";
|
|
@@ -143,7 +168,8 @@ function generateCSharpClassMongoDb(source, className, fields, isRoot, descripti
|
|
|
143
168
|
content +=
|
|
144
169
|
` /// <summary>
|
|
145
170
|
/// ${singleLine(description)}
|
|
146
|
-
/// </summary
|
|
171
|
+
/// </summary>
|
|
172
|
+
`;
|
|
147
173
|
}
|
|
148
174
|
var supperClass = "";
|
|
149
175
|
if (isRoot) {
|
|
@@ -171,12 +197,12 @@ function generateCSharpClassMongoDb(source, className, fields, isRoot, descripti
|
|
|
171
197
|
type = field.codeHint.type.csharp;
|
|
172
198
|
}
|
|
173
199
|
else if (field.type === "object") {
|
|
174
|
-
strClasses += generateCSharpClassMongoDb(source, className + "_" + propertyName, field.fields, false, null, out) +
|
|
200
|
+
strClasses += generateCSharpClassMongoDb(source, className + "_" + propertyName, field.fields, false, null, out) + newLine;
|
|
175
201
|
type = className + "_" + propertyName;
|
|
176
202
|
}
|
|
177
203
|
else if (field.type === "array") {
|
|
178
204
|
if (field.items.type === "object") {
|
|
179
|
-
strClasses += generateCSharpClassMongoDb(source, className + "_" + propertyName, field.items.fields, false, null, out) +
|
|
205
|
+
strClasses += generateCSharpClassMongoDb(source, className + "_" + propertyName, field.items.fields, false, null, out) + newLine;
|
|
180
206
|
type = `System.Collections.Generic.List<${className}_${propertyName}>`;
|
|
181
207
|
} else {
|
|
182
208
|
type = `System.Collections.Generic.List<${getCSharpTypeFromMushroomType(source, field.items.type, field.items.validation && field.items.validation.required)}>`;
|
|
@@ -190,34 +216,34 @@ function generateCSharpClassMongoDb(source, className, fields, isRoot, descripti
|
|
|
190
216
|
`;
|
|
191
217
|
|
|
192
218
|
if (isRoot && fieldName === "id") {
|
|
193
|
-
content += ` [BsonId${field.type === "AutoId" && source.idType === "GUID" ? "(IdGenerator = typeof(MongoDB.Bson.Serialization.IdGenerators.GuidGenerator))" : ""}]
|
|
219
|
+
content += ` [BsonId${field.type === "AutoId" && source.idType === "GUID" ? "(IdGenerator = typeof(MongoDB.Bson.Serialization.IdGenerators.GuidGenerator))" : ""}]${newLine}`;
|
|
194
220
|
}
|
|
195
221
|
content += ` [BsonElement("${isRoot && fieldName === "id" ? "_id" : fieldName}")]
|
|
196
222
|
[BsonIgnoreIfNull]
|
|
197
223
|
[JsonProperty(PropertyName = "${fieldName}", NullValueHandling = NullValueHandling.Ignore)]
|
|
198
224
|
`;
|
|
199
225
|
if ((field.type === "AutoId" || field.type === "IdRef") && source.idType === "ObjectId") {
|
|
200
|
-
content += " [BsonRepresentation(BsonType.ObjectId)]
|
|
226
|
+
content += " [BsonRepresentation(BsonType.ObjectId)]" + newLine;
|
|
201
227
|
}
|
|
202
228
|
if (field.type === "AutoId" && source.idType === "GUID") {
|
|
203
|
-
content += " [BsonRepresentation(BsonType.String)]
|
|
229
|
+
content += " [BsonRepresentation(BsonType.String)]" + newLine;
|
|
204
230
|
}
|
|
205
231
|
if (field.type === "datetime") {
|
|
206
|
-
content += " [BsonDateTimeOptions(Kind = System.DateTimeKind.Local)]
|
|
232
|
+
content += " [BsonDateTimeOptions(Kind = System.DateTimeKind.Local)]" + newLine;
|
|
207
233
|
}
|
|
208
234
|
if (field.type === "array") {
|
|
209
235
|
if ((field.items.type === "AutoId" || field.items.type === "IdRef") && source.idType === "ObjectId") {
|
|
210
|
-
content += " [BsonRepresentation(BsonType.ObjectId)]
|
|
236
|
+
content += " [BsonRepresentation(BsonType.ObjectId)]" + newLine;
|
|
211
237
|
}
|
|
212
238
|
if (field.items.type === "AutoId" && source.idType === "GUID") {
|
|
213
|
-
content += " [BsonRepresentation(BsonType.String)]
|
|
239
|
+
content += " [BsonRepresentation(BsonType.String)]" + newLine;
|
|
214
240
|
}
|
|
215
241
|
if (field.items.type === "datetime") {
|
|
216
|
-
content += " [BsonDateTimeOptions(Kind = System.DateTimeKind.Local)]
|
|
242
|
+
content += " [BsonDateTimeOptions(Kind = System.DateTimeKind.Local)]" + newLine;
|
|
217
243
|
}
|
|
218
244
|
}
|
|
219
245
|
|
|
220
|
-
content += ` public ${type} ${propertyName} { get; set; }
|
|
246
|
+
content += ` public ${type} ${propertyName} { get; set; }${newLine}${newLine}`;
|
|
221
247
|
|
|
222
248
|
strEnumClasses += generateEnumClass(source, out, field, `${className.toUpperCase()}_${propertyName.toUpperCase()}_ENUM`);
|
|
223
249
|
}
|
|
@@ -233,7 +259,7 @@ function generateCSharpClassMongoDb(source, className, fields, isRoot, descripti
|
|
|
233
259
|
//console.log("strEnumClasses("+typeof(strEnumClasses)+"):" + strEnumClasses);
|
|
234
260
|
}
|
|
235
261
|
|
|
236
|
-
return strClasses +
|
|
262
|
+
return strClasses + newLine + content;
|
|
237
263
|
}
|
|
238
264
|
|
|
239
265
|
function getCSharpFunc(methodName, methodType) {
|
|
@@ -250,6 +276,11 @@ function genCSharpMongoDbClasses(args, projectName, project, alias, source) {
|
|
|
250
276
|
throw new Error(`Not found gen c#-mongo configuration. Please run 'npx mr config ${projectName} gen c#-mongo OUTPUT_FILE.cs${alias === "default" ? "" : " alias " + alias}' first`);
|
|
251
277
|
}
|
|
252
278
|
|
|
279
|
+
if (aliasProject.outputToFolder) {
|
|
280
|
+
genCSharpMongoDbClassesToFolder(projectName, alias, source, aliasProject);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
253
284
|
const entities = common.getEntities(source, aliasProject.allowRoles, true);
|
|
254
285
|
|
|
255
286
|
const namespace = toCSharpName(source.name);
|
|
@@ -258,6 +289,17 @@ function genCSharpMongoDbClasses(args, projectName, project, alias, source) {
|
|
|
258
289
|
let strEnumClasses = "";
|
|
259
290
|
let collectionNames = "";
|
|
260
291
|
let resourceNames = "";
|
|
292
|
+
let roleNames = "";
|
|
293
|
+
|
|
294
|
+
source.roles?.forEach(role => {
|
|
295
|
+
if (!role.name) return;
|
|
296
|
+
roleNames += `
|
|
297
|
+
/// <summary>
|
|
298
|
+
/// ${role.description ?? ""}
|
|
299
|
+
/// </summary>
|
|
300
|
+
public static string ${toCSharpName(role.name)} => "${role.name}";
|
|
301
|
+
`;
|
|
302
|
+
});
|
|
261
303
|
|
|
262
304
|
const out = {};
|
|
263
305
|
for (let entityName in entities) {
|
|
@@ -268,7 +310,7 @@ function genCSharpMongoDbClasses(args, projectName, project, alias, source) {
|
|
|
268
310
|
collectionNames += ` public static string ${className}CollectionName => "${entities[entityName].storageName}";
|
|
269
311
|
public static IMongoCollection<Resources.${className}> ${className}Collection => GetDatabase().GetCollection<Resources.${className}>(${className}CollectionName);
|
|
270
312
|
`;
|
|
271
|
-
resourceNames += ` public static string ${className} => "${entityName}"
|
|
313
|
+
resourceNames += ` public static string ${className} => "${entityName}";${newLine}`;
|
|
272
314
|
strClasses += generateCSharpClassMongoDb(source, className, entities[entityName].fields, true, entities[entityName].description, out);
|
|
273
315
|
|
|
274
316
|
const actions = entities[entityName].actions || {};
|
|
@@ -360,7 +402,7 @@ function genCSharpMongoDbClasses(args, projectName, project, alias, source) {
|
|
|
360
402
|
}
|
|
361
403
|
return prev;
|
|
362
404
|
}, {});
|
|
363
|
-
const strDelegatorReferences = Object.keys(delegators).map(method => delegators[method].map(ev => ` // ${ev.source}`).join(
|
|
405
|
+
const strDelegatorReferences = Object.keys(delegators).map(method => delegators[method].map(ev => ` // ${ev.source}`).join(newLine) + newLine + ` RegisterCustomMethod(${getCSharpFunc(method, delegators[method][0].type)});`).join(newLine);
|
|
364
406
|
|
|
365
407
|
const viewHandlers = (out.viewHandlers || []).reduce((prev, cur) => {
|
|
366
408
|
if (cur.method in prev) {
|
|
@@ -373,7 +415,7 @@ function genCSharpMongoDbClasses(args, projectName, project, alias, source) {
|
|
|
373
415
|
}
|
|
374
416
|
return prev;
|
|
375
417
|
}, {});
|
|
376
|
-
const strViewHandlerReferences = Object.keys(viewHandlers).map(method => viewHandlers[method].map(ev => ` // ${ev.source}`).join(
|
|
418
|
+
const strViewHandlerReferences = Object.keys(viewHandlers).map(method => viewHandlers[method].map(ev => ` // ${ev.source}`).join(newLine) + newLine + ` RegisterCustomViewMethod(${getCSharpFunc(method, viewHandlers[method][0].type)});`).join(newLine);
|
|
377
419
|
|
|
378
420
|
const eventHandlers = (out.eventHandlers || []).reduce((prev, cur) => {
|
|
379
421
|
if (cur.method in prev) {
|
|
@@ -386,18 +428,18 @@ function genCSharpMongoDbClasses(args, projectName, project, alias, source) {
|
|
|
386
428
|
}
|
|
387
429
|
return prev;
|
|
388
430
|
}, {});
|
|
389
|
-
const strEventHandlerReferences = Object.keys(eventHandlers).map(method => eventHandlers[method].map(ev => ` // ${ev.source}`).join(
|
|
431
|
+
const strEventHandlerReferences = Object.keys(eventHandlers).map(method => eventHandlers[method].map(ev => ` // ${ev.source}`).join(newLine) + newLine + ` RegisterEvent(${getCSharpFunc(method, eventHandlers[method][0].type)});`).join(newLine);
|
|
390
432
|
|
|
391
433
|
const strJobReferences = Array.isArray(source.jobs)
|
|
392
434
|
? source.jobs
|
|
393
435
|
.filter(job => job !== null && typeof(job) === "object" && job.action && typeof (job.action) === "string" && job.action.indexOf("lib:") === 0)
|
|
394
436
|
.map(job => ` RegisterJob<${job.action.substring(4).split(',')[0]}>();`)
|
|
395
|
-
.join(
|
|
437
|
+
.join(newLine)
|
|
396
438
|
: "";
|
|
397
439
|
|
|
398
440
|
const code = `// This file was generated by Mushroom CLI ${package.version} (https://www.npmjs.com/package/mushroom-cli), please do not modify
|
|
399
441
|
|
|
400
|
-
// NOTE: Compatible with Mushroom Server version 0.2.18 or higher
|
|
442
|
+
// NOTE: Compatible with Mushroom Server version 0.2.18 or higher (https://www.nuget.org/packages/Mushroom/)
|
|
401
443
|
|
|
402
444
|
// ReSharper disable RedundantUsingDirective
|
|
403
445
|
// ReSharper disable InconsistentNaming
|
|
@@ -443,6 +485,10 @@ ${collectionNames}
|
|
|
443
485
|
{
|
|
444
486
|
${resourceNames}
|
|
445
487
|
}
|
|
488
|
+
|
|
489
|
+
public static class RoleNames {
|
|
490
|
+
${roleNames}
|
|
491
|
+
}
|
|
446
492
|
}
|
|
447
493
|
|
|
448
494
|
namespace ${namespace}.Resources
|
|
@@ -488,6 +534,383 @@ ${strJobReferences}
|
|
|
488
534
|
console.log(`Generated CSharp MongoDb Classes for ${projectName}@${alias} to ${aliasProject.output}`);
|
|
489
535
|
});
|
|
490
536
|
}
|
|
537
|
+
|
|
538
|
+
function generateCSharpClassMongoDbToFolder(source, className, fields, isRoot, description, out, aliasProject) {
|
|
539
|
+
var content = "";
|
|
540
|
+
const namespace = toCSharpName(source.name);
|
|
541
|
+
if (description) {
|
|
542
|
+
content +=
|
|
543
|
+
` /// <summary>
|
|
544
|
+
/// ${singleLine(description)}
|
|
545
|
+
/// </summary>
|
|
546
|
+
`;
|
|
547
|
+
}
|
|
548
|
+
var supperClass = "";
|
|
549
|
+
if (isRoot) {
|
|
550
|
+
// thêm cho trường hợp không khai báo id hoặc id.type
|
|
551
|
+
if (!fields.id) {
|
|
552
|
+
fields.id = {
|
|
553
|
+
type: "AutoId"
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
if (!fields.id.type) fields.id.type = "AutoId";
|
|
557
|
+
|
|
558
|
+
supperClass = " : Mushroom.Models.IResource<" + getCSharpTypeFromMushroomType(source, fields.id.type, true) + ">";
|
|
559
|
+
}
|
|
560
|
+
content += ` public class ${className}${supperClass}
|
|
561
|
+
{
|
|
562
|
+
`;
|
|
563
|
+
for (var fieldName in fields) {
|
|
564
|
+
if (!fields.hasOwnProperty(fieldName))
|
|
565
|
+
continue;
|
|
566
|
+
|
|
567
|
+
var field = fields[fieldName];
|
|
568
|
+
var type;
|
|
569
|
+
var propertyName = toCSharpName(fieldName);
|
|
570
|
+
if (field.codeHint && field.codeHint.type && field.codeHint.type.csharp) {
|
|
571
|
+
type = field.codeHint.type.csharp;
|
|
572
|
+
}
|
|
573
|
+
else if (field.type === "object") {
|
|
574
|
+
generateCSharpClassMongoDbToFolder(source, className + "_" + propertyName, field.fields, false, null, out, aliasProject) + newLine;
|
|
575
|
+
type = className + "_" + propertyName;
|
|
576
|
+
}
|
|
577
|
+
else if (field.type === "array") {
|
|
578
|
+
if (field.items.type === "object") {
|
|
579
|
+
generateCSharpClassMongoDbToFolder(source, className + "_" + propertyName, field.items.fields, false, null, out, aliasProject) + newLine;
|
|
580
|
+
type = `System.Collections.Generic.List<${className}_${propertyName}>`;
|
|
581
|
+
} else {
|
|
582
|
+
type = `System.Collections.Generic.List<${getCSharpTypeFromMushroomType(source, field.items.type, field.items.validation && field.items.validation.required)}>`;
|
|
583
|
+
}
|
|
584
|
+
} else
|
|
585
|
+
type = getCSharpTypeFromMushroomType(source, field.type, field.validation && field.validation.required);
|
|
586
|
+
content +=
|
|
587
|
+
` /// <summary>
|
|
588
|
+
/// ${singleLine(field.description)}
|
|
589
|
+
/// </summary>
|
|
590
|
+
`;
|
|
591
|
+
|
|
592
|
+
if (isRoot && fieldName === "id") {
|
|
593
|
+
content += ` [BsonId${field.type === "AutoId" && source.idType === "GUID" ? "(IdGenerator = typeof(MongoDB.Bson.Serialization.IdGenerators.GuidGenerator))" : ""}]${newLine}`;
|
|
594
|
+
}
|
|
595
|
+
content += ` [BsonElement("${isRoot && fieldName === "id" ? "_id" : fieldName}")]
|
|
596
|
+
[BsonIgnoreIfNull]
|
|
597
|
+
[JsonProperty(PropertyName = "${fieldName}", NullValueHandling = NullValueHandling.Ignore)]
|
|
598
|
+
`;
|
|
599
|
+
if ((field.type === "AutoId" || field.type === "IdRef") && source.idType === "ObjectId") {
|
|
600
|
+
content += " [BsonRepresentation(BsonType.ObjectId)]" + newLine;
|
|
601
|
+
}
|
|
602
|
+
if (field.type === "AutoId" && source.idType === "GUID") {
|
|
603
|
+
content += " [BsonRepresentation(BsonType.String)]" + newLine;
|
|
604
|
+
}
|
|
605
|
+
if (field.type === "datetime") {
|
|
606
|
+
content += " [BsonDateTimeOptions(Kind = System.DateTimeKind.Local)]" + newLine;
|
|
607
|
+
}
|
|
608
|
+
if (field.type === "array") {
|
|
609
|
+
if ((field.items.type === "AutoId" || field.items.type === "IdRef") && source.idType === "ObjectId") {
|
|
610
|
+
content += " [BsonRepresentation(BsonType.ObjectId)]" + newLine;
|
|
611
|
+
}
|
|
612
|
+
if (field.items.type === "AutoId" && source.idType === "GUID") {
|
|
613
|
+
content += " [BsonRepresentation(BsonType.String)]" + newLine;
|
|
614
|
+
}
|
|
615
|
+
if (field.items.type === "datetime") {
|
|
616
|
+
content += " [BsonDateTimeOptions(Kind = System.DateTimeKind.Local)]" + newLine;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
content += ` public ${type} ${propertyName} { get; set; }
|
|
621
|
+
|
|
622
|
+
`;
|
|
623
|
+
|
|
624
|
+
let strEnumClasses = generateEnumClass(source, {}, field, `${className.toUpperCase()}_${propertyName.toUpperCase()}_ENUM`);
|
|
625
|
+
|
|
626
|
+
const match = strEnumClasses.match(/public static class\s+(\w+)/);
|
|
627
|
+
if (!match) continue;
|
|
628
|
+
common.ensureFolder(aliasProject.output + "/Enums", true);
|
|
629
|
+
strEnumClasses = `${header}
|
|
630
|
+
|
|
631
|
+
namespace ${namespace}.Enums
|
|
632
|
+
{
|
|
633
|
+
${strEnumClasses}
|
|
634
|
+
}
|
|
635
|
+
`;
|
|
636
|
+
|
|
637
|
+
fs.writeFileSync(aliasProject.output + "/Enums/" + match[1] + ".cs", strEnumClasses);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
content += ` [BsonExtraElements]
|
|
641
|
+
public System.Collections.Generic.Dictionary<string, object> Mushroom_ExtraProperties { get; set; }
|
|
642
|
+
|
|
643
|
+
}
|
|
644
|
+
`;
|
|
645
|
+
|
|
646
|
+
//return strClasses + newLine + content;
|
|
647
|
+
common.ensureFolder(aliasProject.output + "/Resources", true);
|
|
648
|
+
|
|
649
|
+
content = `${header}
|
|
650
|
+
|
|
651
|
+
namespace ${namespace}.Resources
|
|
652
|
+
{
|
|
653
|
+
${content}
|
|
654
|
+
}
|
|
655
|
+
`;
|
|
656
|
+
|
|
657
|
+
fs.writeFileSync(aliasProject.output + "/Resources/" + className + ".cs", content);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function genCSharpMongoDbClassesToFolder(projectName, alias, source, aliasProject) {
|
|
661
|
+
|
|
662
|
+
if (fs.existsSync(aliasProject.output)) {
|
|
663
|
+
try {
|
|
664
|
+
fs.unlinkSync(aliasProject.output); // xóa file nếu có
|
|
665
|
+
} catch(e) {}
|
|
666
|
+
try {
|
|
667
|
+
fs.rmdirSync(aliasProject.output, { recursive: true, force: true }); // xóa thư mục nếu có
|
|
668
|
+
} catch(e) {}
|
|
669
|
+
}
|
|
670
|
+
fs.mkdirSync(aliasProject.output, { recursive: true, mode: 0755 });
|
|
671
|
+
|
|
672
|
+
const entities = common.getEntities(source, aliasProject.allowRoles, true);
|
|
673
|
+
|
|
674
|
+
const namespace = toCSharpName(source.name);
|
|
675
|
+
|
|
676
|
+
let collectionNames = "";
|
|
677
|
+
let resourceNames = "";
|
|
678
|
+
let roleNames = "";
|
|
679
|
+
|
|
680
|
+
source.roles?.forEach(role => {
|
|
681
|
+
if (!role.name) return;
|
|
682
|
+
roleNames += `
|
|
683
|
+
/// <summary>
|
|
684
|
+
/// ${role.description ?? ""}
|
|
685
|
+
/// </summary>
|
|
686
|
+
public static string ${toCSharpName(role.name)} => "${role.name}";
|
|
687
|
+
`;
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
const out = {};
|
|
691
|
+
for (let entityName in entities) {
|
|
692
|
+
if (!entities.hasOwnProperty(entityName))
|
|
693
|
+
continue;
|
|
694
|
+
|
|
695
|
+
const className = toCSharpName(entityName);
|
|
696
|
+
collectionNames += ` public static string ${className}CollectionName => "${entities[entityName].storageName}";
|
|
697
|
+
public static IMongoCollection<Resources.${className}> ${className}Collection => GetDatabase().GetCollection<Resources.${className}>(${className}CollectionName);
|
|
698
|
+
`;
|
|
699
|
+
resourceNames += ` public static string ${className} => "${entityName}";
|
|
700
|
+
`;
|
|
701
|
+
//strClasses += generateCSharpClassMongoDbToFolder(source, className, entities[entityName].fields, true, entities[entityName].description, out);
|
|
702
|
+
|
|
703
|
+
generateCSharpClassMongoDbToFolder(source, className, entities[entityName].fields, true, entities[entityName].description, out, aliasProject);
|
|
704
|
+
|
|
705
|
+
const actions = entities[entityName].actions || {};
|
|
706
|
+
for (let actionName in actions) {
|
|
707
|
+
const action = actions[actionName];
|
|
708
|
+
if (!Array.isArray(action))
|
|
709
|
+
continue;
|
|
710
|
+
|
|
711
|
+
action.forEach(a => {
|
|
712
|
+
if (!a)
|
|
713
|
+
return;
|
|
714
|
+
if (typeof a.delegator === "string" && a.delegator.indexOf("lib:") === 0) {
|
|
715
|
+
out.delegators = out.delegators || [];
|
|
716
|
+
const method = a.delegator.substr(4, a.delegator.length - 4);
|
|
717
|
+
let type = a.codeHint && a.codeHint.delegator && a.codeHint.delegator.csharp
|
|
718
|
+
? a.codeHint.delegator.csharp.methodType
|
|
719
|
+
: null;
|
|
720
|
+
if (type !== "non-static") type = "static";
|
|
721
|
+
out.delegators.push({
|
|
722
|
+
method: method,
|
|
723
|
+
source: `entity: ${entityName}, action: ${actionName}, grant: ${a.grant}`,
|
|
724
|
+
type: type
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (!a.events && !(typeof a.events === "object")) return;
|
|
729
|
+
|
|
730
|
+
for (let eventName in a.events) {
|
|
731
|
+
if (!Array.isArray(a.events[eventName])) return;
|
|
732
|
+
|
|
733
|
+
out.eventHandlers = out.eventHandlers || [];
|
|
734
|
+
a.events[eventName].forEach((ev, i) => {
|
|
735
|
+
if (typeof ev !== "string" || ev.indexOf("lib:") !== 0) return;
|
|
736
|
+
const method = ev.substr(4, ev.length - 4);
|
|
737
|
+
let type = a.codeHint && a.codeHint.events
|
|
738
|
+
&& Array.isArray(a.codeHint.events[eventName])
|
|
739
|
+
&& a.codeHint.events[eventName][i]
|
|
740
|
+
&& a.codeHint.events[eventName][i].csharp
|
|
741
|
+
? a.codeHint.events[eventName][i].csharp.methodType
|
|
742
|
+
: null;
|
|
743
|
+
if (type !== "non-static") type = "static";
|
|
744
|
+
out.eventHandlers.push({
|
|
745
|
+
method: method,
|
|
746
|
+
source: `entity: ${entityName}, action: ${actionName}, grant: ${a.grant}, event: ${eventName}`,
|
|
747
|
+
type: type
|
|
748
|
+
});
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
const views = entities[entityName].views || {};
|
|
755
|
+
for (let viewName in views) {
|
|
756
|
+
const view = views[viewName];
|
|
757
|
+
if (!Array.isArray(view))
|
|
758
|
+
continue;
|
|
759
|
+
|
|
760
|
+
view.forEach(v => {
|
|
761
|
+
if (!v) return;
|
|
762
|
+
|
|
763
|
+
if (typeof v.query === "string" && v.query.indexOf("lib:") === 0) {
|
|
764
|
+
out.viewHandlers = out.viewHandlers || [];
|
|
765
|
+
const method = v.query.substr(4, v.query.length - 4);
|
|
766
|
+
let type = v.codeHint && v.codeHint.query && v.codeHint.query.csharp
|
|
767
|
+
? v.codeHint.query.csharp.methodType
|
|
768
|
+
: null;
|
|
769
|
+
if (type !== "non-static") type = "static";
|
|
770
|
+
out.viewHandlers.push({
|
|
771
|
+
method: method,
|
|
772
|
+
source: `entity: ${entityName}, view: ${viewName}, grant: ${v.grant}`,
|
|
773
|
+
type: type
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
const delegators = (out.delegators || []).reduce((prev, cur) => {
|
|
782
|
+
if (cur.method in prev) {
|
|
783
|
+
if (prev[cur.method].indexOf(cur.source) === -1) {
|
|
784
|
+
prev[cur.method].push(cur);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
prev[cur.method] = [cur];
|
|
789
|
+
}
|
|
790
|
+
return prev;
|
|
791
|
+
}, {});
|
|
792
|
+
const strDelegatorReferences = Object.keys(delegators).map(method => delegators[method].map(ev => ` // ${ev.source}`).join(newLine) + newLine + ` RegisterCustomMethod(${getCSharpFunc(method, delegators[method][0].type)});`).join(newLine);
|
|
793
|
+
|
|
794
|
+
const viewHandlers = (out.viewHandlers || []).reduce((prev, cur) => {
|
|
795
|
+
if (cur.method in prev) {
|
|
796
|
+
if (prev[cur.method].indexOf(cur.source) === -1) {
|
|
797
|
+
prev[cur.method].push(cur);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
801
|
+
prev[cur.method] = [cur];
|
|
802
|
+
}
|
|
803
|
+
return prev;
|
|
804
|
+
}, {});
|
|
805
|
+
const strViewHandlerReferences = Object.keys(viewHandlers).map(method => viewHandlers[method].map(ev => ` // ${ev.source}`).join(newLine) + newLine + ` RegisterCustomViewMethod(${getCSharpFunc(method, viewHandlers[method][0].type)});`).join(newLine);
|
|
806
|
+
|
|
807
|
+
const eventHandlers = (out.eventHandlers || []).reduce((prev, cur) => {
|
|
808
|
+
if (cur.method in prev) {
|
|
809
|
+
if (prev[cur.method].indexOf(cur.source) === -1) {
|
|
810
|
+
prev[cur.method].push(cur);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
prev[cur.method] = [cur];
|
|
815
|
+
}
|
|
816
|
+
return prev;
|
|
817
|
+
}, {});
|
|
818
|
+
const strEventHandlerReferences = Object.keys(eventHandlers).map(method => eventHandlers[method].map(ev => ` // ${ev.source}`).join(newLine) + newLine + ` RegisterEvent(${getCSharpFunc(method, eventHandlers[method][0].type)});`).join(newLine);
|
|
819
|
+
|
|
820
|
+
const strJobReferences = Array.isArray(source.jobs)
|
|
821
|
+
? source.jobs
|
|
822
|
+
.filter(job => job !== null && typeof(job) === "object" && job.action && typeof (job.action) === "string" && job.action.indexOf("lib:") === 0)
|
|
823
|
+
.map(job => ` RegisterJob<${job.action.substring(4).split(',')[0]}>();`)
|
|
824
|
+
.join(newLine)
|
|
825
|
+
: "";
|
|
826
|
+
|
|
827
|
+
const dbConnectorFileContent = `${header}
|
|
828
|
+
|
|
829
|
+
namespace ${namespace}
|
|
830
|
+
{
|
|
831
|
+
public static class DbConnector
|
|
832
|
+
{
|
|
833
|
+
${collectionNames}
|
|
834
|
+
public static IMongoDatabase GetDatabase() => Mushroom.MushroomDaoFactory.GetDbAdapter() as IMongoDatabase;
|
|
835
|
+
|
|
836
|
+
public static async System.Threading.Tasks.Task<T> FindByIdAsync<T, TId>(this IMongoCollection<T> collection, TId id) where T : Mushroom.Models.IResource<TId>
|
|
837
|
+
{
|
|
838
|
+
return await (await collection.FindAsync(Builders<T>.Filter.Eq(doc => doc.Id, id))).FirstOrDefaultAsync();
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
public static async System.Threading.Tasks.Task<T> FindByIdAsync<T, TId>(this IMongoCollection<T> collection, TId id, FindOptions<T> findOptions) where T : Mushroom.Models.IResource<TId>
|
|
842
|
+
{
|
|
843
|
+
return await (await collection.FindAsync(Builders<T>.Filter.Eq(doc => doc.Id, id), findOptions)).FirstOrDefaultAsync();
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
public static async System.Threading.Tasks.Task<T> FindByIdAsync<T, TId>(this IMongoCollection<T> collection, TId id, ProjectionDefinition<T> projection) where T : Mushroom.Models.IResource<TId>
|
|
847
|
+
{
|
|
848
|
+
return await (await collection.FindAsync(Builders<T>.Filter.Eq(doc => doc.Id, id), new FindOptions<T> { Projection = projection })).FirstOrDefaultAsync();
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
`;
|
|
853
|
+
|
|
854
|
+
const resourceNamesFileContent = `${header}
|
|
855
|
+
|
|
856
|
+
namespace ${namespace}
|
|
857
|
+
{
|
|
858
|
+
public static class ResourceNames
|
|
859
|
+
{
|
|
860
|
+
${resourceNames}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
`;
|
|
864
|
+
|
|
865
|
+
const roleNamesFileContent = `${header}
|
|
866
|
+
|
|
867
|
+
namespace ${namespace}
|
|
868
|
+
{
|
|
869
|
+
public static class RoleNames
|
|
870
|
+
{
|
|
871
|
+
${roleNames}
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
`;
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
const refactorHelperFileContent = `${header}
|
|
878
|
+
|
|
879
|
+
#if DEBUG
|
|
880
|
+
namespace ${namespace}
|
|
881
|
+
{
|
|
882
|
+
class RefactorHelper
|
|
883
|
+
{
|
|
884
|
+
private RefactorHelper() { }
|
|
885
|
+
private void RegisterCustomMethod(System.Func<Mushroom.ActionArguments, System.Threading.Tasks.Task> fn) { }
|
|
886
|
+
private void RegisterCustomMethod(System.Action<Mushroom.ActionArguments> fn) { }
|
|
887
|
+
private void RegisterCustomViewMethod(System.Func<Mushroom.ViewArguments, System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<object>>> fn) { }
|
|
888
|
+
private void RegisterCustomViewMethod(System.Action<Mushroom.ViewArguments> fn) { }
|
|
889
|
+
private void RegisterEvent(System.Func<object, Mushroom.ActionArguments, System.Threading.Tasks.Task> fn) { }
|
|
890
|
+
private void RegisterEvent(System.Action<object, Mushroom.ActionArguments> fn) { }
|
|
891
|
+
private void RegisterJob<T>() where T : Mushroom.Job.JobBase { }
|
|
892
|
+
private void Help()
|
|
893
|
+
{
|
|
894
|
+
${strDelegatorReferences}
|
|
895
|
+
${strViewHandlerReferences}
|
|
896
|
+
${strEventHandlerReferences}
|
|
897
|
+
${strJobReferences}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
#endif
|
|
902
|
+
`;
|
|
903
|
+
|
|
904
|
+
const infoFileContent = `// Mushroom CLI version: ${package.version}${newLine}// Compatible with Mushroom Server version: 0.2.18 or higher (https://www.nuget.org/packages/Mushroom/)`
|
|
905
|
+
|
|
906
|
+
fs.writeFileSync(aliasProject.output + "/DbConnector.cs", dbConnectorFileContent);
|
|
907
|
+
fs.writeFileSync(aliasProject.output + "/ResourceNames.cs", resourceNamesFileContent);
|
|
908
|
+
fs.writeFileSync(aliasProject.output + "/RoleNames.cs", roleNamesFileContent);
|
|
909
|
+
fs.writeFileSync(aliasProject.output + "/RefactorHelper.cs", refactorHelperFileContent);
|
|
910
|
+
fs.writeFileSync(aliasProject.output + "/Info.cs", infoFileContent);
|
|
911
|
+
|
|
912
|
+
console.log(`Generated CSharp MongoDb Classes for ${projectName}@${alias} to folder ${aliasProject.output}`);
|
|
913
|
+
}
|
|
491
914
|
//#endregion
|
|
492
915
|
|
|
493
916
|
//#region CSharp Driver
|
|
@@ -515,12 +938,12 @@ function generateCSharpClassDriver(source, className, fields, description, out)
|
|
|
515
938
|
const propertyName = toCSharpName(fieldName);
|
|
516
939
|
let type;
|
|
517
940
|
if (field.type === "object") {
|
|
518
|
-
strClasses += generateCSharpClassDriver(source, className + "_" + propertyName, field.fields, null, out) +
|
|
941
|
+
strClasses += generateCSharpClassDriver(source, className + "_" + propertyName, field.fields, null, out) + newLine;
|
|
519
942
|
type = className + "_" + propertyName;
|
|
520
943
|
}
|
|
521
944
|
else if (field.type === "array") {
|
|
522
945
|
if (field.items.type === "object") {
|
|
523
|
-
strClasses += generateCSharpClassDriver(source, className + "_" + propertyName, field.items.fields, null, out) +
|
|
946
|
+
strClasses += generateCSharpClassDriver(source, className + "_" + propertyName, field.items.fields, null, out) + newLine;
|
|
524
947
|
type = `System.Collections.Generic.List<${className}_${propertyName}>`;
|
|
525
948
|
} else {
|
|
526
949
|
type = `System.Collections.Generic.List<${getCSharpTypeFromMushroomType(source, field.items.type, field.items.validation && field.items.validation.required)}>`;
|
|
@@ -545,8 +968,8 @@ function generateCSharpClassDriver(source, className, fields, description, out)
|
|
|
545
968
|
}
|
|
546
969
|
|
|
547
970
|
|
|
548
|
-
content += " }
|
|
549
|
-
return strClasses +
|
|
971
|
+
content += " }" + newLine;
|
|
972
|
+
return strClasses + newLine + content;
|
|
550
973
|
}
|
|
551
974
|
|
|
552
975
|
function genCSharpDriver(args, projectName, project, alias, source) {
|
|
@@ -578,8 +1001,8 @@ function genCSharpDriver(args, projectName, project, alias, source) {
|
|
|
578
1001
|
var className = toCSharpName(entityName);
|
|
579
1002
|
|
|
580
1003
|
strClasses += generateCSharpClassDriver(source, className, entities[entityName].fields, entities[entityName].description, out);
|
|
581
|
-
codeStaticCtor += ` ${className} = new ${className}Connector(client)
|
|
582
|
-
codeStaticProperties += ` public static ${className}Connector ${className} { get; }
|
|
1004
|
+
codeStaticCtor += ` ${className} = new ${className}Connector(client);${newLine}`;
|
|
1005
|
+
codeStaticProperties += ` public static ${className}Connector ${className} { get; }${newLine}${newLine}`;
|
|
583
1006
|
|
|
584
1007
|
var entity = entities[entityName];
|
|
585
1008
|
|
|
@@ -833,15 +1256,15 @@ function genCSharpDriver(args, projectName, project, alias, source) {
|
|
|
833
1256
|
${viewExtraClass}`;
|
|
834
1257
|
}
|
|
835
1258
|
|
|
836
|
-
codeConnectors += " }
|
|
1259
|
+
codeConnectors += " }" + newLine + newLine;
|
|
837
1260
|
}
|
|
838
1261
|
|
|
839
1262
|
if (out.strEnumClasses)
|
|
840
1263
|
strEnumClasses += out.strEnumClasses;
|
|
841
1264
|
|
|
842
|
-
const code = `// This file was generated by Mushroom CLI (https://www.npmjs.com/package/mushroom-cli), please do not modify
|
|
1265
|
+
const code = `// This file was generated by Mushroom CLI ${package.version} (https://www.npmjs.com/package/mushroom-cli), please do not modify
|
|
843
1266
|
|
|
844
|
-
// Require package: Mushroom.Driver version 1.4.0 or later https://www.nuget.org/packages/Mushroom.Driver/
|
|
1267
|
+
// Require package: Mushroom.Driver version 1.4.0 or later (https://www.nuget.org/packages/Mushroom.Driver/)
|
|
845
1268
|
|
|
846
1269
|
// ReSharper disable InconsistentNaming
|
|
847
1270
|
// ReSharper disable PartialTypeWithSinglePart
|
package/mr.js
CHANGED
|
@@ -5,12 +5,12 @@ const config = require("./config.js");
|
|
|
5
5
|
const gen = require("./gen.js");
|
|
6
6
|
const help = require("./help.js");
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const pkg = require("./package.json");
|
|
9
9
|
const checkUpdate = require("./check-update");
|
|
10
10
|
|
|
11
|
-
const versionGroup =
|
|
11
|
+
const versionGroup = pkg.version.match(/([^-]+)(-(\w+).*)?/);
|
|
12
12
|
const currentDistTag = versionGroup[3] ? versionGroup[3] : "latest";
|
|
13
|
-
console.log("Mushroom CLI version " +
|
|
13
|
+
console.log("Mushroom CLI version " + pkg.version);
|
|
14
14
|
|
|
15
15
|
async function run(args) {
|
|
16
16
|
let debugMode = false;
|
|
@@ -63,7 +63,7 @@ async function run(args) {
|
|
|
63
63
|
if (checkUpdateTask !== null)
|
|
64
64
|
try {
|
|
65
65
|
var data = await checkUpdateTask;
|
|
66
|
-
if (data["dist-tags"] && data["dist-tags"][currentDistTag] && data["dist-tags"][currentDistTag] !==
|
|
66
|
+
if (data["dist-tags"] && data["dist-tags"][currentDistTag] && data["dist-tags"][currentDistTag] !== pkg.version) {
|
|
67
67
|
console.warn(`YOUR mushroom-cli VERSION IS NOT LATEST (dist-tags: ${currentDistTag}). PLEASE RUN: npm i mushroom-cli@${data["dist-tags"][currentDistTag]}`);
|
|
68
68
|
}
|
|
69
69
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mushroom-cli",
|
|
3
|
-
"version": "1.1.0-alpha.
|
|
3
|
+
"version": "1.1.0-alpha.14",
|
|
4
4
|
"description": "Mushroom CLI",
|
|
5
5
|
"main": "mr.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
"author": "suco2007",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"jsdom": "^
|
|
21
|
-
"json5": "^2.1
|
|
22
|
-
"node-fetch": "^2.6.
|
|
23
|
-
"open": "^
|
|
20
|
+
"jsdom": "^20.0.0",
|
|
21
|
+
"json5": "^2.2.1",
|
|
22
|
+
"node-fetch": "^2.6.7",
|
|
23
|
+
"open": "^8.4.0",
|
|
24
24
|
"readline-sync": "^1.4.10",
|
|
25
25
|
"targz": "^1.0.1"
|
|
26
26
|
}
|