fix(codegen): Creates multiple steps for serialization source generator (#628)
Roslyn Source generators are not supposed to access I/O. To get around this we have to use `AdditionalFiles` to give the analyzer access to read/load the schema files. To write the schema files we have to use a separate program altogether. - [X] Adds SerializationSchemaGenerator - [X] Splits out the SerializationGenerator code
This commit is contained in:
parent
f2d44e0283
commit
b6779a7c09
48 changed files with 641 additions and 297 deletions
|
|
@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerializationGenerator", "P
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerializationGenerator.Tests", "Projects\SerializationGenerator.Tests\SerializationGenerator.Tests.csproj", "{2BC92375-BB66-4CA5-B39C-36215749FE64}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerializationGenerator.Tests", "Projects\SerializationGenerator.Tests\SerializationGenerator.Tests.csproj", "{2BC92375-BB66-4CA5-B39C-36215749FE64}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerializationSchemaGenerator", "Projects\SerializationSchemaGenerator\SerializationSchemaGenerator.csproj", "{A30150A3-796C-4C6D-B3E4-B7BEB0021701}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Analyze|x64 = Analyze|x64
|
Analyze|x64 = Analyze|x64
|
||||||
|
|
@ -65,6 +67,12 @@ Global
|
||||||
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Debug|x64.Build.0 = Debug|x64
|
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Debug|x64.Build.0 = Debug|x64
|
||||||
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Release|x64.ActiveCfg = Release|x64
|
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Release|x64.ActiveCfg = Release|x64
|
||||||
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Release|x64.Build.0 = Release|x64
|
{2BC92375-BB66-4CA5-B39C-36215749FE64}.Release|x64.Build.0 = Release|x64
|
||||||
|
{A30150A3-796C-4C6D-B3E4-B7BEB0021701}.Analyze|x64.ActiveCfg = Analyze|x64
|
||||||
|
{A30150A3-796C-4C6D-B3E4-B7BEB0021701}.Analyze|x64.Build.0 = Analyze|x64
|
||||||
|
{A30150A3-796C-4C6D-B3E4-B7BEB0021701}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{A30150A3-796C-4C6D-B3E4-B7BEB0021701}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{A30150A3-796C-4C6D-B3E4-B7BEB0021701}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{A30150A3-796C-4C6D-B3E4-B7BEB0021701}.Release|x64.Build.0 = Release|x64
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using SerializationGenerator;
|
using SourceGeneration;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace SerializationGeneratorTests
|
namespace SerializationGeneratorTests
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,10 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.Text;
|
using Microsoft.CodeAnalysis.Text;
|
||||||
|
using SerializableMigration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
|
|
@ -27,12 +26,6 @@ namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
public void Initialize(GeneratorInitializationContext context)
|
public void Initialize(GeneratorInitializationContext context)
|
||||||
{
|
{
|
||||||
context.RegisterForPostInitialization(i =>
|
|
||||||
{
|
|
||||||
SerializerSyntaxReceiver.AttributeTypes.Add("Server.SerializableAttribute");
|
|
||||||
SerializerSyntaxReceiver.AttributeTypes.Add("Server.SerializableFieldAttribute");
|
|
||||||
});
|
|
||||||
|
|
||||||
context.RegisterForSyntaxNotifications(() => new SerializerSyntaxReceiver());
|
context.RegisterForSyntaxNotifications(() => new SerializerSyntaxReceiver());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,29 +36,28 @@ namespace SerializationGenerator
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var migrationPath = SerializableMigration.GetMigrationPath(context);
|
var jsonOptions = SerializableMigrationSchema.GetJsonSerializerOptions();
|
||||||
var jsonOptions = SerializableMigration.GetJsonSerializerOptions(context.Compilation);
|
|
||||||
// List of types that _will_ become ISerializable
|
// List of types that _will_ become ISerializable
|
||||||
var serializableList = receiver
|
var serializableList = receiver.SerializableList;
|
||||||
.ClassAndFields
|
|
||||||
.Select(g => g.Key)
|
|
||||||
.Where(t => t.WillBeSerializable(context))
|
|
||||||
.ToImmutableArray();
|
|
||||||
|
|
||||||
foreach (var kvp in receiver.ClassAndFields)
|
foreach (var (classSymbol, (serializableAttr, fieldsList)) in receiver.ClassAndFields)
|
||||||
{
|
{
|
||||||
string classSource = SerializableEntityGeneration.GenerateSerializationPartialClass(
|
if (serializableAttr == null)
|
||||||
kvp.Key,
|
{
|
||||||
kvp.Value.ToImmutableArray(),
|
continue;
|
||||||
context,
|
}
|
||||||
migrationPath,
|
|
||||||
|
string classSource = context.GenerateSerializationPartialClass(
|
||||||
|
classSymbol,
|
||||||
|
serializableAttr,
|
||||||
|
fieldsList.ToImmutableArray(),
|
||||||
jsonOptions,
|
jsonOptions,
|
||||||
serializableList
|
serializableList
|
||||||
);
|
);
|
||||||
|
|
||||||
if (classSource != null)
|
if (classSource != null)
|
||||||
{
|
{
|
||||||
context.AddSource($"{kvp.Key.ToDisplayString()}.Serialization.cs", SourceText.From(classSource, Encoding.UTF8));
|
context.AddSource($"{classSymbol.ToDisplayString()}.Serialization.cs", SourceText.From(classSource, Encoding.UTF8));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,61 +20,29 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SerializableMigration;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
public static partial class SerializableEntityGeneration
|
public static partial class SerializableEntityGeneration
|
||||||
{
|
{
|
||||||
public static bool WillBeSerializable(this INamedTypeSymbol classSymbol, GeneratorExecutionContext context)
|
|
||||||
{
|
|
||||||
var compilation = context.Compilation;
|
|
||||||
|
|
||||||
var serializableEntityAttribute =
|
|
||||||
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
|
|
||||||
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
|
|
||||||
|
|
||||||
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!classSymbol.ContainsInterface(serializableInterface))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var versionValue = classSymbol.GetAttributes()
|
|
||||||
.FirstOrDefault(
|
|
||||||
attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableEntityAttribute)
|
|
||||||
)?.ConstructorArguments.FirstOrDefault().Value;
|
|
||||||
|
|
||||||
return versionValue != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GenerateSerializationPartialClass(
|
public static string GenerateSerializationPartialClass(
|
||||||
|
this GeneratorExecutionContext context,
|
||||||
INamedTypeSymbol classSymbol,
|
INamedTypeSymbol classSymbol,
|
||||||
|
AttributeData serializableAttr,
|
||||||
ImmutableArray<ISymbol> fieldsAndProperties,
|
ImmutableArray<ISymbol> fieldsAndProperties,
|
||||||
GeneratorExecutionContext context,
|
|
||||||
string migrationPath,
|
|
||||||
JsonSerializerOptions jsonSerializerOptions,
|
JsonSerializerOptions jsonSerializerOptions,
|
||||||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var compilation = context.Compilation;
|
var compilation = context.Compilation;
|
||||||
|
|
||||||
var serializableEntityAttribute =
|
|
||||||
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
|
|
||||||
var serializableFieldAttribute =
|
var serializableFieldAttribute =
|
||||||
compilation.GetTypeByMetadataName(SERIALIZABLE_FIELD_ATTRIBUTE);
|
compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_FIELD_ATTRIBUTE);
|
||||||
var serializableFieldAttrAttribute =
|
var serializableFieldAttrAttribute =
|
||||||
compilation.GetTypeByMetadataName(SERIALIZABLE_FIELD_ATTR_ATTRIBUTE);
|
compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_FIELD_ATTR_ATTRIBUTE);
|
||||||
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
|
var serializableInterface = compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_INTERFACE);
|
||||||
|
|
||||||
// This is a class symbol if the containing symbol is the namespace
|
|
||||||
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have a parent that is or derives from ISerializable, then we are in override
|
// If we have a parent that is or derives from ISerializable, then we are in override
|
||||||
var isOverride = classSymbol.BaseType.ContainsInterface(serializableInterface);
|
var isOverride = classSymbol.BaseType.ContainsInterface(serializableInterface);
|
||||||
|
|
@ -84,13 +52,8 @@ namespace SerializationGenerator
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var serializableAttribute = classSymbol.GetAttributes()
|
var version = (int)serializableAttr.ConstructorArguments[0].Value!;
|
||||||
.FirstOrDefault(
|
var encodedVersion = (bool)serializableAttr.ConstructorArguments[1].Value!;
|
||||||
attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableEntityAttribute)
|
|
||||||
);
|
|
||||||
|
|
||||||
var version = (int)serializableAttribute?.ConstructorArguments[0].Value!;
|
|
||||||
var encodedVersion = (bool)serializableAttribute.ConstructorArguments[1].Value!;
|
|
||||||
|
|
||||||
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
||||||
var className = classSymbol.Name;
|
var className = classSymbol.Name;
|
||||||
|
|
@ -161,31 +124,15 @@ namespace SerializationGenerator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string propertyName;
|
|
||||||
ITypeSymbol propertyType;
|
|
||||||
|
|
||||||
if (fieldOrPropertySymbol is IFieldSymbol fieldSymbol)
|
if (fieldOrPropertySymbol is IFieldSymbol fieldSymbol)
|
||||||
{
|
{
|
||||||
source.GenerateSerializableProperty(fieldSymbol, compilation);
|
source.GenerateSerializableProperty(fieldSymbol, compilation);
|
||||||
source.AppendLine();
|
source.AppendLine();
|
||||||
|
|
||||||
propertyName = fieldSymbol.GetPropertyName();
|
|
||||||
propertyType = fieldSymbol.Type;
|
|
||||||
}
|
|
||||||
else if (fieldOrPropertySymbol is IPropertySymbol propertySymbol)
|
|
||||||
{
|
|
||||||
propertyName = fieldOrPropertySymbol.Name;
|
|
||||||
propertyType = propertySymbol.Type;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception($"Invalid node {fieldOrPropertySymbol.Name}. Expecting a field or property node.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||||
compilation,
|
compilation,
|
||||||
propertyName,
|
fieldOrPropertySymbol,
|
||||||
propertyType,
|
|
||||||
order,
|
order,
|
||||||
allAttributes,
|
allAttributes,
|
||||||
serializableTypes
|
serializableTypes
|
||||||
|
|
@ -219,46 +166,17 @@ namespace SerializationGenerator
|
||||||
AccessModifier.None,
|
AccessModifier.None,
|
||||||
indent
|
indent
|
||||||
);
|
);
|
||||||
|
|
||||||
// bool ISerializable.UseDirtyChecking { get; } = true;
|
|
||||||
// source.GenerateAutoProperty(
|
|
||||||
// AccessModifier.None,
|
|
||||||
// "bool",
|
|
||||||
// "ISerializable.UseDirtyChecking",
|
|
||||||
// AccessModifier.None,
|
|
||||||
// null,
|
|
||||||
// indent,
|
|
||||||
// defaultValue: "true"
|
|
||||||
// );
|
|
||||||
// source.AppendLine();
|
|
||||||
}
|
}
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// If this type does not *directly* inherit `ISerializable`, then we assume it has an overridable `UseDirtyChecking`
|
|
||||||
// public override bool ISerializable.UseDirtyChecking { get; } = true;
|
|
||||||
// source.GenerateAutoProperty(
|
|
||||||
// AccessModifier.Public,
|
|
||||||
// "bool",
|
|
||||||
// "UseDirtyChecking",
|
|
||||||
// AccessModifier.None,
|
|
||||||
// null,
|
|
||||||
// indent,
|
|
||||||
// defaultValue: "true",
|
|
||||||
// isOverride: true
|
|
||||||
// );
|
|
||||||
// source.AppendLine();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Serial constructor
|
// Serial constructor
|
||||||
source.GenerateSerialCtor(context, className, isOverride);
|
source.GenerateSerialCtor(compilation, className, isOverride);
|
||||||
source.AppendLine();
|
source.AppendLine();
|
||||||
|
|
||||||
List<SerializableMetadata> migrations = new List<SerializableMetadata>();
|
List<SerializableMetadata> migrations = new List<SerializableMetadata>();
|
||||||
|
|
||||||
if (version > 0)
|
if (version > 0)
|
||||||
{
|
{
|
||||||
migrations = SerializableMigration.GetMigrations(
|
migrations = context.GetMigrationsByAnalyzerConfig(
|
||||||
migrationPath,
|
|
||||||
classSymbol,
|
classSymbol,
|
||||||
version,
|
version,
|
||||||
jsonSerializerOptions
|
jsonSerializerOptions
|
||||||
|
|
@ -298,15 +216,6 @@ namespace SerializationGenerator
|
||||||
source.GenerateClassEnd();
|
source.GenerateClassEnd();
|
||||||
source.GenerateNamespaceEnd();
|
source.GenerateNamespaceEnd();
|
||||||
|
|
||||||
// Write the migration file
|
|
||||||
var newMigration = new SerializableMetadata
|
|
||||||
{
|
|
||||||
Version = version,
|
|
||||||
Type = classSymbol.ToDisplayString(),
|
|
||||||
Properties = serializableProperties
|
|
||||||
};
|
|
||||||
SerializableMigration.WriteMigration(migrationPath, newMigration, jsonSerializerOptions);
|
|
||||||
|
|
||||||
return source.ToString();
|
return source.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SerializableMigration;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
|
|
@ -34,7 +36,7 @@ namespace SerializationGenerator
|
||||||
ImmutableArray<SerializableProperty> properties
|
ImmutableArray<SerializableProperty> properties
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
|
var genericReaderInterface = compilation.GetTypeByMetadataName(SymbolMetadata.GENERIC_READER_INTERFACE);
|
||||||
|
|
||||||
source.GenerateMethodStart(
|
source.GenerateMethodStart(
|
||||||
"Deserialize",
|
"Deserialize",
|
||||||
|
|
@ -109,7 +111,7 @@ namespace SerializationGenerator
|
||||||
.Any(
|
.Any(
|
||||||
attr => SymbolEqualityComparer.Default.Equals(
|
attr => SymbolEqualityComparer.Default.Equals(
|
||||||
attr.AttributeClass,
|
attr.AttributeClass,
|
||||||
compilation.GetTypeByMetadataName(AFTERDESERIALIZATION_ATTRIBUTE)
|
compilation.GetTypeByMetadataName(SymbolMetadata.AFTERDESERIALIZATION_ATTRIBUTE)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
|
|
@ -34,7 +35,7 @@ namespace SerializationGenerator
|
||||||
.OfType<AttributeData>()
|
.OfType<AttributeData>()
|
||||||
.FirstOrDefault(
|
.FirstOrDefault(
|
||||||
attr => attr.AttributeClass?.Equals(
|
attr => attr.AttributeClass?.Equals(
|
||||||
compilation.GetTypeByMetadataName(INVALIDATEPROPERTIES_ATTRIBUTE),
|
compilation.GetTypeByMetadataName(SymbolMetadata.INVALIDATEPROPERTIES_ATTRIBUTE),
|
||||||
SymbolEqualityComparer.Default
|
SymbolEqualityComparer.Default
|
||||||
) ?? false
|
) ?? false
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
|
|
@ -24,12 +25,12 @@ namespace SerializationGenerator
|
||||||
private static readonly ImmutableArray<string> _baseParameters = new[] { "serial" }.ToImmutableArray();
|
private static readonly ImmutableArray<string> _baseParameters = new[] { "serial" }.ToImmutableArray();
|
||||||
public static void GenerateSerialCtor(
|
public static void GenerateSerialCtor(
|
||||||
this StringBuilder source,
|
this StringBuilder source,
|
||||||
GeneratorExecutionContext context,
|
Compilation compilation,
|
||||||
string className,
|
string className,
|
||||||
bool isOverride
|
bool isOverride
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var serialType = (ITypeSymbol)context.Compilation.GetTypeByMetadataName("Server.Serial");
|
var serialType = (ITypeSymbol)compilation.GetTypeByMetadataName("Server.Serial");
|
||||||
|
|
||||||
source.GenerateConstructorStart(
|
source.GenerateConstructorStart(
|
||||||
className,
|
className,
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SerializableMigration;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
|
|
@ -29,7 +31,7 @@ namespace SerializationGenerator
|
||||||
ImmutableArray<SerializableProperty> properties
|
ImmutableArray<SerializableProperty> properties
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
|
var genericWriterInterface = compilation.GetTypeByMetadataName(SymbolMetadata.GENERIC_WRITER_INTERFACE);
|
||||||
|
|
||||||
source.GenerateMethodStart(
|
source.GenerateMethodStart(
|
||||||
"Serialize",
|
"Serialize",
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
* ModernUO *
|
* ModernUO *
|
||||||
* Copyright 2019-2021 - ModernUO Development Team *
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
* Email: hi@modernuo.com *
|
* Email: hi@modernuo.com *
|
||||||
* File: SerializableMigration.ContentStruct.cs *
|
* File: SerializationGenerator.ContentStruct.cs *
|
||||||
* *
|
* *
|
||||||
* This program is free software: you can redistribute it and/or modify *
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
* it under the terms of the GNU General Public License as published by *
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
|
@ -14,10 +14,11 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using SerializableMigration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
public static partial class SerializableMigration
|
public static partial class SerializableEntityGeneration
|
||||||
{
|
{
|
||||||
public static void GenerateMigrationContentStruct(
|
public static void GenerateMigrationContentStruct(
|
||||||
this StringBuilder source,
|
this StringBuilder source,
|
||||||
|
|
@ -17,7 +17,7 @@ using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public interface ISerializableMigrationRule
|
public interface ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class ArrayMigrationRule : ISerializableMigrationRule
|
public class ArrayMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class EnumMigrationRule : ISerializableMigrationRule
|
public class EnumMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class HashSetMigrationRule : ISerializableMigrationRule
|
public class HashSetMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class KeyValuePairMigrationRule : ISerializableMigrationRule
|
public class KeyValuePairMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
@ -124,7 +125,7 @@ namespace SerializationGenerator
|
||||||
);
|
);
|
||||||
|
|
||||||
source.AppendLine(
|
source.AppendLine(
|
||||||
$"{indent}{property.Name} = new {SerializableEntityGeneration.KEYVALUEPAIR_STRUCT}<{keyType}, {valueType}>(key, value);"
|
$"{indent}{property.Name} = new {SymbolMetadata.KEYVALUEPAIR_STRUCT}<{keyType}, {valueType}>(key, value);"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class ListMigrationRule : ISerializableMigrationRule
|
public class ListMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,9 @@ using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class PrimitiveTypeMigrationRule : ISerializableMigrationRule
|
public class PrimitiveTypeMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
@ -89,7 +90,7 @@ namespace SerializationGenerator
|
||||||
var propertyName = property.Name;
|
var propertyName = property.Name;
|
||||||
var argument = property.RuleArguments.Length >= 1 ? property.RuleArguments[0] : null;
|
var argument = property.RuleArguments.Length >= 1 ? property.RuleArguments[0] : null;
|
||||||
|
|
||||||
const string ipAddress = SerializableEntityGeneration.IPADDRESS_CLASS;
|
const string ipAddress = SymbolMetadata.IPADDRESS_CLASS;
|
||||||
const string date = "System.DateTime";
|
const string date = "System.DateTime";
|
||||||
|
|
||||||
var readMethod = property.Type switch
|
var readMethod = property.Type switch
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class PrimitiveUOTypeMigrationRule : ISerializableMigrationRule
|
public class PrimitiveUOTypeMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class SerializableInterfaceMigrationRule : ISerializableMigrationRule
|
public class SerializableInterfaceMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class SerializationMethodSignatureMigrationRule : ISerializableMigrationRule
|
public class SerializationMethodSignatureMigrationRule : ISerializableMigrationRule
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public record SerializableMetadata
|
public record SerializableMetadata
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class SerializableMetadataComparer : IComparer<SerializableMetadata>
|
public class SerializableMetadataComparer : IComparer<SerializableMetadata>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,9 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public static class SerializableMigrationRulesEngine
|
public static class SerializableMigrationRulesEngine
|
||||||
{
|
{
|
||||||
|
|
@ -45,6 +46,42 @@ namespace SerializationGenerator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static SerializableProperty? GenerateSerializableProperty(
|
||||||
|
Compilation compilation,
|
||||||
|
ISymbol fieldOrPropertySymbol,
|
||||||
|
int order,
|
||||||
|
ImmutableArray<AttributeData> attributes,
|
||||||
|
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||||
|
)
|
||||||
|
{
|
||||||
|
string propertyName;
|
||||||
|
ITypeSymbol propertyType;
|
||||||
|
|
||||||
|
if (fieldOrPropertySymbol is IFieldSymbol fieldSymbol)
|
||||||
|
{
|
||||||
|
propertyName = fieldSymbol.GetPropertyName();
|
||||||
|
propertyType = fieldSymbol.Type;
|
||||||
|
}
|
||||||
|
else if (fieldOrPropertySymbol is IPropertySymbol propertySymbol)
|
||||||
|
{
|
||||||
|
propertyName = fieldOrPropertySymbol.Name;
|
||||||
|
propertyType = propertySymbol.Type;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return GenerateSerializableProperty(
|
||||||
|
compilation,
|
||||||
|
propertyName,
|
||||||
|
propertyType,
|
||||||
|
order,
|
||||||
|
attributes,
|
||||||
|
serializableTypes
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public static SerializableProperty GenerateSerializableProperty(
|
public static SerializableProperty GenerateSerializableProperty(
|
||||||
Compilation compilation,
|
Compilation compilation,
|
||||||
string propertyName,
|
string propertyName,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
* ModernUO *
|
* ModernUO *
|
||||||
* Copyright 2019-2021 - ModernUO Development Team *
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
* Email: hi@modernuo.com *
|
* Email: hi@modernuo.com *
|
||||||
* File: SerializableMigration.cs *
|
* File: SerializableMigrationSchema.cs *
|
||||||
* *
|
* *
|
||||||
* This program is free software: you can redistribute it and/or modify *
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
* it under the terms of the GNU General Public License as published by *
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
|
@ -16,15 +16,15 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public static partial class SerializableMigration
|
public static class SerializableMigrationSchema
|
||||||
{
|
{
|
||||||
public static JsonSerializerOptions GetJsonSerializerOptions(Compilation compilation) =>
|
public static JsonSerializerOptions GetJsonSerializerOptions() =>
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
WriteIndented = true,
|
WriteIndented = true,
|
||||||
|
|
@ -33,32 +33,39 @@ namespace SerializationGenerator
|
||||||
ReadCommentHandling = JsonCommentHandling.Skip
|
ReadCommentHandling = JsonCommentHandling.Skip
|
||||||
};
|
};
|
||||||
|
|
||||||
public static string GetMigrationPath(GeneratorExecutionContext context)
|
private static Dictionary<string, SerializableMetadata> _cache = new();
|
||||||
{
|
|
||||||
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
|
|
||||||
"build_property.SerializableMigrationPath",
|
|
||||||
out var migrationPath
|
|
||||||
);
|
|
||||||
|
|
||||||
return migrationPath;
|
private static Regex _fileRegex = new(@"\S+\.v\d+\.json$");
|
||||||
}
|
|
||||||
|
|
||||||
public static List<SerializableMetadata> GetMigrations(
|
public static List<SerializableMetadata> GetMigrationsByAnalyzerConfig(
|
||||||
string migrationPath,
|
this GeneratorExecutionContext context,
|
||||||
INamedTypeSymbol typeSymbol,
|
INamedTypeSymbol typeSymbol,
|
||||||
int version,
|
int version,
|
||||||
JsonSerializerOptions options
|
JsonSerializerOptions options
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var typeName = typeSymbol.ToDisplayString();
|
var typeName = typeSymbol.ToDisplayString();
|
||||||
|
|
||||||
var migrations = new SortedSet<SerializableMetadata>(new SerializableMetadataComparer());
|
var migrations = new SortedSet<SerializableMetadata>(new SerializableMetadataComparer());
|
||||||
var migrationFiles = Directory.GetFiles(migrationPath, $"{typeName}.v*.json");
|
|
||||||
|
|
||||||
foreach (var migrationFile in migrationFiles)
|
foreach (var additionalText in context.AdditionalFiles)
|
||||||
{
|
{
|
||||||
var text = File.ReadAllText(migrationFile, Encoding.UTF8);
|
var fi = new FileInfo(additionalText.Path);
|
||||||
var migration = JsonSerializer.Deserialize<SerializableMetadata>(text, options);
|
if (!_fileRegex.IsMatch(fi.Name))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_cache.TryGetValue(fi.Name, out var migration))
|
||||||
|
{
|
||||||
|
var text = additionalText.GetText(context.CancellationToken)?.ToString();
|
||||||
|
if (text == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
migration = JsonSerializer.Deserialize<SerializableMetadata>(text, options);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeName == migration!.Type && version > migration.Version)
|
if (typeName == migration!.Type && version > migration.Version)
|
||||||
{
|
{
|
||||||
migrations.Add(migration);
|
migrations.Add(migration);
|
||||||
|
|
@ -67,12 +74,5 @@ namespace SerializationGenerator
|
||||||
|
|
||||||
return migrations.ToList();
|
return migrations.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteMigration(string migrationPath, SerializableMetadata metadata, JsonSerializerOptions options)
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(migrationPath);
|
|
||||||
var filePath = Path.Combine(migrationPath, $"{metadata.Type}.v{metadata.Version}.json");
|
|
||||||
File.WriteAllText(filePath, JsonSerializer.Serialize(metadata, options));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public record SerializableProperty
|
public record SerializableProperty
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializableMigration
|
||||||
{
|
{
|
||||||
public class SerializablePropertyComparer : IComparer<SerializableProperty>
|
public class SerializablePropertyComparer : IComparer<SerializableProperty>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
|
||||||
<PackageReference Include="Humanizer.Core" Version="2.10.1" GeneratePathProperty="true" PrivateAssets="all" />
|
<PackageReference Include="Humanizer.Core" Version="2.10.1" GeneratePathProperty="true" PrivateAssets="all" />
|
||||||
<PackageReference Include="System.Text.Json" Version="4.7.2" GeneratePathProperty="true" PrivateAssets="all" />
|
<PackageReference Include="System.Text.Json" Version="5.0.0" GeneratePathProperty="true" PrivateAssets="all" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
|
|
||||||
|
|
@ -14,69 +14,91 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
|
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
|
||||||
{
|
{
|
||||||
#pragma warning disable RS1024
|
#pragma warning disable RS1024
|
||||||
public Dictionary<INamedTypeSymbol, List<ISymbol>> ClassAndFields { get; } = new(SymbolEqualityComparer.Default);
|
public Dictionary<INamedTypeSymbol, (AttributeData?, List<ISymbol>)> ClassAndFields { get; } = new(SymbolEqualityComparer.Default);
|
||||||
#pragma warning restore RS1024
|
#pragma warning restore RS1024
|
||||||
|
|
||||||
public static HashSet<string> AttributeTypes { get; } = new();
|
public ImmutableArray<INamedTypeSymbol> SerializableList => ClassAndFields.Keys.ToImmutableArray();
|
||||||
|
|
||||||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
public void OnVisitSyntaxNode(SyntaxNode node, SemanticModel semanticModel)
|
||||||
{
|
{
|
||||||
if (context.Node is ClassDeclarationSyntax { AttributeLists: { Count: > 0 } } classDeclarationSyntax)
|
var compilation = semanticModel.Compilation;
|
||||||
|
|
||||||
|
if (node is ClassDeclarationSyntax { AttributeLists: { Count: > 0 } } classDeclarationSyntax)
|
||||||
{
|
{
|
||||||
if (context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol)
|
if (semanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (classSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString()) && !ClassAndFields.ContainsKey(classSymbol)))
|
if (classSymbol.WillBeSerializable(compilation, out var attrData))
|
||||||
{
|
{
|
||||||
ClassAndFields.Add(classSymbol, new List<ISymbol>());
|
if (ClassAndFields.TryGetValue(classSymbol, out var value))
|
||||||
|
{
|
||||||
|
var (_, fieldsList) = value;
|
||||||
|
ClassAndFields[classSymbol] = (attrData, fieldsList);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ClassAndFields.Add(classSymbol, (attrData, new List<ISymbol>()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
|
if (node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
|
||||||
{
|
{
|
||||||
foreach (var variable in fieldDeclarationSyntax.Declaration.Variables)
|
foreach (var variable in fieldDeclarationSyntax.Declaration.Variables)
|
||||||
{
|
{
|
||||||
if (context.SemanticModel.GetDeclaredSymbol(variable) is IFieldSymbol fieldSymbol)
|
if (semanticModel.GetDeclaredSymbol(variable) is IFieldSymbol fieldSymbol)
|
||||||
{
|
{
|
||||||
AddFieldOrProperty(fieldSymbol);
|
AddFieldOrProperty(fieldSymbol, compilation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (context.Node is PropertyDeclarationSyntax { AttributeLists: { Count: > 0 } } propertyDeclarationSyntax)
|
else if (node is PropertyDeclarationSyntax { AttributeLists: { Count: > 0 } } propertyDeclarationSyntax)
|
||||||
{
|
{
|
||||||
if (context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax) is IPropertySymbol propertySymbol)
|
if (semanticModel.GetDeclaredSymbol(propertyDeclarationSyntax) is IPropertySymbol propertySymbol)
|
||||||
{
|
{
|
||||||
AddFieldOrProperty(propertySymbol);
|
AddFieldOrProperty(propertySymbol, compilation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddFieldOrProperty(ISymbol symbol)
|
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) =>
|
||||||
|
OnVisitSyntaxNode(context.Node, context.SemanticModel);
|
||||||
|
|
||||||
|
private void AddFieldOrProperty(ISymbol symbol, Compilation compilation)
|
||||||
{
|
{
|
||||||
if (symbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
|
var serializableFieldAttr = compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_FIELD_ATTRIBUTE);
|
||||||
|
|
||||||
|
if (symbol.GetAttribute(serializableFieldAttr) == null)
|
||||||
{
|
{
|
||||||
var classSymbol = symbol.ContainingType;
|
return;
|
||||||
if (ClassAndFields.TryGetValue(classSymbol, out var fieldsList))
|
}
|
||||||
{
|
|
||||||
fieldsList.Add(symbol);
|
var classSymbol = symbol.ContainingType;
|
||||||
}
|
if (ClassAndFields.TryGetValue(classSymbol, out var value))
|
||||||
else
|
{
|
||||||
{
|
var (_, fieldsList) = value;
|
||||||
ClassAndFields.Add(classSymbol, new List<ISymbol> { symbol });
|
fieldsList.Add(symbol);
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (classSymbol.WillBeSerializable(compilation, out var attrData))
|
||||||
|
{
|
||||||
|
ClassAndFields.Add(classSymbol, (attrData, new List<ISymbol> { symbol }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static class Helpers
|
public static class Helpers
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public enum AccessModifier
|
public enum AccessModifier
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SourceGeneration
|
public static partial class SourceGeneration
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SourceGeneration
|
public static partial class SourceGeneration
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SourceGeneration
|
public static partial class SourceGeneration
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public enum InstanceModifier
|
public enum InstanceModifier
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SourceGeneration
|
public static partial class SourceGeneration
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,9 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SourceGeneration
|
public static partial class SourceGeneration
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ using System.Text;
|
||||||
using Humanizer;
|
using Humanizer;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SourceGeneration
|
public static partial class SourceGeneration
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* ModernUO *
|
||||||
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
|
* Email: hi@modernuo.com *
|
||||||
|
* File: SymbolMetadata.Builtin.cs *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace SourceGeneration
|
||||||
|
{
|
||||||
|
public static partial class SymbolMetadata
|
||||||
|
{
|
||||||
|
public const string LIST_CLASS = "System.Collections.Generic.List`1";
|
||||||
|
public const string HASHSET_CLASS = "System.Collections.Generic.HashSet`1";
|
||||||
|
public const string IPADDRESS_CLASS = "System.Net.IPAddress";
|
||||||
|
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
|
||||||
|
|
||||||
|
public static bool IsIpAddress(this ISymbol symbol, Compilation compilation) =>
|
||||||
|
symbol.Equals(
|
||||||
|
compilation.GetTypeByMetadataName(IPADDRESS_CLASS),
|
||||||
|
SymbolEqualityComparer.Default
|
||||||
|
);
|
||||||
|
|
||||||
|
public static bool IsKeyValuePair(this ISymbol symbol, Compilation compilation) =>
|
||||||
|
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
||||||
|
compilation.GetTypeByMetadataName(KEYVALUEPAIR_STRUCT),
|
||||||
|
SymbolEqualityComparer.Default
|
||||||
|
) == true;
|
||||||
|
|
||||||
|
public static bool IsList(this ISymbol symbol, Compilation compilation) =>
|
||||||
|
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
||||||
|
compilation.GetTypeByMetadataName(LIST_CLASS),
|
||||||
|
SymbolEqualityComparer.Default
|
||||||
|
) == true;
|
||||||
|
|
||||||
|
public static bool IsHashSet(this ISymbol symbol, Compilation compilation) =>
|
||||||
|
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
||||||
|
compilation.GetTypeByMetadataName(HASHSET_CLASS),
|
||||||
|
SymbolEqualityComparer.Default
|
||||||
|
) == true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
* ModernUO *
|
* ModernUO *
|
||||||
* Copyright 2019-2021 - ModernUO Development Team *
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
* Email: hi@modernuo.com *
|
* Email: hi@modernuo.com *
|
||||||
* File: SerializableEntityGeneration.MetadataTypes.cs *
|
* File: SymbolMetadata.UO.cs *
|
||||||
* *
|
* *
|
||||||
* This program is free software: you can redistribute it and/or modify *
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
* it under the terms of the GNU General Public License as published by *
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
|
@ -13,19 +13,15 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace SerializationGenerator
|
namespace SourceGeneration
|
||||||
{
|
{
|
||||||
public static partial class SerializableEntityGeneration
|
public static partial class SymbolMetadata
|
||||||
{
|
{
|
||||||
public const string LIST_CLASS = "System.Collections.Generic.List`1";
|
|
||||||
public const string HASHSET_CLASS = "System.Collections.Generic.HashSet`1";
|
|
||||||
public const string IPADDRESS_CLASS = "System.Net.IPAddress";
|
|
||||||
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
|
|
||||||
|
|
||||||
public const string INVALIDATEPROPERTIES_ATTRIBUTE = "Server.InvalidatePropertiesAttribute";
|
public const string INVALIDATEPROPERTIES_ATTRIBUTE = "Server.InvalidatePropertiesAttribute";
|
||||||
public const string AFTERDESERIALIZATION_ATTRIBUTE = "Server.AfterDeserializationAttribute";
|
public const string AFTERDESERIALIZATION_ATTRIBUTE = "Server.AfterDeserializationAttribute";
|
||||||
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
|
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
|
||||||
|
|
@ -132,12 +128,6 @@ namespace SerializationGenerator
|
||||||
SymbolEqualityComparer.Default
|
SymbolEqualityComparer.Default
|
||||||
);
|
);
|
||||||
|
|
||||||
public static bool IsIpAddress(this ISymbol symbol, Compilation compilation) =>
|
|
||||||
symbol.Equals(
|
|
||||||
compilation.GetTypeByMetadataName(IPADDRESS_CLASS),
|
|
||||||
SymbolEqualityComparer.Default
|
|
||||||
);
|
|
||||||
|
|
||||||
public static bool IsRace(this ISymbol symbol, Compilation compilation) =>
|
public static bool IsRace(this ISymbol symbol, Compilation compilation) =>
|
||||||
symbol.Equals(
|
symbol.Equals(
|
||||||
compilation.GetTypeByMetadataName(RACE_CLASS),
|
compilation.GetTypeByMetadataName(RACE_CLASS),
|
||||||
|
|
@ -150,22 +140,28 @@ namespace SerializationGenerator
|
||||||
SymbolEqualityComparer.Default
|
SymbolEqualityComparer.Default
|
||||||
);
|
);
|
||||||
|
|
||||||
public static bool IsKeyValuePair(this ISymbol symbol, Compilation compilation) =>
|
public static AttributeData? GetAttribute(this ISymbol symbol, ISymbol attrSymbol) =>
|
||||||
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
symbol
|
||||||
compilation.GetTypeByMetadataName(KEYVALUEPAIR_STRUCT),
|
.GetAttributes()
|
||||||
SymbolEqualityComparer.Default
|
.FirstOrDefault(
|
||||||
) == true;
|
ad => ad.AttributeClass != null && SymbolEqualityComparer.Default.Equals(ad.AttributeClass, attrSymbol)
|
||||||
|
);
|
||||||
|
|
||||||
public static bool IsList(this ISymbol symbol, Compilation compilation) =>
|
public static bool WillBeSerializable(this INamedTypeSymbol classSymbol, Compilation compilation, out AttributeData? attributeData)
|
||||||
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
{
|
||||||
compilation.GetTypeByMetadataName(LIST_CLASS),
|
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
|
||||||
SymbolEqualityComparer.Default
|
|
||||||
) == true;
|
|
||||||
|
|
||||||
public static bool IsHashSet(this ISymbol symbol, Compilation compilation) =>
|
if (!classSymbol.ContainsInterface(serializableInterface))
|
||||||
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
{
|
||||||
compilation.GetTypeByMetadataName(HASHSET_CLASS),
|
attributeData = null;
|
||||||
SymbolEqualityComparer.Default
|
return false;
|
||||||
) == true;
|
}
|
||||||
|
|
||||||
|
var serializableEntityAttribute =
|
||||||
|
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
|
||||||
|
|
||||||
|
attributeData = classSymbol.GetAttribute(serializableEntityAttribute);
|
||||||
|
return attributeData != null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
28
Projects/SerializationGenerator/Utility.cs
Normal file
28
Projects/SerializationGenerator/Utility.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* ModernUO *
|
||||||
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
|
* Email: hi@modernuo.com *
|
||||||
|
* File: Utility.cs *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SerializationGenerator
|
||||||
|
{
|
||||||
|
public static class Utility
|
||||||
|
{
|
||||||
|
public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
|
||||||
|
{
|
||||||
|
key = tuple.Key;
|
||||||
|
value = tuple.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
Projects/SerializationSchemaGenerator/.gitignore
vendored
Normal file
1
Projects/SerializationSchemaGenerator/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Output/
|
||||||
84
Projects/SerializationSchemaGenerator/Application.cs
Normal file
84
Projects/SerializationSchemaGenerator/Application.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* ModernUO *
|
||||||
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
|
* Email: hi@modernuo.com *
|
||||||
|
* File: Application.cs *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using SerializationGenerator;
|
||||||
|
|
||||||
|
namespace SerializationSchemaGenerator
|
||||||
|
{
|
||||||
|
public static class Application
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
if (args.Length < 1)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Usage: dotnet SerializationSchemaGenerator.dll <path to solution>");
|
||||||
|
}
|
||||||
|
|
||||||
|
var solutionPath = args[0];
|
||||||
|
|
||||||
|
Parallel.ForEach(
|
||||||
|
SourceCodeAnalysis.GetCompilation(solutionPath),
|
||||||
|
(projectCompilation) =>
|
||||||
|
{
|
||||||
|
var (project, compilation) = projectCompilation;
|
||||||
|
if (project.Name.EndsWith(".Tests", StringComparison.Ordinal) || project.Name == "Benchmarks")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var projectFile = new FileInfo(project.FilePath!);
|
||||||
|
var migrationPath = Path.Join(projectFile.Directory?.FullName, "Migrations");
|
||||||
|
Directory.CreateDirectory(migrationPath);
|
||||||
|
|
||||||
|
var syntaxReceiver = new SerializerSyntaxReceiver();
|
||||||
|
|
||||||
|
foreach (var syntaxTree in compilation.SyntaxTrees)
|
||||||
|
{
|
||||||
|
var root = syntaxTree.GetRoot();
|
||||||
|
var syntaxVisitor = new SyntaxVisitor(compilation.GetSemanticModel(syntaxTree), syntaxReceiver);
|
||||||
|
syntaxVisitor.Visit(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
var jsonOptions = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
AllowTrailingCommas = true,
|
||||||
|
IgnoreNullValues = true,
|
||||||
|
ReadCommentHandling = JsonCommentHandling.Skip
|
||||||
|
};
|
||||||
|
|
||||||
|
var serializableTypes = syntaxReceiver.SerializableList;
|
||||||
|
|
||||||
|
foreach (var (classSymbol, (attributeData, fieldsList)) in syntaxReceiver.ClassAndFields)
|
||||||
|
{
|
||||||
|
compilation.GenerateSchema(
|
||||||
|
classSymbol,
|
||||||
|
attributeData,
|
||||||
|
fieldsList.ToImmutableArray(),
|
||||||
|
migrationPath,
|
||||||
|
jsonOptions,
|
||||||
|
serializableTypes
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
98
Projects/SerializationSchemaGenerator/SchemaGenerator.cs
Normal file
98
Projects/SerializationSchemaGenerator/SchemaGenerator.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* ModernUO *
|
||||||
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
|
* Email: hi@modernuo.com *
|
||||||
|
* File: SchemaGenerator.cs *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using SerializableMigration;
|
||||||
|
using SourceGeneration;
|
||||||
|
|
||||||
|
namespace SerializationSchemaGenerator
|
||||||
|
{
|
||||||
|
public static class SchemaGenerator
|
||||||
|
{
|
||||||
|
public static void GenerateSchema(
|
||||||
|
this Compilation compilation,
|
||||||
|
INamedTypeSymbol classSymbol,
|
||||||
|
AttributeData serializableAttr,
|
||||||
|
ImmutableArray<ISymbol> fieldsAndProperties,
|
||||||
|
string migrationPath,
|
||||||
|
JsonSerializerOptions jsonSerializerOptions,
|
||||||
|
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var serializableFieldAttribute =
|
||||||
|
compilation.GetTypeByMetadataName(SymbolMetadata.SERIALIZABLE_FIELD_ATTRIBUTE);
|
||||||
|
|
||||||
|
var version = (int)serializableAttr.ConstructorArguments[0].Value!;
|
||||||
|
|
||||||
|
var serializablePropertySet = new SortedSet<SerializableProperty>(new SerializablePropertyComparer());
|
||||||
|
|
||||||
|
foreach (var fieldOrPropertySymbol in fieldsAndProperties)
|
||||||
|
{
|
||||||
|
var allAttributes = fieldOrPropertySymbol.GetAttributes();
|
||||||
|
|
||||||
|
var serializableFieldAttr = allAttributes
|
||||||
|
.FirstOrDefault(
|
||||||
|
attr =>
|
||||||
|
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttribute)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (serializableFieldAttr == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var order = (int)serializableFieldAttr.ConstructorArguments[0].Value!;
|
||||||
|
|
||||||
|
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||||
|
compilation,
|
||||||
|
fieldOrPropertySymbol,
|
||||||
|
order,
|
||||||
|
allAttributes,
|
||||||
|
serializableTypes
|
||||||
|
);
|
||||||
|
|
||||||
|
if (serializableProperty == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
serializablePropertySet.Add(serializableProperty);
|
||||||
|
}
|
||||||
|
|
||||||
|
var serializableProperties = serializablePropertySet.ToImmutableArray();
|
||||||
|
|
||||||
|
// Write the migration file
|
||||||
|
var newMigration = new SerializableMetadata
|
||||||
|
{
|
||||||
|
Version = version,
|
||||||
|
Type = classSymbol.ToDisplayString(),
|
||||||
|
Properties = serializableProperties
|
||||||
|
};
|
||||||
|
WriteMigration(migrationPath, newMigration, jsonSerializerOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteMigration(string migrationPath, SerializableMetadata metadata, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(migrationPath);
|
||||||
|
var filePath = Path.Combine(migrationPath, $"{metadata.Type}.v{metadata.Version}.json");
|
||||||
|
File.WriteAllText(filePath, JsonSerializer.Serialize(metadata, options));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
<OutDir>Output</OutDir>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.9.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
49
Projects/SerializationSchemaGenerator/SourceCodeAnalysis.cs
Normal file
49
Projects/SerializationSchemaGenerator/SourceCodeAnalysis.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* ModernUO *
|
||||||
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
|
* Email: hi@modernuo.com *
|
||||||
|
* File: SourceCodeAnalysis.cs *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.Build.Locator;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.MSBuild;
|
||||||
|
|
||||||
|
namespace SerializationSchemaGenerator
|
||||||
|
{
|
||||||
|
public static class SourceCodeAnalysis
|
||||||
|
{
|
||||||
|
public static List<(Project, Compilation)> GetCompilation(string solutionPath)
|
||||||
|
{
|
||||||
|
if (!File.Exists(solutionPath) || !solutionPath.EndsWith(".sln", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
throw new FileNotFoundException($"Could not open a valid solution at location {solutionPath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
MSBuildLocator.RegisterDefaults();
|
||||||
|
|
||||||
|
var workspace = MSBuildWorkspace.Create();
|
||||||
|
|
||||||
|
var solutionToAnalyze = workspace.OpenSolutionAsync(solutionPath).Result;
|
||||||
|
|
||||||
|
var results = solutionToAnalyze.Projects.AsParallel()
|
||||||
|
.Select((project) => (project, project?.GetCompilationAsync().Result))
|
||||||
|
.Where((value) => value.Result != null)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
49
Projects/SerializationSchemaGenerator/SyntaxVisitor.cs
Normal file
49
Projects/SerializationSchemaGenerator/SyntaxVisitor.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* ModernUO *
|
||||||
|
* Copyright 2019-2021 - ModernUO Development Team *
|
||||||
|
* Email: hi@modernuo.com *
|
||||||
|
* File: SyntaxVisitor.cs *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU General Public License as published by *
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or *
|
||||||
|
* (at your option) any later version. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU General Public License *
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
using SerializationGenerator;
|
||||||
|
|
||||||
|
namespace SerializationSchemaGenerator
|
||||||
|
{
|
||||||
|
public class SyntaxVisitor : CSharpSyntaxWalker
|
||||||
|
{
|
||||||
|
private readonly SemanticModel _semanticModel;
|
||||||
|
private readonly SerializerSyntaxReceiver _syntaxReceiver;
|
||||||
|
|
||||||
|
public SyntaxVisitor(SemanticModel semanticModel, SerializerSyntaxReceiver syntaxReceiver)
|
||||||
|
{
|
||||||
|
_semanticModel = semanticModel;
|
||||||
|
_syntaxReceiver = syntaxReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
|
||||||
|
{
|
||||||
|
_syntaxReceiver.OnVisitSyntaxNode(node, _semanticModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
|
||||||
|
{
|
||||||
|
_syntaxReceiver.OnVisitSyntaxNode(node, _semanticModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
|
||||||
|
{
|
||||||
|
_syntaxReceiver.OnVisitSyntaxNode(node, _semanticModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -41,32 +41,26 @@
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
||||||
<PackageReference Include="Zlib.Bindings" Version="1.5.0" />
|
<PackageReference Include="Zlib.Bindings" Version="1.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<!-- <ItemGroup>-->
|
<ItemGroup>
|
||||||
<!-- <ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj">-->
|
<ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj">
|
||||||
<!-- <SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>-->
|
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
|
||||||
<!-- <OutputItemType>Analyzer</OutputItemType>-->
|
<OutputItemType>Analyzer</OutputItemType>
|
||||||
<!-- <ReferenceOutputAssembly>false</ReferenceOutputAssembly>-->
|
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
|
||||||
<!-- <PrivateAssets>all</PrivateAssets>-->
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<!-- </ProjectReference>-->
|
</ProjectReference>
|
||||||
<!-- </ItemGroup>-->
|
</ItemGroup>
|
||||||
<!-- <Target Name="AddSourceGeneratedFiles" AfterTargets="CoreCompile">-->
|
<Target Name="AddSourceGeneratedFiles" AfterTargets="CoreCompile">
|
||||||
<!-- <ItemGroup>-->
|
<ItemGroup>
|
||||||
<!-- <Compile Include="Generated\**" />-->
|
<Compile Include="Generated\**" />
|
||||||
<!-- </ItemGroup>-->
|
</ItemGroup>
|
||||||
<!-- </Target>-->
|
</Target>
|
||||||
<!-- <Target Name="RemoveSourceGeneratedFiles" BeforeTargets="CoreCompile">-->
|
<Target Name="RemoveSourceGeneratedFiles" BeforeTargets="CoreCompile">
|
||||||
<!-- <ItemGroup>-->
|
<ItemGroup>
|
||||||
<!-- <Compile Remove="Generated\**" />-->
|
<Compile Remove="Generated\**" />
|
||||||
<!-- </ItemGroup>-->
|
</ItemGroup>
|
||||||
<!-- <RemoveDir Directories="Generated" />-->
|
<RemoveDir Directories="Generated" />
|
||||||
<!-- </Target>-->
|
</Target>
|
||||||
<!-- <ItemGroup>-->
|
<ItemGroup>
|
||||||
<!-- <CompilerVisibleProperty Include="SerializableMigrationPath" />-->
|
<AdditionalFiles Include="Migrations/*.v*.json" />
|
||||||
<!-- </ItemGroup>-->
|
</ItemGroup>
|
||||||
<!-- <ItemGroup>-->
|
|
||||||
<!-- <Folder Include="Migrations" />-->
|
|
||||||
<!-- </ItemGroup>-->
|
|
||||||
<!-- <PropertyGroup>-->
|
|
||||||
<!-- <SerializableMigrationPath>.\Migrations\</SerializableMigrationPath>-->
|
|
||||||
<!-- </PropertyGroup>-->
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -39,24 +39,13 @@
|
||||||
<PackageReference Include="Zlib.Bindings" Version="1.5.0" />
|
<PackageReference Include="Zlib.Bindings" Version="1.5.0" />
|
||||||
<PackageReference Include="Argon2.Bindings" Version="1.9.1" />
|
<PackageReference Include="Argon2.Bindings" Version="1.9.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<!-- <ItemGroup>-->
|
<ItemGroup>
|
||||||
<!-- <ProjectReference Include="..\SerializationGenerator\SerializationGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />-->
|
<AdditionalFiles Include="Migrations/*.v*.json" />
|
||||||
<!-- </ItemGroup>-->
|
</ItemGroup>
|
||||||
<!-- <Target Name="AddSourceGeneratedFiles" AfterTargets="CoreCompile">-->
|
<!-- <Target Name="GenerateMigrationSchemas" AfterTargets="AfterBuild">-->
|
||||||
<!-- <ItemGroup>-->
|
<!-- <Message Importance="high" Text="Building serialization schema generator" />-->
|
||||||
<!-- <Compile Include="Generated\**" />-->
|
<!-- <Exec Command="dotnet build -c Release $(ProjectDir)../SerializationSchemaGenerator/SerializationSchemaGenerator.csproj" />-->
|
||||||
<!-- </ItemGroup>-->
|
<!-- <Message Importance="high" Text="Generating serialization schemas" />-->
|
||||||
<!-- </Target>-->
|
<!-- <Exec Command="dotnet $(ProjectDir)../SerializationSchemaGenerator/Output/SerializationSchemaGenerator.dll $(ProjectDir)../../ModernUO.sln" />-->
|
||||||
<!-- <Target Name="RemoveSourceGeneratedFiles" BeforeTargets="CoreCompile">-->
|
<!-- </Target>-->
|
||||||
<!-- <ItemGroup>-->
|
|
||||||
<!-- <Compile Remove="Generated\**" />-->
|
|
||||||
<!-- </ItemGroup>-->
|
|
||||||
<!-- <RemoveDir Directories="Generated" />-->
|
|
||||||
<!-- </Target>-->
|
|
||||||
<!-- <ItemGroup>-->
|
|
||||||
<!-- <CompilerVisibleProperty Include="SerializableMigrationPath" />-->
|
|
||||||
<!-- </ItemGroup>-->
|
|
||||||
<!-- <PropertyGroup>-->
|
|
||||||
<!-- <SerializableMigrationPath>.\Migrations\</SerializableMigrationPath>-->
|
|
||||||
<!-- </PropertyGroup>-->
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
13
publish.cmd
13
publish.cmd
|
|
@ -30,10 +30,13 @@ fi
|
||||||
echo dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
echo dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
||||||
dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
||||||
|
|
||||||
echo dotnet publish ${config} ${os} --no-restore --self-contained=false -o Distribution Projects/Server/Server.csproj
|
|
||||||
dotnet publish ${config} ${os} --no-restore --self-contained=false -o Distribution Projects/Server/Server.csproj
|
|
||||||
echo dotnet publish ${config} ${os} --no-restore --self-contained=false -o Distribution/Assemblies Projects/UOContent/UOContent.csproj
|
echo dotnet publish ${config} ${os} --no-restore --self-contained=false -o Distribution/Assemblies Projects/UOContent/UOContent.csproj
|
||||||
dotnet publish ${config} ${os} --no-restore --self-contained=false -o Distribution/Assemblies Projects/UOContent/UOContent.csproj
|
dotnet publish ${config} ${os} --no-restore --self-contained=false -o Distribution/Assemblies Projects/UOContent/UOContent.csproj
|
||||||
|
echo dotnet build -c Release Projects/SerializationSchemaGenerator/SerializationSchemaGenerator.csproj
|
||||||
|
dotnet build -c Release Projects/SerializationSchemaGenerator/SerializationSchemaGenerator.csproj
|
||||||
|
echo Generating serialization schemas
|
||||||
|
dotnet Projects/SerializationSchemaGenerator/Output/SerializationSchemaGenerator.dll ModernUO.sln
|
||||||
|
|
||||||
exit $?
|
exit $?
|
||||||
|
|
||||||
:CMDSCRIPT
|
:CMDSCRIPT
|
||||||
|
|
@ -57,7 +60,9 @@ IF "%~2" == "" (
|
||||||
echo dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
echo dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
||||||
dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
dotnet restore --force-evaluate --source https://api.nuget.org/v3/index.json
|
||||||
|
|
||||||
echo dotnet publish %config% %os% --no-restore --self-contained=false -o Distribution Projects\Server\Server.csproj
|
|
||||||
dotnet publish %config% %os% --no-restore --self-contained=false -o Distribution Projects\Server\Server.csproj
|
|
||||||
echo dotnet publish %config% %os% --no-restore --self-contained=false -o Distribution\Assemblies Projects\UOContent\UOContent.csproj
|
echo dotnet publish %config% %os% --no-restore --self-contained=false -o Distribution\Assemblies Projects\UOContent\UOContent.csproj
|
||||||
dotnet publish %config% %os% --no-restore --self-contained=false -o Distribution\Assemblies Projects\UOContent\UOContent.csproj
|
dotnet publish %config% %os% --no-restore --self-contained=false -o Distribution\Assemblies Projects\UOContent\UOContent.csproj
|
||||||
|
echo dotnet build -c Release Projects/SerializationSchemaGenerator/SerializationSchemaGenerator.csproj
|
||||||
|
dotnet build -c Release Projects/SerializationSchemaGenerator/SerializationSchemaGenerator.csproj
|
||||||
|
echo Generating serialization schemas
|
||||||
|
dotnet Projects/SerializationSchemaGenerator/Output/SerializationSchemaGenerator.dll ModernUO.sln
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue