Skip to content

Commit d97e179

Browse files
author
akravtsova
committed
* update readme, FieldVisitor, PropertyVisitor. No base types like int in <...>. Refactoring
1 parent b464639 commit d97e179

File tree

6 files changed

+143
-49
lines changed

6 files changed

+143
-49
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ This is a generator to create a class-diagram of PlantUML from the C# source cod
99

1010
**README.md Version revision history**
1111

12-
| Version | Commit | Comment |
13-
|---------| ------------------------------------------------------------ |--------------------------------------------------------------------------------------------------------------|
14-
| 1.2 | [df40b74](https://github.com/pierre3/PlantUmlClassDiagramGenerator/commit/df40b748875c36c22e0f1267ddf4c93cd928b6b9) | Add "-addPackageTags" option |
12+
| Version | Commit | Comment |
13+
|---------|---------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
14+
| 1.3 | [b464639](https://github.com/AnastasiaKallisto/PlantUmlClassDiagramGenerator/commit/b464639c3e8027559f175fae1ac45945e56954be) | Add "-removeSystemCollectionsAssociations" option |
15+
| 1.2 | [cd9eed2](https://github.com/AnastasiaKallisto/PlantUmlClassDiagramGenerator/commit/cd9eed2539f9bac73410e6b6f1566ce979429f6f) | Add "-addPackageTags" option |
1516
| 1.1 | [e73b4fe](https://github.com/pierre3/PlantUmlClassDiagramGenerator/commit/e73b4feed9cd261271eb990a9c859f53536e8d7c) | Add "-excludeUmlBeginEndTags" option |
1617
| 1.0 | [70bb820](https://github.com/pierre3/PlantUmlClassDiagramGenerator/commit/70bb8202f7f489aa2d85ce9c25c58121c8f63aed) | Because the README.md for other languages is not always updated at the same time, a version number is needed |
1718

@@ -43,20 +44,21 @@ Run the "puml-gen" command.
4344
puml-gen InputPath [OutputPath] [-dir] [-addPackageTags] [-public | -ignore IgnoreAccessibilities] [-excludePaths ExcludePathList] [-createAssociation]
4445
```
4546

46-
- InputPath: (Required) Sets a input source file or directory name.
47-
- OutputPath: (Optional) Sets a output file or directory name.
47+
- **InputPath:** (Required) Sets a input source file or directory name.
48+
- **OutputPath:** (Optional) Sets a output file or directory name.
4849
If you omit this option, plantuml files are outputted to same directory as the input files.
49-
- -dir: (Optional) Specify when InputPath and OutputPath are directory names.
50-
- -addPackageTags: (Optional) If there is "-dir" tag, then program adds "package" tags and puts all relations in the end of include.puml
51-
- -public: (Optional) If specified, only public accessibility members are output.
52-
- -ignore: (Optional) Specify the accessibility of members to ignore, with a comma separated list.
53-
- -excludePaths: (Optional) Specify the exclude file and directory.
50+
- **-dir**: (Optional) Specify when InputPath and OutputPath are directory names.
51+
- **-addPackageTags:** (Optional) If there is "-dir" tag, then program adds "package" tags and puts all relations in the end of include.puml. Relations will not be shown in other files
52+
- **-removeSystemCollectionsAssociations**: (Optional) If there are properties or fields like "IList<T>" and other SystemCollections, there will be no relation with IList, but relation with T will be shown, if it isn't base type (string, int ...)
53+
- **-public:** (Optional) If specified, only public accessibility members are output.
54+
- **-ignore:** (Optional) Specify the accessibility of members to ignore, with a comma separated list.
55+
- **-excludePaths:** (Optional) Specify the exclude file and directory.
5456
Specifies a relative path from the "InputPath", with a comma separated list.
5557
To exclude multiple paths, which contain a specific folder name, preceed the name by "\*\*/". Example: "**/bin"
56-
- -createAssociation: (Optional) Create object associations from references of fields and properites.
57-
- -allInOne: (Optional) Only if -dir is set: copy the output of all diagrams to file include.puml (this allows a PlanUMLServer to render it).
58-
- -attributeRequired: (Optional) When this switch is enabled, only types with "PlantUmlDiagramAttribute" in the type declaration will be output.
59-
- -excludeUmlBeginEndTags: (Optional) When this switch is enabled, it will exclude the \"@startuml\" and \"@enduml\" tags from the puml file.
58+
- **-createAssociation:** (Optional) Create object associations from references of fields and properites.
59+
- **-allInOne:** (Optional) Only if -dir is set: copy the output of all diagrams to file include.puml (this allows a PlanUMLServer to render it).
60+
- **-attributeRequired:** (Optional) When this switch is enabled, only types with "PlantUmlDiagramAttribute" in the type declaration will be output.
61+
- **-excludeUmlBeginEndTags:** (Optional) When this switch is enabled, it will exclude the \"@startuml\" and \"@enduml\" tags from the puml file.
6062

6163
examples
6264
```bat

src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator/ClassDiagramGenerator.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,14 @@ private static bool HasAccessModifier(SyntaxTokenList modifiers)
193193
|| token.IsKind(SyntaxKind.ProtectedKeyword)
194194
|| token.IsKind(SyntaxKind.InternalKeyword));
195195
}
196+
197+
private static string CapitalizeFirstLetter(string input)
198+
{
199+
if (string.IsNullOrEmpty(input))
200+
return input;
201+
if (input.Length == 1)
202+
return char.ToUpper(input[0]) + "";
203+
204+
return char.ToUpper(input[0]) + input.Substring(1);
205+
}
196206
}

src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator/FieldVisitor.cs

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Microsoft.CodeAnalysis;
55
using Microsoft.CodeAnalysis.CSharp;
66
using Microsoft.CodeAnalysis.CSharp.Syntax;
7+
using PlantUmlClassDiagramGenerator.Attributes;
8+
using PlantUmlClassDiagramGenerator.Library.Enums;
79

810
namespace PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator;
911

@@ -37,21 +39,60 @@ public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
3739
|| fieldType == typeof(NullableTypeSyntax)
3840
|| isTypeParameterField)
3941
{
40-
var useLiteralInit = field.Initializer?.Value?.Kind().ToString().EndsWith("LiteralExpression") ?? false;
41-
var initValue = useLiteralInit
42-
? (" = " + escapeDictionary.Aggregate(field.Initializer.Value.ToString(),
43-
(f, e) => Regex.Replace(f, e.Key, e.Value)))
44-
: "";
45-
WriteLine($"{modifiers}{field.Identifier} : {type}{initValue}");
42+
FillAssociatedField(field, modifiers, type);
4643
}
4744
else
4845
{
49-
if (fieldType == typeof(GenericNameSyntax))
50-
{
51-
additionalTypeDeclarationNodes.Add(type);
52-
}
53-
relationships.AddAssociationFrom(node, field);
46+
if (type.GetType() == typeof(GenericNameSyntax))
47+
ProcessGenericType(node, type, field, modifiers);
48+
else
49+
relationships.AddAssociationFrom(node, field);
5450
}
5551
}
5652
}
53+
54+
private void FillAssociatedField(VariableDeclaratorSyntax field, string modifiers, TypeSyntax type)
55+
{
56+
var useLiteralInit = field.Initializer?.Value?.Kind().ToString().EndsWith("LiteralExpression") ?? false;
57+
var initValue = useLiteralInit
58+
? (" = " + escapeDictionary.Aggregate(field.Initializer.Value.ToString(),
59+
(f, e) => Regex.Replace(f, e.Key, e.Value)))
60+
: "";
61+
WriteLine($"{modifiers}{field.Identifier} : {type}{initValue}");
62+
}
63+
64+
private void ProcessGenericType(FieldDeclarationSyntax node, TypeSyntax type, VariableDeclaratorSyntax field, string modifiers)
65+
{
66+
if (this.removeSystemCollectionsAssociations)
67+
{
68+
ProcessWithoutSystemCollections(node, type, field, modifiers);
69+
}
70+
else
71+
{
72+
additionalTypeDeclarationNodes.Add(type);
73+
relationships.AddAssociationFrom(node, field);
74+
}
75+
}
76+
77+
private void ProcessWithoutSystemCollections(FieldDeclarationSyntax node, TypeSyntax type, VariableDeclaratorSyntax field, string modifiers)
78+
{
79+
var t = type.ToString().Split('<')[0];
80+
if (!Enum.TryParse(t, out SystemCollectionsTypes _))
81+
{
82+
additionalTypeDeclarationNodes.Add(type);
83+
relationships.AddAssociationFrom(node, field);
84+
}
85+
else
86+
{
87+
FillAssociatedField(field, modifiers, type);
88+
var s = type.ToString().Split('<')[1];
89+
s = s.Remove(s.Length - 1);
90+
if (!Enum.TryParse(CapitalizeFirstLetter(s), out BaseTypes _))
91+
relationships.AddAssociationFrom(node, new PlantUmlAssociationAttribute()
92+
{
93+
Association = "o--",
94+
Name = s
95+
});
96+
}
97+
}
5798
}

src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator/PropertyVisitor.cs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.CodeAnalysis.CSharp;
66
using Microsoft.CodeAnalysis.CSharp.Syntax;
77
using PlantUmlClassDiagramGenerator.Attributes;
8+
using PlantUmlClassDiagramGenerator.Library.Enums;
89

910
namespace PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator;
1011

@@ -39,33 +40,47 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
3940
else
4041
{
4142
if (type.GetType() == typeof(GenericNameSyntax))
42-
{
43-
if (this.removeSystemCollectionsAssociations)
44-
{
45-
var t = node.Type.ToString().Split('<')[0];
46-
if (!Enum.TryParse(t, out SystemCollectionsTypes _))
47-
additionalTypeDeclarationNodes.Add(type);
48-
else
49-
{
50-
FillAssociatedProperty(node, type);
51-
var s = node.Type.ToString();
52-
relationships.AddAssociationFrom(node, new PlantUmlAssociationAttribute()
53-
{
54-
Association = "o--",
55-
Name = s.Substring(s.IndexOf('<') + 1,s.LastIndexOf('>') - s.IndexOf('<') - 1)
56-
});
57-
}
58-
}
59-
else
60-
{
61-
additionalTypeDeclarationNodes.Add(type);
62-
relationships.AddAssociationFrom(node, typeIgnoringNullable);
63-
}
64-
} else
43+
ProcessGenericType(node, type, typeIgnoringNullable);
44+
else
6545
relationships.AddAssociationFrom(node, typeIgnoringNullable);
6646
}
6747
}
6848

49+
private void ProcessGenericType(PropertyDeclarationSyntax node, TypeSyntax type, TypeSyntax typeIgnoringNullable)
50+
{
51+
if (this.removeSystemCollectionsAssociations)
52+
{
53+
ProcessWithoutSystemCollections(node, type, typeIgnoringNullable);
54+
}
55+
else
56+
{
57+
additionalTypeDeclarationNodes.Add(type);
58+
relationships.AddAssociationFrom(node, typeIgnoringNullable);
59+
}
60+
}
61+
62+
private void ProcessWithoutSystemCollections(PropertyDeclarationSyntax node, TypeSyntax type, TypeSyntax typeIgnoringNullable)
63+
{
64+
var t = node.Type.ToString().Split('<')[0];
65+
if (!Enum.TryParse(t, out SystemCollectionsTypes _))
66+
{
67+
additionalTypeDeclarationNodes.Add(type);
68+
relationships.AddAssociationFrom(node, typeIgnoringNullable);
69+
}
70+
else
71+
{
72+
FillAssociatedProperty(node, type);
73+
var s = node.Type.ToString();
74+
s = s.Substring(s.IndexOf('<') + 1, s.LastIndexOf('>') - s.IndexOf('<') - 1);
75+
if (!Enum.TryParse(CapitalizeFirstLetter(s), out BaseTypes _))
76+
relationships.AddAssociationFrom(node, new PlantUmlAssociationAttribute()
77+
{
78+
Association = "o--",
79+
Name = s
80+
});
81+
}
82+
}
83+
6984
private void FillAssociatedProperty(PropertyDeclarationSyntax node, TypeSyntax type)
7085
{
7186
var modifiers = GetMemberModifiersText(node.Modifiers,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace PlantUmlClassDiagramGenerator.Library.Enums;
2+
3+
public enum BaseTypes
4+
{
5+
Int,
6+
Int16,
7+
Int32,
8+
Int64,
9+
Int128,
10+
Long,
11+
Short,
12+
Byte,
13+
Decimal,
14+
Double,
15+
Float,
16+
Char,
17+
Bool,
18+
String,
19+
StringBuilder,
20+
Object,
21+
Dynamic,
22+
DateTime,
23+
TimeSpan,
24+
Guid
25+
}
26+

src/PlantUmlClassDiagramGenerator.Library/SystemCollectionsTypes.cs renamed to src/PlantUmlClassDiagramGenerator.Library/Enums/SystemCollectionsTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace PlantUmlClassDiagramGenerator.Library;
1+
namespace PlantUmlClassDiagramGenerator.Library.Enums;
22

33
public enum SystemCollectionsTypes
44
{

0 commit comments

Comments
 (0)