-
Notifications
You must be signed in to change notification settings - Fork 10
/
startLazarusPortable.lpr
389 lines (323 loc) · 16.3 KB
/
startLazarusPortable.lpr
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
program startLazarusPortable;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, sysutils, process, windows, RegExpr, fileinfo, laz2_XMLRead, laz2_XMLWrite, Laz2_DOM
{ you can add units after this };
const
confPath = 'LazarusConfig\';
lazDirEnvVar = '$LAZDIR$';
var
lazarusDir: String;
XMLDoc: TXMLDocument;
nodeEnvOpt: TDOMNode;
tmpDOMNode: TDOMNode;
outStr: String;
FPCCompilerDir: String;
tmpDirString: String;
stringListConf: TStringList;
I: Integer;
function FileCopy(Source, Target: string): boolean;
var
MemBuffer: TMemoryStream;
begin
result := false;
MemBuffer := TMemoryStream.Create;
try
MemBuffer.LoadFromFile(Source);
MemBuffer.SaveToFile(Target);
result := true
except
//swallow exception; function result is false by default
end;
// Clean up
MemBuffer.Free
end;
function checkLazarusInstallation: Boolean;
begin
if FileExists(lazarusDir+'lazarus.exe') and FileExists(lazarusDir+'startlazarus.exe') and FileExists(lazarusDir+'environmentoptions.xml') and DirectoryExists(lazarusDir+'fpc') then
Result := True
else
Result := False;
end;
{$R *.res}
begin
outStr := '';
WriteLn('startLazarusPortable v1.1');
lazarusDir := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0)));
WriteLn('LazarusDir: '+lazarusDir);
Write('Checking for Lazarus installation... ');
if checkLazarusInstallation then
begin
WriteLn('SUCCESS');
Write('Loading '+lazarusDir+'environmentoptions.xml...');
try
ReadXMLFile(XMLDoc,lazarusDir+'environmentoptions.xml');
nodeEnvOpt := XMLDoc.DocumentElement.FindNode('EnvironmentOptions');
tmpDOMNode := nodeEnvOpt.FindNode('LazarusDirectory');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('Value').TextContent := lazarusDir;
tmpDOMNode := nodeEnvOpt.FindNode('CompilerFilename');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
begin
tmpDirString := ExtractFileDir(tmpDOMNode.Attributes.GetNamedItem('Value').TextContent);
tmpDirString := StringReplace(tmpDirString,'$(LazarusDir)',lazarusDir,[rfIgnoreCase]);
tmpDirString := StringReplace(tmpDirString,'$LazarusDir',lazarusDir,[rfIgnoreCase]);
FPCCompilerDir := IncludeTrailingPathDelimiter(StringReplace(tmpDirString,'\\','\',[rfReplaceAll]));
end;
WriteLn('DONE');
Write('Writing back to file...');
WriteXMLFile(XMLDoc,lazarusDir+'environmentoptions.xml');
WriteLn('DONE');
finally
tmpDOMNode.Free;
nodeEnvOpt.Free;
XMLDoc.Free;
end;
Write('Checking for FPC directory...');
if DirectoryExists(FPCCompilerDir) then
begin
WriteLn('SUCCESS');
WriteLn('FPC Configuration File Location is: '+FPCCompilerDir+'fpc.cfg');
Write('Checking for FPC Configuration File...');
if FileExists(FPCCompilerDir+'fpc.cfg') then
begin
WriteLn('SUCCESS');
Write('Editing FPC Configuration file...');
try
stringListConf := TStringList.Create;
stringListConf.LoadFromFile(FPCCompilerDir+'fpc.cfg');
//WriteLn('Conf File Line Count: '+IntToStr(stringListConf.Count));
for I := 0 to (stringListConf.Count - 1) do
begin
if (Length(stringListConf.Strings[I])>0 ) then
stringListConf.Strings[I] := ReplaceRegExpr('(-F.).+(fpc\\)',stringListConf.Strings[I],'$1'+QuoteRegExprMetaChars(lazDirEnvVar)+'$2',True);
end;
stringListConf.SaveToFile(FPCCompilerDir+'fpc.cfg');
WriteLn('DONE');
finally
stringListConf.Free;
end;
Write('Checking for Lazarus Configuration directory...');
if DirectoryExists(lazarusDir+confPath) then
begin
WriteLn('SUCCESS');
Write('Checking for environmentoptions.xml...');
if FileExists(lazarusDir+confPath+'environmentoptions.xml') then
begin
WriteLn('SUCCESS');
Write('Making backup of environmentoptions.xml...');
if FileCopy(lazarusDir+confPath+'environmentoptions.xml',lazarusDir+confPath+'environmentoptions.xml.bak') then
begin
WriteLn('SUCCESS');
Write('Editing File: '+lazarusDir+confPath+'environmentoptions.xml...');
try
ReadXMLFile(XMLDoc,lazarusDir+confPath+'environmentoptions.xml');
nodeEnvOpt := XMLDoc.DocumentElement.FindNode('EnvironmentOptions');
tmpDOMNode := nodeEnvOpt.FindNode('LazarusDirectory');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('CompilerFilename');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('MakeFilename');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('TestBuildDirectory');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('FPCSourceDirectory');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('DebuggerFilename');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('CompilerMessagesFilename');
if tmpDOMNode <> Nil then
if tmpDOMNode.FindNode('History') <> Nil then
tmpDOMNode.FindNode('History').Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('Recent');
if tmpDOMNode <> Nil then
tmpDOMNode.Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('LastCalledByLazarusFullPath');
if tmpDOMNode <> Nil then
tmpDOMNode.Destroy;
tmpDOMNode := nodeEnvOpt.FindNode('LazarusDirectory');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('Value').TextContent := lazarusDir;
tmpDOMNode := nodeEnvOpt.FindNode('TestBuildDirectory');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('Value').TextContent := '$(LazarusDir)\Temp\';
WriteLn('DONE');
Write('Writing back to file...');
WriteXMLFile(XMLDoc,lazarusDir+confPath+'environmentoptions.xml');
WriteLn('DONE');
finally
tmpDOMNode.Free;
nodeEnvOpt.Free;
XMLDoc.Free;
end;
//Check OnlinePackageManager Paths
Write('Checking for OnlinePackageManager config...');
if DirectoryExists(lazarusDir+confPath+'onlinepackagemanager') then
begin
if FileExists(lazarusDir+confPath+'onlinepackagemanager\config\options.xml') then
begin
WriteLn('SUCCESS');
Write('Editing OnlinePackageManager config...');
try
ReadXMLFile(XMLDoc,lazarusDir+confPath+'onlinepackagemanager\config\options.xml',[xrfAllowSpecialCharsInAttributeValue,xrfAllowSpecialCharsInComments,xrfPreserveWhiteSpace]);
nodeEnvOpt := XMLDoc.DocumentElement.FindNode('Folders');
tmpDOMNode := nodeEnvOpt.FindNode('LocalRepositoryPackages');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('Value').TextContent := lazarusDir+confPath+'onlinepackagemanager\'+'packages\';
tmpDOMNode := nodeEnvOpt.FindNode('LocalRepositoryArchive');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('Value').TextContent := lazarusDir+confPath+'onlinepackagemanager\'+'archive\';
tmpDOMNode := nodeEnvOpt.FindNode('LocalRepositoryUpdate');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('Value') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('Value').TextContent := lazarusDir+confPath+'onlinepackagemanager\'+'update\';
WriteLn('DONE');
Write('Writing OnlinePackageManager Config back to file...');
WriteXMLFile(XMLDoc,lazarusDir+confPath+'onlinepackagemanager\config\options.xml',[xwfPreserveWhiteSpace,xwfSpecialCharsInAttributeValue]);
WriteLn('DONE');
finally
tmpDOMNode.Free;
nodeEnvOpt.Free;
XMLDoc.Free;
end;
end
else
WriteLn('FAIL');
end
else
WriteLn('FAIL');
Write('Checking for editoroptions.xml...');
if FileExists(lazarusDir+confPath+'editoroptions.xml') then
begin
WriteLn('SUCCESS');
Write('Editing file: '+lazarusDir+confPath+'editoroptions.xml...');
try
ReadXMLFile(XMLDoc,lazarusDir+confPath+'editoroptions.xml');
nodeEnvOpt := XMLDoc.DocumentElement.FindNode('EditorOptions');
tmpDOMNode := nodeEnvOpt.FindNode('CodeTools');
if tmpDOMNode <> Nil then
if tmpDOMNode.Attributes.GetNamedItem('CodeTemplateFileName') <> Nil then
tmpDOMNode.Attributes.GetNamedItem('CodeTemplateFileName').TextContent := lazarusDir+confPath+'lazarus.dci';
WriteLn('DONE');
Write('Writing editoroptions.xml back to file...');
WriteXMLFile(XMLDoc,lazarusDir+confPath+'editoroptions.xml');
WriteLn('DONE');
finally
tmpDOMNode.Free;
nodeEnvOpt.Free;
XMLDoc.Free;
end;
end
else
WriteLn('FAIL');
Write('Setting environment variable "LAZDIR" to '+lazarusDir+'...');
if SetEnvironmentVariable('LAZDIR',PChar(lazarusDir)) then
begin
WriteLn('SUCCESS');
Write('Starting Lazarus...');
//ReadLn;
RunCommand('cmd',['/C','"start "" "'+lazarusDir+'startlazarus.exe" --lazarusdir="'+lazarusDir+'" --pcp="'+lazarusDir+confPath+'" --scp="'+lazarusDir+'" --skip-last-project --nsc"'],outStr);
WriteLn(outStr);
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end
else
begin
WriteLn('FAIL');
WriteLn;
WriteLn('Lazarus needs to run at least once to generate config files.');
WriteLn('Press any key to start Lazarus...');
ReadLn;
Write('Setting environment variable "LAZDIR" to '+lazarusDir+'...');
if SetEnvironmentVariable('LAZDIR',PChar(lazarusDir)) then
begin
WriteLn('SUCCESS');
Write('Starting Lazarus...');
RunCommand('cmd',['/C','"start "" "'+lazarusDir+'startlazarus.exe" --lazarusdir="'+lazarusDir+'" --pcp="'+lazarusDir+confPath+'" --scp="'+lazarusDir+'" --skip-last-project --nsc"'],outStr);
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end;
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Creating Config Directory("'+lazarusDir+confPath+'")...');
if CreateDir(lazarusDir+confPath) then
begin
WriteLn('SUCCESS');
end
else
begin
WriteLn('FAIL');
end;
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end
else
begin
WriteLn('FAIL');
WriteLn;
Write('Press any key to exit...');
ReadLn;
end;
end.