forked from Memnarch/Delphinus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DN.DPRProperties.pas
121 lines (102 loc) · 2.45 KB
/
DN.DPRProperties.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
unit DN.DPRProperties;
interface
uses
Classes,
SysUtils,
DN.DPRProperties.Intf;
type
TDPRProperties = class(TInterfacedObject, IDPRProperties)
private
FFileName: string;
FBackup: string;
FContent: TStringList;
procedure Open;
procedure Close;
procedure SetDefine(const AName, AValue: string);
public
constructor Create(const AFileName: string);
destructor Destroy; override;
procedure BeginTemporaryOverride;
procedure EndTemporaryOverride;
procedure SetLibVersion(const AVersion: string);
procedure SetPrefix(const APrefix: string);
procedure SetSuffix(const ASuffix: string);
end;
implementation
uses
IOUtils,
StrUtils;
{ TDPKProperties }
procedure TDPRProperties.BeginTemporaryOverride;
begin
if FBackup <> '' then
raise Exception.Create('can not recursively enter temporary override');
FBackup := FFileName + '.bak';
TFile.Copy(FFileName, FBackup);
end;
procedure TDPRProperties.Close;
begin
try
FContent.SaveToFile(FFileName);
finally
FContent.Free;
end;
end;
constructor TDPRProperties.Create(const AFileName: string);
begin
inherited Create();
FFileName := AFileName;
end;
destructor TDPRProperties.Destroy;
begin
if FBackup <> '' then
EndTemporaryOverride();
inherited;
end;
procedure TDPRProperties.EndTemporaryOverride;
begin
if FBackup = '' then
raise Exception.Create('Tried to exit nonexisting temporary overridesession');
TFile.Copy(FBackup, FFileName, True);
TFile.Delete(FBackup);
FBackup := '';
end;
procedure TDPRProperties.Open;
begin
FContent := TStringList.Create();
FContent.LoadFromFile(FFileName);
end;
procedure TDPRProperties.SetDefine(const AName, AValue: string);
var
i: Integer;
const
CDefine = '{$%s %s}';
begin
Open();
try
for i := FContent.Count - 1 downto 0 do
begin
if StartsText('end.', Trim(FContent[i])) and (i > 0) then
begin
FContent.Insert(i, Format(CDefine, [AName, AValue]));
Exit;
end;
end;
raise Exception.Create('failed to set Define ' + AName + ' in ' + FFileName);
finally
Close();
end;
end;
procedure TDPRProperties.SetLibVersion(const AVersion: string);
begin
SetDefine('LibVersion', QuotedStr(AVersion));
end;
procedure TDPRProperties.SetPrefix(const APrefix: string);
begin
SetDefine('LibPrefix', QuotedStr(APrefix));
end;
procedure TDPRProperties.SetSuffix(const ASuffix: string);
begin
SetDefine('LibSuffix', QuotedStr(ASuffix));
end;
end.