-
Notifications
You must be signed in to change notification settings - Fork 3
/
MyAppsContext.cs
177 lines (156 loc) · 6.49 KB
/
MyAppsContext.cs
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
using System.Text;
namespace CodeContext;
public class MyAppsContext
{
public static string GitRepoRoot { get; private set; }
public static string GetUserInput(string prompt)
{
Console.Write(prompt);
return Console.ReadLine();
}
public static string GetProjectStructure(string path, int indent = 0)
{
if (GitRepoRoot == null) GitRepoRoot = FindGitRepoRoot(path);
if (indent == 0) Console.WriteLine("📁 Analyzing directory structure...");
var entries = Directory.EnumerateFileSystemEntries(path)
.OrderBy(e => e)
.Where(e => !FileChecker.ShouldSkip(new FileInfo(e), GitRepoRoot))
.ToList();
var sb = new StringBuilder();
// Group files by their type/purpose
var files = entries.Where(e => !Directory.Exists(e))
.Select(f => new FileInfo(f))
.ToList();
var projectFiles = files.Where(f => f.Extension == ".csproj" || f.Extension == ".sln");
var configFiles = files.Where(f =>
f.Name.StartsWith("appsettings") ||
f.Name.EndsWith(".json") ||
f.Name.EndsWith(".yml") ||
f.Name.EndsWith(".config"));
var sourceFiles = files.Where(f =>
f.Extension == ".cs" ||
f.Extension == ".ts" ||
f.Extension == ".js" ||
f.Extension == ".py");
var otherFiles = files.Except(projectFiles)
.Except(configFiles)
.Except(sourceFiles);
// Add project overview if it's the root
if (indent == 0)
{
var projFiles = projectFiles.ToList();
if (projFiles.Any())
{
sb.AppendLine("📦 Project Files:");
foreach (var proj in projFiles)
{
WriteProgress(files.IndexOf(proj) + 1, files.Count);
sb.AppendLine($"{new string(' ', (indent + 1) * 2)}[{proj.Extension}] {proj.Name}");
}
}
var configs = configFiles.ToList();
if (configs.Any())
{
sb.AppendLine("\n⚙️ Configuration Files:");
foreach (var config in configs)
{
WriteProgress(files.IndexOf(config) + 1, files.Count);
// Extract environment for appsettings files
var env = config.Name.Contains("appsettings.")
? $"({config.Name.Split('.')[1]}) "
: "";
sb.AppendLine($"{new string(' ', (indent + 1) * 2)}[{config.Extension}] {config.Name} {env}");
}
}
}
// Add source files
var srcFiles = sourceFiles.ToList();
if (srcFiles.Any())
{
if (indent == 0) sb.AppendLine("\n💻 Source Files:");
foreach (var src in srcFiles)
{
WriteProgress(files.IndexOf(src) + 1, files.Count);
var filePurpose = GetFilePurpose(src.Name);
sb.AppendLine($"{new string(' ', (indent + 1) * 2)}[{src.Extension}] {src.Name} {filePurpose}");
}
}
// Add remaining files
var remaining = otherFiles.ToList();
if (remaining.Any())
{
if (indent == 0) sb.AppendLine("\n📄 Other Files:");
foreach (var file in remaining)
{
WriteProgress(files.IndexOf(file) + 1, files.Count);
sb.AppendLine($"{new string(' ', (indent + 1) * 2)}{file.Name}");
}
}
// Process subdirectories
var directories = entries.Where(e => Directory.Exists(e))
.Select(d => new DirectoryInfo(d))
.ToList();
if (directories.Any())
{
if (indent == 0) sb.AppendLine("\n📂 Directories:");
foreach (var dir in directories)
{
var dirPurpose = GetDirectoryPurpose(dir.Name);
sb.AppendLine($"{new string(' ', (indent + 1) * 2)}[{dir.Name}/] {dirPurpose}")
.Append(GetProjectStructure(dir.FullName, indent + 1));
}
}
return sb.ToString();
}
private static string GetFilePurpose(string fileName) => fileName.ToLower() switch
{
var n when n.EndsWith("controller.cs") => "(API Endpoint)",
var n when n.EndsWith("service.cs") => "(Business Logic)",
var n when n.EndsWith("repository.cs") => "(Data Access)",
var n when n.EndsWith("model.cs") => "(Data Model)",
var n when n.EndsWith("request.cs") => "(API Request)",
var n when n.EndsWith("response.cs") => "(API Response)",
var n when n.EndsWith("dto.cs") => "(Data Transfer)",
var n when n.EndsWith("mapper.cs") => "(Object Mapping)",
var n when n == "program.cs" => "(Application Entry)",
var n when n == "startup.cs" => "(App Configuration)",
_ => ""
};
private static string GetDirectoryPurpose(string dirName) => dirName.ToLower() switch
{
"controllers" => "(API Endpoints)",
"services" => "(Business Logic)",
"models" => "(Data Models)",
"views" => "(UI Templates)",
"repositories" => "(Data Access)",
"tests" => "(Test Cases)",
"migrations" => "(DB Migrations)",
"scripts" => "(Utility Scripts)",
"docs" => "(Documentation)",
"config" => "(Configuration)",
"wwwroot" => "(Static Files)",
_ => ""
};
public static string GetFileContents(string path)
{
if (GitRepoRoot == null) GitRepoRoot = FindGitRepoRoot(path);
Console.WriteLine("\n📄 Processing files...");
var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
.Where(f => !FileChecker.ShouldSkip(new FileInfo(f), GitRepoRoot))
.ToList();
return string.Join("\n\n", files.Select((f, i) =>
{
WriteProgress(i + 1, files.Count);
return $"{f}\n{new string('-', 100)}\n{File.ReadAllText(f)}";
}));
}
private static string FindGitRepoRoot(string path) =>
Directory.Exists(Path.Combine(path, ".git"))
? path
: string.IsNullOrEmpty(path) ? null : FindGitRepoRoot(Path.GetDirectoryName(path));
private static void WriteProgress(int current, int total)
{
var percent = (int)((current / (double)total) * 100);
Console.Write($"\r⏳ Progress: {percent}% ({current}/{total})");
}
}