-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,25 @@ | ||
namespace Dependify.Cli.Formatters; | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Dependify.Core.Graph; | ||
using Depends.Core.Graph; | ||
using Dependify.Core.Serializers; | ||
|
||
internal class JsonOutputFormatter(TextWriter textWriter) : IOutputFormatter | ||
{ | ||
private static readonly JsonSerializerOptions JsonOptions = | ||
new() | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
TypeInfoResolver = new PolymorphicTypeResolver() | ||
}; | ||
|
||
public void Dispose() => textWriter.Dispose(); | ||
|
||
public void Write<T>(T data) | ||
{ | ||
textWriter.WriteLine(JsonSerializer.Serialize(data, JsonOptions)); | ||
|
||
textWriter.Flush(); | ||
} | ||
var graph = data as DependencyGraph; | ||
|
||
internal class PolymorphicTypeResolver : DefaultJsonTypeInfoResolver | ||
{ | ||
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) | ||
if (graph is null) | ||
{ | ||
var jsonTypeInfo = base.GetTypeInfo(type, options); | ||
|
||
var baseType = typeof(Node); | ||
if (jsonTypeInfo.Type == baseType) | ||
{ | ||
jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions | ||
{ | ||
TypeDiscriminatorPropertyName = "$type", | ||
IgnoreUnrecognizedTypeDiscriminators = true, | ||
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization, | ||
DerivedTypes = | ||
{ | ||
new JsonDerivedType(typeof(SolutionReferenceNode), nameof(SolutionReferenceNode)), | ||
new JsonDerivedType(typeof(ProjectReferenceNode), nameof(ProjectReferenceNode)), | ||
new JsonDerivedType(typeof(PackageReferenceNode), nameof(PackageReferenceNode)), | ||
} | ||
}; | ||
} | ||
|
||
return jsonTypeInfo; | ||
textWriter.WriteLine(JsonGraphSerializer.Serialize(data)); | ||
} | ||
else | ||
{ | ||
textWriter.WriteLine(JsonGraphSerializer.ToString(data as DependencyGraph)); | ||
} | ||
|
||
textWriter.Flush(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace Dependify.Core.Serializers; | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Dependify.Core.Graph; | ||
|
||
public static class JsonGraphSerializer | ||
{ | ||
private static readonly JsonSerializerOptions JsonOptions = | ||
new() | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
TypeInfoResolver = new PolymorphicTypeResolver() | ||
}; | ||
|
||
public static string Serialize<T>(T value) => JsonSerializer.Serialize(value, JsonOptions); | ||
|
||
public static string ToString(DependencyGraph graph) | ||
{ | ||
ArgumentNullException.ThrowIfNull(graph); | ||
|
||
return JsonSerializer.Serialize(graph, JsonOptions); | ||
} | ||
|
||
internal class PolymorphicTypeResolver : DefaultJsonTypeInfoResolver | ||
{ | ||
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) | ||
{ | ||
var jsonTypeInfo = base.GetTypeInfo(type, options); | ||
|
||
var baseType = typeof(Node); | ||
if (jsonTypeInfo.Type == baseType) | ||
{ | ||
jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions | ||
{ | ||
TypeDiscriminatorPropertyName = "$type", | ||
IgnoreUnrecognizedTypeDiscriminators = true, | ||
UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FailSerialization, | ||
DerivedTypes = | ||
{ | ||
new JsonDerivedType(typeof(SolutionReferenceNode), nameof(SolutionReferenceNode)), | ||
new JsonDerivedType(typeof(ProjectReferenceNode), nameof(ProjectReferenceNode)), | ||
new JsonDerivedType(typeof(PackageReferenceNode), nameof(PackageReferenceNode)), | ||
} | ||
}; | ||
} | ||
|
||
return jsonTypeInfo; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace Dependify.Core.Serializers; | ||
|
||
using System.CodeDom.Compiler; | ||
using Dependify.Core.Graph; | ||
|
||
public static class MermaidC4Serializer | ||
{ | ||
public static string ToString(DependencyGraph graph) | ||
{ | ||
ArgumentNullException.ThrowIfNull(graph); | ||
|
||
using var stringWriter = new StringWriter(); | ||
using var writer = new IndentedTextWriter(stringWriter); | ||
|
||
writer.WriteLine("C4Component"); | ||
writer.WriteLine($"title {graph.Root.Id}"); | ||
Check warning on line 16 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 16 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 16 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
|
||
|
||
var projects = graph.Nodes.Where(n => n.Type == NodeConstants.Project); | ||
|
||
foreach (var project in projects) | ||
Check warning on line 20 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 20 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 20 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
Check warning on line 20 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
|
||
{ | ||
writer.WriteLine($"Container_Boundary({project.Id}, \"{project.Id}\", \"\", \"\") {{"); | ||
writer.Indent++; | ||
|
||
writer.WriteLine($"Component({project.Id}, \"{project.Id}\", \"Project\", \"\")"); | ||
|
||
var packages = graph.FindDescendants(project).OfType<PackageReferenceNode>(); | ||
|
||
if (packages.Any()) | ||
Check warning on line 29 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 29 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 29 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
Check warning on line 29 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
|
||
{ | ||
writer.WriteLine($"Container_Boundary(Packages.{project.Id}, \"Packages\", \"\", \"\") {{"); | ||
writer.Indent++; | ||
|
||
foreach (var component in packages) | ||
Check warning on line 34 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 34 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 34 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
Check warning on line 34 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
|
||
{ | ||
writer.WriteLine($"Component({component.Id}, \"{component.Id}\", \"Package\", \"\")"); | ||
writer.WriteLine( | ||
$"UpdateElementStyle({component.Id}, $fontColor=\"white\", $bgColor=\"grey\", $borderColor=\"#99CB0E\")" | ||
); | ||
} | ||
writer.Indent--; | ||
writer.WriteLine("}"); | ||
} | ||
|
||
writer.Indent--; | ||
writer.WriteLine("}"); | ||
} | ||
|
||
foreach (var project in projects) | ||
Check warning on line 49 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 49 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-ubuntu-latest
Check warning on line 49 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
Check warning on line 49 in src/Dependify.Core/Serializers/MermaidC4Serializer.cs GitHub Actions / Build-windows-latest
|
||
{ | ||
foreach (var child in graph.FindDescendants(project).OfType<ProjectReferenceNode>()) | ||
{ | ||
writer.WriteLine($"Rel({project.Id}, {child.Id}, \"\")"); | ||
} | ||
} | ||
|
||
writer.Flush(); | ||
return stringWriter.ToString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
namespace Dependify.Core; | ||
|
||
using Dependify.Core.Graph; | ||
using Depends.Core.Graph; | ||
|
||
public static class Utils | ||
{ | ||
|