Skip to content

Commit

Permalink
also try to handle types in referenced assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
volkanceylan committed Oct 18, 2023
1 parent ce87569 commit 3a43cb5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ protected override void GenerateAll()
}
}

private void ScanAnnotationTypeAttributes(TypeDefinition fromType)
protected virtual void ScanAnnotationTypeAttributes(TypeDefinition fromType)
{
var annotationTypeAttrs = TypingsUtils.GetAttrs(
fromType.GetAttributes(),
Expand Down
1 change: 0 additions & 1 deletion src/Serenity.Net.CodeGenerator/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Serenity.CodeGeneration;
using Serenity.Data.Schema;
using Spectre.Console;

Expand Down
14 changes: 4 additions & 10 deletions src/Serenity.Net.CodeGenerator/Generator/Templates.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Scriban;
using Scriban;
using Scriban.Runtime;

namespace Serenity.CodeGenerator;
Expand Down Expand Up @@ -38,6 +38,7 @@ private static Template GetTemplate(IGeneratorFileSystem fileSystem, string temp
return t;
}


public static string Render(IGeneratorFileSystem fileSystem, string templateKey, object model,
Action<CodeWriter> initWriter = null)
{
Expand All @@ -54,17 +55,10 @@ public static string Render(IGeneratorFileSystem fileSystem, string templateKey,
ScriptMemberImportFlags.Field | ScriptMemberImportFlags.Property,
null, x => x.Name);


var cw = new CodeWriter
{
AllowUsing = ns => ns == "Serenity" ||
ns.StartsWith("Serenity.", StringComparison.Ordinal) ||
ns == "Microsoft.AspNetCore.Mvc" ||
ns == "System.Globalization" ||
ns == "System.Data" ||
ns == "System" ||
ns == "System.IO" ||
ns == "System.ComponentModel" ||
ns == "System.Collections.Generic",
AllowUsing = CodeWriter.SafeSetOfUsings.Contains,
IsCSharp = true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Serenity.CodeGenerator;

public class ApplicationMetadata : IApplicationMetadata
{
private class Scanner : ServerTypingsGenerator
private class Scanner : TypingsGeneratorBase
{
public List<TypeDefinition> RowTypes { get; } = new();

Expand All @@ -15,6 +15,12 @@ public Scanner(IGeneratorFileSystem fileSystem, params string[] assemblyLocation

protected override void GenerateCodeFor(TypeDefinition type)
{
}

protected override void ScanAnnotationTypeAttributes(TypeDefinition type)
{
base.ScanAnnotationTypeAttributes(type);

if (TypingsUtils.IsSubclassOf(type, "Serenity.Data", "Row") ||
TypingsUtils.IsSubclassOf(type, "Serenity.Data", "Row`1"))
{
Expand Down
27 changes: 27 additions & 0 deletions src/Serenity.Net.Core/Reflection/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,31 @@ public override string ToString()

return nsb.ToString().TrimEnd();
}

/// <summary>
/// List of usings that can be safely used during code generation
/// without causing type name clashes
/// </summary>
public static readonly HashSet<string> SafeSetOfUsings = new()
{
"Serenity",
"Serenity.Abstractions",
"Serenity.ComponentModel",
"Serenity.Data",
"Serenity.Data.Mapping",
"Serenity.Extensions",
"Serenity.Localization",
"Serenity.Reflection",
"Serenity.Services",
"Serenity.Web",
"Microsoft.AspNetCore.Mvc",
"System.Globalization",
"System.Data",
"System",
"System.IO",
"System.ComponentModel",
"System.Collections.Generic"
};


}

0 comments on commit 3a43cb5

Please sign in to comment.