Skip to content

Commit e5b49eb

Browse files
committed
Fix issues
1 parent 1bf2bd8 commit e5b49eb

1 file changed

Lines changed: 38 additions & 68 deletions

File tree

plugins/csharp/parser/src_csharp/Program.cs

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using static System.Console;
2-
using System.Linq;
1+
using System.Linq;
32
using Microsoft.CodeAnalysis;
43
using Microsoft.CodeAnalysis.CSharp;
54
using Microsoft.EntityFrameworkCore;
@@ -25,7 +24,16 @@ class Program
2524

2625
static async Task<int> Main(string[] args)
2726
{
28-
MSBuildLocator.RegisterDefaults();
27+
try
28+
{
29+
MSBuildLocator.RegisterDefaults();
30+
}
31+
catch (Exception ex)
32+
{
33+
Console.Error.WriteLine($"Failed to register MSBuild: {ex.Message}");
34+
return 1;
35+
}
36+
2937
_rootDir = new List<string>();
3038
int threadNum = 4;
3139

@@ -43,7 +51,7 @@ static async Task<int> Main(string[] args)
4351
}
4452
catch (Exception)
4553
{
46-
WriteLine("Error in parsing command!");
54+
Console.WriteLine("Error in parsing command!");
4755
return 1;
4856
}
4957
/*if (args.Length < 3)
@@ -85,23 +93,24 @@ static async Task<int> Main(string[] args)
8593
return 1;
8694
}*/
8795

88-
//Converting the connectionstring into entiy framwork style connectionstring
96+
if (_rootDir.Count == 0)
97+
{
98+
Console.WriteLine("Missing input path argument in CSharpParser!");
99+
return 1;
100+
}
101+
102+
string primaryInput = _rootDir[0];
103+
104+
//Converting the connectionstring into entity framework style connectionstring
89105
string csharpConnectionString = transformConnectionString();
90106

91107
var options = new DbContextOptionsBuilder<CsharpDbContext>()
92108
.UseNpgsql(csharpConnectionString)
93109
.Options;
94110

95-
CsharpDbContext _context = new CsharpDbContext(options);
111+
await using var _context = new CsharpDbContext(options);
96112
_context.Database.Migrate();
97113

98-
if (_rootDir.Count == 0)
99-
{
100-
WriteLine("Missing input path argument in CSharpParser!");
101-
return 1;
102-
}
103-
104-
string primaryInput = _rootDir[0];
105114

106115
if (IsSolutionInput(primaryInput))
107116
{
@@ -133,7 +142,7 @@ static async Task<int> Main(string[] args)
133142
return 1;
134143
}
135144

136-
WriteLine("Unsupported input path in CSharpParser!");
145+
Console.WriteLine("Unsupported input path in CSharpParser!");
137146
return 1;
138147
}
139148

@@ -157,7 +166,7 @@ private static async Task ParseLegacyMode(string csharpConnectionString, int thr
157166
{
158167
if (!Directory.Exists(p))
159168
{
160-
WriteLine("Skipping non-directory input in legacy mode: " + p);
169+
Console.WriteLine("Skipping non-directory input in legacy mode: " + p);
161170
continue;
162171
}
163172

@@ -167,7 +176,7 @@ private static async Task ParseLegacyMode(string csharpConnectionString, int thr
167176

168177
foreach (var f in allFiles)
169178
{
170-
WriteLine(f);
179+
Console.WriteLine(f);
171180
}
172181

173182
IEnumerable<string> assemblies = GetSourceFilesFromDir(_buildDir, ".dll");
@@ -184,7 +193,7 @@ private static async Task ParseLegacyMode(string csharpConnectionString, int thr
184193
SyntaxTree tree = CSharpSyntaxTree.ParseText(programText, null, file);
185194
trees.Add(tree);
186195
}
187-
Write(trees.Count);
196+
Console.Write(trees.Count);
188197

189198
CSharpCompilation compilation = CSharpCompilation.Create("CSharpCompilation")
190199
.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
@@ -220,7 +229,7 @@ private static async Task<int> ParalellRun(string csharpConnectionString, int th
220229

221230
var ParsingTasks = new List<Task<int>>();
222231
int maxThread = threadNum < trees.Count() ? threadNum : trees.Count();
223-
WriteLine(threadNum);
232+
Console.WriteLine(threadNum);
224233
for (int i = 0; i < maxThread; i++)
225234
{
226235
ParsingTasks.Add(ParseTree(contextList[i],trees[i],compilation,i));
@@ -254,11 +263,11 @@ private static async Task<int> ParseTree(CsharpDbContext context,
254263
{
255264
var ParsingTask = Task.Run(() =>
256265
{
257-
WriteLine("ParallelRun " + tree.FilePath);
266+
Console.WriteLine("ParallelRun " + tree.FilePath);
258267
SemanticModel model = compilation.GetSemanticModel(tree);
259268
var visitor = new AstVisitor(context, model, tree);
260269
visitor.Visit(tree.GetCompilationUnitRoot());
261-
WriteLine((visitor.FullyParsed ? "+" : "-") + tree.FilePath);
270+
Console.WriteLine((visitor.FullyParsed ? "+" : "-") + tree.FilePath);
262271
return index;
263272
});
264273
return await ParsingTask;
@@ -287,12 +296,12 @@ public static IEnumerable<string> GetSourceFilesFromDir(string root, string exte
287296
}
288297
catch (UnauthorizedAccessException e)
289298
{
290-
WriteLine(e.Message);
299+
Console.WriteLine(e.Message);
291300
continue;
292301
}
293302
catch (System.IO.DirectoryNotFoundException e)
294303
{
295-
WriteLine(e.Message);
304+
Console.WriteLine(e.Message);
296305
continue;
297306
}
298307

@@ -485,36 +494,6 @@ private static MSBuildWorkspace CreateMsBuildWorkspace()
485494
return workspace;
486495
}
487496

488-
private static async Task<Solution> LoadInputAsync(string inputPath)
489-
{
490-
if (File.Exists(inputPath))
491-
{
492-
if (inputPath.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
493-
inputPath.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase))
494-
{
495-
return await LoadSolutionAsync(inputPath);
496-
}
497-
if (inputPath.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
498-
{
499-
return await LoadProjectAsync(inputPath);
500-
}
501-
else
502-
{
503-
Console.Error.WriteLine($"Unsupported file type: {inputPath}");
504-
Console.Error.WriteLine("Supported: .sln, .slnx, .csproj");
505-
return null;
506-
}
507-
}
508-
if (Directory.Exists(inputPath))
509-
{
510-
return null;
511-
}
512-
else
513-
{
514-
Console.Error.WriteLine($"Input path not found: {inputPath}");
515-
return null;
516-
}
517-
}
518497

519498
private static async Task<Solution> LoadSolutionAsync(string solutionPath)
520499
{
@@ -527,16 +506,6 @@ private static async Task<Solution> LoadSolutionAsync(string solutionPath)
527506
return solution;
528507
}
529508

530-
private static async Task<Solution> LoadProjectAsync(string projectPath)
531-
{
532-
Console.WriteLine($"Loading project: {projectPath}");
533-
using var workspace = CreateMsBuildWorkspace();
534-
535-
var project = await workspace.OpenProjectAsync(projectPath);
536-
Console.WriteLine($"Project loaded: {project.Name}");
537-
538-
return project.Solution;
539-
}
540509

541510
private static async Task ParseSolutionAsync(
542511
Solution solution,
@@ -597,14 +566,15 @@ private static async Task ProcessDocumentsInParallel(
597566
int effectiveThreadCount = Math.Max(1, Math.Min(threadCount, documents.Count));
598567
var documentGroups = documents.Chunk(documents.Count / effectiveThreadCount + 1);
599568

569+
var options = new DbContextOptionsBuilder<CsharpDbContext>()
570+
.UseNpgsql(csharpConnectionString)
571+
.Options;
572+
600573
foreach (var group in documentGroups)
601574
{
602575
tasks.Add(Task.Run(async () =>
603576
{
604-
var options = new DbContextOptionsBuilder<CsharpDbContext>()
605-
.UseNpgsql(csharpConnectionString)
606-
.Options;
607-
var localDbContext = new CsharpDbContext(options);
577+
await using var localDbContext = new CsharpDbContext(options);
608578

609579
foreach (var document in group)
610580
{
@@ -644,12 +614,12 @@ private static async Task ProcessSingleDocument(
644614
visitor.Visit(root);
645615

646616
string status = visitor.FullyParsed ? "+" : "-";
647-
Console.WriteLine($"{status}{document.FilePath}");
617+
Console.WriteLine($"{status} [{projectName}] {document.FilePath}");
648618
}
649619
catch (Exception ex)
650620
{
651621
Console.Error.WriteLine($"Error parsing {document.FilePath}: {ex.Message}");
652-
Console.WriteLine($"-{document.FilePath}");
622+
Console.WriteLine($"- [{projectName}] {document.FilePath}");
653623
}
654624
}
655625
}

0 commit comments

Comments
 (0)