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
|
unit pjscontroller;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, process,
// LazUtils
LazLoggerBase, LazUtilities,
// LCL
Forms, Controls,
// IdeIntf
MacroIntf, MacroDefIntf, LazIDEIntf;
Type
{ TServerInstance }
TServerInstance = Class(TCollectionItem)
private
FlastProject: String;
FPort: Word;
FProcess: TProcess;
FRunError: String;
FServerName: String;
FString: String;
function GetRunning: Boolean;
Protected
Property Process : TProcess Read FProcess;
Public
Destructor Destroy; override;
Procedure StartServer;
Procedure StopServer;
Property Port : Word Read FPort Write FPort;
Property BaseDir : String Read FString Write FString;
Property ServerName : String Read FServerName Write FServerName;
Property Running : Boolean Read GetRunning;
Property RunError : String Read FRunError;
Property LastProject : String Read FlastProject Write Flastproject;
end;
{ TServerInstanceList }
TServerInstanceList = Class(TCollection)
private
function GetInstance(AIndex : Integer): TServerInstance;
Public
Function IndexOfPort(APort: Word) : integer;
Function FindByPort(Aindex : Integer) : TServerInstance;
Function AddInstance(aPort : Word; Const ABaseURL, aServerName : String) : TServerInstance;
Property Instances [AIndex : Integer] : TServerInstance Read GetInstance; default;
end;
{ TPJSController }
TPJSController = Class
Private
FOnRefresh: TNotifyEvent;
FServerInstances: TServerInstanceList;
function GetPas2JSPath(const s: string; const {%H-}Data: PtrInt; var Abort: boolean): string;
function GetPas2JSBrowser(const s: string; const {%H-}Data: PtrInt; var Abort: boolean): string;
function GetPas2JSNodeJS(const s: string; const {%H-}Data: PtrInt; var Abort: boolean): string;
function GetPas2jsProjectURL(const s: string; const {%H-}Data: PtrInt; var Abort: boolean): string;
function MaybeStartServer(Sender: TObject; var Handled: boolean): TModalResult;
Public
Constructor Create;
Destructor Destroy; override;
Class Procedure DoneInstance;
Class Function instance : TPJSController;
Procedure Hook; virtual;
Procedure UnHook; virtual;
Procedure RefreshView;
Property ServerInstances : TServerInstanceList Read FServerInstances;
Property OnRefresh : TNotifyEvent Read FOnRefresh Write FonRefresh;
end;
Const
// Custom settings in .lpi
PJSProjectWebBrowser = 'PasJSWebBrowserProject';
PJSProjectHTMLFile = 'PasJSHTMLFile';
PJSIsProjectHTMLFile = 'PasJSIsProjectHTMLFile';
PJSProjectMaintainHTML = 'MaintainHTML';
PJSProjectUseBrowserConsole = 'BrowserConsole';
PJSProjectRunAtReady = 'RunAtReady';
PJSProjectPort = 'PasJSPort';
PJSProjectURL = 'PasJSURL';
implementation
uses FileUtil, LazFileUtils, PJSDsgnOptions;
Var
ctrl : TPJSController;
{ TServerInstanceList }
function TServerInstanceList.GetInstance(AIndex : Integer): TServerInstance;
begin
Result:=Items[AIndex] as TServerInstance;
end;
function TServerInstanceList.IndexOfPort(APort: Word): integer;
begin
Result:=Count-1;
While (Result>=0) and (GetInstance(Result).Port<>APort) do Dec(Result);
end;
function TServerInstanceList.FindByPort(Aindex: Integer): TServerInstance;
Var
I : Integer;
begin
I:=IndexOfPort(Aindex);
If I=-1 then
Result:=nil
else
Result:=GetInstance(I);
end;
function TServerInstanceList.AddInstance(aPort: Word; const ABaseURL,
aServerName: String): TServerInstance;
begin
Result:=Add as TServerInstance;
Result.Port:=aPort;
Result.BaseDir:=ABaseURL;
Result.ServerName:=aServerName;
end;
{ TServerInstance }
function TServerInstance.GetRunning: Boolean;
begin
Result:=Assigned(FProcess);
if Result then
Result:=Process.Running;
end;
destructor TServerInstance.Destroy;
begin
StopServer;
FreeAndNil(FProcess);
inherited;
end;
procedure TServerInstance.StartServer;
begin
if Running then
exit;
If not Assigned(FProcess) then
FProcess:=TProcess.Create(Nil);
FProcess.Executable:=ServerName;
FProcess.Parameters.Add('-q');
FProcess.Parameters.Add('-p');
FProcess.Parameters.Add(IntToStr(Port));
{$IFDEF WINDOWS}
FProcess.Options:=[poNoConsole];
{$ENDIF}
if ConsoleVerbosity>=0 then
DebugLN(['Starting server from Directory : ',BaseDir]);
FProcess.CurrentDirectory:=BaseDir;
try
FProcess.Execute;
except
On E : Exception do
begin
FRunError:=E.Message;
Raise;
end;
end;
TPJSController.Instance.RefreshView;
end;
procedure TServerInstance.StopServer;
begin
if Running then
FProcess.Terminate(0);
TPJSController.Instance.RefreshView;
end;
class procedure TPJSController.DoneInstance;
begin
FreeAndNil(Ctrl)
end;
class function TPJSController.instance: TPJSController;
begin
if ctrl=Nil then
Ctrl:=TPJSController.Create;
Result:=Ctrl;
end;
{ TPJSController }
function TPJSController.GetPas2JSPath(const s: string; const Data: PtrInt;
var Abort: boolean): string;
begin
Abort:=False;
if (s<>'') and (ConsoleVerbosity>=0) then
debugln(['Hint: (lazarus) [TPJSController.GetPas2JSPath] ignoring macro Pas2JS parameter "',s,'"']);
Result:=PJSOptions.GetParsedCompilerFilename;
end;
function TPJSController.GetPas2JSBrowser(const s: string; const Data: PtrInt; var Abort: boolean): string;
begin
Abort:=False;
if (s<>'') and (ConsoleVerbosity>=0) then
debugln(['Hint: (lazarus) [TPJSController.GetPas2JSBrowser] ignoring macro Pas2JSBrowser parameter "',s,'"']);
Result:=PJSOptions.GetParsedBrowserFilename;
end;
function TPJSController.GetPas2JSNodeJS(const s: string; const Data: PtrInt; var Abort: boolean): string;
begin
Abort:=False;
if (s<>'') and (ConsoleVerbosity>=0) then
debugln(['Hint: (lazarus) [TPJSController.GetPas2JSNodeJS] ignoring macro Pas2JSNodeJS parameter "',s,'"']);
Result:=PJSOptions.GetParsedNodeJSFilename;
end;
function TPJSController.GetPas2jsProjectURL(const s: string; const Data: PtrInt; var Abort: boolean): string;
Var
FN : String;
begin
if (s<>'') and (ConsoleVerbosity>=0) then
debugln(['Hint: (lazarus) [TPJSController.GetPas2jsProjectURL] ignoring macro Pas2JSProjectURL parameter "',s,'"']);
if ConsoleVerbosity>0 then
DebugLN(['LazarusIDE.ActiveProject.CustomData[PJSProjectWebBrowser]: ',LazarusIDE.ActiveProject.CustomData[PJSProjectWebBrowser]]);
Abort:=LazarusIDE.ActiveProject.CustomData[PJSProjectWebBrowser]<>'1';
if Abort then
exit;
if ConsoleVerbosity>0 then
DebugLN(['LazarusIDE.ActiveProject.CustomData[PJSProjectURL]: ',LazarusIDE.ActiveProject.CustomData[PJSProjectURL]]);
Result:=LazarusIDE.ActiveProject.CustomData[PJSProjectURL];
if (Result='') then
begin
FN:=LazarusIDE.ActiveProject.CustomData[PJSProjectHTMLFile];
if ConsoleVerbosity>0 then
DebugLN(['LazarusIDE.ActiveProject.CustomData[PJSProjectHTMLFile]: ',LazarusIDE.ActiveProject.CustomData[PJSProjectHTMLFile]]);
if (FN='') then
FN:=ChangeFileExt(ExtractFileName(LazarusIDE.ActiveProject.ProjectInfoFile),'.html');
Result:=LazarusIDE.ActiveProject.CustomData[PJSProjectPort];
if (Result<>'') and (Result<>'0') then
Result:=Format('http://localhost:%s/%s',[Result,FN])
else
{$IFDEF WINDOWS}
Result:=Format('file:///%s',[ExtractFilePath(LazarusIDE.ActiveProject.ProjectInfoFile)+FN]);
{$ELSE}
Result:=Format('file://%s',[ExtractFilePath(LazarusIDE.ActiveProject.ProjectInfoFile)+FN]);
{$ENDIF}
end;
Abort:=(Result='');
if ConsoleVerbosity>0 then
DebugLN(['GetPas2jsProjectURL : ',Result]);
end;
function TPJSController.MaybeStartServer(Sender: TObject; var Handled: boolean): TModalResult;
Var
ServerPort : Word;
WebProject : Boolean;
BaseDir : String;
aInstance : TServerInstance;
begin
Result:=mrOK;
With LazarusIDE.ActiveProject do
begin
if ConsoleVerbosity>=0 then
begin
DebugLn(['WebProject=',CustomData[PJSProjectWebBrowser]]);
DebugLn(['ServerPort=',CustomData[PJSProjectPort]]);
DebugLn(['BaseDir=',ProjectInfoFile]);
end;
WebProject:=CustomData[PJSProjectWebBrowser]='1';
ServerPort:=StrToIntDef(CustomData[PJSProjectPort],0);
BaseDir:=ExtractFilePath(ProjectInfoFile);
end;
// Exit if we don't need to do anything
if Not (WebProject and (ServerPort>0)) then
Exit;
aInstance:=ServerInstances.FindByPort(ServerPort);
If Ainstance<>Nil then
begin
if ConsoleVerbosity>=0 then
Writeln('Have instance running on port ',ServerPort);
if Not SameFileName(BaseDir,aInstance.BaseDir) then
begin
if ConsoleVerbosity>=0 then
Writeln('Instance on port ',ServerPort,' serves different directory: ',aInstance.BaseDir);
// We should ask the user what to do ?
If aInstance.Running then
aInstance.StopServer;
end;
end
else
begin
// Writeln('No instance running on port ',ServerPort, 'allocating it');
aInstance:=ServerInstances.AddInstance(ServerPort,BaseDir,PJSOptions.GetParsedHTTPServerFilename);
end;
aInstance.LastProject:=LazarusIDE.ActiveProject.ProjectInfoFile;
aInstance.StartServer;
Handled:=False;
end;
constructor TPJSController.Create;
begin
// Nothing for the moment
FServerInstances:=TServerInstanceList.Create(TServerInstance);
end;
destructor TPJSController.Destroy;
begin
Unhook;
FreeAndNil(FServerInstances);
inherited Destroy;
end;
procedure TPJSController.Hook;
begin
IDEMacros.Add(TTransferMacro.Create('Pas2JS','','Pas2JS executable',@GetPas2JSPath,[]));
IDEMacros.Add(TTransferMacro.Create('Pas2JSBrowser','','Pas2JS selected Browser executable',@GetPas2JSBrowser,[]));
IDEMacros.Add(TTransferMacro.Create('Pas2JSNodeJS','','Pas2JS selected NodeJS excutable',@GetPas2JSNodeJS,[]));
IDEMacros.Add(TTransferMacro.Create('Pas2JSProjectURL','','Pas2JS current project URL',@GetPas2jsProjectURL,[]));
LazarusIDE.AddHandlerOnRunWithoutDebugInit(@MaybeStartServer);
end;
procedure TPJSController.UnHook;
begin
// Nothing for the moment
end;
procedure TPJSController.RefreshView;
begin
If Assigned(FOnRefresh) then
FOnRefresh(Self);
end;
finalization
TPJSController.DoneInstance;
end.
|