-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFMStabilizerCLI.pas
More file actions
226 lines (199 loc) · 5.83 KB
/
Copy pathDFMStabilizerCLI.pas
File metadata and controls
226 lines (199 loc) · 5.83 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
unit DFMStabilizerCLI;
{
Command-line interface for the DFM stabilizer tool.
Argument syntax:
DFMStabilizerTool [-s] <file|pattern|@listfile> [...]
-s Recurse into subdirectories when expanding wildcard patterns.
file Exact path to a DFM file.
pattern Wildcard pattern: *.dfm, path\*.dfm, etc.
@listfile Text file listing one path or pattern per line.
Lines starting with # are treated as comments.
Exit code: 0 if all files were converted successfully, 1 if any failed.
}
interface
procedure Run;
implementation
uses
System.SysUtils,
System.Classes,
DFMTextStabilizerCore;
// ---------------------------------------------------------------------------
type
TDFMProcessor = class
private
FRecursive : Boolean;
FSuccessCount: Integer;
FFailCount : Integer;
procedure ProcessFile(const AFileName: string);
procedure ExpandAndProcess(const Pattern: string);
procedure ProcessListFile(const AListFileName: string);
public
constructor Create(ARecursive: Boolean);
procedure ProcessArg(const Arg: string);
property SuccessCount: Integer read FSuccessCount;
property FailCount : Integer read FFailCount;
end;
constructor TDFMProcessor.Create(ARecursive: Boolean);
begin
inherited Create;
FRecursive := ARecursive;
FSuccessCount := 0;
FFailCount := 0;
end;
procedure TDFMProcessor.ProcessFile(const AFileName: string);
begin
try
Write(' ', AFileName, ' ... ');
if ConvertDFMFile(AFileName) then
Writeln('converted')
else
Writeln('already up to date');
Inc(FSuccessCount);
except
on E: Exception do
begin
Writeln('FAILED: ', E.Message);
Inc(FFailCount);
end;
end;
end;
// Expand a pattern (which may contain * or ?) into actual files, then process
// each one. If FRecursive is True, descend into subdirectories as well.
procedure TDFMProcessor.ExpandAndProcess(const Pattern: string);
var
Dir : string;
FilePat: string;
SR : TSearchRec;
begin
Dir := ExtractFilePath(Pattern);
FilePat := ExtractFileName(Pattern);
if Dir = '' then
Dir := '.';
// Files matching the pattern in this directory
if FindFirst(IncludeTrailingPathDelimiter(Dir) + FilePat, faAnyFile, SR) = 0 then
begin
try
repeat
if (SR.Attr and faDirectory) = 0 then
ProcessFile(IncludeTrailingPathDelimiter(Dir) + SR.Name);
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
end;
// Recurse into subdirectories
if FRecursive then
begin
if FindFirst(IncludeTrailingPathDelimiter(Dir) + '*', faDirectory, SR) = 0 then
begin
try
repeat
if ((SR.Attr and faDirectory) <> 0) and
(SR.Name <> '.') and (SR.Name <> '..') then
ExpandAndProcess(
IncludeTrailingPathDelimiter(Dir) + SR.Name +
PathDelim + FilePat);
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
end;
end;
end;
procedure TDFMProcessor.ProcessListFile(const AListFileName: string);
var
List: TStringList;
I : Integer;
Line: string;
begin
if not FileExists(AListFileName) then
raise EFileNotFoundException.CreateFmt('List file not found: %s', [AListFileName]);
List := TStringList.Create;
try
List.LoadFromFile(AListFileName);
for I := 0 to List.Count - 1 do
begin
Line := Trim(List[I]);
if (Line <> '') and not Line.StartsWith('#') then
ProcessArg(Line);
end;
finally
List.Free;
end;
end;
procedure TDFMProcessor.ProcessArg(const Arg: string);
begin
if Arg.StartsWith('@') then
ProcessListFile(Arg.Substring(1))
else if (Pos('*', Arg) > 0) or (Pos('?', Arg) > 0) then
ExpandAndProcess(Arg)
else
ProcessFile(Arg);
end;
// ---------------------------------------------------------------------------
procedure PrintUsage;
begin
Writeln('Usage: DFMStabilizerTool [-s] <file|pattern|@listfile> [...]');
Writeln;
Writeln('Converts DFM files in-place to the stabilized UTF-8 text format:');
Writeln(' - strings are not broken at 64 characters (limit raised to 700)');
Writeln(' - embedded newlines (#13/#10) cause a line break at that position');
Writeln(' - non-ASCII characters are written literally as UTF-8');
Writeln(' - file always starts with a UTF-8 BOM');
Writeln;
Writeln('Options:');
Writeln(' -s Recurse into subdirectories when expanding wildcard patterns');
Writeln;
Writeln('Arguments:');
Writeln(' file Exact path to a DFM file');
Writeln(' pattern Wildcard pattern (e.g. *.dfm or forms\*.dfm)');
Writeln(' @listfile Text file with one path/pattern per line (# = comment)');
Writeln;
Writeln('Examples:');
Writeln(' DFMStabilizerTool MainForm.dfm');
Writeln(' DFMStabilizerTool -s *.dfm');
Writeln(' DFMStabilizerTool -s src\*.dfm @extra_forms.txt');
Writeln(' DFMStabilizerTool @all_forms.txt');
end;
// ---------------------------------------------------------------------------
procedure Run;
var
Recursive: Boolean;
Processor: TDFMProcessor;
I : Integer;
Arg : string;
begin
if ParamCount = 0 then
begin
PrintUsage;
Halt(1);
end;
Recursive := False;
for I := 1 to ParamCount do
if SameText(ParamStr(I), '-s') then
begin
Recursive := True;
Break;
end;
Processor := TDFMProcessor.Create(Recursive);
try
for I := 1 to ParamCount do
begin
Arg := ParamStr(I);
if SameText(Arg, '-s') then
Continue;
Processor.ProcessArg(Arg);
end;
Writeln;
if Processor.SuccessCount + Processor.FailCount = 0 then
Writeln('No files matched.')
else
Writeln(Format('%d converted, %d failed.',
[Processor.SuccessCount, Processor.FailCount]));
if Processor.FailCount > 0 then
Halt(1);
finally
Processor.Free;
end;
end;
end.