-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
108a5ae
commit e2a867f
Showing
1 changed file
with
118 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,123 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
using System.Text.Json; | ||
using CodeContext; | ||
|
||
Console.OutputEncoding = Encoding.UTF8; | ||
var path = args.FirstOrDefault() ?? MyAppsContext.GetUserInput("Enter the path to index: "); | ||
var defaultOutput = Path.Combine(Path.GetDirectoryName(path), "context.txt"); | ||
var output = args.ElementAtOrDefault(1) ?? MyAppsContext.GetUserInput($"Enter output file (default: {defaultOutput}): "); | ||
output = string.IsNullOrWhiteSpace(output) ? defaultOutput : output; | ||
|
||
var sw = Stopwatch.StartNew(); | ||
var structure = MyAppsContext.GetProjectStructure(path); | ||
var contents = MyAppsContext.GetFileContents(path); | ||
|
||
var content = new StringBuilder() | ||
.AppendLine("Project Structure:") | ||
.AppendLine(structure) | ||
.AppendLine("\nFile Contents:") | ||
.AppendLine(contents) | ||
.ToString(); | ||
|
||
var fileCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length; | ||
var lineCount = content.Count(c => c == '\n'); | ||
var timeTaken = sw.Elapsed; | ||
|
||
var stats = $""" | ||
📊 Stats: | ||
📁 Files processed: {fileCount} | ||
📝 Total lines: {lineCount} | ||
⏱️ Time taken: {timeTaken.TotalSeconds:F2}s | ||
💾 Output size: {content.Length} characters | ||
"""; | ||
|
||
var outputPath = Directory.Exists(output) ? Path.Combine(output, "context.txt") : output; | ||
File.WriteAllText(outputPath, content); | ||
Console.WriteLine($"✅ Output written to {outputPath}"); | ||
Console.WriteLine(stats); | ||
|
||
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 (structure, contents) = GetProjectInfo(path); | ||
var content = BuildContent(structure, contents); | ||
var stats = CalculateStats(path, content, sw.Elapsed); | ||
|
||
WriteOutput(output, content, config.OutputFormat); | ||
Console.WriteLine($"✅ Output written to {output}"); | ||
Console.WriteLine(stats); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"❌ Error: {ex.Message}"); | ||
Environment.Exit(1); | ||
} | ||
|
||
static Config LoadConfig() | ||
{ | ||
const string configPath = "config.json"; | ||
try | ||
{ | ||
return File.Exists(configPath) | ||
? JsonSerializer.Deserialize<Config>(File.ReadAllText(configPath)) ?? new Config() | ||
: new Config(); | ||
} | ||
catch (JsonException ex) | ||
{ | ||
throw new InvalidOperationException("Error parsing config file", ex); | ||
} | ||
} | ||
|
||
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 structure, string contents) GetProjectInfo(string path) | ||
{ | ||
try | ||
{ | ||
return (MyAppsContext.GetProjectStructure(path), MyAppsContext.GetFileContents(path)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException($"Error processing project at {path}", ex); | ||
} | ||
} | ||
|
||
static string BuildContent(string structure, string contents) => | ||
new StringBuilder() | ||
.AppendLine("Project Structure:") | ||
.AppendLine(structure) | ||
.AppendLine("\nFile Contents:") | ||
.AppendLine(contents) | ||
.ToString(); | ||
|
||
static string CalculateStats(string path, string content, TimeSpan timeTaken) | ||
{ | ||
try | ||
{ | ||
var fileCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length; | ||
var lineCount = content.Count(c => c == '\n'); | ||
return $""" | ||
📊 Stats: | ||
📁 Files processed: {fileCount} | ||
📝 Total lines: {lineCount} | ||
⏱️ Time taken: {timeTaken.TotalSeconds:F2}s | ||
💾 Output size: {content.Length} characters | ||
"""; | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException("Error calculating stats", ex); | ||
} | ||
} | ||
|
||
static void WriteOutput(string output, string content, string format) | ||
{ | ||
try | ||
{ | ||
var outputPath = Directory.Exists(output) ? Path.Combine(output, "context.txt") : output; | ||
var formattedContent = format.ToLower() switch | ||
{ | ||
"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"; | ||
} |