-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
342 lines (307 loc) · 13.7 KB
/
Program.cs
File metadata and controls
342 lines (307 loc) · 13.7 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System.Diagnostics;
using System.Reflection;
using System.Text;
using TextCopy;
namespace PreCoding
{
class Program
{
static void Main(string[] args)
{
// Get the path to the running EXE
var metadata = GetExeMetadata();
Console.WriteLine($"PreCoding Tool [Version: {metadata.appVersion}]");
Console.WriteLine($"Author: {metadata.companyName}");
Console.WriteLine($"License: GPLv3");
Console.WriteLine($"Build: {metadata.lastWriteTime}");
Console.WriteLine($"Ensure your source code files are UTF-8 encoded.\n");
string targetDirectory = Directory.GetCurrentDirectory();
//string targetDirectory = @"C:\Users\shawhu\ProjectC\heheng\hehengclient";
bool fileListMode = (args.Length > 0 && args[0].Equals("-files", StringComparison.OrdinalIgnoreCase));
bool helpMode = (args.Length > 0 && args[0].Equals("-help", StringComparison.OrdinalIgnoreCase));
string[] fileList = [];
if (helpMode)
{
Console.WriteLine("Usages examples:");
Console.WriteLine("1. Specify file search patterns:");
Console.WriteLine("precoding.exe -files file1.cs file2.json ...");
Console.WriteLine();
Console.WriteLine("Specify targetDir");
Console.WriteLine("precoding.exe <targetDir>");
Console.WriteLine();
Console.WriteLine("it will search the currentDir with default search patterns");
Console.WriteLine("precoding.exe");
Console.WriteLine();
return;
}
if (fileListMode)
{
// ADDED: File list mode, get list of files after the -files argument
if (args.Length < 2)
{
Console.WriteLine("Usage: precoding.exe -files file1.cs file2.json ...");
return;
}
fileList = args.Skip(1).ToArray(); // CORRECT: this gets all arguments after -files
}
else if (args.Length > 0)
{
targetDirectory = args[0];
}
Console.WriteLine("========== File Aggregation Started ==========");
if (!Directory.Exists(targetDirectory))
{
Console.WriteLine($"[ERROR] Target directory does not exist: {targetDirectory}");
return;
}
// Folders to ignore (case-insensitive)
var ignoredFolderNames = new HashSet<string>(new[] {
"node_modules",
"Migrations",
"obj",
"app-example",
"staticdata",
"docs"
}, StringComparer.OrdinalIgnoreCase);
// File patterns to search
string[] searchPatterns;
if (fileListMode)
{
// CHANGED: Use exact filenames as searchPatterns
searchPatterns = fileList;
}
else
{
searchPatterns = new[] {
"*.cs",
"*.tsx",
"*.ts",
"package.json",
"*.csproj"
};
}
// Printout the criteria
Console.WriteLine($"Mode: {(fileListMode ? "File List" : "Pattern Search")}");
Console.WriteLine($"Search Targets: {string.Join(", ", searchPatterns)}");
Console.WriteLine($"Ignored Folders: {string.Join(", ", ignoredFolderNames)}");
// Find all files, skipping hidden, ignored, and dot-prefixed folders/files
var files = GetFilesSkippingFolders(targetDirectory, searchPatterns, ignoredFolderNames)
.Distinct()
.OrderBy(f => f)
.ToList();
if (!files.Any())
{
Console.WriteLine("No matching source files found. Exiting.");
Console.WriteLine("========== Aggregation Complete ==============");
return;
}
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
string templatePath = Path.Combine(AppContext.BaseDirectory ?? Directory.GetCurrentDirectory(), "prompt_header.md");
if (File.Exists(templatePath))
{
output.AppendLine(File.ReadAllText(templatePath));
}
else
{
output.AppendLine("## Instructions:");
output.AppendLine("1. **Code-First Replies:** Always respond with a code block containing the exact code (method, function, or class) to be replaced or inserted.");
output.AppendLine(" - For long files, include only the full method/function/class that is being changed.");
output.AppendLine("2. **Clear Change Comments:** Clearly mark your changes using `// CHANGED`, `// ADDED`, `// REMOVED`, etc.");
output.AppendLine("3. **No Redundant Suggestions:** Double-check my code and **do not suggest fixes for issues already handled**.");
output.AppendLine("4. **Prefer Inline Solutions:** Use concise, inline code when possible. Avoid multiple lines for changes that can be made in one.");
output.AppendLine("5. **Be Accurate, Direct, and Minimal:**");
output.AppendLine(" - Do not add extra features or explanations unless requested.");
output.AppendLine(" - Provide only code and necessary comments.");
output.AppendLine("6. **Reference the Provided Codebase:** Use the code files and their filenames below as context for your responses.");
output.AppendLine();
output.AppendLine("## Below are all the source code files for reference:");
}
foreach (var file in files)
{
try
{
string filetype = GetFileType(file);
string relativePath = Path.GetRelativePath(targetDirectory, file);
output.AppendLine($"### --- FILENAME: {relativePath} ---");
output.AppendLine($"```{filetype}");
string content = File.ReadAllText(file, Encoding.UTF8);
output.AppendLine(content);
output.AppendLine("```");
}
catch (Exception ex)
{
error.AppendLine($"ERROR reading {file}: {ex.Message} ---");
error.AppendLine();
}
}
// Output file
string outputStr = output.ToString();
string outputFile = Path.Combine(targetDirectory, "AllSourceFiles.txt");
File.WriteAllText(outputFile, outputStr, Encoding.UTF8);
// Get file size in MB
FileInfo fi = new FileInfo(outputFile);
double sizeInMb = fi.Length / (1024.0 * 1024.0);
Console.WriteLine($"Completed! Aggregated {files.Count} files.");
Console.WriteLine($"Output written to: {outputFile} [{sizeInMb:F2} MB]");
if (sizeInMb <= 1)
Console.WriteLine("Output copied to clipboard.");
else
Console.WriteLine("Output too large for clipboard; only written to file.");
Console.WriteLine("========== Aggregation Complete ==============");
// Copy to clipboard
if (sizeInMb <= 1) ClipboardService.SetText(outputStr);
else Console.WriteLine($"Output file is too big, skipping automatic clipboard copying.");
//if error
if (!string.IsNullOrWhiteSpace(error.ToString()))
Console.WriteLine("[WARN] Some files could not be read:\n" + error.ToString());
}
static IEnumerable<string> GetFilesSkippingFolders(
string rootPath,
string[] searchPatterns,
HashSet<string> ignoredFolderNames
)
{
var dirs = new Stack<DirectoryInfo>();
dirs.Push(new DirectoryInfo(rootPath));
while (dirs.Count > 0)
{
DirectoryInfo currentDir = dirs.Pop();
// Skip hidden directories
if ((currentDir.Attributes & FileAttributes.Hidden) != 0)
continue;
// Skip ignored directories by name (e.g., node_modules)
if (ignoredFolderNames.Contains(currentDir.Name))
continue;
// Skip directories that start with a dot (.)
if (currentDir.Name.StartsWith("."))
continue;
// Get files for each pattern
foreach (var pattern in searchPatterns)
{
FileInfo[] files = [];
try
{
files = currentDir.GetFiles(pattern, SearchOption.TopDirectoryOnly);
}
catch
{
// Could not access this directory, skip
continue;
}
foreach (var file in files)
{
// Skip dot-prefixed files
if (file.Name.StartsWith("."))
continue;
yield return file.FullName;
}
}
// Push subdirectories that are not hidden, not ignored, and not dot-starting
DirectoryInfo[] subDirs = [];
try
{
subDirs = currentDir.GetDirectories();
}
catch
{
continue;
}
foreach (var subDir in subDirs)
{
if ((subDir.Attributes & FileAttributes.Hidden) == 0 &&
!ignoredFolderNames.Contains(subDir.Name) &&
!subDir.Name.StartsWith("."))
{
dirs.Push(subDir);
}
}
}
}
static string GetFileType(string file)
{
var extension = Path.GetExtension(file).ToLowerInvariant();
var typeMap = new Dictionary<string, string>
{ { ".bat", "bat" },
{ ".c", "c" },
{ ".cc", "cpp" },
{ ".cmd", "bat" },
{ ".cpp", "cpp" },
{ ".cs", "csharp" },
{ ".css", "css" },
{ ".dart", "dart" },
{ ".diff", "diff" },
{ ".env", "env" },
{ ".go", "go" },
{ ".graphql", "graphql" },
{ ".gql", "graphql" },
{ ".h", "cpp" },
{ ".htm", "html" },
{ ".html", "html" },
{ ".ini", "ini" },
{ ".java", "java" },
{ ".jl", "julia" },
{ ".js", "javascript" },
{ ".json", "json" },
{ ".kt", "kotlin" },
{ ".kts", "kotlin" },
{ ".latex", "latex" },
{ ".lisp", "lisp" },
{ ".lsp", "lisp" },
{ ".lua", "lua" },
{ ".md", "markdown" },
{ ".mjs", "javascript" },
{ ".php", "php" },
{ ".pl", "perl" },
{ ".pm", "perl" },
{ ".properties", "properties" },
{ ".ps1", "powershell" },
{ ".psm1", "powershell" },
{ ".py", "python" },
{ ".pyw", "python" },
{ ".r", "r" },
{ ".rb", "ruby" },
{ ".rs", "rust" },
{ ".scala", "scala" },
{ ".sh", "bash" },
{ ".sql", "sql" },
{ ".swift", "swift" },
{ ".tex", "latex" },
{ ".toml", "toml" },
{ ".ts", "typescript" },
{ ".tsx", "typescript" },
{ ".txt", "plaintext" },
{ ".xml", "xml" },
{ ".yml", "yaml" },
{ ".yaml", "yaml" }
};
return typeMap.TryGetValue(extension, out var type) ? type : "";
}
static (string exePath, string appVersion, string companyName, DateTime lastWriteTime) GetExeMetadata()
{
string exePath = Process.GetCurrentProcess().MainModule?.FileName ?? "";
DateTime lastWriteTime = DateTime.MinValue;
string appVersion = "Unknown";
string companyName = "Unknown";
// Get assembly info from the file itself (EXE)
if (!string.IsNullOrEmpty(exePath) && File.Exists(exePath))
{
try
{
var assembly = Assembly.LoadFrom(exePath);
appVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "Unknown";
companyName = assembly.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company ?? "Unknown";
}
catch
{
// fallback: try FileVersionInfo
var fvi = FileVersionInfo.GetVersionInfo(exePath);
appVersion = fvi.ProductVersion ?? "Unknown";
companyName = fvi.CompanyName ?? "Unknown";
}
lastWriteTime = File.GetLastWriteTime(exePath);
}
return (exePath, appVersion, companyName, lastWriteTime);
}
}
}