-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathupdatelazconfignew.pas
622 lines (568 loc) · 19.2 KB
/
updatelazconfignew.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
unit updatelazconfig;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
xmlconf,DOM, XMLRead, XMLWrite;
//XMLCfg, Laz2_DOM, Laz2_XMLRead, Laz2_XMLWrite;
const
// Some fixed configuration files.
StaticsConfig='staticpackages.inc';
// Editor options:
EditorConfig='editoroptions.xml';
// General options:
EnvironmentConfig='environmentoptions.xml';
// Help optons:
HelpConfig='helpoptions.xml';
// Packages:
PackageConfig='packagefiles.xml';
// Miscellaneous Options
MiscellaneousConfig='miscellaneousoptions.xml';
// FPC defines (source cache):
FPCDefines='fpcdefines.xml';
// File history:
History='inputhistory.xml';
// Pas2js configuration options:
Pas2jsConfig='pas2jsdsgnoptions.xml';
// BuildIDE config file
DefaultIDEMakeOptionFilename='idemake.cfg';
// Versions used when new config files are generated.
// Lazarus pre 1.0: 106
// We can assume Lazarus trunk can parse this version:
TrunkVersionNewEnvironmentConfig='110';
TrunkLazarusNewEnvironmentConfig='2.3.0';
// We use a hardcoded version for Lazarus below
VersionNewHelpConfig='1';
VersionNewPackageConfig='3';
type
{ TConfigVariable }
TConfig=class; //forward declaration
TUpdateLazConfig=class; //forward declaration
TConfig = class(TObject)
private
bChanged: boolean;
FFilename: string;
FNew: boolean;
Doc: TXMLDocument;
public
constructor Create(const AFilename: String); overload; // create and load
destructor Destroy; override;
// Did the config file exist before using it?
property New: boolean read FNew;
// Delete a child from a different part of the tree
procedure DeletePath(OldPath: string);
procedure DeleteValue(const APath: string);
function FindNode(APath: string;var AttrName:DOMString;bCreate:boolean):TDomNode;
function GetValue(const APath, ADefault: String): String;
function GetValue(const APath: String; ADefault: Integer): Integer;
function GetValue(const APath: String; ADefault: Boolean): Boolean;
// Move a child from a different part of the tree
procedure MovePath(OldPath, NewPath: string);
// Save our changes to the config variable
procedure Save;
procedure SetValue(const APath, AValue: DOMString);
procedure SetValue(const APath: String; AValue: Integer);
procedure SetValue(const APath: String; AValue: Boolean);
end;
{ TUpdateLazConfig }
TUpdateLazConfig = class(TObject)
private
//List of TConfigs, with absolute file names
FConfigs: TStringList;
// Place where config files are stored if no path component given
FDefaultConfigPath: string;
FLazarusMajorVer: integer; //major part of the version number, e.g. 1 for 1.0.8, or -1 if unknown
FLazarusMinor: integer; //minor part of the version number, e.g. 0 for 1.0.8, or -1 if unknown
FLazarusRelease: integer; //release part of the version number, e.g. 8 for 1.0.8, or -1 if unknown
FLazarusPatch: integer; //candidate part of the version number, e.g. 2 for 1.0.8RC2, or -1 if unknown
function GetConfig(const ConfigFile: string): TConfig;
procedure WriteConfig;
public
{ Remove path leading up to a variable (e.g. EnvironmentOptions/ExternalTools) }
procedure DeletePath(ConfigFile, Path: string);
{ Remove entire variable }
procedure DeleteVariable(ConfigFile, Variable:string);
{ Returns string variable content, or empty string if it doesn't exist }
function GetVariable(ConfigFile, Variable: string): string;
{ Returns integer variable content, or Default if it doesn't exit }
function GetVariable(ConfigFile, Variable: string; Default: integer): integer;
{ Returns boolean variable content, or Default if it doesn't exit }
function GetVariable(ConfigFile, Variable: string; Default: boolean): boolean;
{ Move part of the tree to another part of the tree: can be used to move children }
procedure MovePath(ConfigFile, OldPath, NewPath: string);
{ Sets string variable to a certain value.}
procedure SetVariable(ConfigFile, Variable, Value: string);
{ Sets integer variable to a certain value}
procedure SetVariable(ConfigFile, Variable: string; Value: integer);
{ Sets boolean variable to a certain value}
procedure SetVariable(ConfigFile, Variable: string; Value: boolean);
{ Is config file is created for us.}
function IfNewFile(ConfigFile:string):boolean;
{ Sets variable to a certain value, only if a config file is created for us.}
procedure SetVariableIfNewFile(ConfigFile, Variable, Value: string);
{ Create object; specify
path (primary config path) where option files should be created or updated
Lazarus major, minor and release version that is downloaded (or -1 if unknown
in which case it's assumed to be latest trunk)}
constructor Create(ConfigPath: string;
LazarusMajorVersion: integer=-1;
LazarusMinorVersion: integer=-1;
LazarusReleaseVersion: integer=-1;
LazarusPatchVersion: integer=-1);
destructor Destroy; override;
end;
procedure LazDocPathAdd(const PathToAdd: string; LazarusConfig: TUpdateLazConfig); //Add a path to the LazDoc/fpcdoc list
implementation
uses
fpcuputil;
procedure LazDocPathAdd(const PathToAdd: string; LazarusConfig: TUpdateLazConfig);
var
CleanedPath: string;
FoundIt: boolean;
i: integer;
TempList: TStringList;
key,LazDocPath,xmlfile: string;
begin
if PathToAdd<>'' then
begin
// Normalize path so we can compare:
CleanedPath:=ExcludeTrailingPathDelimiter(SafeExpandFileName(PathToAdd));
FoundIt:=false;
xmlfile:=EnvironmentConfig;
key:='EnvironmentOptions/LazDoc/Paths';
LazDocPath:=LazarusConfig.GetVariable(xmlfile, key);
// In an empty config, we just add our CleanedPath.
// If it's not empty, we need to check if the config already contains our path:
if LazDocPath<>'' then
begin
TempList:=TStringList.Create;
try
// Analyze all paths specified
TempList.Delimiter:=';';
TempList.StrictDelimiter:=True;
TempList.DelimitedText:=LazDocPath;
// Normalize all paths stored in setting and look for our value:
for i := 0 to TempList.Count - 1 do
begin
TempList[i]:=ExcludeLeadingPathDelimiter(SafeExpandFileName(TempList[i]));
if TempList[i]=CleanedPath then
begin
// Settings already include this dir
FoundIt:=true;
break;
end;
end;
// Only add our setting if not already found
if FoundIt then
CleanedPath:=LazDocPath
else
CleanedPath:=CleanedPath+';'+LazDocPath;
finally
TempList.Free;
end;
end;
LazarusConfig.SetVariable(xmlfile, key, CleanedPath);
end;
end;
{ TConfig }
procedure TConfig.DeletePath(OldPath: string);
var
OldChild: TDOMNode;
AttrName: DOMString;
begin
if OldPath[length(OldPath)]='/' then
SetLength(OldPath,length(OldPath)-1);
OldChild:=FindNode(OldPath+'/blah',AttrName,false); // add dummy attribute to path
if not Assigned(OldChild) then
exit;
OldChild.ParentNode.RemoveChild(OldChild);
bChanged:=true;
end;
procedure TConfig.DeleteValue(const APath: string);
var
Node: TDomNode;
AttrName: DOMString;
begin
Node:=FindNode(APath,AttrName,false);
if Node=nil then
exit;
if Assigned(TDOMElement(Node).GetAttributeNode(AttrName)) then begin
begin
TDOMElement(Node).RemoveAttribute(AttrName);
bChanged:=true;
end;
end;
end;
function TConfig.FindNode(APath: string;var AttrName:DOMString;bCreate:boolean): TDomNode;
var
Node,Parent: TDOMNode;
NodeName: DOMString;
StartPos: integer;
begin
result:=nil;
AttrName:='';
Node:=Doc.FindNode('CONFIG');
while assigned(Node) and (pos('/',APath)>0) do //walk in tree until no more /
begin
NodeName:=DOMString(copy(APath,1,pos('/',APath)-1));
Delete(APath,1,length(NodeName)+1);
Parent:=Node;
Node:=Node.FindNode(NodeName);
if not assigned(Node) and bCreate then
begin
Node:=Doc.CreateElement(NodeName);
Parent.AppendChild(Node);
end;
end;
if assigned(Node) then
begin
AttrName:=APath;
result:=Node;
end;
end;
function TConfig.GetValue(const APath, ADefault: String): String;
var
Node, Attr: TDOMNode;
AttrName: DOMString;
StartPos: integer;
begin
Result:=ADefault;
Node:=FindNode(APath,{%H-}AttrName,false);
if Node=nil then
exit;
Attr := Node.Attributes.GetNamedItem(AttrName);
if Assigned(Attr) then
Result := Attr.NodeValue;
end;
function TConfig.GetValue(const APath: String; ADefault: Integer): Integer;
begin
Result := StrToIntDef(GetValue(APath, IntToStr(ADefault)),ADefault);
end;
function TConfig.GetValue(const APath: String; ADefault: Boolean): Boolean;
var
s: String;
begin
if ADefault then
s := 'True'
else
s := 'False';
s := GetValue(APath, s);
if CompareText(s,'TRUE')=0 then
Result := True
else if CompareText(s,'FALSE')=0 then
Result := False
else
Result := ADefault;
end;
procedure TConfig.MovePath(OldPath, NewPath: string);
var
NewChild, OldChild: TDOMNode;
AttrName:DOMString;
i:integer;
begin
if NewPath[length(NewPath)]='/' then
SetLength(NewPath,length(NewPath)-1);
if OldPath[length(OldPath)]='/' then
SetLength(OldPath,length(OldPath)-1);
NewChild:=FindNode(NewPath+'/blah',AttrName,false); // append dummy attribute to path
OldChild:=FindNode(OldPath+'/bloh',AttrName,false);
while Assigned(NewChild.FirstChild) do
NewChild.RemoveChild(NewChild.FirstChild);
for i:=0 to OldChild.ChildNodes.Count-1 do
begin
NewChild.AppendChild(OldChild.ChildNodes.Item[i].CloneNode(True));
end;
bChanged:=true;
end;
procedure TConfig.Save;
begin
WriteXMLFile(Doc,FFilename);
end;
procedure TConfig.SetValue(const APath, AValue: DOMString);
var
Node: TDOMNode;
AttrName: DOMString;
StartPos: integer;
begin
Node:=FindNode(APath,AttrName,true);
if Node=nil then
exit;
if (not Assigned(TDOMElement(Node).GetAttributeNode(AttrName))) or
(TDOMElement(Node)[AttrName] <> AValue) then
begin
TDOMElement(Node)[AttrName] := AValue;
bChanged:=true;
end;
end;
procedure TConfig.SetValue(const APath: String; AValue: Integer);
begin
SetValue(APath, IntToStr(AValue));
end;
procedure TConfig.SetValue(const APath: String; AValue: Boolean);
begin
if AValue then
SetValue(APath, 'True')
else
SetValue(APath, 'False');
end;
constructor TConfig.Create(const AFilename: String);
begin
FFilename:=AFilename;
FNew:=not(FileExists(AFileName));
if FNew then
begin
Doc:=TXMLDocument.Create;
// CONFIG node present in all Lazarus configs=>we ensure the config file gets created if it doesn't exist yet:
Doc.AppendChild(Doc.CreateElement('CONFIG'));
end
else
//ReadXMLFile(Doc,AFilename,[xrfAllowLowerThanInAttributeValue,xrfAllowSpecialCharsInAttributeValue,xrfAllowSpecialCharsInComments]);
ReadXMLFile(Doc,AFilename);
bChanged:=false;
end;
destructor TConfig.Destroy;
begin
If bChanged then
begin
// Make sure path exists:
ForceDirectoriesSafe(ExtractFilePath(FFilename));
Save;
end;
Doc.Free;
inherited Destroy;
end;
procedure TUpdateLazConfig.WriteConfig;
var
Counter:integer;
begin
// Write all configs to disk if necessary
for Counter:=0 to FConfigs.Count-1 do
begin
(FConfigs.Objects[Counter] As TConfig).Save;
end;
end;
procedure TUpdateLazConfig.DeletePath(ConfigFile, Path: string);
var
Config: TConfig;
begin
Config:=GetConfig(ConfigFile);
Config.DeletePath(Path);
end;
function TUpdateLazConfig.GetConfig(const ConfigFile: string): TConfig;
var
ConfigIndex: integer;
FileName: string;
NewConfig: TConfig;
Version:string;
begin
if ExtractFileName(ConfigFile)=Configfile then
begin
// No directory given, place in config path
FileName:=SafeExpandFileName(IncludeTrailingPathDelimiter(FDefaultConfigPath)+
ConfigFile);
end
else
begin
// Normalize
if ConfigFile='' then
raise Exception.Create('GetConfig: ConfigFile may not be empty');
FileName:=SafeExpandFileName(ConfigFile);
end;
ConfigIndex:=FConfigs.IndexOf(FileName);
if ConfigIndex=-1 then
begin
NewConfig:=TConfig.Create(FileName);
ConfigIndex:=FConfigs.AddObject(FileName, NewConfig);
if NewConfig.New then
begin
// Set up default config, including version number
case (ExtractFileName(ConfigFile)) of
EnvironmentConfig:
begin
Version:='';
if FLazarusMajorVer<>-1 then
begin
Version:=Version+IntToStr(FLazarusMajorVer);
if FLazarusMinor<>-1 then
begin
Version:=Version+'.'+IntToStr(FLazarusMinor);
if FLazarusRelease<>-1 then
Version:=Version+'.'+IntToStr(FLazarusRelease);
end;
end;
if FLazarusPatch>0 then
begin
Version:=Version+'RC'+IntToStr(FLazarusPatch);
end;
// If we don't add these, we trigger an upgrade process on first start on Lazarus 1.1+.
NewConfig.SetValue('EnvironmentOptions/Version/Lazarus',Version);
if FLazarusMajorVer=-1 then
begin // default to newest. Update this when new version appears
NewConfig.SetValue('EnvironmentOptions/Version/Value', TrunkVersionNewEnvironmentConfig);
NewConfig.SetValue('EnvironmentOptions/Version/Lazarus', TrunkLazarusNewEnvironmentConfig);
end
else if FLazarusMajorVer=0 then
begin
if FLazarusMinor<=0 then
NewConfig.SetValue('EnvironmentOptions/Version/Lazarus','0.9.31');
NewConfig.SetValue('EnvironmentOptions/Version/Value', '106')
end
else if FLazarusMajorVer=1 then
case FLazarusMinor of
0 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '107'); //for version 1.0
1 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '107'); //for version 1.0,1.1
2 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '108'); //for version 1.2
3 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '108'); //for version 1.3
4 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '108'); //for version 1.4
5 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '109'); //for version 1.5
6 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '109'); //for version 1.6
7 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '110'); //for version 1.7
8 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '110'); //for version 1.8 (fixes)
9 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '110'); //for version 1.9 (old trunk)
end
else if FLazarusMajorVer=2 then
case FLazarusMinor of
0 : NewConfig.SetValue('EnvironmentOptions/Version/Value', '110'); //for version 2.0
else
begin
NewConfig.SetValue('EnvironmentOptions/Version/Value', TrunkVersionNewEnvironmentConfig);
NewConfig.SetValue('EnvironmentOptions/Version/Lazarus', TrunkLazarusNewEnvironmentConfig);
end;
end
else { 3 or higher? keep latest known, we can leave lazarus version though }
begin
NewConfig.SetValue('EnvironmentOptions/Version/Value', TrunkVersionNewEnvironmentConfig);
end;
{$ifdef CPUAARCH64}
// IDE does not size correctly when set to auto
NewConfig.SetValue('Desktops/Desktop1/AutoAdjustIDEHeight/Value', 'False');
{$endif}
end;
HelpConfig:
NewConfig.SetValue('HelpOptions/Version/Value', VersionNewHelpConfig);
PackageConfig:
begin
// Note: Version= in this file is an attribute of UserPkgLinks; might not matter
NewConfig.SetValue('UserPkgLinks/Version', VersionNewPackageConfig);
NewConfig.SetValue('UserPkgLinks/Count', '0');
end;
end;
end;
//NewConfig.Free; //This would remove object from stringlist
end;
Result:=(FConfigs.Objects[ConfigIndex] As TConfig);
end;
procedure TUpdateLazConfig.DeleteVariable(ConfigFile, Variable:string);
var
Config: TConfig;
VariableIndex: integer;
begin
Config:=GetConfig(ConfigFile);
Config.DeleteValue(Variable);
end;
function TUpdateLazConfig.GetVariable(ConfigFile, Variable: string): string;
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
result:=Config.GetValue(Variable, '');
end;
function TUpdateLazConfig.GetVariable(ConfigFile, Variable: string;
Default: integer): integer;
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
result:=Config.GetValue(Variable, Default);
end;
function TUpdateLazConfig.GetVariable(ConfigFile, Variable: string;
Default: boolean): boolean;
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
result:=Config.GetValue(Variable, Default);
end;
procedure TUpdateLazConfig.MovePath(ConfigFile, OldPath, NewPath: string);
var
Config: TConfig;
begin
Config:=GetConfig(ConfigFile);
Config.MovePath(OldPath, NewPath);
end;
procedure TUpdateLazConfig.SetVariable(ConfigFile, Variable, Value: string);
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
Config.SetValue(Variable, Value);
end;
procedure TUpdateLazConfig.SetVariable(ConfigFile, Variable: string;
Value: integer);
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
Config.SetValue(Variable, Value);
end;
procedure TUpdateLazConfig.SetVariable(ConfigFile, Variable: string;
Value: boolean);
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
Config.SetValue(Variable, Value);
end;
function TUpdateLazConfig.IfNewFile(ConfigFile:string):boolean;
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
result:=Config.New;
end;
procedure TUpdateLazConfig.SetVariableIfNewFile(ConfigFile, Variable,
Value: string);
var
Config: TConfig;
begin
// Don't free this one, as it will remove it from the list
Config:=GetConfig(ConfigFile);
if Config.New then Config.SetValue(Variable, Value);
end;
constructor TUpdateLazConfig.Create(ConfigPath: string;
LazarusMajorVersion: integer=-1;
LazarusMinorVersion: integer=-1;
LazarusReleaseVersion: integer=-1;
LazarusPatchVersion: integer=-1);
begin
FLazarusMajorVer:=LazarusMajorVersion;
FLazarusMinor:=LazarusMinorVersion;
FLazarusRelease:=LazarusReleaseVersion;
FLazarusPatch:=LazarusPatchVersion;
FConfigs:=TStringList.Create;
FConfigs.Sorted:=true;
FConfigs.Duplicates:=dupError;
if ConfigPath='' then
raise Exception.Create('TUpdateLazConfig: ConfigPath may not be empty');
FDefaultConfigPath:=IncludeTrailingPathDelimiter(SafeExpandFileName(ConfigPath));
end;
destructor TUpdateLazConfig.Destroy;
var
Counter: integer;
begin
// When FConfigs go out of scope, they save to file first...
for Counter:=0 to FConfigs.Count-1 do
begin
FConfigs.Objects[Counter].Free;
end;
FConfigs.Free;
inherited Destroy;
end;
end.