1
- namespace CodeIngest ;
1
+ using System . Text ;
2
+
3
+ namespace CodeIngest ;
2
4
3
5
static class Program
4
6
{
5
7
private static void Main ( string [ ] args )
6
8
{
9
+ var switches = args . Where ( o => o . StartsWith ( '-' ) ) . ToArray ( ) ;
10
+ var arguments = args . Where ( o => ! o . StartsWith ( '-' ) ) . ToArray ( ) ;
11
+
7
12
// Getting directory argument from the command line (Report error if none found).
8
13
if ( args . Length < 2 || args . Any ( a => a is "-h" or "--help" or "/?" ) )
9
14
{
10
15
ShowUsage ( ) ;
11
16
return ;
12
17
}
13
18
14
- var outputFile = new FileInfo ( args [ ^ 1 ] ) ;
19
+ var outputFile = new FileInfo ( arguments [ ^ 1 ] ) ;
15
20
var patterns = new List < string > ( ) ;
16
21
var directories = new List < DirectoryInfo > ( ) ;
17
22
18
- for ( var i = 0 ; i < args . Length - 1 ; i ++ )
23
+ for ( var i = 0 ; i < arguments . Length - 1 ; i ++ )
19
24
{
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 ) ) ;
22
27
else
23
- directories . Add ( new DirectoryInfo ( args [ i ] ) ) ;
28
+ directories . Add ( new DirectoryInfo ( arguments [ i ] ) ) ;
24
29
}
25
30
26
31
if ( directories . Count == 0 )
27
32
directories . Add ( new DirectoryInfo ( "." ) ) ;
28
33
29
34
if ( patterns . Count == 0 )
30
35
patterns . Add ( "*.cs" ) ;
36
+
37
+ var useFullPaths = switches . Any ( o => o is "-full" ) ;
38
+ var verbose = switches . Any ( o => o is "-v" ) ;
31
39
32
40
// Recurse directory to find all source files.
33
41
var sourceFiles = directories
@@ -45,7 +53,7 @@ private static void Main(string[] args)
45
53
}
46
54
} ) )
47
55
. Where ( f => ! ShouldSkipFile ( f ) )
48
- . ToDictionary ( o => o . Name , o => File . ReadLines ( o . FullName ) ) ;
56
+ . ToDictionary ( o => o . FullName , o => File . ReadLines ( o . FullName ) ) ;
49
57
50
58
if ( sourceFiles . Count == 0 )
51
59
{
@@ -55,29 +63,31 @@ private static void Main(string[] args)
55
63
56
64
// Write header.
57
65
using ( var fileStream = outputFile . Open ( FileMode . Create ) )
58
- using ( var writer = new StreamWriter ( fileStream ) )
66
+ using ( var writer = new StreamWriter ( fileStream , Encoding . UTF8 ) )
59
67
{
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." ) ;
63
71
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 ;
69
77
70
- writer . WriteLine ( $ "// File: { kvp . Key } ") ;
78
+ writer . WriteLine ( $ "// File: { ( useFullPaths ? kvp . Key : Path . GetFileName ( kvp . Key ) ) } ") ;
71
79
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 ( ) } ") ;
77
85
78
- lineNumber ++ ;
79
- }
86
+ lineNumber ++ ;
80
87
}
88
+
89
+ if ( verbose )
90
+ Console . WriteLine ( $ "{ kvp . Key } ({ lines . Sum ( o => o . Length ) : N0} charactes -> { lines . Count : N0} lines)") ;
81
91
}
82
92
}
83
93
@@ -88,11 +98,21 @@ private static void Main(string[] args)
88
98
89
99
private static void ShowUsage ( )
90
100
{
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 ( ) ;
91
104
Console . WriteLine ( "Usage:" ) ;
92
- Console . WriteLine ( " CodeIngest [<directory> ...] [*.ext1;*.ext2] <output.code>" ) ;
105
+ Console . WriteLine ( " CodeIngest [-h] [-full] [-v] [ <directory> ...] [*.ext1;*.ext2] <output.code>" ) ;
93
106
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." ) ;
96
116
Console . WriteLine ( ) ;
97
117
Console . WriteLine ( "Examples:" ) ;
98
118
Console . WriteLine ( " CodeIngest MyProject Out.cs" ) ;
@@ -116,12 +136,14 @@ private static bool ShouldIncludeSourceLine(string s)
116
136
{
117
137
if ( string . IsNullOrWhiteSpace ( s ) )
118
138
return false ;
119
- if ( s . StartsWith ( "using" ) )
139
+ if ( s . StartsWith ( "using" ) || s . StartsWith ( "#include" ) || s . StartsWith ( "#pragma" ) )
120
140
return false ;
121
141
122
142
var trimmed = s . Trim ( ) ;
123
143
if ( trimmed . StartsWith ( "//" ) )
124
144
return false ;
145
+ if ( trimmed . StartsWith ( "/*" ) && trimmed . EndsWith ( "*/" ) )
146
+ return false ;
125
147
if ( trimmed . StartsWith ( "namespace" ) )
126
148
return false ;
127
149
return true ;
@@ -131,6 +153,15 @@ private static string GetCodeLine(string line)
131
153
{
132
154
// Strip all comments.
133
155
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 ;
135
166
}
136
167
}
0 commit comments