-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
109 lines (94 loc) · 3.38 KB
/
Program.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
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using CodeContext;
Console.OutputEncoding = Encoding.UTF8;
try
{
var config = LoadConfig();
var path = GetValidPath(args.FirstOrDefault() ?? config.DefaultInputPath);
var defaultOutput = Path.Combine(Path.GetDirectoryName(path) ?? ".", config.DefaultOutputFileName);
var output = GetValidOutputPath(args.ElementAtOrDefault(1) ?? defaultOutput);
var sw = Stopwatch.StartNew();
var content = BuildContent(path, config);
var stats = CalculateStats(path, content, sw.Elapsed);
WriteOutput(output, content, config.OutputFormat);
Console.WriteLine($"\n✅ Output written to {output}");
Console.WriteLine(stats);
}
catch (Exception ex)
{
Console.WriteLine($"❌ Error: {ex.Message}");
Environment.Exit(1);
}
static Config LoadConfig() =>
JsonSerializer.Deserialize<Config>(
File.Exists("config.json") ? File.ReadAllText("config.json") : "{}"
) ?? new();
static string GetValidPath(string defaultPath)
{
var path = MyAppsContext.GetUserInput($"Enter the path to index (default: {defaultPath}): ");
var finalPath = string.IsNullOrWhiteSpace(path) ? defaultPath : path;
return Directory.Exists(finalPath)
? finalPath
: throw new DirectoryNotFoundException($"Invalid directory path: {finalPath}");
}
static string GetValidOutputPath(string defaultOutput)
{
var output = MyAppsContext.GetUserInput($"Enter output file (default: {defaultOutput}): ");
return string.IsNullOrWhiteSpace(output) ? defaultOutput : output;
}
static string BuildContent(string path, Config config)
{
try
{
var sb = new StringBuilder();
if (config.IncludeStructure)
{
sb.AppendLine("Project Structure:")
.AppendLine(MyAppsContext.GetProjectStructure(path));
}
if (config.IncludeContents)
{
sb.AppendLine("\nFile Contents:")
.AppendLine(MyAppsContext.GetFileContents(path));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new InvalidOperationException($"Error processing project at {path}", ex);
}
}
static string CalculateStats(string path, string content, TimeSpan timeTaken) =>
$"""
📊 Stats:
📁 Files processed: {Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length}
📝 Total lines: {content.Count(c => c == '\n')}
⏱️ Time taken: {timeTaken.TotalSeconds:F2}s
💾 Output size: {content.Length} characters
""";
static void WriteOutput(string output, string content, string format)
{
Console.WriteLine("\n💾 Writing output...");
try
{
var outputPath = Directory.Exists(output) ? Path.Combine(output, "context.txt") : output;
var formattedContent = format.ToLower() == "json"
? JsonSerializer.Serialize(new { content, timestamp = DateTime.Now })
: content;
File.WriteAllText(outputPath, formattedContent);
}
catch (Exception ex)
{
throw new IOException($"Error writing output to {output}", ex);
}
}
record Config
{
public string DefaultInputPath { get; init; } = ".";
public string DefaultOutputFileName { get; init; } = "context.txt";
public string OutputFormat { get; init; } = "text";
public bool IncludeStructure { get; init; } = true;
public bool IncludeContents { get; init; } = true;
}