Skip to content

Commit 1376630

Browse files
committed
Feature: Further expression compression.
...and option to produce verbose output.
1 parent d30bb69 commit 1376630

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,4 @@ FodyWeavers.xsd
398398

399399
# JetBrains Rider
400400
*.sln.iml
401+
.idea/*

Program.cs

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
namespace CodeIngest;
1+
using System.Text;
2+
3+
namespace CodeIngest;
24

35
static class Program
46
{
57
private static void Main(string[] args)
68
{
9+
var switches = args.Where(o => o.StartsWith('-')).ToArray();
10+
var arguments = args.Where(o => !o.StartsWith('-')).ToArray();
11+
712
// Getting directory argument from the command line (Report error if none found).
813
if (args.Length < 2 || args.Any(a => a is "-h" or "--help" or "/?"))
914
{
1015
ShowUsage();
1116
return;
1217
}
1318

14-
var outputFile = new FileInfo(args[^1]);
19+
var outputFile = new FileInfo(arguments[^1]);
1520
var patterns = new List<string>();
1621
var directories = new List<DirectoryInfo>();
1722

18-
for (var i = 0; i < args.Length - 1; i++)
23+
for (var i = 0; i < arguments.Length - 1; i++)
1924
{
20-
if (args[i].Contains('*'))
21-
patterns.AddRange(args[i].Split(';', StringSplitOptions.RemoveEmptyEntries));
25+
if (arguments[i].Contains('*'))
26+
patterns.AddRange(arguments[i].Split(';', StringSplitOptions.RemoveEmptyEntries));
2227
else
23-
directories.Add(new DirectoryInfo(args[i]));
28+
directories.Add(new DirectoryInfo(arguments[i]));
2429
}
2530

2631
if (directories.Count == 0)
2732
directories.Add(new DirectoryInfo("."));
2833

2934
if (patterns.Count == 0)
3035
patterns.Add("*.cs");
36+
37+
var useFullPaths = switches.Any(o => o is "-full");
38+
var verbose = switches.Any(o => o is "-v");
3139

3240
// Recurse directory to find all source files.
3341
var sourceFiles = directories
@@ -45,7 +53,7 @@ private static void Main(string[] args)
4553
}
4654
}))
4755
.Where(f => !ShouldSkipFile(f))
48-
.ToDictionary(o => o.Name, o => File.ReadLines(o.FullName));
56+
.ToDictionary(o => o.FullName, o => File.ReadLines(o.FullName));
4957

5058
if (sourceFiles.Count == 0)
5159
{
@@ -55,29 +63,31 @@ private static void Main(string[] args)
5563

5664
// Write header.
5765
using (var fileStream = outputFile.Open(FileMode.Create))
58-
using (var writer = new StreamWriter(fileStream))
66+
using (var writer = new StreamWriter(fileStream, Encoding.UTF8))
5967
{
60-
{
61-
writer.WriteLine("// CodeIngest Source Dump - A CLI tool that merges and processes code files for GPT reviews.");
62-
writer.WriteLine("// Notes: Some code content may have been removed.");
68+
writer.NewLine = "\n";
69+
writer.WriteLine("// CodeIngest - A CLI tool that merges and processes code files for GPT reviews.");
70+
writer.WriteLine("// Notes: Some code content may have been removed.");
6371

64-
// Combine files into a single output file.
65-
foreach (var kvp in sourceFiles)
66-
{
67-
var lines = kvp.Value.ToList(); // Force evaluation to count
68-
var padWidth = lines.Count.ToString().Length;
72+
// Combine files into a single output file.
73+
foreach (var kvp in sourceFiles)
74+
{
75+
var lines = kvp.Value.ToList(); // Force evaluation to count
76+
var padWidth = lines.Count.ToString().Length;
6977

70-
writer.WriteLine($"// File: {kvp.Key}");
78+
writer.WriteLine($"// File: {(useFullPaths ? kvp.Key : Path.GetFileName(kvp.Key))}");
7179

72-
var lineNumber = 1;
73-
foreach (var line in lines)
74-
{
75-
if (ShouldIncludeSourceLine(line))
76-
writer.WriteLine($"{lineNumber.ToString().PadLeft(padWidth)}|{GetCodeLine(line).Trim()}");
80+
var lineNumber = 1;
81+
foreach (var line in lines)
82+
{
83+
if (ShouldIncludeSourceLine(line))
84+
writer.WriteLine($"{lineNumber.ToString().PadLeft(padWidth)}|{GetCodeLine(line).Trim()}");
7785

78-
lineNumber++;
79-
}
86+
lineNumber++;
8087
}
88+
89+
if (verbose)
90+
Console.WriteLine($"{kvp.Key} ({lines.Sum(o => o.Length):N0} charactes -> {lines.Count:N0} lines)");
8191
}
8292
}
8393

@@ -88,11 +98,21 @@ private static void Main(string[] args)
8898

8999
private static void ShowUsage()
90100
{
101+
Console.WriteLine("CodeIngest - A CLI tool that merges and processes code files for GPT reviews.");
102+
Console.WriteLine(" https://github.com/deanthecoder/CodeIngest");
103+
Console.WriteLine();
91104
Console.WriteLine("Usage:");
92-
Console.WriteLine(" CodeIngest [<directory> ...] [*.ext1;*.ext2] <output.code>");
105+
Console.WriteLine(" CodeIngest [-h] [-full] [-v] [<directory> ...] [*.ext1;*.ext2] <output.code>");
93106
Console.WriteLine();
94-
Console.WriteLine("See:");
95-
Console.WriteLine(" https://github.com/deanthecoder/CodeIngest");
107+
Console.WriteLine("Where:");
108+
Console.WriteLine(" -full Optionally include full path names in the output.");
109+
Console.WriteLine(" -h Show this help message.");
110+
Console.WriteLine(" -v Verbose mode.");
111+
Console.WriteLine(" <directory> One or more directories to search for source files.");
112+
Console.WriteLine(" If not specified, the current working directory will be used.");
113+
Console.WriteLine(" *.ext1;... One or more file extensions to include in the search.");
114+
Console.WriteLine(" If not specified, *.cs will be used by default.");
115+
Console.WriteLine(" <output.code> The output file to write the merged code to.");
96116
Console.WriteLine();
97117
Console.WriteLine("Examples:");
98118
Console.WriteLine(" CodeIngest MyProject Out.cs");
@@ -116,12 +136,14 @@ private static bool ShouldIncludeSourceLine(string s)
116136
{
117137
if (string.IsNullOrWhiteSpace(s))
118138
return false;
119-
if (s.StartsWith("using"))
139+
if (s.StartsWith("using") || s.StartsWith("#include") || s.StartsWith("#pragma"))
120140
return false;
121141

122142
var trimmed = s.Trim();
123143
if (trimmed.StartsWith("//"))
124144
return false;
145+
if (trimmed.StartsWith("/*") && trimmed.EndsWith("*/"))
146+
return false;
125147
if (trimmed.StartsWith("namespace"))
126148
return false;
127149
return true;
@@ -131,6 +153,15 @@ private static string GetCodeLine(string line)
131153
{
132154
// Strip all comments.
133155
var commentIndex = line.IndexOf("//", StringComparison.Ordinal);
134-
return commentIndex >= 0 ? line[..commentIndex] : line;
156+
if (commentIndex >= 0)
157+
line = line[..commentIndex];
158+
159+
foreach (var expr in new[] {"<", "<=", "=", "==", "=>", ">", "!=", "(", ")", "{", "}", "-", "+", "*", "&", "%", "/", "<<", ">>", ";", ",", "||", "|", ":", "?", "|"})
160+
line = line.Replace($"{expr} ", expr).Replace($" {expr}", expr);
161+
162+
while (line.Contains(" "))
163+
line = line.Replace(" ", " ");
164+
165+
return line;
135166
}
136167
}

0 commit comments

Comments
 (0)