-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/pierre3/PlantUmlClassDiag…
- Loading branch information
Showing
81 changed files
with
1,918 additions
and
778 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,4 +213,4 @@ ModelManifest.xml | |
|
||
# Intellij files | ||
.idea | ||
/test/SourceGeneratorTest/generated-uml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/PlantUmlClassDiagramGenerator.SourceGenerator/Extensions/ISymbolExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace PlantUmlClassDiagramGenerator.SourceGenerator.Extensions; | ||
|
||
public static class ISymbolExtensions | ||
{ | ||
public static readonly string AutoGeneratedNamespace = "PlantUmlClassDiagramGenerator.SourceGenerator.Attributes"; | ||
public static readonly string PlantUmlDiagramAttributeName = $"{AutoGeneratedNamespace}.PlantUmlDiagramAttribute"; | ||
public static readonly string PlantUmlIgnoreAttributeName = $"{AutoGeneratedNamespace}.PlantUmlIgnoreAttribute"; | ||
|
||
public static bool HasPlantUmlDiagramAttribute(this ISymbol symbol) | ||
=> symbol.GetAttributes().Any(attr => attr.AttributeClass?.ToString() == PlantUmlDiagramAttributeName); | ||
|
||
public static bool HasPlantUmlIgnoreAttribute(this ISymbol symbol) | ||
=> symbol.GetAttributes().Any(attr => attr.AttributeClass?.ToString() == PlantUmlIgnoreAttributeName); | ||
|
||
public static bool NamespaceStartsWith(this ISymbol symbol, string value) | ||
=> symbol.ContainingNamespace.ToString().StartsWith(value); | ||
|
||
public static object? GetPlantUmlDiagramAttributeArg(this ISymbol symbol, string argName) | ||
{ | ||
var attribute = symbol.GetAttributes().FirstOrDefault(attr => attr.AttributeClass?.ToString() == PlantUmlDiagramAttributeName); | ||
return attribute?.NamedArguments.FirstOrDefault(arg => arg.Key == argName).Value.Value; | ||
} | ||
|
||
public static bool IsAutoGeneratedSymbol(this ISymbol symbol) | ||
{ | ||
return symbol.ContainingNamespace.ToString() == AutoGeneratedNamespace | ||
|| symbol.ContainingNamespace.ToString().Contains("<"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/PlantUmlClassDiagramGenerator.SourceGenerator/Extensions/ITypeSymbolExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace PlantUmlClassDiagramGenerator.SourceGenerator.Extensions; | ||
|
||
public static class ITypeSymbolExtensions | ||
{ | ||
public static string GetMetadataName(this ITypeSymbol typeSymbol, string punctuation = "::") | ||
{ | ||
var parts = typeSymbol.ToDisplayParts( | ||
new SymbolDisplayFormat( | ||
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypes)); | ||
var containingTypes = parts | ||
.Take(parts.Length -1) | ||
.Where(p => p.Kind != SymbolDisplayPartKind.Punctuation) | ||
.Select(p => p.ToString()); | ||
return string.Join(punctuation, [.. containingTypes, typeSymbol.MetadataName]); | ||
} | ||
|
||
public static string GetTypeName(this ITypeSymbol symbol) | ||
{ | ||
return symbol.ToDisplayString( | ||
symbol.NullableAnnotation == NullableAnnotation.Annotated | ||
? NullableFlowState.MaybeNull | ||
: NullableFlowState.None, | ||
SymbolDisplayFormat.MinimallyQualifiedFormat); | ||
} | ||
|
||
public static string GetOutputFilePath(this ITypeSymbol symbol, string basePath) | ||
{ | ||
return Path.Combine([basePath, | ||
symbol.ContainingAssembly.Name, | ||
.. symbol.ContainingNamespace.ToString().Replace("<", "").Replace(">", "").Split('.'), | ||
symbol.GetMetadataName(".") + ".puml"]); | ||
} | ||
} | ||
|
||
|
||
|
18 changes: 0 additions & 18 deletions
18
src/PlantUmlClassDiagramGenerator.SourceGenerator/Extensions/TypeSymbolExtensions.cs
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
src/PlantUmlClassDiagramGenerator.SourceGenerator/GeneratorOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using PlantUmlClassDiagramGenerator.SourceGenerator.Extensions; | ||
namespace PlantUmlClassDiagramGenerator.SourceGenerator; | ||
|
||
public class GeneratorOptions(AnalyzerConfigOptionsProvider config, string assemblyName) | ||
{ | ||
|
||
[Flags] | ||
public enum Accessibilities | ||
{ | ||
None = 0, | ||
Public = 0x01, | ||
Protected = 0x02, | ||
Internal = 0x04, | ||
ProtectedInternal = 0x08, | ||
PrivateProtected = 0x10, | ||
Private = 0x20, | ||
All = Public | Protected | Internal | ProtectedInternal | PrivateProtected | Private | ||
} | ||
|
||
public string AssemblyName { get; set; } = assemblyName; | ||
public string OutputDir { get; set; } = GetOutputDir(config); | ||
public bool AttributeRequierd { get; set; } = GetAttributeRequierd(config); | ||
public Accessibilities IncludeMemberAccessibilities { get; set; } = GetIncludeAccessibilities(config); | ||
public Accessibilities ExcludeMemberAccessibilities { get; set; } = GetExcludeAccessibilities(config); | ||
|
||
public static Accessibilities GetIncludeAccessibilities(AnalyzerConfigOptionsProvider config) | ||
{ | ||
return GetAccessibilities(config, "build_property.PlantUmlGenerator_IncludeMemberAccessibilities", Accessibilities.All); | ||
} | ||
|
||
public static Accessibilities GetExcludeAccessibilities(AnalyzerConfigOptionsProvider config) | ||
{ | ||
return GetAccessibilities(config, "build_property.PlantUmlGenerator_ExcludeMemberAccessibilities", Accessibilities.None); | ||
} | ||
|
||
private static Accessibilities GetAccessibilities(AnalyzerConfigOptionsProvider config, string optionName, Accessibilities defaultValue) | ||
{ | ||
return config.GlobalOptions.TryGetValue(optionName, out var acc) | ||
? (Enum.TryParse<Accessibilities>(acc, out var value) | ||
? value | ||
: defaultValue) | ||
: defaultValue; | ||
} | ||
|
||
public static bool GetAttributeRequierd(AnalyzerConfigOptionsProvider config) | ||
{ | ||
return !config.GlobalOptions.TryGetValue("build_property.PlantUmlGenerator_AttributeRequierd", out var requierd) | ||
|| (!bool.TryParse(requierd, out var boolean) | ||
|| boolean); | ||
} | ||
|
||
public static string GetOutputDir(AnalyzerConfigOptionsProvider config) | ||
{ | ||
return config.GlobalOptions.TryGetValue("build_property.PlantUmlGenerator_OutputDir", out var path) | ||
? path | ||
: (config.GlobalOptions.TryGetValue("build_property.projectDir", out var dir) | ||
? Path.Combine(dir, "generated-uml") | ||
: ""); | ||
} | ||
|
||
public bool DeclaredTypeFilter(INamedTypeSymbol symbol) | ||
{ | ||
return !symbol.IsAutoGeneratedSymbol() | ||
&& !symbol.HasPlantUmlIgnoreAttribute() | ||
&& (!AttributeRequierd || symbol.HasPlantUmlDiagramAttribute()); | ||
} | ||
|
||
public bool MemberTypeFilter(ISymbol memberSymbol, INamedTypeSymbol typeSymbol) | ||
{ | ||
var i = typeSymbol.GetPlantUmlDiagramAttributeArg("IncludeMemberAccessibilities"); | ||
var e = typeSymbol.GetPlantUmlDiagramAttributeArg("ExcludeMemberAccessibilities"); | ||
var includes = i is not null | ||
? (Accessibilities)i | ||
: IncludeMemberAccessibilities; | ||
var excludes = e is not null | ||
? (Accessibilities)e | ||
: ExcludeMemberAccessibilities; | ||
return !memberSymbol.HasPlantUmlIgnoreAttribute() | ||
&& HasAccessibility(memberSymbol, includes, excludes); | ||
} | ||
|
||
private static bool HasAccessibility(ISymbol symbol, Accessibilities includes, Accessibilities excludes) | ||
{ | ||
return symbol.DeclaredAccessibility switch | ||
{ | ||
Accessibility.Public | ||
=> includes.HasFlag(Accessibilities.Public) | ||
&& !excludes.HasFlag(Accessibilities.Public), | ||
Accessibility.Protected | ||
=> includes.HasFlag(Accessibilities.Protected) | ||
&& !excludes.HasFlag(Accessibilities.Protected), | ||
Accessibility.Internal or Accessibility.Friend | ||
=> includes.HasFlag(Accessibilities.Internal) | ||
&& !excludes.HasFlag(Accessibilities.Internal), | ||
Accessibility.ProtectedOrInternal or Accessibility.ProtectedOrFriend | ||
=> includes.HasFlag(Accessibilities.ProtectedInternal) | ||
&& !excludes.HasFlag(Accessibilities.ProtectedInternal), | ||
Accessibility.ProtectedAndInternal or Accessibility.ProtectedAndFriend | ||
=> includes.HasFlag(Accessibilities.PrivateProtected) | ||
&& !excludes.HasFlag(Accessibilities.PrivateProtected), | ||
Accessibility.Private | ||
=> includes.HasFlag(Accessibilities.Private) | ||
&& !excludes.HasFlag(Accessibilities.Private), | ||
_ => false | ||
}; | ||
} | ||
} |
Oops, something went wrong.