Skip to content

Commit

Permalink
make content and structure generation configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVeksler committed Nov 5, 2024
1 parent ffeae1c commit e003206
Showing 1 changed file with 34 additions and 50 deletions.
84 changes: 34 additions & 50 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
var output = GetValidOutputPath(args.ElementAtOrDefault(1) ?? defaultOutput);

var sw = Stopwatch.StartNew();
var (structure, contents) = GetProjectInfo(path);
var content = BuildContent(structure, contents);
var content = BuildContent(path, config);
var stats = CalculateStats(path, content, sw.Elapsed);

WriteOutput(output, content, config.OutputFormat);
Expand All @@ -27,20 +26,10 @@
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 Config LoadConfig() =>
JsonSerializer.Deserialize<Config>(
File.Exists("config.json") ? File.ReadAllText("config.json") : "{}"
) ?? new();

static string GetValidPath(string defaultPath)
{
Expand All @@ -57,56 +46,49 @@ static string GetValidOutputPath(string defaultOutput)
return string.IsNullOrWhiteSpace(output) ? defaultOutput : output;
}

static (string structure, string contents) GetProjectInfo(string path)
static string BuildContent(string path, Config config)
{
try
{
return (MyAppsContext.GetProjectStructure(path), MyAppsContext.GetFileContents(path));
}
catch (Exception ex)
{
throw new InvalidOperationException($"Error processing project at {path}", ex);
}
}
var sb = new StringBuilder();

static string BuildContent(string structure, string contents) =>
new StringBuilder()
.AppendLine("Project Structure:")
.AppendLine(structure)
.AppendLine("\nFile Contents:")
.AppendLine(contents)
.ToString();
if (config.IncludeStructure)
{
sb.AppendLine("Project Structure:")
.AppendLine(MyAppsContext.GetProjectStructure(path));
}

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
""";
if (config.IncludeContents)
{
sb.AppendLine("\nFile Contents:")
.AppendLine(MyAppsContext.GetFileContents(path));
}

return sb.ToString();
}
catch (Exception ex)
{
throw new InvalidOperationException("Error calculating stats", 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)
{
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
};
var formattedContent = format.ToLower() == "json"
? JsonSerializer.Serialize(new { content, timestamp = DateTime.Now })
: content;
File.WriteAllText(outputPath, formattedContent);
}
catch (Exception ex)
Expand All @@ -120,4 +102,6 @@ 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;
}

0 comments on commit e003206

Please sign in to comment.