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
|
{ pas2js options
Author: Mattias Gaertner
}
unit PJSDsgnOptions;
{$mode objfpc}{$H+}
{$Inline on}
interface
uses
Classes, SysUtils,
// LazUtils
LazFileCache, LazConfigStorage, LazFileUtils, FileUtil,
// Codetools
DefineTemplates, FileProcs,
// IdeIntf
MacroIntf, BaseIDEIntf;
const
PJSDsgnOptsFile = 'pas2jsdsgnoptions.xml';
PJSDefaultCompiler = '$MakeExe(IDE,pas2js)';
PJSDefaultPas2jsSrcDir = '';
PJSDefaultFPCSrcDir = '$(Pas2jsSrcDir)/compiler';
PJSDefaultFPCExe= '';
PJSDefaultDTS2Pas = '$MakeExe(IDE,dts2pas)';
PJSDefaultDTS2PasService = 'https://www.freepascal.org/~michael/service/dts2pas.cgi';
PJSDefaultStartAtPort = 3000; // compileserver default port
PJSDefaultHTTPServerParams = '-s -p $()';
PJSDefaultBrowser = '$MakeExe(IDE,firefox)';
PJSDefaultNodeJS = '$MakeExe(IDE,nodejs)';
PJSDefaultElectronExe = '$MakeExe(IDE,electron)';
Type
TPas2jsCachedOption = (
p2jcoCompilerFilename,
p2jcoPas2jsSrcDir,
p2jcoFPCSrcDir, // fpc source directory used for compiling tools and pas2js
p2jcoFPCExe, // fpc used for compiling tools
p2jcoNodeJSFilename,
p2jcoElectronFilename,
p2jcoAtomTemplateDir,
p2jcoVSCodeTemplateDir,
p2jcoDTSToPas,
p2jcoDTSToPasServiceURL
);
TPas2jsCachedOptions = set of TPas2jsCachedOption;
const
p2jcoFilenames = [
p2jcoCompilerFilename,
p2jcoFPCExe,
p2jcoNodeJSFilename,
p2jcoElectronFilename,
p2jcoAtomTemplateDir,
p2jcoVSCodeTemplateDir,
p2jcoDTSToPas
];
p2jcoDirectories = [
p2jcoPas2jsSrcDir,
p2jcoFPCSrcDir
];
type
TPas2jsCachedValue = record
RawValue: string;
Stamp: int64;
ParsedValue: string;
end;
PPas2jsCachedValue = ^TPas2jsCachedValue;
{ TPas2jsOptions }
TPas2jsOptions = class
private
FBrowserFileName: String;
FCachedOptions: array[TPas2jsCachedOption] of TPas2jsCachedValue;
FChangeStamp: int64;
FOldWebServerFileName: string;
FSavedStamp: int64;
FStartAtPort: Word;
function GetAtomTemplateDir: String;
function GetCompilerFilename: string;
function GetDTS2Pas: String;
function GetDTS2PasService: String;
function GetElectronFileName: string;
function GetFPCExe: string;
function GetFPCSrcDir: string;
function GetPas2jsSrcDir: string;
function GetVSCodeTemplateDir: String;
function GetModified: boolean;
function GetNodeJSFileName: string;
function GetParsedOptionValue(Option: TPas2jsCachedOption): string;
procedure SetAtomTemplateDir(AValue: String);
procedure SetDTS2Pas(AValue: String);
procedure SetDTS2PasService(AValue: String);
procedure SetElectronFileName(AValue: string);
procedure SetFPCExe(AValue: string);
procedure SetFPCSrcDir(AValue: string);
procedure SetPas2jsSrcDir(AValue: string);
procedure SetVSCodeTemplateDir(AValue: String);
procedure SetModified(AValue: boolean);
procedure SetCompilerFilename(AValue: string);
procedure SetNodeJSFileName(AValue: string);
procedure SetStartAtPort(AValue: Word);
procedure SetCachedOption(Option: TPas2jsCachedOption; const AValue: string);
public
constructor Create;
destructor Destroy; override;
procedure IncreaseChangeStamp; inline;
procedure Load;
procedure Save;
function GetParsedCompilerFilename: string;
function GetParsedPas2jsSrcDir: string;
function GetParsedNodeJSFilename: string;
function GetParsedElectronExe: string;
procedure LoadFromConfig(Cfg: TConfigStorage);
procedure SaveToConfig(Cfg: TConfigStorage);
public
property ChangeStamp: int64 read FChangeStamp;
property Modified: boolean read GetModified write SetModified;
property CompilerFilename: string read GetCompilerFilename write SetCompilerFilename;
property Pas2jsSrcDir: string read GetPas2jsSrcDir write SetPas2jsSrcDir; // with trailing pathdelim
property FPCSrcDir: string read GetFPCSrcDir write SetFPCSrcDir; // used for compiling tools and pas2js, with trailing pathdelim
property FPCExe: string read GetFPCExe write SetFPCExe; // used for compiling tools
property NodeJSFileName : string Read GetNodeJSFileName Write SetNodeJSFileName;
property ElectronFileName : string Read GetElectronFileName Write SetElectronFileName;
property AtomTemplateDir : String Read GetAtomTemplateDir Write SetAtomTemplateDir;
property VSCodeTemplateDir : String Read GetVSCodeTemplateDir Write SetVSCodeTemplateDir;
property DTS2Pas : String Read GetDTS2Pas Write SetDTS2Pas;
property DTS2PasServiceURL : String Read GetDTS2PasService Write SetDTS2PasService;
property StartAtPort : Word Read FStartAtPort Write SetStartAtPort;
public
property OldWebServerFileName: string read FOldWebServerFileName write FOldWebServerFileName;
property BrowserFileName : String Read FBrowserFileName Write FBrowserFileName;
end;
var
PJSOptions: TPas2jsOptions = nil;
function GetStandardPas2jsExe: string;
function GetStandardElectron: string;
function GetStandardNodeJS: string;
function GetPas2jsQuality(Filename: string; out Msg: string): boolean;
implementation
uses
strpas2jsdesign;
function GetStandardPas2jsExe: string;
begin
Result:=PJSDefaultCompiler;
if not IDEMacros.SubstituteMacros(Result) then
Result:='pas2js'+GetExeExt;
end;
function GetStandardNodeJS: string;
begin
Result:=PJSDefaultNodeJS;
if not IDEMacros.SubstituteMacros(Result) then
Result:='nodejs'+GetExeExt;
end;
function GetStandardElectron: string;
begin
Result:='electron'+GetExeExt;
end;
function GetPas2jsQuality(Filename: string; out Msg: string): boolean;
var
ShortFile: String;
begin
Msg:='';
Filename:=TrimFilename(Filename);
if (Filename='') then begin
Msg:=pjsdMissingPathToPas2js;
exit(false);
end;
if not FileExistsCached(Filename) then begin
Msg:=CTSafeFormat(pjsdFileNotFound, [Filename]);
exit(false);
end;
if not DirPathExistsCached(ExtractFilePath(Filename)) then begin
Msg:=CTSafeFormat(pjsdDirectoryNotFound, [ExtractFilePath(Filename)]);
exit(false);
end;
if not FileIsExecutable(Filename) then begin
Msg:=CTSafeFormat(pjsdFileNotExecutable, [Filename]);
exit(false);
end;
ShortFile:=ExtractFileNameOnly(Filename);
if not CompareText(LeftStr(ShortFile,length('pas2js')),'pas2js')<>0 then begin
Msg:=pjsdFileNameDoesNotStartWithPas2js;
exit(false);
end;
// run it
//RunTool(Filename);
end;
{ TPas2jsOptions }
procedure TPas2jsOptions.SetModified(AValue: boolean);
begin
if AValue then
IncreaseChangeStamp
else
FSavedStamp:=FChangeStamp;
end;
function TPas2jsOptions.GetModified: boolean;
begin
Result:=FSavedStamp<>FChangeStamp;
end;
function TPas2jsOptions.GetAtomTemplateDir: String;
begin
Result:=FCachedOptions[p2jcoAtomTemplateDir].RawValue;
end;
function TPas2jsOptions.GetCompilerFilename: string;
begin
Result:=FCachedOptions[p2jcoCompilerFilename].RawValue;
end;
function TPas2jsOptions.GetDTS2Pas: String;
begin
Result:=FCachedOptions[p2jcoDTSToPas].RawValue
end;
function TPas2jsOptions.GetDTS2PasService: String;
begin
Result:=FCachedOptions[p2jcoDTSToPasServiceURL].RawValue
end;
function TPas2jsOptions.GetElectronFileName: string;
begin
Result:=FCachedOptions[p2jcoElectronFilename].RawValue;
end;
function TPas2jsOptions.GetFPCExe: string;
begin
Result:=FCachedOptions[p2jcoFPCExe].RawValue;
end;
function TPas2jsOptions.GetFPCSrcDir: string;
begin
Result:=FCachedOptions[p2jcoFPCSrcDir].RawValue;
end;
function TPas2jsOptions.GetPas2jsSrcDir: string;
begin
Result:=FCachedOptions[p2jcoPas2jsSrcDir].RawValue;
end;
function TPas2jsOptions.GetVSCodeTemplateDir: String;
begin
Result:=FCachedOptions[p2jcoVSCodeTemplateDir].RawValue;
end;
function TPas2jsOptions.GetNodeJSFileName: string;
begin
Result:=FCachedOptions[p2jcoNodeJSFilename].RawValue;
end;
procedure TPas2jsOptions.SetCompilerFilename(AValue: string);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoCompilerFilename,AValue);
end;
procedure TPas2jsOptions.SetNodeJSFileName(AValue: string);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoNodeJSFilename,AValue);
end;
constructor TPas2jsOptions.Create;
var
o: TPas2jsCachedOption;
begin
FChangeStamp:=LUInvalidChangeStamp64;
FCachedOptions[p2jcoCompilerFilename].RawValue:=PJSDefaultCompiler;
FCachedOptions[p2jcoNodeJSFilename].RawValue:=PJSDefaultNodeJS;
FCachedOptions[p2jcoElectronFilename].RawValue:=PJSDefaultElectronExe;
FCachedOptions[p2jcoAtomTemplateDir].RawValue:='';
FCachedOptions[p2jcoVSCodeTemplateDir].RawValue:='';
FCachedOptions[p2jcoDTSToPas].RawValue:=PJSDefaultDTS2Pas;
FCachedOptions[p2jcoDTSToPasServiceURL].RawValue:=PJSDefaultDTS2PasService;
for o in TPas2jsCachedOption do
FCachedOptions[o].Stamp:=LUInvalidChangeStamp64;
end;
destructor TPas2jsOptions.Destroy;
begin
inherited Destroy;
end;
procedure TPas2jsOptions.IncreaseChangeStamp;
begin
LUIncreaseChangeStamp64(FChangeStamp);
end;
procedure TPas2jsOptions.Load;
var
Cfg: TConfigStorage;
begin
Cfg:=GetIDEConfigStorage(PJSDsgnOptsFile,true);
try
LoadFromConfig(Cfg);
finally
Cfg.Free;
end;
end;
procedure TPas2jsOptions.Save;
var
Cfg: TConfigStorage;
begin
Cfg:=GetIDEConfigStorage(PJSDsgnOptsFile,false);
try
SaveToConfig(Cfg);
finally
Cfg.Free;
end;
end;
Const
KeyCompiler = 'compiler/value';
KeyPas2jsSrcDir = 'sources/value';
KeyFPCSrcDir = 'fpcsrcdir/value';
KeyFPCExe= 'fpc/value';
KeyHTTPServer = 'webserver/value';
KeyBrowser = 'webbrowser/value';
KeyNodeJS = 'nodejs/value';
KeyElectronExe = 'electron/value';
KeyAtomTemplate = 'atomtemplate/value';
KeyVSCodeTemplate = 'vscodetemplate/value';
KeyStartPortAt = 'webserver/startatport/value';
KeyDTS2PasTool = 'dts2pastool/value';
KeyDTS2PasServiceURL= 'dts2passerviceurl/value';
procedure TPas2jsOptions.LoadFromConfig(Cfg: TConfigStorage);
begin
CompilerFilename:=Cfg.GetValue(KeyCompiler,PJSDefaultCompiler);
Pas2jsSrcDir:=Cfg.GetValue(KeyPas2jsSrcDir,PJSDefaultPas2jsSrcDir);
FPCSrcDir:=Cfg.GetValue(KeyFPCSrcDir,PJSDefaultFPCSrcDir);
FPCExe:=Cfg.GetValue(KeyFPCExe,PJSDefaultFPCExe);
NodeJSFileName:=Cfg.GetValue(KeyNodeJS,PJSDefaultNodeJS);
ElectronFileName:=Cfg.GetValue(KeyElectronExe,PJSDefaultElectronExe);
AtomTemplateDir:=Cfg.GetValue(KeyAtomTemplate,'');
VSCodeTemplateDir:=Cfg.GetValue(KeyVSCodeTemplate,'');
DTS2Pas:=cfg.GetValue(KeyDTS2PasTool,PJSDefaultDTS2Pas);
DTS2PasServiceURL:=cfg.GetValue(KeyDTS2PasServiceURL,PJSDefaultDTS2PasService);
StartAtPort :=Cfg.GetValue(KeyStartPortAt,PJSDefaultStartAtPort);
// legacy
FOldWebServerFileName:=Cfg.GetValue(KeyHTTPServer,'');
BrowserFileName:=Cfg.GetValue(KeyBrowser,'');
Modified:=false;
end;
procedure TPas2jsOptions.SaveToConfig(Cfg: TConfigStorage);
begin
Cfg.SetDeleteValue(KeyCompiler,CompilerFilename,PJSDefaultCompiler);
Cfg.SetDeleteValue(KeyPas2jsSrcDir,Pas2jsSrcDir,PJSDefaultPas2jsSrcDir);
Cfg.SetDeleteValue(KeyFPCSrcDir,FPCSrcDir,PJSDefaultFPCSrcDir);
Cfg.SetDeleteValue(KeyFPCExe,FPCExe,PJSDefaultFPCExe);
Cfg.SetDeleteValue(KeyStartPortAt,StartAtPort,PJSDefaultStartAtPort);
Cfg.SetDeleteValue(KeyNodeJS,NodeJSFileName,PJSDefaultNodeJS);
Cfg.SetDeleteValue(KeyElectronExe,ElectronFileName,PJSDefaultElectronExe);
Cfg.SetDeleteValue(KeyAtomTemplate,AtomTemplateDir,'');
Cfg.SetDeleteValue(KeyVSCodeTemplate,VSCodeTemplateDir,'');
cfg.SetDeleteValue(KeyDTS2PasTool,DTS2Pas,PJSDefaultDTS2Pas);
cfg.SetDeleteValue(KeyDTS2PasServiceURL,DTS2PasServiceURL,PJSDefaultDTS2PasService);
// legacy
cfg.SetDeleteValue(KeyHTTPServer,OldWebServerFileName,'');
Cfg.SetDeleteValue(KeyBrowser,BrowserFileName,'');
Modified:=false;
end;
function TPas2jsOptions.GetParsedCompilerFilename: string;
begin
Result:=GetParsedOptionValue(p2jcoCompilerFilename);
end;
function TPas2jsOptions.GetParsedPas2jsSrcDir: string;
begin
Result:=GetParsedOptionValue(p2jcoPas2jsSrcDir);
end;
function TPas2jsOptions.GetParsedNodeJSFilename: string;
begin
Result:=GetParsedOptionValue(p2jcoNodeJSFilename);
end;
function TPas2jsOptions.GetParsedElectronExe: string;
begin
Result:=GetParsedOptionValue(p2jcoElectronFilename);
end;
function TPas2jsOptions.GetParsedOptionValue(Option: TPas2jsCachedOption
): string;
var
p: PPas2jsCachedValue;
IsFilename, IsDirectory: Boolean;
begin
p:=@FCachedOptions[Option];
if p^.Stamp<>IDEMacros.BaseTimeStamp then
begin
p^.Stamp:=IDEMacros.BaseTimeStamp;
p^.ParsedValue:=p^.RawValue;
IDEMacros.SubstituteMacros(p^.ParsedValue);
IsFilename:=Option in p2jcoFilenames;
IsDirectory:=Option in p2jcoDirectories;
if IsFilename then
begin
p^.ParsedValue:=TrimFilename(p^.ParsedValue);
if (p^.ParsedValue<>'')
and not FilenameIsAbsolute(p^.ParsedValue) then
begin
if ExtractFilePath(p^.ParsedValue)='' then
p^.ParsedValue:=FindDefaultExecutablePath(p^.ParsedValue)
else
p^.ParsedValue:=''; // not found
end;
end;
if IsDirectory then
begin
p^.ParsedValue:=TrimFilename(p^.ParsedValue);
if (p^.ParsedValue<>'')
and not FilenameIsAbsolute(p^.ParsedValue) then
begin
p^.ParsedValue:=''; // not found
end;
end;
if p^.ParsedValue='' then
begin
case Option of
p2jcoCompilerFilename: p^.ParsedValue:=GetStandardPas2jsExe;
p2jcoNodeJSFilename: p^.ParsedValue:=GetStandardNodeJS;
p2jcoElectronFilename: p^.ParsedValue:=GetStandardElectron;
end;
if IsFilename and (p^.ParsedValue<>'')
and not FilenameIsAbsolute(p^.ParsedValue) then
begin
if ExtractFilePath(p^.ParsedValue)='' then
p^.ParsedValue:=FindDefaultExecutablePath(p^.ParsedValue)
else
p^.ParsedValue:=''; // not found
end;
end;
end;
Result:=p^.ParsedValue;
end;
procedure TPas2jsOptions.SetAtomTemplateDir(AValue: String);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoAtomTemplateDir,AValue);
end;
procedure TPas2jsOptions.SetDTS2Pas(AValue: String);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoDTSToPas,AValue);
end;
procedure TPas2jsOptions.SetDTS2PasService(AValue: String);
begin
SetCachedOption(p2jcoDTSToPasServiceURL,AValue);
end;
procedure TPas2jsOptions.SetElectronFileName(AValue: string);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoElectronFilename,AValue);
end;
procedure TPas2jsOptions.SetFPCExe(AValue: string);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoFPCExe,AValue);
end;
procedure TPas2jsOptions.SetFPCSrcDir(AValue: string);
begin
AValue:=AppendPathDelim(TrimFilename(AValue));
SetCachedOption(p2jcoFPCSrcDir,AValue);
end;
procedure TPas2jsOptions.SetPas2jsSrcDir(AValue: string);
begin
AValue:=AppendPathDelim(TrimFilename(AValue));
SetCachedOption(p2jcoPas2jsSrcDir,AValue);
end;
procedure TPas2jsOptions.SetVSCodeTemplateDir(AValue: String);
begin
AValue:=TrimFilename(AValue);
SetCachedOption(p2jcoVSCodeTemplateDir,AValue);
end;
procedure TPas2jsOptions.SetStartAtPort(AValue: Word);
begin
if FStartAtPort=AValue then Exit;
FStartAtPort:=AValue;
IncreaseChangeStamp;
end;
procedure TPas2jsOptions.SetCachedOption(Option: TPas2jsCachedOption;
const AValue: string);
begin
if FCachedOptions[Option].RawValue=AValue then exit;
FCachedOptions[Option].RawValue:=AValue;
FCachedOptions[Option].Stamp:=InvalidIDEMacroStamp;
IncreaseChangeStamp;
IDEMacros.IncreaseBaseStamp;
end;
Procedure DonePSJOptions;
begin
if PJSOptions<>nil then
begin
try
if PJSOptions.Modified then
PJSOptions.Save;
except
end;
FreeAndNil(PJSOptions);
end;
end;
finalization
DonePSJOptions;
end.
|