pas2jscompilercfg.pp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 Michael Van Canneyt
  4. Pascal to Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Abstract:
  12. Config file handling for compiler, depends on filesystem.
  13. }
  14. unit Pas2JSCompilerCfg;
  15. {$mode objfpc}{$H+}
  16. interface
  17. uses
  18. {$IFDEF NodeJS}
  19. node.fs,
  20. {$ENDIF}
  21. SysUtils, Pas2jsFileUtils, Pas2JSFS, Pas2jsCompiler;
  22. Type
  23. TPas2JSFileConfigSupport = Class(TPas2JSConfigSupport)
  24. function FindDefaultConfig: String; override;
  25. function GetReader(aFileName: string): TSourceLineReader; override;
  26. end;
  27. implementation
  28. function TPas2JSFileConfigSupport.GetReader(aFileName: string): TSourceLineReader;
  29. Var
  30. CacheFile: TPas2jsFile;
  31. begin
  32. CacheFile:=Compiler.FS.LoadFile(aFilename);
  33. Result:=CacheFile.CreateLineReader(true);
  34. end;
  35. Function TPas2JSFileConfigSupport.FindDefaultConfig : String;
  36. function TryConfig(aFilename: string): boolean;
  37. begin
  38. Result:=false;
  39. if aFilename='' then exit;
  40. aFilename:=ExpandFileName(aFilename);
  41. if Compiler.ShowDebug or Compiler.ShowTriedUsedFiles then
  42. Compiler.Log.LogMsgIgnoreFilter(nConfigFileSearch,[aFilename]);
  43. if not Compiler.FS.FileExists(aFilename) then exit;
  44. FindDefaultConfig:=aFilename;
  45. Result:=true;
  46. end;
  47. var
  48. aFilename: String;
  49. begin
  50. Result:='';
  51. // first try HOME directory
  52. aFilename:=ChompPathDelim(GetEnvironmentVariablePJ('HOME'));
  53. if aFilename<>'' then
  54. begin
  55. aFilename:=aFilename+PathDelim{$IFDEF UNIX}+'.'{$ENDIF}+DefaultConfigFile;
  56. if TryConfig(aFileName) then
  57. exit;
  58. end;
  59. // then try compiler directory
  60. if (Compiler.CompilerExe<>'') then
  61. begin
  62. aFilename:=ExtractFilePath(Compiler.CompilerExe);
  63. if aFilename<>'' then
  64. begin
  65. aFilename:=IncludeTrailingPathDelimiter(aFilename)+DefaultConfigFile;
  66. if TryConfig(aFilename) then
  67. exit;
  68. end;
  69. end;
  70. // finally try global directory
  71. {$IFDEF Unix}
  72. if TryConfig('/etc/'+DefaultConfigFile) then
  73. exit;
  74. {$ENDIF}
  75. end;
  76. end.