-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliRunner.cs
More file actions
208 lines (179 loc) · 7.64 KB
/
CliRunner.cs
File metadata and controls
208 lines (179 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.MSBuild;
using System.Text.Json;
namespace CodeAtlas;
internal sealed class CliRunner
{
internal async Task<int> RunAsync(string[] args)
{
var repoPath = ArgValue(args, "--repo");
var branchOrId = ArgValue(args, "--mr");
var targetBranch = ArgValue(args, "--target") ?? Defaults.DefaultTargetBranch;
var explicitSln = ArgValue(args, "--sln");
if (branchOrId is null || repoPath is null)
{
Console.Error.WriteLine("Usage: CodeAtlas --mr <branch-or-mr-id> --repo <repo-path> [--sln <solution>] [--target <branch>] [--json]");
return 1;
}
var originalRepoDir = Path.GetFullPath(repoPath);
string? sln = ResolveSolution(explicitSln, originalRepoDir);
Console.Error.WriteLine($"Repo: {originalRepoDir}");
Console.Error.WriteLine($"MR/Branch: {branchOrId}");
Console.Error.WriteLine($"Target branch: {targetBranch}");
if (sln is null)
{
Console.Error.WriteLine("No .sln found — running in Lite mode (non-C# analysis only).");
return await RunLiteMode(args, branchOrId, targetBranch, originalRepoDir);
}
Console.Error.WriteLine($"Solution: {sln}");
return await RunRoslynMode(args, branchOrId, targetBranch, sln, originalRepoDir);
}
private static async Task<int> RunRoslynMode(string[] args, string branchOrId, string targetBranch, string sln, string originalRepoDir)
{
WorktreeHelper? worktree = null;
var solutionToLoad = sln;
var gitDir = originalRepoDir;
try
{
worktree = await WorktreeHelper.CreateAsync(originalRepoDir, branchOrId);
if (worktree is not null)
{
gitDir = worktree.OriginalRepoPath;
var worktreeSln = ResolveWorktreeSln(sln, worktree);
if (worktreeSln is not null)
{
solutionToLoad = worktreeSln;
Console.Error.WriteLine($" Using worktree solution: {worktreeSln}");
}
else
{
Console.Error.WriteLine(" Worktree has no .sln file, falling back to original.");
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine($" Worktree setup failed ({ex.Message}), falling back to original.");
}
try
{
MSBuildLocator.RegisterDefaults();
Console.Error.WriteLine("Loading solution...");
using var ws = MSBuildWorkspace.Create();
ws.WorkspaceFailed += (_, e) =>
{
if (e.Diagnostic.Kind == Microsoft.CodeAnalysis.WorkspaceDiagnosticKind.Failure)
Console.Error.WriteLine($" Workspace error: {e.Diagnostic.Message}");
};
var solution = await ws.OpenSolutionAsync(solutionToLoad);
Console.Error.WriteLine($"Loaded {solution.Projects.Count()} projects.");
var analyzer = new MrAnalyzer(solution, solutionToLoad, gitDir, worktree?.WorktreePath);
var mrGraph = await analyzer.AnalyzeAsync(branchOrId, targetBranch);
mrGraph.RepoName = new DirectoryInfo(originalRepoDir).Name;
mrGraph.Config = LoadConfig(originalRepoDir);
return WriteOutput(args, mrGraph, branchOrId, originalRepoDir);
}
finally
{
if (worktree is not null)
await worktree.DisposeAsync();
}
}
private static async Task<int> RunLiteMode(string[] args, string branchOrId, string targetBranch, string originalRepoDir)
{
WorktreeHelper? worktree = null;
var gitDir = originalRepoDir;
try
{
worktree = await WorktreeHelper.CreateAsync(originalRepoDir, branchOrId);
if (worktree is not null)
gitDir = worktree.OriginalRepoPath;
}
catch (Exception ex)
{
Console.Error.WriteLine($" Worktree setup failed ({ex.Message}), falling back to original.");
}
try
{
var analyzer = new MrAnalyzerLite(gitDir);
var mrGraph = await analyzer.AnalyzeAsync(branchOrId, targetBranch);
mrGraph.RepoName = new DirectoryInfo(originalRepoDir).Name;
mrGraph.Config = LoadConfig(originalRepoDir);
return WriteOutput(args, mrGraph, branchOrId, originalRepoDir);
}
finally
{
if (worktree is not null)
await worktree.DisposeAsync();
}
}
private static string? ResolveSolution(string? explicitSln, string repoPath)
{
if (explicitSln is not null)
{
var full = Path.GetFullPath(explicitSln);
if (!File.Exists(full))
{
Console.Error.WriteLine($"Solution file not found: {full}");
return null;
}
return full;
}
return Directory.GetFiles(repoPath, "*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault();
}
private static string? ResolveWorktreeSln(string originalSln, WorktreeHelper worktree)
{
var slnRelative = Path.GetRelativePath(worktree.OriginalRepoPath, originalSln);
var candidate = Path.Combine(worktree.WorktreePath, slnRelative);
if (File.Exists(candidate)) return candidate;
var searchDir = worktree.WorktreeSubPath;
return Directory.Exists(searchDir)
? Directory.GetFiles(searchDir, "*.sln").FirstOrDefault()
: null;
}
private static int WriteOutput(string[] args, MrGraph mrGraph, string branchOrId, string originalRepoDir)
{
Console.Error.WriteLine($"MR: {mrGraph.Files.Count} files, {mrGraph.Edges.Count} edges");
if (args.Any(x => x == "--json"))
{
var jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
Console.WriteLine(JsonSerializer.Serialize(mrGraph, jsonOptions));
return 0;
}
var outputDir = Path.Combine(Directory.GetCurrentDirectory(), "output-codeatlas");
Directory.CreateDirectory(outputDir);
var safeMr = branchOrId.Replace("/", "_").Replace("\\", "_");
var safeRepo = mrGraph.RepoName.Replace("/", "_").Replace("\\", "_");
var outputPath = Path.GetFullPath(Path.Combine(outputDir, $"{safeRepo}-{safeMr}.html"));
File.WriteAllText(outputPath, MrHtmlRenderer.Render(mrGraph));
Console.Error.WriteLine($"Output: {outputPath}");
return 0;
}
private static CanvasConfig LoadConfig(string repoDir)
{
var configPath = Path.Combine(repoDir, ".codeatlas.json");
if (!File.Exists(configPath)) return new CanvasConfig();
try
{
return JsonSerializer.Deserialize<CanvasConfig>(File.ReadAllText(configPath), new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}) ?? new CanvasConfig();
}
catch (Exception ex)
{
Console.Error.WriteLine($"Warning: Could not parse {configPath}: {ex.Message}");
return new CanvasConfig();
}
}
private static string? ArgValue(string[] args, string flag)
{
var idx = Array.IndexOf(args, flag);
return idx >= 0 && idx + 1 < args.Length ? args[idx + 1] : null;
}
}