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
|
|
@ -17,7 +17,7 @@ using System.Collections.Immutable;
|
|||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public interface ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ using System.Collections.Immutable;
|
|||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class ArrayMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class EnumMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class HashSetMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class KeyValuePairMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
@ -124,7 +125,7 @@ namespace SerializationGenerator
|
|||
);
|
||||
|
||||
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.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class ListMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@ using System.Collections.Immutable;
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class PrimitiveTypeMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
@ -89,7 +90,7 @@ namespace SerializationGenerator
|
|||
var propertyName = property.Name;
|
||||
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";
|
||||
|
||||
var readMethod = property.Type switch
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class PrimitiveUOTypeMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class SerializableInterfaceMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class SerializationMethodSignatureMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public record SerializableMetadata
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class SerializableMetadataComparer : IComparer<SerializableMetadata>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,53 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigration.ContentStruct.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.Text;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableMigration
|
||||
{
|
||||
public static void GenerateMigrationContentStruct(
|
||||
this StringBuilder source,
|
||||
SerializableMetadata migration
|
||||
)
|
||||
{
|
||||
const string indent = " ";
|
||||
|
||||
source.AppendLine($"{indent}ref struct V{migration.Version}Content");
|
||||
source.AppendLine($"{indent}{{");
|
||||
foreach (var serializableProperty in migration.Properties)
|
||||
{
|
||||
source.AppendLine($"{indent} internal readonly {serializableProperty.Type} {serializableProperty.Name};");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} internal V{migration.Version}Content(IGenericReader reader)");
|
||||
source.AppendLine($"{indent} {{");
|
||||
|
||||
foreach (var serializableProperty in migration.Properties)
|
||||
{
|
||||
SerializableMigrationRulesEngine.Rules[serializableProperty.Rule].GenerateDeserializationMethod(
|
||||
source,
|
||||
$"{indent} ",
|
||||
serializableProperty
|
||||
);
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} }}");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,8 +17,9 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
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(
|
||||
Compilation compilation,
|
||||
string propertyName,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigration.cs *
|
||||
* File: SerializableMigrationSchema.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 *
|
||||
|
|
@ -16,15 +16,15 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
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()
|
||||
{
|
||||
WriteIndented = true,
|
||||
|
|
@ -33,32 +33,39 @@ namespace SerializationGenerator
|
|||
ReadCommentHandling = JsonCommentHandling.Skip
|
||||
};
|
||||
|
||||
public static string GetMigrationPath(GeneratorExecutionContext context)
|
||||
{
|
||||
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
|
||||
"build_property.SerializableMigrationPath",
|
||||
out var migrationPath
|
||||
);
|
||||
private static Dictionary<string, SerializableMetadata> _cache = new();
|
||||
|
||||
return migrationPath;
|
||||
}
|
||||
private static Regex _fileRegex = new(@"\S+\.v\d+\.json$");
|
||||
|
||||
public static List<SerializableMetadata> GetMigrations(
|
||||
string migrationPath,
|
||||
public static List<SerializableMetadata> GetMigrationsByAnalyzerConfig(
|
||||
this GeneratorExecutionContext context,
|
||||
INamedTypeSymbol typeSymbol,
|
||||
int version,
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
var typeName = typeSymbol.ToDisplayString();
|
||||
|
||||
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 migration = JsonSerializer.Deserialize<SerializableMetadata>(text, options);
|
||||
var fi = new FileInfo(additionalText.Path);
|
||||
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)
|
||||
{
|
||||
migrations.Add(migration);
|
||||
|
|
@ -67,12 +74,5 @@ namespace SerializationGenerator
|
|||
|
||||
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;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public record SerializableProperty
|
||||
{
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SerializationGenerator
|
||||
namespace SerializableMigration
|
||||
{
|
||||
public class SerializablePropertyComparer : IComparer<SerializableProperty>
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue