pas2js.pp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. { Author: Mattias Gaertner 2018 [email protected]
  2. Abstract:
  3. Command line interface for the pas2js compiler.
  4. }
  5. program pas2js;
  6. {$mode objfpc}{$H+}
  7. uses
  8. {$IFDEF UNIX}
  9. cthreads, cwstring,
  10. {$ENDIF}
  11. Classes, SysUtils, CustApp,
  12. Pas2jsFileUtils, Pas2jsLogger, Pas2jsCompiler,
  13. Pas2JSFSCompiler, Pas2JSCompilerPP, Pas2JSCompilerCfg;
  14. Type
  15. { TPas2jsCLI }
  16. TPas2jsCLI = class(TCustomApplication)
  17. private
  18. FCompiler: TPas2JSFSCompiler;
  19. FWriteOutputToStdErr: Boolean;
  20. protected
  21. procedure DoRun; override;
  22. public
  23. constructor Create(TheOwner: TComponent); override;
  24. destructor Destroy; override;
  25. property Compiler: TPas2JSFSCompiler read FCompiler;
  26. property WriteOutputToStdErr: Boolean read FWriteOutputToStdErr write FWriteOutputToStdErr;
  27. end;
  28. procedure TPas2jsCLI.DoRun;
  29. var
  30. ParamList: TStringList;
  31. i: Integer;
  32. begin
  33. ParamList:=TStringList.Create;
  34. try
  35. for i:=1 to ParamCount do
  36. ParamList.Add(Params[i]);
  37. try
  38. Compiler.Run(ParamStr(0),GetCurrentDirPJ,ParamList);
  39. except
  40. on E: ECompilerTerminate do ;
  41. on E: Exception do
  42. begin
  43. {AllowWriteln}
  44. writeln('Error: Unhandled exception '+E.ClassName+': '+E.Message);
  45. {AllowWriteln-}
  46. if ExitCode=0 then
  47. ExitCode:=ExitCodeErrorInternal;
  48. end;
  49. end;
  50. finally
  51. ParamList.Free;
  52. Compiler.Log.CloseOutputFile;
  53. end;
  54. // stop program loop
  55. Terminate; // Keep ExitCode!
  56. end;
  57. constructor TPas2jsCLI.Create(TheOwner: TComponent);
  58. begin
  59. inherited Create(TheOwner);
  60. StopOnException:=True;
  61. FCompiler:=TPas2JSFSCompiler.Create;
  62. FCompiler.ConfigSupport:=TPas2JSFileConfigSupport.Create(FCompiler);
  63. FCompiler.PostProcessorSupport:=TPas2JSFSPostProcessorSupport.Create(FCompiler);
  64. end;
  65. destructor TPas2jsCLI.Destroy;
  66. begin
  67. FreeAndNil(FCompiler);
  68. inherited Destroy;
  69. end;
  70. var
  71. Application: TPas2jsCLI;
  72. begin
  73. Application:=TPas2jsCLI.Create(nil);
  74. Application.Run;
  75. Application.Free;
  76. end.