-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPM.Creator.Dspec.Replacer.pas
86 lines (74 loc) · 2.6 KB
/
DPM.Creator.Dspec.Replacer.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
unit DPM.Creator.Dspec.Replacer;
interface
uses
System.SysUtils,
System.RegularExpressions,
DPM.Core.Types,
DPM.dspec.format,
DPM.Core.Spec.Interfaces
;
type
TClassReplacer = class
private
Fspec : IPackageSpec;
FCompiler : TCompilerVersion;
function matcher(const Match: TMatch): String;
function Replace(inputStr: string): string;
constructor Create(compiler: TCompilerVersion; structure : IPackageSpec);
public
class function ReplaceVars(inputStr: String; compiler: TCompilerVersion; structure: IPackageSpec): string;
end;
implementation
{ TClassReplacer }
constructor TClassReplacer.Create(compiler: TCompilerVersion; structure : IPackageSpec);
begin
FCompiler := compiler;
Fspec := structure;
end;
function TClassReplacer.matcher(const Match: TMatch): String;
begin
if SameText(Match.Groups[1].Value, 'compiler') then
Exit(CompilerToString(FCompiler))
else if SameText(Match.Groups[1].Value, 'compilerNoPoint') then
Exit(CompilerToStringNoPoint(FCompiler))
else if SameText(Match.Groups[1].Value, 'compilerCodeName') then
Exit(CompilerCodeName(FCompiler))
else if SameText(Match.Groups[1].Value, 'compilerWithCodeName') then
Exit(CompilerWithCodeName(FCompiler))
else if SameText(Match.Groups[1].Value, 'compilerVersion') then
Exit(CompilerToCompilerVersionIntStr(FCompiler))
else if SameText(Match.Groups[1].Value, 'libSuffix') then
Exit(CompilerToLibSuffix(FCompiler))
else if SameText(Match.Groups[1].Value, 'bdsVersion') then
Exit(CompilerToBDSVersion(FCompiler))
else if SameText(Match.Groups[1].Value, 'id') then
Exit(FSpec.metadata.id)
else if SameText(Match.Groups[1].Value, 'version') then
Exit(FSpec.metadata.version.ToString)
else if SameText(Match.Groups[1].Value, 'author') then
Exit(FSpec.metadata.authors)
else if SameText(Match.Groups[1].Value, 'title') then
Exit(FSpec.metadata.id)
else if SameText(Match.Groups[1].Value, 'description') then
Exit(FSpec.metadata.description)
else if SameText(Match.Groups[1].Value, 'copyright') then
Exit(FSpec.metadata.copyright)
else
Exit(Match.Value); // In case of no match, return the original placeholder
end;
function TClassReplacer.Replace(inputStr: string): string;
begin
Result := TRegEx.Replace(inputStr, '\$(.*?)\$', matcher);
end;
class function TClassReplacer.ReplaceVars(inputStr: String; compiler: TCompilerVersion; structure: IPackageSpec): string;
var
replacer : TClassReplacer;
begin
replacer := TClassReplacer.Create(compiler, structure);
try
Result := replacer.Replace(inputStr);
finally
FreeAndNil(replacer);
end;
end;
end.