forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cProjectClasses.pas
613 lines (532 loc) · 17.3 KB
/
cProjectClasses.pas
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
{-----------------------------------------------------------------------------
Unit Name: cProjectClasses
Author: Kiriakos Vlahos
Date: 30-Nov-2007
Purpose: Data structures for PyScripter projects
History:
16-Jun-2008 Roman Krivoruchko
Project property ExtraPythonPath added with persistence
-----------------------------------------------------------------------------}
unit cProjectClasses;
interface
uses
SysUtils, Classes, Contnrs, JvAppStorage, cPyBaseDebugger;
type
TAbstractProjectNode = class;
TAbstractProjectNodeClass = class of TAbstractProjectNode;
TProjectNodeAction = function (Node: TAbstractProjectNode; Data : Pointer):boolean;
TAbstractProjectNode = class(TInterfacedPersistent, IJvAppStorageHandler, IJvAppStoragePublishedProps)
private
fChildren : TObjectList;
fParent : TAbstractProjectNode;
fModified : Boolean;
function GetRootNode: TAbstractProjectNode;
function GetModified: Boolean;
procedure SetModified(const Value: Boolean);
procedure SetParent(const Value: TAbstractProjectNode);
protected
function GetCaption: string; virtual; abstract;
// IJvAppStorageHandler implementation
procedure ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string); virtual;
procedure WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string); virtual;
function CreateListItem(Sender: TJvCustomAppStorage; const Path: string;
Index: Integer): TPersistent;
public
constructor Create; virtual;
destructor Destroy; override;
procedure ClearChildren;
procedure AddChild(Child:TAbstractProjectNode);
procedure RemoveChild(Child:TAbstractProjectNode);
procedure ForEach(Proc: TProjectNodeAction; Data:Pointer = nil);
function FirstThat(Proc: TProjectNodeAction; Data:Pointer = nil): TAbstractProjectNode;
procedure SortChildren; virtual;
property Parent : TAbstractProjectNode read fParent write SetParent;
property Children : TObjectList read fChildren;
property RootNode : TAbstractProjectNode read GetRootNode;
property Modified : Boolean read GetModified write SetModified;
property Caption : string read GetCaption;
end;
TProjectRootNode = class(TAbstractProjectNode)
private
fFileName: string;
fStoreRelativePaths : Boolean;
fShowFileExtensions : Boolean;
fExtraPythonPath: TStrings;
function GetName: string;
protected
function GetCaption: string; override;
procedure ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string); override;
procedure WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string); override;
public
constructor Create; override;
destructor Destroy; override;
function HasFile(const FileName : string) : Boolean;
procedure AppendExtraPaths;
procedure RemoveExtraPaths;
property Name : string read GetName;
property FileName : string read fFileName write fFileName;
published
property StoreRelativePaths : Boolean read fStoreRelativePaths write fStoreRelativePaths;
property ShowFileExtensions : Boolean read fShowFileExtensions write fShowFileExtensions;
property ExtraPythonPath: TStrings read fExtraPythonPath;
end;
TProjectFileNode = class;
TProjectFolderNode = class;
TProjectFilesNode = class(TAbstractProjectNode)
private
function GetFileChild(FileName: string): TProjectFileNode;
function GetFolderChild(FolderName: string): TProjectFolderNode;
{There should be only one FilesNode per project under the Project Root}
protected
function GetCaption: string; override;
public
procedure SortChildren; override;
procedure ImportDirectory(const Directory, Masks : string; Recursive : Boolean);
property FileChild[FileName : string] : TProjectFileNode read GetFileChild;
property FolderChild[FolderName : string] : TProjectFolderNode read GetFolderChild;
end;
TProjectFolderNode = class(TProjectFilesNode)
{Like ProjectFilesNode but with a name that can be changed}
private
fName: string;
protected
function GetCaption: string; override;
published
property Name : string read fName write fName;
end;
TProjectFileNode = class(TAbstractProjectNode)
private
fFileName: string;
function GetName: string;
protected
function GetCaption: string; override;
procedure ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string); override;
procedure WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string); override;
public
property Name : string read GetName;
property FileName : string read fFileName write fFileName;
end;
TProjectRunConfiguationsNode = class(TAbstractProjectNode)
protected
function GetCaption: string; override;
end;
TProjectRunConfiguationNode = class(TAbstractProjectNode)
private
fName: string;
fRunConfig : TRunConfiguration;
procedure SetRunConfig(const Value: TRunConfiguration);
protected
function GetCaption: string; override;
published
constructor Create; override;
destructor Destroy; override;
property Name : string read fName write fName;
property RunConfig : TRunConfiguration read FRunConfig write SetRunConfig;
end;
Var
ActiveProject : TProjectRootNode;
implementation
uses
cParameters, uCommonFunctions, cPyDebugger;
{ TAbstractProjectNode }
procedure TAbstractProjectNode.AddChild(Child: TAbstractProjectNode);
begin
fChildren.Add(Child);
Child.fParent := Self;
if fChildren.Count > 1 then SortChildren;
fModified := True;
end;
procedure TAbstractProjectNode.ClearChildren;
begin
while fChildren.Count > 0 do
fChildren.Last.Free;
end;
constructor TAbstractProjectNode.Create;
begin
inherited;
fChildren := TObjectList.Create(False);
end;
function TAbstractProjectNode.CreateListItem(Sender: TJvCustomAppStorage;
const Path: string; Index: Integer): TPersistent;
procedure RaiseError;
begin
raise Exception.Create('Error in reading project data');
end;
Var
ClassName : string;
NodeClass : TAbstractProjectNodeClass;
begin
Result := nil;
ClassName := Sender.ReadString(Path + '\ClassName');
if ClassName = '' then
RaiseError;
try
NodeClass := TAbstractProjectNodeClass(GetClass(ClassName));
if not Assigned(NodeClass) then
RaiseError;
Result := NodeClass.Create;
except
RaiseError;
end;
end;
destructor TAbstractProjectNode.Destroy;
begin
SetParent(nil);
ClearChildren;
fChildren.Free;
inherited;
end;
function TAbstractProjectNode.FirstThat(Proc: TProjectNodeAction;
Data: Pointer): TAbstractProjectNode;
var
i : integer;
begin
Result := nil;
if Proc(Self, Data) then
Result := Self
else
for i := 0 to fChildren.Count-1 do begin
Result := TAbstractProjectNode(fChildren[i]).FirstThat(Proc, Data);
if Result <> nil then exit;
end;
end;
procedure TAbstractProjectNode.ForEach(Proc: TProjectNodeAction; Data: Pointer);
var
i : integer;
begin
Proc(Self, Data);
for i := 0 to fChildren.Count-1 do
TAbstractProjectNode(Children[i]).ForEach(Proc, Data);
end;
function CheckModified(Node: TAbstractProjectNode; Data : Pointer):boolean;
begin
Result := Node.fModified;
end;
function TAbstractProjectNode.GetModified: Boolean;
var
Node : TAbstractProjectNode;
begin
Result := fModified;
if not Result then begin
Node := FirstThat(CheckModified);
Result := Assigned(Node);
end;
end;
function TAbstractProjectNode.GetRootNode: TAbstractProjectNode;
begin
if fParent = nil then
Result := Self
else
Result := fParent.RootNode;
end;
procedure TAbstractProjectNode.ReadFromAppStorage(
AppStorage: TJvCustomAppStorage; const BasePath: string);
var
i : integer;
begin
if AppStorage.PathExists(BasePath+'\ChildNodes') then begin
ClearChildren;
AppStorage.ReadObjectList(BasePath+'\ChildNodes', fChildren, CreateListItem, True, 'Node');
for i := 0 to fChildren.Count - 1 do
TAbstractProjectNode(fChildren[i]).fParent := Self;
if fChildren.Count > 1 then SortChildren;
end;
end;
procedure TAbstractProjectNode.RemoveChild(Child: TAbstractProjectNode);
begin
if Child = fChildren.Last then
fChildren.Delete(fChildren.Count - 1) //speed up when destroying
else
fChildren.Remove(Child);
Child.fParent := nil;
fModified := True;
end;
function ReSetModified(Node: TAbstractProjectNode; Data : Pointer):boolean;
begin
Node.fModified := False;
Result := True;
end;
procedure TAbstractProjectNode.SetModified(const Value: Boolean);
begin
fModified := Value;
// Only apply to children if Modified is False
if not Value then
ForEach(ReSetModified);
end;
procedure TAbstractProjectNode.SetParent(const Value: TAbstractProjectNode);
begin
if fParent <> Value then
begin
if fParent <> nil then fParent.RemoveChild(Self);
if Value <> nil then Value.AddChild(Self);
end;
end;
procedure TAbstractProjectNode.SortChildren;
begin
// Do nothing by default
end;
procedure TAbstractProjectNode.WriteToAppStorage(
AppStorage: TJvCustomAppStorage; const BasePath: string);
begin
AppStorage.WriteString(BasePath+'\ClassName', Self.ClassName);
if fChildren.Count > 0 then
AppStorage.WriteObjectList(BasePath+'\ChildNodes', fChildren, 'Node');
end;
{ TProjectRootNode }
procedure TProjectRootNode.AppendExtraPaths;
var
i: Integer;
begin
if not (Assigned(PyControl) and Assigned(PyControl.ActiveInterpreter)) then Exit;
for i := 0 to fExtraPythonPath.Count-1 do begin
InternalInterpreter.SysPathAdd(fExtraPythonPath[i]);
if PyControl.ActiveInterpreter <> InternalInterpreter then
PyControl.ActiveInterpreter.SysPathAdd(fExtraPythonPath[i]);
end;
end;
constructor TProjectRootNode.Create;
begin
inherited;
AddChild(TProjectFilesNode.Create);
AddChild(TProjectRunConfiguationsNode.Create);
Modified := False;
fExtraPythonPath := TStringList.Create;
fStoreRelativePaths := True;
end;
destructor TProjectRootNode.Destroy;
begin
RemoveExtraPaths;
FreeAndNil(fExtraPythonPath);
inherited;
end;
function TProjectRootNode.GetCaption: string;
begin
Result := Name;
end;
function TProjectRootNode.GetName: string;
begin
if fFileName <> '' then
Result := ChangeFileExt(ExtractFileName(fFileName), '')
else
Result := 'Untitled';
end;
function NodeHasFile(Node: TAbstractProjectNode; Data : Pointer):boolean;
begin
Result := False;
if (Node is TProjectFileNode) and
AnsiSameText(PWideChar(Data), TProjectFileNode(Node).FileName)
then
Result := True;
end;
function TProjectRootNode.HasFile(const FileName: string): Boolean;
begin
Result := FirstThat(NodeHasFile, PWideChar(FileName)) <> nil;
end;
procedure TProjectRootNode.ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
begin
inherited;
RemoveExtraPaths;
fExtraPythonPath.Clear;
AppStorage.ReadStringList(BasePath+'\ExtraPythonPath', fExtraPythonPath);
AppendExtraPaths;
end;
procedure TProjectRootNode.RemoveExtraPaths;
var
i: Integer;
begin
if not (Assigned(PyControl) and Assigned(PyControl.ActiveInterpreter)) then Exit;
for i := 0 to fExtraPythonPath.Count-1 do begin
InternalInterpreter.SysPathRemove(fExtraPythonPath[i]);
if PyControl.ActiveInterpreter <> InternalInterpreter then
PyControl.ActiveInterpreter.SysPathRemove(fExtraPythonPath[i]);
end;
end;
procedure TProjectRootNode.WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
begin
inherited;
AppStorage.WriteStringList(BasePath+'\ExtraPythonPath', fExtraPythonPath);
end;
{ TProjectFolderNode }
function TProjectFolderNode.GetCaption: string;
begin
Result := fName;
end;
{ TProjectFilesNode }
function TProjectFilesNode.GetCaption: string;
begin
Result := 'Files';
end;
function CompareFolderChildren(P1, P2: Pointer): integer;
Var
Node1, Node2 : TAbstractProjectNode;
begin
Result := 0; // to keep compiler happy...
Node1 := TAbstractProjectNode(P1);
Node2 := TAbstractProjectNode(P2);
if (Node1 is TProjectFolderNode) and (Node2 is TProjectFileNode) then
Result := -1
else if (Node1 is TProjectFileNode) and (Node2 is TProjectFolderNode) then
Result := 1
else if (Node1 is TProjectFolderNode) and (Node2 is TProjectFolderNode) then
Result := AnsiCompareText(TProjectFolderNode(Node1).Name, TProjectFolderNode(Node2).Name)
else if (Node1 is TProjectFileNode) and (Node2 is TProjectFileNode) then
Result := AnsiCompareText(TProjectFileNode(Node1).Name, TProjectFileNode(Node2).Name)
else
Assert(False, 'Unexpected Child types in TProjectFilesNode');
end;
function TProjectFilesNode.GetFileChild(FileName: string): TProjectFileNode;
var
i: Integer;
begin
Result := nil;
for i := 0 to fChildren.Count - 1 do begin
if (fChildren[i] is TProjectFileNode) and
(AnsiCompareText(TProjectFileNode(fChildren[i]).fFileName, FileName) = 0) then
begin
Result := TProjectFileNode(fChildren[i]);
break;
end;
end;
end;
function TProjectFilesNode.GetFolderChild(FolderName: string): TProjectFolderNode;
var
i: Integer;
begin
Result := nil;
for i := 0 to fChildren.Count - 1 do begin
if (fChildren[i] is TProjectFolderNode) and
(AnsiCompareText(TProjectFolderNode(fChildren[i]).fName, FolderName) = 0) then
begin
Result := TProjectFolderNode(fChildren[i]);
break;
end;
end;
end;
procedure TProjectFilesNode.ImportDirectory(const Directory, Masks: string;
Recursive: Boolean);
Var
FileList: TStringList;
i : integer;
FileNode : TProjectFileNode;
FolderNode : TProjectFolderNode;
FolderName : string;
FileName : string;
begin
FolderName := ExtractFileName(Directory);
if (FolderName = '.') or (FolderName = '..') then
Exit;
FolderNode := FolderChild[FolderName];
if not Assigned(FolderNode) then begin
FolderNode := TProjectFolderNode.Create;
FolderNode.fName := FolderName;
AddChild(FolderNode);
end;
FileList := TStringList.Create;
try
GetFilesInPaths(Directory, Masks, FileList, False);
for i := 0 to FileList.Count - 1 do begin
FileName := FileList[i];
if not Assigned(FileChild[FileName]) then begin
FileNode := TProjectFileNode.Create;
FileNode.fFileName := FileName;
FolderNode.AddChild(FileNode);
end;
end;
if Recursive then begin
FileList.Clear;
GetDirectoriesInPaths(Directory, '*.*', FileList, False);
for i := 0 to FileList.Count - 1 do begin
FolderName := FileList[i];
if (FolderName = '.') or (FolderName = '..') then
continue;
FolderNode.ImportDirectory(FolderName, Masks, Recursive);
end;
end;
finally
FileList.Free;
end;
if FolderNode.Children.Count = 0 then
FolderNode.Free; //Delete empty nodes
end;
procedure TProjectFilesNode.SortChildren;
begin
fChildren.Sort(CompareFolderChildren);
end;
{ TProjectRunConfiguationsNode }
function TProjectRunConfiguationsNode.GetCaption: string;
begin
Result := 'Run Configurations';
end;
{ TProjectFileNode }
function TProjectFileNode.GetCaption: string;
begin
Result := Name;
end;
function TProjectFileNode.GetName: string;
begin
if fFileName <> '' then begin
Result := ExtractFileName(Parameters.ReplaceInText(fFileName));
if not ActiveProject.ShowFileExtensions then
Result := ChangeFileExt(Result, '');
end else
Result := 'Untitled';
end;
procedure TProjectFileNode.ReadFromAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
begin
inherited;
FileName := AppStorage.ReadString(BasePath + '\FileName');
end;
procedure TProjectFileNode.WriteToAppStorage(AppStorage: TJvCustomAppStorage;
const BasePath: string);
Var
BaseDir : string;
begin
inherited;
BaseDir := '';
if ActiveProject.StoreRelativePaths then begin
BaseDir := ExtractFilePath(ActiveProject.fFileName);
if BaseDir <> '' then begin
if Pos(BaseDir, fFileName) = 1 then begin
Delete(fFileName, 1, Length(BaseDir));
fFileName := '$[Project-Path]'+fFileName;
end;
end;
end else
fFileName := Parameters.ReplaceInText(fFileName);
AppStorage.WriteString(BasePath + '\FileName', fFileName);
end;
{ TProjectRunConfiguationNode }
constructor TProjectRunConfiguationNode.Create;
begin
inherited;
fRunConfig := TRunConfiguration.Create;
end;
destructor TProjectRunConfiguationNode.Destroy;
begin
fRunConfig.Free;
inherited;
end;
function TProjectRunConfiguationNode.GetCaption: string;
begin
Result := Name;
end;
procedure TProjectRunConfiguationNode.SetRunConfig(const Value: TRunConfiguration);
begin
FRunConfig.Assign(Value);
end;
initialization
RegisterClasses([TProjectRootNode, TProjectFolderNode, TProjectFilesNode,
TProjectFileNode, TProjectRunConfiguationsNode, TProjectRunConfiguationNode]);
ActiveProject := TProjectRootNode.Create;
finalization
FreeAndNil(ActiveProject);
end.