Skip to content

Commit df40b74

Browse files
author
akravtsova
committed
+ new tag, works with -dir: -addPackageTags
1 parent 089fccd commit df40b74

File tree

6 files changed

+122
-11
lines changed

6 files changed

+122
-11
lines changed

README.md

Lines changed: 6 additions & 4 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.1 | [e73b4fe](https://github.com/pierre3/PlantUmlClassDiagramGenerator/commit/e73b4feed9cd261271eb990a9c859f53536e8d7c) | Add "-excludeUmlBeginEndTags" option |
12+
| Version | Commit | Comment |
13+
|---------| ------------------------------------------------------------ |--------------------------------------------------------------------------------------------------------------|
14+
| 1.2 | [e73b4fe](https://github.com/pierre3/PlantUmlClassDiagramGenerator/commit/e73b4feed9cd261271eb990a9c859f53536e8d7c) | Add "-addPackageTags" option |
15+
| 1.1 | [e73b4fe](https://github.com/pierre3/PlantUmlClassDiagramGenerator/commit/e73b4feed9cd261271eb990a9c859f53536e8d7c) | Add "-excludeUmlBeginEndTags" option |
1516
| 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 |
1617

1718
## Roslyn Source Generator
@@ -39,13 +40,14 @@ dotnet tool install --global PlantUmlClassDiagramGenerator
3940
Run the "puml-gen" command.
4041

4142
```bat
42-
puml-gen InputPath [OutputPath] [-dir] [-public | -ignore IgnoreAccessibilities] [-excludePaths ExcludePathList] [-createAssociation]
43+
puml-gen InputPath [OutputPath] [-dir] [-addPackageTags] [-public | -ignore IgnoreAccessibilities] [-excludePaths ExcludePathList] [-createAssociation]
4344
```
4445

4546
- InputPath: (Required) Sets a input source file or directory name.
4647
- OutputPath: (Optional) Sets a output file or directory name.
4748
If you omit this option, plantuml files are outputted to same directory as the input files.
4849
- -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
4951
- -public: (Optional) If specified, only public accessibility members are output.
5052
- -ignore: (Optional) Specify the accessibility of members to ignore, with a comma separated list.
5153
- -excludePaths: (Optional) Specify the exclude file and directory.

src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator.cs renamed to src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator/ClassDiagramGenerator.cs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
using Microsoft.CodeAnalysis.CSharp.Syntax;
99
using PlantUmlClassDiagramGenerator.Attributes;
1010

11-
namespace PlantUmlClassDiagramGenerator.Library;
11+
namespace PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator;
1212

1313
public class ClassDiagramGenerator(
1414
TextWriter writer,
1515
string indent,
1616
Accessibilities ignoreMemberAccessibilities = Accessibilities.None,
1717
bool createAssociation = true,
1818
bool attributeRequired = false,
19-
bool excludeUmlBeginEndTags = false) : CSharpSyntaxWalker
19+
bool excludeUmlBeginEndTags = false,
20+
bool addPackageTags = false) : CSharpSyntaxWalker
2021
{
2122
private readonly HashSet<string> types = [];
2223
private readonly List<SyntaxNode> additionalTypeDeclarationNodes = [];
2324
private readonly Accessibilities ignoreMemberAccessibilities = ignoreMemberAccessibilities;
24-
private readonly RelationshipCollection relationships = new();
25+
public readonly RelationshipCollection relationships = new();
2526
private readonly TextWriter writer = writer;
2627
private readonly string indent = indent;
2728
private int nestingDepth = 0;
2829
private readonly bool createAssociation = createAssociation;
2930
private readonly bool attributeRequired = attributeRequired;
3031
private readonly bool excludeUmlBeginEndTags = excludeUmlBeginEndTags;
32+
private readonly bool addPackageTags = addPackageTags;
3133
private readonly Dictionary<string, string> escapeDictionary = new()
3234
{
3335
{@"(?<before>[^{]){(?<after>{[^{])", "${before}&#123;${after}"},
@@ -45,7 +47,24 @@ public void GenerateInternal(SyntaxNode root)
4547
{
4648
Visit(root);
4749
GenerateAdditionalTypeDeclarations();
48-
GenerateRelationships();
50+
if (!this.addPackageTags)
51+
GenerateRelationships();
52+
}
53+
54+
public override void VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDeclarationSyntax node)
55+
{
56+
if (this.addPackageTags)
57+
VisitFileScopedNamespaceDeclaration(node, () => base.VisitFileScopedNamespaceDeclaration(node));
58+
else
59+
base.VisitFileScopedNamespaceDeclaration(node);
60+
}
61+
62+
public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
63+
{
64+
if (this.addPackageTags)
65+
VisitNamespaceDeclaration(node, () => base.VisitNamespaceDeclaration(node));
66+
else
67+
base.VisitNamespaceDeclaration(node);
4968
}
5069

5170
public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
@@ -371,7 +390,7 @@ private void WriteLine(string line)
371390
private bool SkipInnerTypeDeclaration(SyntaxNode node)
372391
{
373392
if (nestingDepth <= 0) return false;
374-
393+
if (nestingDepth == 1 && addPackageTags) return false;
375394
additionalTypeDeclarationNodes.Add(node);
376395
return true;
377396
}
@@ -411,6 +430,47 @@ private void GenerateRelationships()
411430
WriteLine(relationship.ToString());
412431
}
413432
}
433+
434+
public static string[] GenerateRelationships(RelationshipCollection relationshipCollection)
435+
{
436+
List<string> strings = new List<string>();
437+
foreach (var relationship in relationshipCollection)
438+
{
439+
strings.AddRange(relationshipCollection.Select(r => r.ToString()));
440+
}
441+
442+
return strings.ToArray();
443+
}
444+
445+
private void VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDeclarationSyntax node, Action visitBase)
446+
{
447+
if (attributeRequired && !node.AttributeLists.HasDiagramAttribute()) { return; }
448+
if (node.AttributeLists.HasIgnoreAttribute()) { return; }
449+
if (SkipInnerTypeDeclaration(node)) { return; }
450+
451+
var typeName = NamespaceNameText.From(node);
452+
453+
WriteLine($"package \"{typeName.Identifier}\" {{");
454+
nestingDepth++;
455+
visitBase();
456+
nestingDepth--;
457+
WriteLine("}");
458+
}
459+
460+
private void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node, Action visitBase)
461+
{
462+
if (attributeRequired && !node.AttributeLists.HasDiagramAttribute()) { return; }
463+
if (node.AttributeLists.HasIgnoreAttribute()) { return; }
464+
if (SkipInnerTypeDeclaration(node)) { return; }
465+
466+
var typeName = NamespaceNameText.From(node);
467+
468+
WriteLine($"package \"{typeName.Identifier}\"{{");
469+
nestingDepth++;
470+
visitBase();
471+
nestingDepth--;
472+
WriteLine("}");
473+
}
414474

415475
private void VisitTypeDeclaration(TypeDeclarationSyntax node, Action visitBase)
416476
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace PlantUmlClassDiagramGenerator.Library;
2+
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
5+
public class NamespaceNameText
6+
{
7+
public string Identifier { get; set; }
8+
9+
public static NamespaceNameText From(FileScopedNamespaceDeclarationSyntax syntax)
10+
{
11+
return new NamespaceNameText
12+
{
13+
Identifier = syntax.Name.ToString()
14+
};
15+
}
16+
17+
public static NamespaceNameText From(NamespaceDeclarationSyntax syntax)
18+
{
19+
return new NamespaceNameText
20+
{
21+
Identifier = syntax.Name.ToString()
22+
};
23+
}
24+
}

src/PlantUmlClassDiagramGenerator.Library/RelationshipCollection.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Microsoft.CodeAnalysis;
45
using Microsoft.CodeAnalysis.CSharp.Syntax;
56
using PlantUmlClassDiagramGenerator.Attributes;
@@ -10,6 +11,14 @@ public class RelationshipCollection : IEnumerable<Relationship>
1011
{
1112
private readonly IList<Relationship> items = new List<Relationship>();
1213

14+
public void AddAll(RelationshipCollection collection)
15+
{
16+
foreach (var c in collection)
17+
{
18+
items.Add(c);
19+
}
20+
}
21+
1322
public void AddInheritanceFrom(TypeDeclarationSyntax syntax)
1423
{
1524
if (syntax.BaseList == null) return;

src/PlantUmlClassDiagramGenerator/Program.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using PlantUmlClassDiagramGenerator.Library;
99
using System.Runtime.InteropServices;
10+
using PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator;
1011

1112
namespace PlantUmlClassDiagramGenerator;
1213

@@ -27,7 +28,8 @@ enum OptionType
2728
["-createAssociation"] = OptionType.Switch,
2829
["-allInOne"] = OptionType.Switch,
2930
["-attributeRequired"] = OptionType.Switch,
30-
["-excludeUmlBeginEndTags"] = OptionType.Switch
31+
["-excludeUmlBeginEndTags"] = OptionType.Switch,
32+
["-addPackageTags"] = OptionType.Switch
3133
};
3234

3335
static int Main(string[] args)
@@ -153,6 +155,7 @@ private static bool GeneratePlantUmlFromDir(Dictionary<string, string> parameter
153155

154156
var error = false;
155157
var filesToProcess = ExcludeFileFilter.GetFilesToProcess(files, excludePaths, inputRoot);
158+
RelationshipCollection relationships = new();
156159
foreach (var inputFile in filesToProcess)
157160
{
158161
Console.WriteLine($"Processing \"{inputFile}\"...");
@@ -177,8 +180,10 @@ private static bool GeneratePlantUmlFromDir(Dictionary<string, string> parameter
177180
ignoreAcc,
178181
parameters.ContainsKey("-createAssociation"),
179182
parameters.ContainsKey("-attributeRequired"),
180-
excludeUmlBeginEndTags);
183+
excludeUmlBeginEndTags,
184+
parameters.ContainsKey("-addPackageTags"));
181185
gen.Generate(root);
186+
relationships.AddAll(gen.relationships);
182187
}
183188

184189
if (parameters.ContainsKey("-allInOne"))
@@ -205,6 +210,16 @@ private static bool GeneratePlantUmlFromDir(Dictionary<string, string> parameter
205210
error = true;
206211
}
207212
}
213+
214+
if (parameters.ContainsKey("-addPackageTags"))
215+
{
216+
var lines = ClassDiagramGenerator.GenerateRelationships(relationships);
217+
foreach (string line in lines.Distinct())
218+
{
219+
includeRefs.AppendLine(line);
220+
}
221+
}
222+
208223
if (!excludeUmlBeginEndTags) includeRefs.AppendLine("@enduml");
209224
File.WriteAllText(CombinePath(outputRoot, "include.puml"), includeRefs.ToString());
210225

test/PlantUmlClassDiagramGeneratorTest/ClassDiagramGeneratorTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text;
44
using System.IO;
55
using PlantUmlClassDiagramGenerator.Library;
6+
using PlantUmlClassDiagramGenerator.Library.ClassDiagramGenerator;
67
using Xunit;
78
using Xunit.Abstractions;
89

0 commit comments

Comments
 (0)