This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
uprojectbuilding.pas
510 lines (412 loc) · 12.2 KB
/
uprojectbuilding.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
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
{-------------------------------------------------------------------------------
Copyright (C) 2015 Helltar <mail@helltar.com>
This file is part of AMPASIDE.
AMPASIDE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AMPASIDE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AMPASIDE. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------}
unit uProjectBuilding;
{$mode objfpc}{$H+}
interface
uses
Classes, FileUtil, RegExpr, SysUtils, LazFileUtils;
type
{ TProjectBuilding }
TProjectBuilding = class
private
ManifestFileName: string;
function CompileFile(FileName: string): boolean;
function CreateManifest(const FileName: string): boolean;
function DeleteCharacters(const AValue: string): string;
function ExecPreBuildAct: boolean;
function IsErr(const AValue: string): boolean;
function RebuildPreBuildDir: boolean;
procedure CreateJad;
public
constructor Create;
function Build(isAndroid: boolean = False): boolean;
function CompileMainModule: boolean;
procedure Run;
end;
{ TBuildingThread }
TBuildingThread = class(TThread)
private
FMode: integer;
ProjBuilding: TProjectBuilding;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: boolean);
destructor Destroy; override;
property Mode: integer read FMode write FMode;
end;
resourcestring
LOG_INFO_ANT_COMPLETED_WORK = 'Ant has completed its work, details';
LOG_INFO_ARCHIVING_START = 'Starting archiving';
LOG_INFO_COMPILING = 'Compiling';
LOG_INFO_COMPLETED = 'Completed';
LOG_INFO_EMULATOR_STOP = 'Emulator operation completed';
TEXT_PLATFORM = 'Platform';
TEXT_SIZE = 'Size';
TEXT_VERSION = 'Version';
MSG_BUILD_SUCCESSFUL = 'Build successful';
TEXT_LAUCH = 'launch';
TEXT_PROCESS_BUSY = 'file may be busy with another process';
implementation
uses
uAMPASCore,
uProjectManager,
uProjectConfig,
uIDEConfig;
{ TBuildingThread }
constructor TBuildingThread.Create(CreateSuspended: boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
ProjBuilding := TProjectBuilding.Create;
end;
destructor TBuildingThread.Destroy;
begin
FreeAndNil(ProjBuilding);
inherited Destroy;
end;
procedure TBuildingThread.Execute;
begin
with ProjBuilding do
case FMode of
0: CompileMainModule;
1: Build;
2: Run;
end;
end;
{ TProjectBuilding }
constructor TProjectBuilding.Create;
begin
ManifestFileName := ProjManager.ProjDirPreBuild + 'META-INF' + DIR_SEP + 'MANIFEST.MF';
end;
function TProjectBuilding.CreateManifest(const FileName: string): boolean;
var
ManifestExtraOptions: string;
mName, mDeleteConfirm, mDescription, mInfoURL: string;
begin
Result := False;
with ProjConfig do
begin
ManifestExtraOptions := MnfExtraOptions;
mDeleteConfirm := MIDletDeleteConfirm;
mDescription := MIDletDescription;
mInfoURL := MIDletInfoURL;
mName := MIDletName;
end;
with TStringList.Create do
begin
try
Add('Manifest-Version: 1.0');
Add('Created-By: ' + APP_NAME + ' ' + GetProgramVersion);
Add('MIDlet-1: ' + mName + ', ' + ProjConfig.MIDletIcon + ', FW');
Add('MIDlet-Name: ' + mName);
Add('MIDlet-Version: ' + ProjManager.MIDletVersion);
Add('MIDlet-Vendor: ' + ProjConfig.MIDletVendor);
if mDescription <> '' then
Add('MIDlet-Description: ' + mDescription);
if mInfoURL <> '' then
Add('MIDlet-Info-URL: ' + mInfoURL);
if mDeleteConfirm <> '' then
Add('MIDlet-Delete-Confirm: ' + mDeleteConfirm);
Add('MicroEdition-Configuration: CLDC-1.1');
if ProjConfig.CanvasType <= 0 then
Add('MicroEdition-Profile: MIDP-1.0')
else
Add('MicroEdition-Profile: MIDP-2.0');
if ProjConfig.MnfExtraOptionsEnabled then
if ManifestExtraOptions <> '' then
Add(ManifestExtraOptions);
Text := DelEmptyLines(Text);
try
SaveToFile(FileName);
Result := True;
except
AddLogMsg(ERR_SAVING + ': ' + FileName, lmtErr);
end;
finally
Free;
end;
end;
end;
function TProjectBuilding.DeleteCharacters(const AValue: string): string;
var
i: integer;
begin
with TStringList.Create do
begin
try
Text := AValue;
for i := Count - 1 downto 0 do
if Pos('@', Strings[i]) > 0 then
Delete(i);
Text := StringReplace(Text, '^1', 'Lib: ', [rfReplaceAll]);
Text := StringReplace(Text, '^2', '', [rfReplaceAll]);
Text := StringReplace(Text, '^3', '', [rfReplaceAll]);
Text := StringReplace(Text, '[Pascal Error] ', '', [rfReplaceAll]);
Result := Text;
finally
Free;
end;
end;
end;
function TProjectBuilding.CompileFile(FileName: string): boolean;
var
P: TProcFunc;
procedure CopyClass;
var
StubFileName: string;
begin
with TRegExpr.Create do
begin
try
Expression := '\^2(.*?)' + EXT_CLASS;
if Exec(P.Output) then
repeat
StubFileName := GetAppPath + APP_DIR_STUBS + Match[1] + EXT_CLASS;
if CheckFile(StubFileName) then
CopyFile(StubFileName, ProjManager.ProjDirPreBuild + ExtractFileName(StubFileName));
until not ExecNext;
finally
Free;
end;
end;
end;
procedure CopyLib;
const
LibPrefix = 'Lib_';
var
LibFileName, NewLibFileName: string;
begin
with TRegExpr.Create do
begin
try
Expression := '\^1(.*?)' + LE;
if Exec(P.Output) then
repeat
LibFileName := ProjManager.ProjDirLibs + LibPrefix + Match[1] + EXT_CLASS;
NewLibFileName := ProjManager.ProjDirPreBuild + ExtractFileName(LibFileName);
if FileExists(LibFileName) then
begin
if CheckFile(LibFileName) then
CopyFile(LibFileName, NewLibFileName);
end
else
begin
LibFileName := GetAppPath + APP_DIR_LIBS + ExtractFileName(LibFileName);
if CheckFile(LibFileName) then
CopyFile(LibFileName, NewLibFileName);
end;
until not ExecNext;
finally
Free;
end;
end;
end;
var
MPCompiler, CmdParameters: string;
OrigName: string;
begin
Result := False;
if not FileExists(FileName) then
begin
AddLogMsg(ERR_FAILED_FIND_LIB + ': "' +
ExtractFileNameOnly(FileName) + '"', lmtErr);
Exit;
end;
MPCompiler := GetAppPath + MP3CC;
CmdParameters :=
' -s "' + FileName + '"' +
' -o "' + ExcludeTrailingPathDelimiter(ProjManager.ProjDirPreBuild) + '"' +
' -l "' + ExcludeTrailingPathDelimiter(GetAppPath + APP_DIR_LIBS) + '"' +
' -p "' + ExcludeTrailingPathDelimiter(ProjManager.ProjDirLibs) + '"' +
' -m ' + IntToStr(ProjConfig.MathType) +
' -c ' + IntToStr(ProjConfig.CanvasType);
P := ProcStart(MPCompiler, CmdParameters + ' -d');
if not P.Completed then
Exit;
OrigName := ExtractFileName(FileName);
with TRegExpr.Create do
begin
try
Expression := '\^0(.*?)' + LE;
if Exec(P.Output) then
begin
repeat
if not FileExists(ProjManager.ProjDirPreBuild + Match[1] + EXT_CLASS) then // ._.
begin
FileName := ProjManager.ProjDirSrc + Match[1] + EXT_MODULE;
if not CompileFile(FileName) then
Exit;
end;
until not ExecNext;
end;
finally
Free;
end;
end;
AddLogMsg(LOG_INFO_COMPILING + ' ' + OrigName + '...');
P := ProcStart(MPCompiler, CmdParameters);
if P.Completed then
begin
CopyClass;
CopyLib;
if not IsErr(P.Output) then
begin
AddLogMsg(DeleteCharacters(P.Output) + LOG_INFO_COMPLETED, lmtOk);
Result := True;
end
else
AddLogMsg(DeleteCharacters(P.Output), lmtErr);
end;
end;
function TProjectBuilding.IsErr(const AValue: string): boolean;
begin
Result := False;
if Pos('[Pascal Error]', AValue) > 0 then
Result := True;
end;
function TProjectBuilding.ExecPreBuildAct: boolean;
var
FW_Class: string;
begin
Result := False;
if ProjManager.CreateProjDir(ProjManager.ProjDirHome) then
if RebuildPreBuildDir then
begin
FW_Class := GetAppPath + APP_DIR_STUBS + CLASS_FW;
if CheckFile(FW_Class) then
if CopyFile(FW_Class, ProjManager.ProjDirPreBuild + CLASS_FW) then
if MakeDir(ExtractFilePath(ManifestFileName)) then
if CreateManifest(ManifestFileName) then
Result := True;
end;
end;
function TProjectBuilding.RebuildPreBuildDir: boolean;
var
PreBuildDir: string;
begin
Result := False;
PreBuildDir := ProjManager.ProjDirPreBuild;
if CheckDir(PreBuildDir) then
if DeleteDirectory(PreBuildDir, False) then
if MakeDir(PreBuildDir) then
Result := True;
end;
procedure TProjectBuilding.CreateJad;
var
JadFile: string;
begin
JadFile := ProjManager.JadFile;
if not CopyFile(ManifestFileName, JadFile) then
Exit;
with TStringList.Create do
begin
try
LoadFromFile(JadFile);
Delete(0);
Delete(0);
if ProjConfig.MIDletInstallNotify <> '' then
Add('MIDlet-Install-Notify: ' + ProjConfig.MIDletInstallNotify);
Add('MIDlet-Jar-URL: ' + ExtractFileName(ProjManager.JARFile));
Add('MIDlet-Jar-Size: ' + IntToStr(FileSize(ProjManager.JARFile)));
Text := DelEmptyLines(Text);
SaveToFile(JadFile);
finally
Free;
end;
end;
end;
function TProjectBuilding.Build(isAndroid: boolean = False): boolean;
procedure IncBuildVers;
var
vMajor, vMinor, vBuild: integer;
begin
vMajor := ProjConfig.VersMajor;
vMinor := ProjConfig.VersMinor;
vBuild := ProjConfig.VersBuild;
Inc(vBuild);
// >= 100 not working (Nokia)
if vBuild > 99 then
begin
vBuild := 0;
Inc(vMinor);
end;
if vMinor > 99 then
begin
vMinor := 0;
Inc(vMajor);
end;
ProjConfig.VersMajor := vMajor;
ProjConfig.VersMinor := vMinor;
ProjConfig.VersBuild := vBuild;
end;
var
FileArchiver, CmdParameters: string;
JARFileName: string;
msg: string;
begin
Result := False;
if not ExecPreBuildAct then
Exit;
if not CompileFile(ProjManager.MainModule) then
Exit;
JARFileName := ProjManager.JarFile;
if FileExists(JARFileName) then
if not DeleteFile(JARFileName) then
begin
AddLogMsg(ERR_FAILED_DEL + ' ' + ExtractFileName(JARFileName) +
' ' + TEXT_PROCESS_BUSY, lmtErr);
Exit;
end;
AddLogMsg(LOG_INFO_ARCHIVING_START + ' ' + ExtractFileName(JARFileName) + '...' + LE);
FileArchiver := GetAppPath + FILE_ARCHIVER;
CmdParameters := 'a "' + JARFileName + '" "';
if ProcStart(FileArchiver, CmdParameters + ProjManager.ProjDirPreBuild + '*"').Completed then
begin
if ProcStart(FileArchiver, CmdParameters + ProjManager.ProjDirRes + '*"').Completed then
begin
CreateJad;
if IDEConfig.AutoIncBuildVers then
IncBuildVers;
if not isAndroid then
begin
msg :=
MSG_BUILD_SUCCESSFUL + LE +
TEXT_VERSION + ': ' + ProjManager.MIDletVersion + LE +
TEXT_SIZE + ': ' + GetFileSize(JARFileName) + LE +
TEXT_PLATFORM + ': JavaME';
end;
AddLogMsg(msg, lmtOk);
Result := True;
end;
end;
end;
function TProjectBuilding.CompileMainModule: boolean;
begin
Result := False;
if ProjManager.CreateProjDir(ProjManager.ProjDirHome) then
if RebuildPreBuildDir then
if CompileFile(ProjManager.MainModule) then
Result := True;
end;
procedure TProjectBuilding.Run;
begin
if not Build then
Exit;
AddLogMsg('Emulator: ' + TEXT_LAUCH + ' ' + ExtractFileName(ProjManager.JarFile) + '...');
if ProcStart(IDEConfig.MacrosReplace(IDEConfig.EmulatorCmd)).Completed then
AddLogMsg(LOG_INFO_EMULATOR_STOP);
end;
end.