-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchPath.Project.pas
More file actions
1290 lines (1100 loc) · 34.2 KB
/
SearchPath.Project.pas
File metadata and controls
1290 lines (1100 loc) · 34.2 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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit SearchPath.Project;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections,
ToolsAPI, Semin64.Xml, SearchPath.XMLDocument, SearchPath.Common;
type
TProjectLogMessageEvent = procedure (Sender: TObject; Text: string; LogLevel: TLogLevel) of object;
TProject = class;
TProjectMapping = class
private
FProject: TProject;
FDelphiProject: IOTAProject;
public
constructor Create(AProject: TProject; ADelphiProject: IOTAProject); overload;
destructor Destroy; override;
property Project: TProject read FProject;
property DelphiProject: IOTAProject read FDelphiProject;
end;
TDocumentBase = class
strict private
FCol: Integer;
FRow: Integer;
FDoc: TXMLDocumentObject;
FElement: PsanXMLObjElement;
FProject: TProject;
procedure SetLocation(AElement: PsanXMLObjElement);
protected
function GetAttributeValue(const AttrName: string): string;
function GetElement(AElementName: string): PsanXMLObjElement; overload;
function GetElement(AElementIndex: Integer): PsanXMLObjElement; overload;
function GetChildElementValue(AElementName: string): string;
function ChildCount: Integer;
function GetName: string;
function GetValue: string;
function iif<T>(B: Boolean; T1, T2: T): T;
public
constructor Create(AProject: TProject; AElement: PsanXMLObjElement); overload;
procedure Debug(Text: string); overload;
procedure Info(Text: string); overload;
procedure Warn(Text: string); overload;
procedure Error(Text: string); overload;
procedure Debug(const Format: string; const Args: array of const); overload;
procedure Info(const Format: string; const Args: array of const); overload;
procedure Warn(const Format: string; const Args: array of const); overload;
procedure Error(const Format: string; const Args: array of const); overload;
property Project: TProject read FProject;
property Row: Integer read FRow;
property Col: Integer read FCol;
end;
TProxyFilter = class(TDocumentBase)
strict private
FFilter: string;
public
destructor Destroy; override;
procedure Parse;
property Filter: string read FFilter;
end;
TProxyFilters = class(TDocumentBase)
strict private
FFilters: TList<TProxyFilter>;
public
destructor Destroy; override;
procedure Parse;
property Filters: TList<TProxyFilter> read FFilters;
end;
TProxy = class(TDocumentBase)
strict private
FHost: string;
FPort: Integer;
FUser: string;
FPassword: string;
FEnabled: Boolean;
FExcludeFilters: TProxyFilters;
FIncludeFilters: TProxyFilters;
public
destructor Destroy; override;
procedure Parse;
property Host: string read FHost;
property Port: Integer read FPort;
property User: string read FUser write FUser;
property Password: string read FPassword write FPassword;
property Enabled: Boolean read FEnabled;
property IncludeFilters: TProxyFilters read FIncludeFilters;
property ExcludeFilters: TProxyFilters read FExcludeFilters;
end;
TProperty = class(TDocumentBase)
strict private
FName: string;
FValue: string;
public
procedure Parse;
procedure AddSystemProperty(AName, AValue: string);
procedure AddSpecialProperty(AName, AValue: string);
function ToString: string; override;
property Name: string read FName;
property Value: string read FValue;
end;
TProperties = class(TDocumentBase)
const
DUP_MESSAGE = 'Duplicates %s property: %s, skipped %s';
strict private
FProperties: TStrings;
procedure ResolvePropertyValues;
procedure AddCustomProperties;
procedure AddSpecialProperties;
procedure AddSystemProperties;
public
destructor Destroy; override;
procedure Parse(ExistsCustomeProperties: Boolean);
function GetValue(const AName: string): string;
property Properties: TStrings read FProperties;
end;
TLibPath = class(TDocumentBase)
strict private
FPath: string;
public
procedure Parse(PathPrefix: string);
property Path: string read FPath;
end;
TLibPaths = class(TDocumentBase)
strict private
FPaths: TStrings;
public
destructor Destroy; override;
procedure Parse(const PathPrefix: string);
function GetSearchPath(Strings: TStrings): string;
property Paths: TStrings read FPaths;
end;
TDependencie = class(TDocumentBase)
strict private
FGroup: string;
FVersion: string;
FArtifactId: string;
FCompileVersion: string;
FType: string;
FPath: string;
FName: string;
FLibPaths: TLibPaths;
FRepo: string;
procedure Unzip(const ZipFile, FilesRoot: string);
public
destructor Destroy; override;
procedure Parse;
procedure GetSearchPath(Strings: TStrings);
procedure DownloadPackage;
property Group: string read FGroup;
property ArtifactId: string read FArtifactId;
property Version: string read FVersion;
property CompileVersion: string read FCompileVersion;
property &Type: string read FType;
property LibPaths: TLibPaths read FLibPaths;
property Repo: string read FRepo;
end;
TDependencies = class(TDocumentBase)
strict private
FDependencies: TList<TDependencie>;
public
destructor Destroy; override;
procedure Parse;
procedure GetSearchPath(Strings: TStrings);
procedure DownloadPackage;
end;
TRepository = class(TDocumentBase)
private
FId: string;
FName: string;
FURL: string;
public
procedure Parse;
property Id: string read FId write FId;
property Name: string read FName write FName;
property URL: string read FURL write FURL;
end;
TRepositories = class(TDocumentBase)
strict private
FRepositories: TList<TRepository>;
public
constructor Create(AProject: TProject; AElement: PsanXMLObjElement);
destructor Destroy; override;
procedure Parse;
property Repositories: TList<TRepository> read FRepositories;
end;
TProject = class(TDocumentBase)
private
const REPOSITORIES_KEY = 'repositories';
const PROXY_KEY = 'proxy';
const PROPERTIES_KEY = 'properties';
const DEPENDENCIES_KEY = 'dependencies';
strict private
FVer: string;
FLogLevel: TLogLevel;
FProperties: TProperties;
FProxy: TProxy;
FRepositories: TRepositories;
FDependencies: TDependencies;
FParent: TProject;
FDocument: TXMLDocumentObject;
FProjectName: string;
FLogMessage: TProjectLogMessageEvent;
function GetRepositories: TList<TRepository>;
procedure ParseRepositories;
procedure ParseDependencies;
procedure ParseProperties;
procedure ParseProxy;
public
constructor Create(AParent: TProject; AProjectName: string);
destructor Destroy; override;
procedure Parse;
function GetPropertyValue(const AName: string): string;
property Document: TXMLDocumentObject read FDocument;
property ProjectName: string read FProjectName;
// 文档中的内容
property Ver: string read FVer;
property LogLevel: TLogLevel read FLogLevel;
property Proxy: TProxy read FProxy;
property Repositories: TList<TRepository> read GetRepositories;
property Properties: TProperties read FProperties;
property Dependencies: TDependencies read FDependencies;
//
property LogMessage: TProjectLogMessageEvent read FLogMessage write FLogMessage;
end;
TProjects = class
strict private
FRootProject: TProject;
FSearchPath: ISearchPathManager;
FProjects: TDictionary<string, TProjectMapping>;
procedure Init;
procedure ParseProject(const AProjectName: string);
procedure UpdateProjectLib(const AProjectName: string);
procedure ApplySearchPath(const AProjectName: string);
procedure ProjectLogMessage(Sender: TObject; Text: string; LogLevel: TLogLevel);
public
constructor Create(ASearchPathManager: ISearchPathManager);
destructor Destroy; override;
procedure Parse(const AProjectName: string = '');
procedure UpdateProjectLibs(const AProjectName: string = '');
procedure ApplyProjectSearchPath(const AProjectName: string = '');
end;
implementation
uses
System.IOUtils, System.Net.HttpClient, System.Zip,
Vcl.Graphics,
Winapi.Windows,
SearchPath.Config;
{ TLocation }
procedure TDocumentBase.Debug(Text: string);
begin
FProject.LogMessage(Self, Text, llDebug);
end;
procedure TDocumentBase.Error(Text: string);
begin
FProject.LogMessage(Self, Text, llError);
end;
procedure TDocumentBase.Info(Text: string);
begin
FProject.LogMessage(Self, Text, llInfo);
end;
procedure TDocumentBase.SetLocation(AElement: PsanXMLObjElement);
begin
if AElement <> nil then begin
FRow := PsanXMLObjElementEx2(AElement).LineNo;
FCol := PsanXMLObjElementEx2(AElement).CharNoInLine;
end else begin
FRow := 0;
FCol := 0;
end;
end;
procedure TDocumentBase.Warn(const Format: string; const Args: array of const);
begin
Warn(System.SysUtils.Format(Format, Args));
end;
procedure TDocumentBase.Warn(Text: string);
begin
FProject.LogMessage(Self, Text, llWarn);
end;
constructor TProject.Create(AParent: TProject; AProjectName: string);
begin
FProjectName := AProjectName;
FParent := AParent;
FDocument := TXMLDocumentObject.Create;
FDocument.LoadFromFile(AProjectName);
inherited Create(Self, FDocument.RootElement);
end;
destructor TProject.Destroy;
begin
if FDependencies <> nil then FreeAndNil(FDependencies);
if FRepositories <> nil then FreeAndNil(FRepositories);
if FProperties <> nil then FreeAndNil(FProperties);
if FProxy <> nil then FreeAndNil(FProxy);
if FDocument <> nil then FreeAndNil(FDocument);
inherited;
end;
function TProject.GetPropertyValue(const AName: string): string;
begin
Result := FProperties.GetValue(AName);
end;
function TProject.GetRepositories: TList<TRepository>;
begin
Result := FRepositories.Repositories;
end;
procedure TProject.Parse;
begin
FVer := GetAttributeValue('ver');
FLogLevel := TLogLevel.From(GetAttributeValue('logLevel'));
ParseProxy;
ParseProperties;
ParseRepositories;
ParseDependencies;
end;
procedure TProject.ParseDependencies;
begin
var DependenciesElement := GetElement(DEPENDENCIES_KEY);
if DependenciesElement <> nil then begin
FDependencies := TDependencies.Create(Self, DependenciesElement);
FDependencies.Parse;
end else FDependencies := nil;
end;
procedure TProject.ParseProperties;
begin
var PropertiesElement := GetElement(PROPERTIES_KEY);
FProperties := TProperties.Create(Self, PropertiesElement);
FProperties.Parse(PropertiesElement <> nil);
end;
procedure TProject.ParseProxy;
begin
var ProxyElement := GetElement(PROXY_KEY);
if ProxyElement <> nil then begin
FProxy := TProxy.Create(Self, ProxyElement);
FProxy.Parse;
end else FProxy := nil;
end;
procedure TProject.ParseRepositories;
begin
var RepositoriesElement := GetElement(REPOSITORIES_KEY);
FRepositories := TRepositories.Create(Self, RepositoriesElement);
if RepositoriesElement <> nil then begin
FRepositories.Parse;
end;
end;
function TDocumentBase.ChildCount: Integer;
begin
Result := FDoc.ChildCount(FElement);
end;
constructor TDocumentBase.Create(AProject: TProject; AElement: PsanXMLObjElement);
begin
FProject := AProject;
FDoc := AProject.Document;
FElement := AElement;
SetLocation(AElement);
end;
procedure TDocumentBase.Debug(const Format: string; const Args: array of const);
begin
Debug(System.SysUtils.Format(Format, Args));
end;
procedure TDocumentBase.Error(const Format: string; const Args: array of const);
begin
Error(System.SysUtils.Format(Format, Args));
end;
function TDocumentBase.GetAttributeValue(const AttrName: string): string;
begin
Result := FDoc.GetAttributeValue(FElement, AttrName).Trim;
end;
function TDocumentBase.GetElement(AElementIndex: Integer): PsanXMLObjElement;
begin
Result := FDoc.GetElement(FElement, AElementIndex);
end;
function TDocumentBase.GetChildElementValue(AElementName: string): string;
begin
var E := GetElement(AElementName);
if E <> nil then begin
Result := StrPas(E.pValue).Trim;
end else Result := '';
end;
function TDocumentBase.GetName: string;
begin
if FElement <> nil then begin
Result := StrPas(FElement.pName).Trim;
end else Result := '';
end;
function TDocumentBase.GetValue: string;
begin
if FElement <> nil then begin
Result := StrPas(FElement.pValue).Trim;
end else Result := '';
end;
function TDocumentBase.iif<T>(B: Boolean; T1, T2: T): T;
begin
if B then Result := T1 else Result := T2;
end;
function TDocumentBase.GetElement(AElementName: string): PsanXMLObjElement;
begin
Result := FDoc.GetElement(FElement, AElementName);
end;
procedure TDocumentBase.Info(const Format: string; const Args: array of const);
begin
Info(System.SysUtils.Format(Format, Args));
end;
{ TProxy }
destructor TProxy.Destroy;
begin
if FIncludeFilters <> nil then FreeAndNil(FIncludeFilters);
if FExcludeFilters <> nil then FreeAndNil(FExcludeFilters);
inherited;
end;
procedure TProxy.Parse;
begin
Debug('Begin process proxy config');
FHost := GetAttributeValue('host');
try
FPort := StrToInt(GetAttributeValue('port'));
except
on E: Exception do begin
Error('Wrong port number: %s', [E.Message]);
end;
end;
FUser := GetAttributeValue('user');
FPassword := GetAttributeValue('password');
var strEnabled := GetAttributeValue('enabled');
FEnabled := strEnabled.IsEmpty or (CompareText(strEnabled, 'true') = 0);
var IncludeItems := GetElement('includes');
if IncludeItems <> nil then begin
FIncludeFilters := TProxyFilters.Create(Project, IncludeItems);
FIncludeFilters.Debug('Handle include proxy filter');
FIncludeFilters.Parse;
end else FIncludeFilters := nil;
var ExcludeItems := GetElement('excludes');
if ExcludeItems <> nil then begin
FExcludeFilters := TProxyFilters.Create(Project, ExcludeItems);
FExcludeFilters.Debug('Handle exclude proxy filter');
FExcludeFilters.Parse;
end;
Debug('End process proxy config: %s', [FHost]);
end;
{ TDependencies }
destructor TDependencies.Destroy;
begin
if FDependencies <> nil then begin
for var I := FDependencies.Count-1 downto 0 do begin
TDependencie(FDependencies[I]).Free;
FDependencies.Delete(I);
end;
FreeAndNil(FDependencies);
end;
inherited;
end;
procedure TDependencies.DownloadPackage;
begin
Info('Begin download source code');
for var Dependencie in FDependencies do begin
Dependencie.DownloadPackage;
end;
Info('End download source code');
end;
procedure TDependencies.GetSearchPath(Strings: TStrings);
var
FullPath: string;
begin
Info('Begin config search path');
if FDependencies = nil then Exit;
for var Dependencie in FDependencies do begin
Dependencie.GetSearchPath(Strings);
end;
{$region 'Validate all the path exists'}
Info('Validate all the search path exists');
var LibRoot := IncludeTrailingPathDelimiter(Project.Properties.GetValue(LIBS_ROOT_KEY));
if LibRoot = '' then
LibRoot := LIBS_ROOT_DEFAULT;
var ProjectDir := ExtractFileDir(Project.ProjectName);
for var Path in Strings do begin
if Path.StartsWith(LIBS_ROOT_DEFAULT, True) then begin
FullPath := TPath.Combine(ProjectDir, Path.Substring(2));
end else begin
FullPath := Path.Replace(Format('$(%s)%s', [LIBS_ROOT_KEY, TPath.DirectorySeparatorChar]), LibRoot, [rfIgnoreCase]);
end;
if not TDirectory.Exists(FullPath) then begin
Warn('Path not exists "%s"', [FullPath]);
Warn('You need check it your self', [FullPath]);
end;
end;
{$endregion}
Info('End config search path');
end;
procedure TDependencies.Parse;
begin
Debug('Begin process dependencies');
var Count := ChildCount;
if Count > 0 then FDependencies := TList<TDependencie>.Create else FDependencies := nil;
for var I := 0 to Count-1 do begin
var DependencieElement := GetElement(I);
var Dependencie := TDependencie.Create(Project, DependencieElement);
Dependencie.Parse;
FDependencies.Add(Dependencie);
end;
Debug('End process dependencies');
end;
{ TDependencie }
destructor TDependencie.Destroy;
begin
if FLibPaths <> nil then FreeAndNil(FLibPaths);
inherited;
end;
procedure TDependencie.DownloadPackage;
function HttpDownload(const URL, DownloadDir: string): string;
var
Http: THttpClient;
Stream: TFileStream;
begin
Info('Begin download "%s"', [URL]);
Result := TPath.Combine(DownloadDir, FVersion) + '.' + FType;
if not TFile.Exists(Result) then begin
if not TDirectory.Exists(DownloadDir) then begin
ForceDirectories(DownloadDir);
end;
Http := THttpClient.Create;
try
Http.ConnectionTimeout := 10;
Http.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59';
if (Project.Proxy <> nil) and Project.Proxy.Enabled then begin
http.ProxySettings.Create(Project.Proxy.Host, Project.Proxy.port, Project.Proxy.UnitName, Project.Proxy.Password);
end;
var tmpFile := ChangeFileExt(Result, '.tmp');
try
if TFile.Exists(tmpFile) then
TFile.Delete(tmpFile);
Stream := TFileStream.Create(tmpFile, fmCreate);
try
var Response := Http.Get(URL, Stream);
for var h in Response.Headers do begin
Debug('Response head: %s=%s', [h.Name, h.Value]);
end;
finally
FreeAndNil(Stream);
end;
TFile.Move(tmpFile, Result);
except
on NetExcept: ENetHTTPClientException do begin
TFile.Delete(tmpFile);
raise Exception.Create(NetExcept.Message);
end;
end;
finally
FreeAndNil(Http);
end;
end else Info('Download file exists "%s", skipped', [Result]);
Info('End download "%s"', [URL]);
end;
function ResolveURL(const Repo: string): string;
begin
var LRepo := Repo.Replace('\', '/',[rfReplaceAll]);
var URL := LRepo.Replace('$(group)', FGroup.Replace('.', '/', [rfReplaceAll]), [rfReplaceAll, rfIgnoreCase]);
URL := URL.Replace('$(artifactId)', ArtifactId, [rfReplaceAll, rfIgnoreCase]);
URL := URL.Replace('$(version)', FVersion, [rfReplaceAll, rfIgnoreCase]);
URL := URL.Replace('$(compileVersion)', FVersion, [rfReplaceAll, rfIgnoreCase]);
URL := URL.Replace('$(type)', FType, [rfReplaceAll, rfIgnoreCase]);
Result := URL;
end;
function Download(DownloadDir: string; var ZipFile: string): Boolean;
var
LRepo: string;
begin
if FRepo.IsEmpty then begin
Result := False;
for var R in Project.Repositories do begin
LRepo := iif(R.URL.EndsWith('/'), R.URL, R.URL + '/') + FPath + '/' + FName;
try
var URL := ResolveURL(LRepo);
ZipFile := HttpDownload(URL, DownloadDir);
Result := True;
except
on E: Exception do begin
Error('Download file error: %s', [E.Message]);
Result := False;
end;
end;
if Result then Break;
end;
end else begin
try
var URL := ResolveURL(FRepo);
ZipFile := HttpDownload(URL, DownloadDir);
Result := True;
except
on E: Exception do begin
Error('Download file error: %s', [E.Message]);
Result := False;
end;
end;
end;
end;
var
ZipFile, DownloadDir: string;
begin
if FLibPaths = nil then Exit;
var LibsRoot := Project.Properties.GetValue(LIBS_ROOT_KEY);
if LibsRoot = '' then
LibsRoot := LIBS_ROOT_DEFAULT;
LibsRoot := IncludeTrailingPathDelimiter(LibsRoot);
var ProjectDir := ExtractFileDir(Project.ProjectName);
if LibsRoot.Equals(LIBS_ROOT_DEFAULT) then begin
var LibPath := TPath.Combine(LIBS_ROOT_DEFAULT.Substring(2), FPath);
DownloadDir := TPath.Combine(ProjectDir, LibPath);
end else begin
DownloadDir := TPath.Combine(LibsRoot, FPath);
end;
try
if Download(DownloadDir, ZipFile) then begin
Info('Begin unzip "%s"', [ZipFile]);
Unzip(ZipFile, DownloadDir);
Info('End unzip "%s"', [ZipFile]);
end;
except
on E: Exception do
Error('Unzip file error: %s', [E.Message]);
end;
end;
procedure TDependencie.GetSearchPath(Strings: TStrings);
begin
if FLibPaths <> nil then
FLibPaths.GetSearchPath(Strings);
end;
procedure TDependencie.Parse;
begin
FGroup := GetChildElementValue('group');
FArtifactId := GetChildElementValue('artifactId');
FVersion := GetChildElementValue('version');
FCompileVersion := GetChildElementValue('compileVersion');
var LType := GetChildElementValue('type');
FType := iif<string>(LType.IsEmpty, 'zip', LType);
//[FGroup]/[artifactId]/[version](-[folderItegRev])/[artifactId]-[version](-[fileItegRev])(-[compileVersion]).[type]
if FCompileVersion.IsEmpty then begin
FPath := Format('%s\%s\%s', [FGroup, FArtifactId, FVersion]);
FName := Format('%s-%s.%s', [FArtifactId, FVersion, FType]);
end else begin
// 最后一个是Delphi的版本号,用于dcu
FPath := Format('%s\%s\%s\%s', [FGroup, FArtifactId, FVersion, FCompileVersion]);
FName := Format('%s-%s-%s.%s', [FArtifactId, FVersion, FCompileVersion, FType]);
end;
var Lib := GetElement('searchPath');
if Lib <> nil then begin
FLibPaths := TLibPaths.Create(Project, Lib);
FLibPaths.Parse(FPath);
end else FLibPaths := nil;
FRepo := GetChildElementValue('repo');
Debug('Found dependencie config: %s\%s\%s', [FGroup, FArtifactId, FVersion]);
end;
procedure TDependencie.Unzip(const ZipFile, FilesRoot: string);
var
Zip: TZipFile;
I: Integer;
TopFolder: string;
HasTopFolder: Boolean;
ZipStream: TStream;
ZipHeader: TZipHeader;
ZippedFileName: string;
FileName: string;
begin
if not TZipFile.IsValid(ZipFile) then begin
Error('Invalidate zip file');
Exit;
end;
Zip := TZipFile.Create;
try
Zip.Open(ZipFile, zmRead);
TopFolder := '';
HasTopFolder := True;
I := 0;
for FileName in Zip.FileNames do begin
var Folders := FileName.Split(['/']);
if I = 0 then begin
TopFolder := Folders[0];
Inc(I);
end;
if TopFolder <> Folders[0] then begin
HasTopFolder := False;
Break;
end;
end;
for ZippedFileName in Zip.FileNames do begin
if ZippedFileName.EndsWith('/') then Continue;
Debug('Zipped file name: %s', [ZippedFileName]);
if HasTopFolder then begin
FileName := ZippedFileName.Substring(TopFolder.Length +1);
end else begin
FileName := ZippedFileName;
end;
var FullName := TPath.Combine(FilesRoot, FileName.Replace('/','\'));
if TFile.Exists(FullName) then begin
TFile.Delete(FullName);
end else begin
var FileDir := ExtractFileDir(FullName);
if not TDirectory.Exists(FileDir) then
TDirectory.CreateDirectory(FileDir);
end;
try
Zip.Read(ZippedFileName, ZipStream, ZipHeader);
var F := TFileStream.Create(FullName, fmCreate);
try
F.CopyFrom(ZipStream, ZipHeader.UncompressedSize);
finally
FreeAndNil(F);
end;
except
on E: Exception do begin
Debug('Un compress %s error', [ZippedFileName]);
end;
end;
end;
finally
FreeAndNil(Zip);
end;
end;
{ TProperties }
procedure TProperties.AddCustomProperties;
begin
var Count := ChildCount;
for var I := 0 to Count-1 do begin
var PropertyElement := GetElement(I);
var Prop := TProperty.Create(Project, PropertyElement);
Prop.Parse;
// 重复的属性先入为主
var ObjIndex := FProperties.IndexOf(Prop.Name);
if ObjIndex <> -1 then begin
Warn(DUP_MESSAGE, ['custom', Prop.Name, Prop.Value]);
FreeAndNil(Prop);
end else FProperties.AddObject(Prop.Name, Prop);
end;
end;
procedure TProperties.AddSpecialProperties;
begin
// var LibsRoot := Config.LibsRoot;
// if LibsRoot <> '' then begin
// var Prop := TProperty.Create(Project, nil);
// Prop.AddSpecialProperty(LIBS_ROOT_KEY, LibsRoot);
// FProperties.AddObject(Prop.Name, Prop);
// end;
end;
procedure TProperties.AddSystemProperties;
begin
GetEnvList(function(const AName, AValue: string): Boolean begin
// 重复的属性先入为主,自定义的优先
var ObjIndex := FProperties.IndexOf(AName);
if ObjIndex = -1 then begin
var Prop := TProperty.Create(Project, nil);
Prop.AddSystemProperty(AName, AValue);
FProperties.AddObject(Prop.Name, Prop);
end else Warn(DUP_MESSAGE, ['system', AName, AValue]);
Result := True;
end);
end;
destructor TProperties.Destroy;
begin
for var I := FProperties.Count-1 downto 0 do begin
TProperty(FProperties.Objects[I]).Free;
FProperties.Delete(I);
end;
FreeAndNil(FProperties);
inherited;
end;
function TProperties.GetValue(const AName: string): string;
begin
var i := FProperties.IndexOf(AName);
if i <> -1 then begin
Result := TProperty(FProperties.Objects[I]).Value;
end else begin
Result := '';
end;
end;
procedure TProperties.Parse(ExistsCustomeProperties: Boolean);
begin
Debug('Begin process properties');
FProperties := TStringList.Create;
AddSpecialProperties;
AddSystemProperties;
if ExistsCustomeProperties then begin
AddCustomProperties;
end;
ResolvePropertyValues;
Debug('End process properties');
end;
procedure TProperties.ResolvePropertyValues;
begin
end;
{ TProperty }
procedure TProperty.AddSpecialProperty(AName, AValue: string);
begin
FName := AName.Trim;
FValue := AValue.Trim;
Debug('Add special property: %s', [ToString]);
end;
procedure TProperty.AddSystemProperty(AName, AValue: string);
begin
FName := AName.Trim;
FValue := AValue.Trim;
Debug('Add system property: %s', [ToString]);
end;
procedure TProperty.Parse;
begin
FName := GetName;
FValue := GetValue;
Debug('Add custom property: %s', [ToString]);
end;
function TProperty.ToString: string;
begin
Result := Format('Key=%s, Value=%s',[FName, FValue]);
end;
{ TProxyFilters }
destructor TProxyFilters.Destroy;
begin
if FFilters <> nil then begin
for var I := FFilters.Count-1 downto 0 do begin
TProxyFilter(FFilters[I]).Free;
FFilters.Delete(I);
end;
FreeAndNil(FFilters);
end;
inherited;
end;
procedure TProxyFilters.Parse;
begin
var Count := ChildCount;
if Count > 0 then begin
FFilters := TList<TProxyFilter>.Create;
for var I := 0 to Count-1 do begin
var Filter := GetElement(I);
var ProxyFilter := TProxyFilter.Create(Project, Filter);
ProxyFilter.Parse;
FFilters.Add(ProxyFilter);
end;
end else FFilters := nil;;
end;
{ TProxyFilter }
destructor TProxyFilter.Destroy;
begin
inherited;
end;
procedure TProxyFilter.Parse;
begin
FFilter := GetValue;
Debug('Found filter: %s', [FFilter]);
end;
{ TLibPaths }
destructor TLibPaths.Destroy;
begin
if FPaths <> nil then begin
for var I := FPaths.Count-1 downto 0 do begin
TLibPath(FPaths.Objects[I]).Free;
FPaths.Delete(I);
end;
FreeAndNil(FPaths);
end;
inherited;
end;