-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildLogCapture.pas
More file actions
325 lines (281 loc) · 9.51 KB
/
Copy pathBuildLogCapture.pas
File metadata and controls
325 lines (281 loc) · 9.51 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
unit BuildLogCapture;
{
Koppelt den BuildLog-Plugin an den IDE-Kompilierungsvorgang.
Mechanismus A – IOTAToolsFilterNotifier (deprecated seit MSBuild):
Empfaengt rohen Compiler-Stdout. In Delphi 12 in der Regel nicht aktiv.
Mechanismus B – IOTACompileNotifier + ScanForLogFile:
Merkt sich beim Build-Start alle Projektverzeichnisse.
Nach dem Build wird die juengste *.log-/ *.all-Datei gesucht und geladen.
Durchsucht: Projektgruppenverzeichnis, uebergeordnetes Verzeichnis sowie
die Verzeichnisse jedes einzelnen Projekts in der Gruppe.
}
interface
uses
System.SysUtils, System.Classes, System.IOUtils,
ToolsAPI;
type
TBuildCapture = class(TInterfacedObject, IOTAToolsFilterNotifier, IOTACompileNotifier)
private
FLines: TStringList;
FProjectDirs: TStringList;
FFoundFile: string;
FFilterIdx: Integer;
FCompileIdx: Integer;
FInGroupBuild: Boolean;
procedure CollectProjectDirs;
procedure FindNewestLogFile(out AFile: string);
procedure WriteBridgeResult(AResult: TOTACompileResult);
public
constructor Create;
destructor Destroy; override;
procedure RegisterNotifiers;
procedure UnregisterNotifiers;
procedure ScanForLogFile;
{ IOTANotifier }
procedure AfterSave;
procedure BeforeSave;
procedure Destroyed;
procedure Modified;
{ IOTAToolsFilterNotifier }
procedure Filter(FileName: string; ErrorCode: Integer;
StdOut, StdError: TStrings);
function GetFilterName: string;
{ IOTACompileNotifier }
procedure ProjectCompileStarted(const Project: IOTAProject;
Mode: TOTACompileMode);
procedure ProjectCompileFinished(const Project: IOTAProject;
Result: TOTACompileResult);
procedure ProjectGroupCompileStarted(Mode: TOTACompileMode);
procedure ProjectGroupCompileFinished(Result: TOTACompileResult);
property Lines: TStringList read FLines;
property FoundFile: string read FFoundFile;
end;
var
GBuildCapture: TBuildCapture = nil;
implementation
{ --------------------------------------------------------------------------- }
constructor TBuildCapture.Create;
begin
inherited;
FLines := TStringList.Create;
FProjectDirs := TStringList.Create;
FProjectDirs.CaseSensitive := False;
FProjectDirs.Duplicates := dupIgnore;
FProjectDirs.Sorted := True;
FFilterIdx := -1;
FCompileIdx := -1;
end;
destructor TBuildCapture.Destroy;
begin
FProjectDirs.Free;
FLines.Free;
inherited;
end;
{ --------------------------------------------------------------------------- }
{ Registrierung }
{ --------------------------------------------------------------------------- }
procedure TBuildCapture.RegisterNotifiers;
var
Filter: IOTAToolsFilter;
Compile: IOTACompileServices;
begin
{$WARN SYMBOL_DEPRECATED OFF}
if Supports(BorlandIDEServices, IOTAToolsFilter, Filter) then
FFilterIdx := Filter.AddNotifier(Self as IOTANotifier);
{$WARN SYMBOL_DEPRECATED ON}
if Supports(BorlandIDEServices, IOTACompileServices, Compile) then
FCompileIdx := Compile.AddNotifier(Self as IOTACompileNotifier);
end;
procedure TBuildCapture.UnregisterNotifiers;
var
Filter: IOTAToolsFilter;
Compile: IOTACompileServices;
begin
{$WARN SYMBOL_DEPRECATED OFF}
if (FFilterIdx >= 0) and Supports(BorlandIDEServices, IOTAToolsFilter, Filter) then
begin
Filter.RemoveNotifier(FFilterIdx);
FFilterIdx := -1;
end;
{$WARN SYMBOL_DEPRECATED ON}
if (FCompileIdx >= 0) and Supports(BorlandIDEServices, IOTACompileServices, Compile) then
begin
Compile.RemoveNotifier(FCompileIdx);
FCompileIdx := -1;
end;
end;
{ --------------------------------------------------------------------------- }
{ Verzeichnisse sammeln + Datei suchen }
{ --------------------------------------------------------------------------- }
procedure TBuildCapture.CollectProjectDirs;
var
ModSvc: IOTAModuleServices;
ProjGroup: IOTAProjectGroup;
Project: IOTAProject;
Dir: string;
I, J: Integer;
begin
FProjectDirs.Clear;
if not Supports(BorlandIDEServices, IOTAModuleServices, ModSvc) then Exit;
for I := 0 to ModSvc.ModuleCount - 1 do
if Supports(ModSvc.Modules[I], IOTAProjectGroup, ProjGroup) then
begin
Dir := IncludeTrailingPathDelimiter(ExtractFilePath(ProjGroup.FileName));
FProjectDirs.Add(Dir);
Dir := IncludeTrailingPathDelimiter(
ExtractFilePath(ExcludeTrailingPathDelimiter(
ExtractFilePath(ProjGroup.FileName))));
FProjectDirs.Add(Dir);
for J := 0 to ProjGroup.ProjectCount - 1 do
begin
Project := ProjGroup.Projects[J];
if Project.FileName <> '' then
FProjectDirs.Add(IncludeTrailingPathDelimiter(
ExtractFilePath(Project.FileName)));
end;
Break;
end;
end;
procedure TBuildCapture.FindNewestLogFile(out AFile: string);
var
SR: TSearchRec;
Age: TDateTime;
Best: TDateTime;
Dir: string;
Ext: string;
I: Integer;
begin
AFile := '';
Best := 0;
for I := 0 to FProjectDirs.Count - 1 do
begin
Dir := FProjectDirs[I];
if not DirectoryExists(Dir) then Continue;
for Ext in ['.log', '.all'] do
begin
if FindFirst(Dir + '*' + Ext, faAnyFile, SR) = 0 then
try
repeat
if FileAge(Dir + SR.Name, Age) and (Age > Best) then
begin
Best := Age;
AFile := Dir + SR.Name;
end;
until FindNext(SR) <> 0;
finally
System.SysUtils.FindClose(SR);
end;
end;
end;
end;
procedure TBuildCapture.ScanForLogFile;
var
Found: string;
begin
if FProjectDirs.Count = 0 then
CollectProjectDirs;
if FProjectDirs.Count = 0 then Exit;
FindNewestLogFile(Found);
FFoundFile := Found;
if Found <> '' then
FLines.LoadFromFile(Found, TEncoding.UTF8);
end;
{ --------------------------------------------------------------------------- }
{ VS-Code-Bridge: Ergebnis + Log in %TEMP%\DelphiBuildBridge ablegen }
{ }
{ Der VS-Code-Task loescht build.status/build.log, triggert den Build per }
{ Tastendruck und wartet auf das Wiederauftauchen von build.status. }
{ build.status wird ZULETZT geschrieben -> dessen Existenz heisst "fertig". }
{ --------------------------------------------------------------------------- }
procedure TBuildCapture.WriteBridgeResult(AResult: TOTACompileResult);
var
Dir: string;
Status: string;
begin
Dir := TPath.Combine(TPath.GetTempPath, 'DelphiBuildBridge');
try
if not TDirectory.Exists(Dir) then
TDirectory.CreateDirectory(Dir);
// 1) Compiler-/Messages-Log zuerst schreiben
FLines.SaveToFile(TPath.Combine(Dir, 'build.log'), TEncoding.UTF8);
// 2) Status zuletzt -> Existenz signalisiert dem Task "Build fertig"
case AResult of
crOTASucceeded: Status := 'SUCCEEDED';
crOTAFailed: Status := 'FAILED';
crOTABackground: Status := 'BACKGROUND';
else
Status := 'UNKNOWN';
end;
TFile.WriteAllText(
TPath.Combine(Dir, 'build.status'),
Status + sLineBreak + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now) + sLineBreak,
TEncoding.UTF8);
except
// Die Bridge darf den Build niemals stoeren
end;
end;
{ --------------------------------------------------------------------------- }
{ IOTANotifier (No-ops) }
{ --------------------------------------------------------------------------- }
procedure TBuildCapture.AfterSave; begin end;
procedure TBuildCapture.BeforeSave; begin end;
procedure TBuildCapture.Modified; begin end;
procedure TBuildCapture.Destroyed;
begin
FFilterIdx := -1;
FCompileIdx := -1;
end;
{ --------------------------------------------------------------------------- }
{ IOTAToolsFilterNotifier }
{ --------------------------------------------------------------------------- }
procedure TBuildCapture.Filter(FileName: string; ErrorCode: Integer;
StdOut, StdError: TStrings);
begin
if Assigned(StdOut) then FLines.AddStrings(StdOut);
if Assigned(StdError) and (StdError.Count > 0) then
FLines.AddStrings(StdError);
end;
function TBuildCapture.GetFilterName: string;
begin
Result := 'BuildLog_Plugin';
end;
{ --------------------------------------------------------------------------- }
{ IOTACompileNotifier }
{ --------------------------------------------------------------------------- }
procedure TBuildCapture.ProjectGroupCompileStarted(Mode: TOTACompileMode);
begin
FInGroupBuild := True;
FLines.Clear;
FFoundFile := '';
CollectProjectDirs;
end;
procedure TBuildCapture.ProjectGroupCompileFinished(Result: TOTACompileResult);
begin
if FLines.Count = 0 then
ScanForLogFile;
WriteBridgeResult(Result);
FInGroupBuild := False;
end;
procedure TBuildCapture.ProjectCompileStarted(const Project: IOTAProject;
Mode: TOTACompileMode);
begin
// Einzelprojekt-Build (Umschalt+F9 auf aktivem Projekt) ist nicht Teil eines
// Gruppen-Builds -> hier selbst initialisieren.
if not FInGroupBuild then
begin
FLines.Clear;
FFoundFile := '';
CollectProjectDirs;
end;
end;
procedure TBuildCapture.ProjectCompileFinished(const Project: IOTAProject;
Result: TOTACompileResult);
begin
if not FInGroupBuild then
begin
if FLines.Count = 0 then
ScanForLogFile;
WriteBridgeResult(Result);
end;
end;
end.