-
Notifications
You must be signed in to change notification settings - Fork 63
/
DN.Package.Finder.pas
67 lines (57 loc) · 1.39 KB
/
DN.Package.Finder.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
unit DN.Package.Finder;
interface
uses
DN.Package.Intf,
DN.Package.Finder.Intf;
type
TDNPackageFinder = class(TInterfacedObject, IDNPackageFinder)
private
FPackages: TArray<IDNPackage>;
public
constructor Create(const APackages: TArray<IDNPackage>);
function TryFind(const ANameOrID: string; out APackage: IDNPackage): Boolean;
function Find(const ANameOrID: string): IDNPackage;
end;
implementation
uses
SysUtils,
StrUtils;
{ TPackageFinder }
constructor TDNPackageFinder.Create(const APackages: TArray<IDNPackage>);
begin
inherited Create();
FPackages := APackages;
end;
function TDNPackageFinder.Find(const ANameOrID: string): IDNPackage;
begin
if not TryFind(ANameOrID, Result) then
raise Exception.Create('Could not resolve Package ' + ANameOrID);
end;
function TDNPackageFinder.TryFind(const ANameOrID: string;
out APackage: IDNPackage): Boolean;
var
LPackage: IDNPackage;
LID: TGUID;
begin
if StartsStr('{', ANameOrID) and EndsStr('}', ANameOrID) then
begin
LID := StringToGUID(ANameOrID);
for LPackage in FPackages do
if IsEqualGUID(LID, LPackage.ID) then
begin
APackage := LPackage;
Exit(True);
end;
end
else
begin
for LPackage in FPackages do
if SameText(LPackage.Name, ANameOrID) then
begin
APackage := LPackage;
Exit(True);
end;
end;
Result := False;
end;
end.