File: mainunit.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (478 lines) | stat: -rw-r--r-- 13,883 bytes parent folder | download | duplicates (2)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Tom Gregorovic
}
unit MainUnit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
  FPDocFiles, StdCtrls, ComCtrls, FileUtil, ExtCtrls,
  LCLIntf, LCLType, LCLProc, Process, UTF8Process, EditBtn, Laz_XMLCfg;

type

  { TFormMain }

  TFormMain = class(TForm)
    ButtonRefresh: TButton;
    ButtonUpdateNew: TButton;
    ButtonUpdate: TButton;
    ButtonUpdateAll: TButton;
    CheckBoxShowSummary: TCheckBox;
    CheckBoxBackup: TCheckBox;
    EditInclude: TDirectoryEdit;
    EditMakeSkel: TFileNameEdit;
    EditPackage: TEdit;
    EditBackup: TEdit;
    EditUnits: TDirectoryEdit;
    EditDocs: TDirectoryEdit;
    Label1: TLabel;
    LabelInclude: TLabel;
    LabelMakeSkel: TLabel;
    LabelPackage: TLabel;
    LabelBackup: TLabel;
    LabelUnits: TLabel;
    LabelDocs: TLabel;
    ListBox: TListBox;
    OpenDialog: TOpenDialog;
    StatusBar: TStatusBar;
    procedure ButtonRefreshClick(Sender: TObject);
    procedure ButtonUpdateAllClick(Sender: TObject);
    procedure ButtonUpdateClick(Sender: TObject);
    procedure ButtonUpdateNewClick(Sender: TObject);
    procedure EditDocsChange(Sender: TObject);
    procedure EditUnitsChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListBoxDrawItem(Control: TWinControl; Index: Integer;
      ARect: TRect; State: TOwnerDrawState);
  private
  public
    procedure BeginUpdate;
    procedure EndUpdate;
    procedure UpdateList;
    procedure UpdateFile(const AFileName: String);
    procedure BackupFile(const AFileName: String);
    procedure WriteStatus(const S: String);
    
    procedure MoveElement(const SrcPackage: TFPDocPackage;
      const SrcModule: TFPDocModule; const Src: TFPDocElement;
      const DestList: TStrings; var Dest: Integer);
  end; 

var
  FormMain: TFormMain;
  XMLConfig: TXMLConfig;
  BackupList: TStringList;

implementation

{$R *.lfm}

uses
  UnitMove, UnitSummary;

function FindFiles(const Path, Mask: String; OnlyFileName: Boolean = False): TStringList;
var
  I: Integer;
begin
  Result := FindAllFiles(Path, Mask, False);
  if OnlyFileName then
    for I := 0 to Result.Count - 1 do
      Result[I] := ExtractFileNameOnly(Result[I]);
end;

{ TFormMain }

procedure TFormMain.FormShow(Sender: TObject);
begin
  UpdateList;
end;

procedure TFormMain.ListBoxDrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
begin
  if (Index < 0) or (Index >= ListBox.Items.Count) then Exit;
  
  with ListBox.Canvas do
  begin
    if odSelected in State then
      Brush.Color := clHighlight
    else
    begin
      Brush.Color := ListBox.Color;
      case PtrInt(ListBox.Items.Objects[Index]) of
      0: SetTextColor(ListBox.Canvas.Handle, ListBox.Canvas.Font.Color); // normal
      1: SetTextColor(ListBox.Canvas.Handle, clRed);                     // new
      end;
    end;
      
    FillRect(ARect);
    TextRect(ARect, ARect.Left + 8, ARect.Top + 2, ExtractFileNameOnly(ListBox.Items[Index]));
  end;
end;

procedure TFormMain.BeginUpdate;
begin
  BackupList := TStringList.Create;
  BackupList.Sorted := True;
  WriteStatus('Updating started.');
end;

procedure TFormMain.EndUpdate;
begin
  BackupList.Free;
  UpdateList;
  WriteStatus('Updating done.');
  Sleep(5000);
  WriteStatus('');
end;

procedure TFormMain.ButtonRefreshClick(Sender: TObject);
begin
  UpdateList;
end;

procedure TFormMain.ButtonUpdateAllClick(Sender: TObject);
var
  I: Integer;
begin
  BeginUpdate;
  try
    for I := 0 to ListBox.Items.Count - 1 do
      UpdateFile(ListBox.Items[I]);
  finally
    EndUpdate;
  end;
end;

procedure TFormMain.ButtonUpdateClick(Sender: TObject);
var
  I: Integer;
begin
  BeginUpdate;
  try
    for I := 0 to ListBox.Items.Count - 1 do
      if ListBox.Selected[I] then UpdateFile(ListBox.Items[I]);
  finally
    EndUpdate;
  end;
end;

procedure TFormMain.ButtonUpdateNewClick(Sender: TObject);
var
  I: Integer;
begin
  BeginUpdate;
  try
    for I := 0 to ListBox.Items.Count - 1 do
      if PtrInt(ListBox.items.Objects[I]) = 1 then UpdateFile(ListBox.Items[I]);
  finally
    EndUpdate;
  end;
end;

procedure TFormMain.EditDocsChange(Sender: TObject);
begin
  if DirectoryExistsUTF8(EditDocs.Text) then UpdateList;
end;

procedure TFormMain.EditUnitsChange(Sender: TObject);
begin
  if DirectoryExistsUTF8(EditUnits.Text) then UpdateList;
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  XMLConfig := TXMLConfig.Create('FPDocUpdater.xml');

 EditDocs.Directory := XMLConfig.GetValue('FPDocsPath/Value', 'Please set path to <Docs\xml\lcl\>');
  EditUnits.Directory := XMLConfig.GetValue('UnitsPath/Value', 'Please set path to <LCL\>');
  EditInclude.Directory := XMLConfig.GetValue('IncludePath/Value', 'Please set path to <LCL\Include\>');
  EditMakeSkel.FileName := XMLConfig.GetValue('MakeSkelPath/Value', 'Please set path to <makeskel.exe>');
  CheckBoxBackup.Checked := XMLConfig.GetValue('BackupFPDocs/Value', True);
  EditBackup.Text := XMLConfig.GetValue('BackupExt/Value', 'bak');
  EditPackage.Text := XMLConfig.GetValue('Package/Value', 'lcl');
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  XMLConfig.Clear;
  
  XMLConfig.SetValue('FPDocsPath/Value', EditDocs.Directory);
  XMLConfig.SetValue('UnitsPath/Value', EditUnits.Directory);
  XMLConfig.SetValue('IncludePath/Value', EditInclude.Directory);
  XMLConfig.SetValue('MakeSkelPath/Value', EditMakeSkel.FileName);
  XMLConfig.SetValue('BackupFPDocs/Value', CheckBoxBackup.Checked);
  XMLConfig.SetValue('BackupExt/Value', EditBackup.Text);
  XMLConfig.SetValue('Package/Value', EditPackage.Text);
  
  XMLConfig.Free;
end;

procedure TFormMain.UpdateList;
var
  Docs, Units: TStringList;
  DocsPath, UnitsPath: String;
  I: Integer;
  N: String;
  State: PtrInt;
begin
  ListBox.Items.BeginUpdate;
  try
    ListBox.Items.Clear;
    
    DocsPath := AppendPathDelim(EditDocs.Directory);
    UnitsPath := AppendPathDelim(EditUnits.Directory);
    
    Docs := FindFiles(DocsPath, '*.xml', True);
    Units := FindFiles(UnitsPath, '*.pas;*.pp');
    try
      Units.Sorted := True;
      for I := 0 to Units.Count - 1 do
      begin
        N := ExtractFileNameOnly(Units[I]);

        if Docs.IndexOf(N) = -1 then
          State := 1
        else
          State := 0;
            
        ListBox.Items.AddObject(Units[I], TObject(State));
      end;
    finally
      Units.Free;
      Docs.Free;
    end;
  finally
    ListBox.Items.EndUpdate;
  end;
end;

procedure RaiseError(const S: String);
begin
  DebugLn(S);
  raise Exception.Create(S);
end;

procedure TFormMain.UpdateFile(const AFileName: String);
var
  DocFileName: String;
  MakeSkelPath: String;
  AProcess: TProcessUTF8;
  AStringList, AErrorList: TStringList;
  M: TMemoryStream;
  N, BytesRead: LongInt;
  OldDoc, NewDoc: TFPDocFile;
const
   READ_BYTES = 2048;
   
begin
  try
    if not FileExistsUTF8(AFileName) then
    begin
      RaiseError('Update ' + AFileName + ' failed!');
    end;

    MakeSkelPath := FindDefaultExecutablePath(EditMakeSkel.FileName);

    if not FileIsExecutable(MakeSkelPath) then
      RaiseError('Unable to find MakeSkel tool executable "' + EditMakeSkel.Text +'"!');

    DocFileName := AppendPathDelim(EditDocs.Directory) + ExtractFileNameOnly(AFileName) + '.xml';

    if CheckBoxBackup.Checked then BackupFile(DocFileName);

    WriteStatus('Updating ' + AFileName);

    AProcess := TProcessUTF8.Create(nil);
    AStringList := TStringList.Create;
    AErrorList := TStringList.Create;
    M := TMemoryStream.Create;
    try
      AProcess.CommandLine :=
        Format(MakeSkelPath + ' --package="%s" --input="%s -Fi%s"',
          [EditPackage.Text, AFileName, EditInclude.Directory]);
      AProcess.Options := AProcess.Options + [poUsePipes, poNoConsole, poStderrToOutPut];
      AProcess.Execute;

      BytesRead := 0;
      while AProcess.Running do
      begin
        M.SetSize(BytesRead + READ_BYTES);
        N := AProcess.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
        if N > 0 then Inc(BytesRead, N)
        else Sleep(100);
      end;

      repeat
        M.SetSize(BytesRead + READ_BYTES);
        N := AProcess.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
        if N > 0 then Inc(BytesRead, N);
      until N <= 0;
      M.SetSize(BytesRead);

      AStringList.LoadFromStream(M);

      while (AStringList.Count > 0) and
        (AStringList.Strings[AStringList.Count - 1] = '') do
        AStringList.Delete(AStringList.Count - 1);

      while (AStringList.Count > 0) and
        (AStringList.Strings[AStringList.Count - 1] <> '') and
        (AStringList.Strings[AStringList.Count - 1] <> '</fpdoc-descriptions>') do
      begin
        AErrorList.Add(AStringList.Strings[AStringList.Count - 1]);
        AStringList.Delete(AStringList.Count - 1);
      end;

      while (AStringList.Count > 0) and
        (AStringList.Strings[AStringList.Count - 1] <> '</fpdoc-descriptions>') do
        AStringList.Delete(AStringList.Count - 1);

      if AStringList.Count = 0 then
        RaiseError('Update ' + AFileName + ' failed, because' + AErrorList.Text);

      M.Clear;
      AStringList.SaveToStream(M);
      M.Position := 0;
      NewDoc := TFPDocFile.Create(M);
      if FileExistsUTF8(DocFileName) then OldDoc := TFPDocFile.Create(DocFileName)
      else OldDoc := nil;

      try
        if OldDoc <> nil then
        begin
          FormSummary.OldInfo := OldDoc.GetInfo;
          OldDoc.AssignToSkeleton(NewDoc, @MoveElement);
          FormSummary.NewInfo := NewDoc.GetInfo;
        end
        else
        begin
          FormSummary.OldInfo := EmptyFPDocInfo;
          FormSummary.NewInfo := NewDoc.GetInfo;
        end;

        if CheckBoxShowSummary.Checked then
        begin
          FormSummary.LabelFileName.Caption := DocFileName;
          if FormSummary.ShowModal = mrOk then
          begin
            NewDoc.SaveToFile(DocFileName);
            WriteStatus('Update ' + AFileName + ' to ' + DocFileName + ' succeeds!');
          end
          else
            WriteStatus('Update ' + AFileName + ' to ' + DocFileName + ' cancelled!');
        end
        else
        begin
          NewDoc.SaveToFile(DocFileName);
          WriteStatus('Update ' + AFileName + ' to ' + DocFileName + ' succeeds!');
        end;

      finally
        if OldDoc <> nil then OldDoc.Free;
        NewDoc.Free;
      end;

    finally
      M.Free;
      AStringList.Free;
      AErrorList.Free;
      AProcess.Free;
    end;
  except
    on E: Exception do
      MessageDlg('Error', E.Message, mtError, [mbOK], '');
  end;
end;

procedure TFormMain.BackupFile(const AFileName: String);
var
  BackupFileName: String;
begin
  if not FileExistsUTF8(AFileName) then Exit;

  if BackupList.IndexOf(AFileName) = -1 then
  begin
    BackupFileName := ChangeFileExt(AFileName, '.' + EditBackup.Text);

    if CopyFile(AFileName, BackupFileName, True) then
    begin
      WriteStatus('Backup ' + AFileName + ' to ' + BackupFileName + ' succeeds.');
      BackupList.Add(AFileName);
    end
    else
      RaiseError('Backup ' + AFileName + ' to ' + BackupFileName + ' failed!');
  end;
end;

procedure TFormMain.WriteStatus(const S: String);
begin
  DebugLn(S);
  StatusBar.SimpleText := S;
  StatusBar.Update;
end;

procedure TFormMain.MoveElement(const SrcPackage: TFPDocPackage;
  const SrcModule: TFPDocModule; const Src: TFPDocElement;
  const DestList: TStrings; var Dest: Integer);
var
  JumpList: TStringList;
  I: Integer;
  Prefix: String;
begin
  FormMove.LabelSrc.Caption := Format('Package: %sModule: %s',
    [SrcPackage.Name + LineEnding, SrcModule.Name]);
  FormMove.LabelSrcElement.Caption := 'Element: ' + Src.Name;

  JumpList := TStringList.Create;
  try
    for I := 0 to DestList.Count - 1 do
    begin
      if Pos('.', DestList[I]) = 0 then Continue;
      Prefix := Copy(DestList[I], 1, LastDelimiter('.', DestList[I]) - 1);
      if JumpList.IndexOf(Prefix) = -1 then JumpList.Add(Prefix);
    end;
    
    FormMove.ComboBoxJump.Items.Assign(JumpList);
  finally
    JumpList.Free;
  end;
  
  FormMove.ListBoxDest.Items.Assign(DestList);
  
  case FormMove.ShowModal of
    mrYes: Dest := FormMove.ListBoxDest.ItemIndex;
  end;
  
  if Dest <> -1 then
    WriteStatus('Move Element: ' + SrcPackage.Name + '\' + SrcModule.Name + '\' + Src.Name +
      ' Dest: ' + DestList[Dest]);
end;

end.