Skip to content

Commit 90aa60c

Browse files
committed
feat: implement graph initialization and refactor graph processing methods in DependencyGraphGenerator
1 parent a1f1daa commit 90aa60c

2 files changed

Lines changed: 55 additions & 20 deletions

File tree

CodeLineCounter.Tests/DependencyGraphGeneratorTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ public void enclose_string_in_quotes_returns_quoted_null_for_null_input()
111111
Assert.Equal(string.Empty, result);
112112
}
113113

114+
// Graph initialization with empty values
115+
[Fact]
116+
public void initialize_graph_sets_default_label_and_identifier()
117+
{
118+
var graph = DependencyGraphGenerator.InitializeGraph();
119+
120+
Assert.Equal("DependencyGraph", graph.Label.Value);
121+
Assert.Equal("DependencyGraph", graph.Identifier.Value);
122+
}
123+
124+
125+
114126

115127
}
116128
}

CodeLineCounter/Services/DependencyGraphGenerator.cs

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,33 @@ public static DotGraph GenerateGraphOnly(List<DependencyRelation> dependencies,
1616
filteredDependencies = FilterNamespaceFromDependencies(dependencies, filterNamespace, filteredDependencies);
1717
filteredDependencies = FilterAssemblyFromDependencies(filterAssembly, filteredDependencies);
1818

19-
var graph = new DotGraph();
20-
graph.Directed = true;
21-
2219
var vertexInfo = new Dictionary<string, (int incoming, int outgoing)>();
2320
var namespaceGroups = new Dictionary<string, List<string>>();
24-
graph.WithLabel("DependencyGraph");
25-
graph.WithIdentifier("DependencyGraph", true);
21+
DotGraph graph = InitializeGraph();
22+
2623
// Collect degree information and group by namespace
24+
CollectDegreeInformationAndGroupByNamespace(filteredDependencies, vertexInfo, namespaceGroups);
25+
26+
// Create clusters and add nodes
27+
CreateClustersAndNodes(vertexInfo, namespaceGroups, graph);
28+
29+
// Add edges
30+
AddEdgesBetweenDependencies(filteredDependencies, graph);
31+
32+
return graph;
33+
}
34+
35+
public static void AddEdgesBetweenDependencies(List<DependencyRelation> filteredDependencies, DotGraph graph)
36+
{
2737
foreach (var dep in filteredDependencies)
2838
{
29-
GroupByNamespace(vertexInfo, namespaceGroups, dep);
39+
DotEdge edge = CreateEdge(dep);
40+
graph.Elements.Add(edge);
3041
}
42+
}
3143

32-
// Create clusters and add nodes
44+
public static void CreateClustersAndNodes(Dictionary<string, (int incoming, int outgoing)> vertexInfo, Dictionary<string, List<string>> namespaceGroups, DotGraph graph)
45+
{
3346
foreach (var nsGroup in namespaceGroups)
3447
{
3548
DotSubgraph cluster = CreateCluster(nsGroup);
@@ -43,14 +56,22 @@ public static DotGraph GenerateGraphOnly(List<DependencyRelation> dependencies,
4356

4457
graph.Elements.Add(cluster);
4558
}
59+
}
4660

47-
// Add edges
61+
public static void CollectDegreeInformationAndGroupByNamespace(List<DependencyRelation> filteredDependencies, Dictionary<string, (int incoming, int outgoing)> vertexInfo, Dictionary<string, List<string>> namespaceGroups)
62+
{
4863
foreach (var dep in filteredDependencies)
4964
{
50-
DotEdge edge = CreateEdge(dep);
51-
graph.Elements.Add(edge);
65+
GroupByNamespace(vertexInfo, namespaceGroups, dep);
5266
}
67+
}
5368

69+
public static DotGraph InitializeGraph()
70+
{
71+
var graph = new DotGraph();
72+
graph.Directed = true;
73+
graph.WithLabel("DependencyGraph");
74+
graph.WithIdentifier("DependencyGraph", true);
5475
return graph;
5576
}
5677

@@ -144,19 +165,21 @@ private static void GroupByNamespace(Dictionary<string, (int incoming, int outgo
144165

145166
public static async Task CompileGraphAndWriteToFile(string outputPath, DotGraph graph)
146167
{
147-
await using var writer = new StringWriter();
148-
var options = new CompilationOptions
149-
{
150-
Indented = true
151-
};
168+
// Use memory buffer
169+
using var memoryStream = new MemoryStream();
170+
using var writer = new StreamWriter(memoryStream);
171+
172+
var options = new CompilationOptions { Indented = true };
152173
var context = new CompilationContext(writer, options);
153174
graph.Directed = true;
154175
context.DirectedGraph = true;
155176

156177
await graph.CompileAsync(context);
157-
var result = writer.GetStringBuilder().ToString();
158-
//using sync write for reliability with async we got some issues
159-
File.WriteAllText(outputPath, result);
178+
await writer.FlushAsync(); // Ensure all data is written to memory
179+
180+
memoryStream.Position = 0; // Reset position to start
181+
using var fileStream = File.Create(outputPath);
182+
await memoryStream.CopyToAsync(fileStream); // Write complete buffer to file
160183
}
161184

162185
private static List<DependencyRelation> FilterAssemblyFromDependencies(string? filterAssembly, List<DependencyRelation> filteredDependencies)
@@ -187,13 +210,13 @@ public static string EncloseNotEmptyOrNullStringInQuotes(string? str)
187210
{
188211
if (!string.IsNullOrEmpty(str))
189212
{
190-
return $"\"{str}\"";
213+
return $"\"{str}\"";
191214
}
192215
else
193216
{
194217
return string.Empty;
195218
}
196-
219+
197220
}
198221

199222
public static string RemoveQuotes(string str)

0 commit comments

Comments
 (0)