Repository navigation
Expand file tree
/
Copy pathCodeMeshRefreshWorkflow.cs
More file actions
146 lines (120 loc) · 4.44 KB
/
Copy pathCodeMeshRefreshWorkflow.cs
File metadata and controls
146 lines (120 loc) · 4.44 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
namespace CodeMesh.Control.Configuration;
public sealed record CodeMeshRefreshTarget(string RootPath, string? SolutionPath);
public static class CodeMeshRefreshWorkflow
{
public static CodeMeshRefreshTarget ResolveTarget(
string? startPath = null,
string? solutionPath = null,
string? projectPath = null,
string? language = null)
{
var rootPath = FindRepositoryRoot(startPath);
var solution = ResolveSolutionPath(rootPath, solutionPath, projectPath, language);
return new CodeMeshRefreshTarget(rootPath, solution);
}
public static string[] BuildRefreshIngestArgs(string[] args, CodeMeshRefreshTarget target)
{
var normalized = new List<string> { "ingest" };
var skipNext = false;
for (var index = 0; index < args.Length; index++)
{
if (skipNext)
{
skipNext = false;
continue;
}
var arg = args[index];
if (index == 0 && string.Equals(arg, "refresh", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (IsOptionWithValue(arg, "--root", "--repository-root", "--solution", "--solution-path"))
{
skipNext = true;
continue;
}
normalized.Add(arg);
}
normalized.Add("--root");
normalized.Add(target.RootPath);
if (!string.IsNullOrWhiteSpace(target.SolutionPath))
{
normalized.Add("--solution");
normalized.Add(target.SolutionPath);
}
return normalized.ToArray();
}
private static string? ResolveSolutionPath(
string rootPath,
string? solutionPath,
string? projectPath,
string? language)
{
if (!string.IsNullOrWhiteSpace(solutionPath))
{
var fullSolutionPath = Path.GetFullPath(solutionPath);
if (!File.Exists(fullSolutionPath))
{
throw new InvalidOperationException($"Solution file not found: {fullSolutionPath}");
}
return fullSolutionPath;
}
if (!string.IsNullOrWhiteSpace(projectPath) || !IsCSharpLanguage(language))
{
return null;
}
return FindDefaultSolution(rootPath);
}
private static string FindRepositoryRoot(string? startPath)
{
var candidate = string.IsNullOrWhiteSpace(startPath)
? Directory.GetCurrentDirectory()
: startPath;
var fullPath = Path.GetFullPath(candidate);
var directory = File.Exists(fullPath)
? new DirectoryInfo(Path.GetDirectoryName(fullPath) ?? fullPath)
: new DirectoryInfo(fullPath);
while (directory is not null)
{
if (IsRepositoryRoot(directory.FullName))
{
return directory.FullName;
}
directory = directory.Parent;
}
return fullPath;
}
private static bool IsRepositoryRoot(string path)
{
return Directory.Exists(Path.Combine(path, ".git")) ||
File.Exists(Path.Combine(path, CodeMeshSelfWorkflow.MarkerFileName)) ||
File.Exists(Path.Combine(path, CodeMeshSelfWorkflow.SolutionFileName)) ||
Directory.EnumerateFiles(path, "*.sln", SearchOption.TopDirectoryOnly).Any() ||
Directory.EnumerateFiles(path, "*.slnx", SearchOption.TopDirectoryOnly).Any();
}
private static string? FindDefaultSolution(string rootPath)
{
var codeMeshSolution = Path.Combine(rootPath, CodeMeshSelfWorkflow.SolutionFileName);
if (File.Exists(codeMeshSolution))
{
return codeMeshSolution;
}
var solutions = Directory
.EnumerateFiles(rootPath, "*.sln", SearchOption.TopDirectoryOnly)
.Concat(Directory.EnumerateFiles(rootPath, "*.slnx", SearchOption.TopDirectoryOnly))
.ToArray();
return solutions.Length == 1 ? solutions[0] : null;
}
private static bool IsCSharpLanguage(string? language)
{
if (string.IsNullOrWhiteSpace(language))
{
return true;
}
return language.Trim().ToLowerInvariant() is "csharp" or "cs" or "c#";
}
private static bool IsOptionWithValue(string arg, params string[] names)
{
return names.Any(name => string.Equals(arg, name, StringComparison.OrdinalIgnoreCase));
}
}