forked from Memnarch/Delphinus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DN.ProjectInfo.pas
285 lines (260 loc) · 8.97 KB
/
DN.ProjectInfo.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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.ProjectInfo;
interface
uses
Classes,
Types,
SysUtils,
XMLIntf,
DN.ProjectInfo.Intf,
DN.Types,
DN.DPRProperties.Intf;
type
TDNProjectInfo = class(TInterfacedObject, IDNProjectInfo)
private
FBinaryName: string;
FDCPName: string;
FFileName: string;
FIsPackage: Boolean;
FIsRuntimeOnlyPackage: Boolean;
FSupportedPlatforms: TDNCompilerPlatforms;
FLoadingError: string;
function GetBinaryName: string;
function GetDCPName: string;
function GetIsPackage: Boolean;
function GetDefaultExtension(const AAppType: string): string;
function GetPropertyGroupOfConfig(const AProject: IXMLNode; const AConfig: string): IXMLNode;
function GetIsRuntimeOnlyPackage: Boolean;
function GetFileName: string;
function GetSupportedPlatforms: TDNCompilerPlatforms;
function GetLoadingError: string;
procedure FindDLLVersionDefines(const AProject: IXMLNode; out ADLLPrefix, ADLLSuffix, ADLLVersion: string);
procedure SetError(const AError: string);
public
function LoadFromFile(const AProjectFile: string): Boolean;
function CreateDPRProperties: IDPRProperties;
property IsPackage: Boolean read GetIsPackage;
property IsRuntimeOnlyPackage: Boolean read GetIsRuntimeOnlyPackage;
property BinaryName: string read GetBinaryName;
property DCPName: string read GetDCPName;
property FileName: string read GetFileName;
property SupportedPlatforms: TDNCompilerPlatforms read GetSupportedPlatforms;
property LoadingError: string read GetLoadingError;
end;
implementation
uses
IOUtils,
XMLDoc,
StrUtils,
Variants,
DN.DPRProperties;
const
CPropertyGroup = 'PropertyGroup';
CProject = 'Project';
CCondition = '''$(%s)''!=''''';
CBase = 'Base';
CBaseWin32 = 'Base_Win32';
CBaseWin64 = 'Base_Win64';
{ TDNProjectInfo }
function TDNProjectInfo.CreateDPRProperties: IDPRProperties;
begin
if IsPackage then
Result := TDPRProperties.Create(ChangeFileExt(FileName, '.dpk'))
else
Result := TDPRProperties.Create(ChangeFileExt(FileName, '.dpr'));
end;
procedure TDNProjectInfo.FindDLLVersionDefines(const AProject: IXMLNode;
out ADLLPrefix, ADLLSuffix, ADLLVersion: string);
var
LGroup: IXMLNode;
LValue: string;
begin
ADLLPrefix := '';
ADLLSuffix := '';
ADLLVersion := '';
LGroup := AProject.ChildNodes.First;
while Assigned(LGroup) do
begin
LValue := VarToStr(LGroup.ChildValues['DllPrefix']);
if LValue <> '' then
ADLLPrefix := LValue;
LValue := VarToStr(LGroup.ChildValues['DllSuffix']);
if LValue <> '' then
ADLLSuffix := LValue;
LValue := VarToStr(LGroup.ChildValues['DllVersion']);
if LValue <> '' then
ADLLVersion := LValue;
LGroup := LGroup.NextSibling;
end;
end;
function TDNProjectInfo.GetBinaryName: string;
begin
Result := FBinaryName;
end;
function TDNProjectInfo.GetDCPName: string;
begin
Result := FDCPName;
end;
function TDNProjectInfo.GetDefaultExtension(const AAppType: string): string;
begin
if SameText('Library', AAppType) then
Result := '.dll'
else if SameText('Package', AAppType) then
Result := '.bpl'
else
Result := '.exe';
end;
function TDNProjectInfo.GetFileName: string;
begin
Result := FFileName;
end;
function TDNProjectInfo.GetIsPackage: Boolean;
begin
Result := FIsPackage;
end;
function TDNProjectInfo.GetIsRuntimeOnlyPackage: Boolean;
begin
Result := FIsRuntimeOnlyPackage;
end;
function TDNProjectInfo.GetLoadingError: string;
begin
Result := FLoadingError;
end;
function TDNProjectInfo.GetPropertyGroupOfConfig(const AProject: IXMLNode;
const AConfig: string): IXMLNode;
var
LGroup: IXMLNode;
LCondition: string;
begin
Result := nil;
LCondition := Format(CCondition, [AConfig]);
LGroup := AProject.ChildNodes.First();
while Assigned(LGroup) do
begin
if SameText(LGroup.LocalName, CPropertyGroup) then
begin
if LGroup.HasAttribute('Condition') and SameText(LGroup.Attributes['Condition'], LCondition) then
begin
Exit(LGroup);
end;
end;
LGroup := LGroup.NextSibling;
end;
end;
function TDNProjectInfo.GetSupportedPlatforms: TDNCompilerPlatforms;
begin
Result := FSupportedPlatforms;
end;
function TDNProjectInfo.LoadFromFile(const AProjectFile: string): Boolean;
var
LXML: IXMLDocument;
LBaseName, LCoreFile, LAppType, LExtension: string;
LPrefix, LSuffix, LVersion: string;
LProject, LPropertyGroup, LBase: IXMLNode;
LProjectExtension, LBorlandProject, LPlatforms, LPlatform: IXMLNode;
begin
Result := False;
if TFile.Exists(AProjectFile) then
begin
FFileName := AProjectFile;
LXML := TXMLDocument.Create(nil);
LXML.LoadFromFile(AProjectFile);
LProject := LXML.ChildNodes.FindNode(CProject);
if Assigned(LProject) then
begin
LPropertyGroup := LProject.ChildNodes.FindNode(CPropertyGroup);
if Assigned(LPropertyGroup) then
begin
LCoreFile := LPropertyGroup.ChildValues['MainSource'];
LBaseName := ChangeFileExt(LCoreFile, '');
LAppType := VarToStr(LPropertyGroup.ChildValues['AppType']);
FIsPackage := SameText(LAppType, 'Package');
if FIsPackage then
FDCPName := LBaseName + '.dcp'
else
FDCPName := '';
FIsRuntimeOnlyPackage := False;
LBase := GetPropertyGroupOfConfig(LProject, CBase);
if Assigned(LBase) then
begin
FIsRuntimeOnlyPackage := StrToBool(VarToStrDef(LBase.ChildValues['RuntimeOnlyPackage'], 'False'));
//read extensions, post/suffix and version
LExtension := VarToStr(LBase.ChildValues['OutputExt']);
if (LExtension <> '') and (not StartsText('.', LExtension)) then
LExtension := '.' + LExtension;
FindDLLVersionDefines(LProject, LPrefix, LSuffix, LVersion);
if LVersion <> '' then
LVersion := '.' + LVersion;
end;
if LExtension = '' then
LExtension := GetDefaultExtension(LAppType);
FBinaryName := LPrefix + LBaseName + LSuffix + LVersion + LExtension;
//detect supported platforms
FSupportedPlatforms := [];
LProjectExtension := LProject.ChildNodes.FindNode('ProjectExtensions');
if Assigned(LProjectExtension) then
begin
LBorlandProject := LProjectExtension.ChildNodes.FindNode('BorlandProject');
if Assigned(LBorlandProject) then
begin
LPlatforms := LBorlandProject.ChildNodes.FindNode('Platforms');
if Assigned(LPlatforms) then
begin
LPlatform := LPlatforms.ChildNodes.First;
while Assigned(LPlatform) do
begin
if SameText(LPlatform.Text, 'True') then
begin
if LPlatform.HasAttribute('value') then
begin
if SameText(LPlatform.Attributes['value'], 'Win32') then
FSupportedPlatforms := FSupportedPlatforms + [cpWin32]
else if SameText(LPlatform.Attributes['value'], 'Win64') then
FSupportedPlatforms := FSupportedPlatforms + [cpWin64]
else if SameText(LPlatform.Attributes['value'], 'OSX32') then
FSupportedPlatforms := FSupportedPlatforms + [cpOSX32]
else if SameText(LPlatform.Attributes['value'], 'Android') then
FSupportedPlatforms := FSupportedPlatforms + [cpAndroid]
else if SameText(LPlatform.Attributes['value'], 'iOSDevice32') then
FSupportedPlatforms := FSupportedPlatforms + [cpIOSDevice32]
else if SameText(LPlatform.Attributes['value'], 'iOSDevice64') then
FSupportedPlatforms := FSupportedPlatforms + [cpIOSDevice64]
else if SameText(LPlatform.Attributes['value'], 'Linux64') then
FSupportedPlatforms := FSupportedPlatforms + [cpLinux64]
end;
end;
LPlatform := LPlatform.NextSibling;
end;
end;
end;
end;
if FSupportedPlatforms = [] then
FSupportedPlatforms := [cpWin32];
Result := True;
end
else
begin
SetError('PropertyGroup-Node not found');
end;
end
else
begin
SetError('Project-Node not found');
end;
end
else
begin
SetError('File not found');
end;
end;
procedure TDNProjectInfo.SetError(const AError: string);
begin
FLoadingError := AError;
end;
end.