brookideintf.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. (*
  2. Brook for Free Pascal
  3. Copyright (C) 2014-2019 Silvio Clecio
  4. See the file LICENSE.txt, included in this distribution,
  5. for details about the copyright.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. *)
  10. { IDE intf unit. }
  11. unit BrookIDEIntf;
  12. {$i brook.inc}
  13. interface
  14. uses
  15. BrookClasses, frmBrookChooseProjectType, frmBrookNewProject, frmBrookNewBroker,
  16. frmBrookActEdit, ProjectIntf, NewItemIntf, LazIDEIntf, ProjectResourcesIntf,
  17. FormEditingIntf, Classes, SysUtils, Controls, ComCtrls, Forms, Dialogs;
  18. type
  19. TBrookBrokersFileDescPascalUnit = class;
  20. { TBrookCustomSimpleCGIProjectDescriptor }
  21. TBrookCustomSimpleCGIProjectDescriptor = class(TProjectDescriptor)
  22. private
  23. FProjectType: Integer;
  24. protected
  25. function DoInitDescriptor: TModalResult; override;
  26. public
  27. constructor Create; override;
  28. function CreateStartFiles(AProject: TLazProject): TModalResult; override;
  29. function InitProject(AProject: TLazProject): TModalResult; override;
  30. function GetLocalizedName: string; override;
  31. function GetLocalizedDescription: string; override;
  32. property ProjectType: Integer read FProjectType write FProjectType; // 0 - runtime, 1 - designtime
  33. end;
  34. { TBrookSimpleCGIProjectDescriptor }
  35. TBrookSimpleCGIProjectDescriptor = class(TBrookCustomSimpleCGIProjectDescriptor)
  36. protected
  37. procedure ConfigureBrokerItem(AItem: TBrookBrokersFileDescPascalUnit); virtual;
  38. procedure CreateProjectFile(AProject: TLazProject); virtual;
  39. public
  40. constructor Create; override;
  41. function InitProject(AProject: TLazProject): TModalResult; override;
  42. function CreateStartFiles(AProject: TLazProject): TModalResult; override;
  43. function GetLocalizedName: string; override;
  44. function GetLocalizedDescription: string; override;
  45. end;
  46. { TBrookSimpleFastCGIProjectDescriptor }
  47. TBrookSimpleFastCGIProjectDescriptor = class(TBrookSimpleCGIProjectDescriptor)
  48. protected
  49. procedure ConfigureBrokerItem(AItem: TBrookBrokersFileDescPascalUnit); override;
  50. public
  51. constructor Create; override;
  52. function InitProject(AProject: TLazProject): TModalResult; override;
  53. function GetLocalizedName: string; override;
  54. function GetLocalizedDescription: string; override;
  55. end;
  56. { TBrookHttpAppProjectDescriptor }
  57. TBrookHttpAppProjectDescriptor = class(TBrookSimpleCGIProjectDescriptor)
  58. protected
  59. procedure ConfigureBrokerItem(AItem: TBrookBrokersFileDescPascalUnit); override;
  60. procedure CreateProjectFile(AProject: TLazProject); override;
  61. public
  62. constructor Create; override;
  63. function InitProject(AProject: TLazProject): TModalResult; override;
  64. function GetLocalizedName: string; override;
  65. function GetLocalizedDescription: string; override;
  66. end;
  67. { TBrookHttpDaemonProjectDescriptor }
  68. TBrookHttpDaemonProjectDescriptor = class(TBrookHttpAppProjectDescriptor)
  69. protected
  70. procedure ConfigureBrokerItem(AItem: TBrookBrokersFileDescPascalUnit); override;
  71. public
  72. constructor Create; override;
  73. function InitProject(AProject: TLazProject): TModalResult; override;
  74. function GetLocalizedName: string; override;
  75. function GetLocalizedDescription: string; override;
  76. end;
  77. { TBrookProjectDescriptor }
  78. TBrookProjectDescriptor = class(TProjectDescriptor)
  79. private
  80. FProjectType: Integer;
  81. protected
  82. function DoInitDescriptor: TModalResult; override;
  83. public
  84. constructor Create; override;
  85. function CreateStartFiles(AProject: TLazProject): TModalResult; override;
  86. function InitProject(AProject: TLazProject): TModalResult; override;
  87. function GetLocalizedName: string; override;
  88. function GetLocalizedDescription: string; override;
  89. property ProjectType: Integer read FProjectType write FProjectType;
  90. end;
  91. { TBrookFileDescPascalUnit }
  92. TBrookFileDescPascalUnit = class(TFileDescPascalUnit)
  93. private
  94. FQuiet: Boolean;
  95. end;
  96. { TBrookFileDescPascalUnitWithResource }
  97. TBrookFileDescPascalUnitWithResource = class(TFileDescPascalUnitWithResource)
  98. protected
  99. function GetResourceType: TResourceType; override;
  100. end;
  101. { TBrookBrokersFileDescPascalUnit }
  102. TBrookBrokersFileDescPascalUnit = class(TBrookFileDescPascalUnit)
  103. private
  104. FAppType: Integer;
  105. FAppDefCharset: Integer;
  106. FFullBrk: Boolean;
  107. public
  108. constructor Create; override;
  109. function Init(var ANewFilename: string; ANewOwner: TObject;
  110. var ANewSource: string; AQuiet: Boolean): TModalResult; override;
  111. function CreateSource(const AFileName, ASourceName,
  112. AResourceName: string): string; override;
  113. function GetLocalizedName: string; override;
  114. function GetLocalizedDescription: string; override;
  115. end;
  116. { TBrookActionFileDescPascalUnit }
  117. TBrookActionFileDescPascalUnit = class(TBrookFileDescPascalUnit)
  118. private
  119. FActName: string;
  120. FActPattern: string;
  121. FActDefault: Boolean;
  122. public
  123. constructor Create; override;
  124. function Init(var ANewFilename: string; ANewOwner: TObject;
  125. var ANewSource: string; AQuiet: Boolean): TModalResult; override;
  126. function CreateSource(const AFileName, ASourceName,
  127. AResourceName: string): string; override;
  128. function GetLocalizedName: string; override;
  129. function GetLocalizedDescription: string; override;
  130. property ActName: string read FActName write FActName;
  131. property ActPattern: string read FActPattern write FActPattern;
  132. property ActDefault: Boolean read FActDefault write FActDefault;
  133. end;
  134. { TBrookDataModuleFileDescPascalUnitWithResource }
  135. TBrookDataModuleFileDescPascalUnitWithResource = class(TBrookFileDescPascalUnitWithResource)
  136. public
  137. constructor Create; override;
  138. function GetInterfaceUsesSection: string; override;
  139. function GetLocalizedName: string; override;
  140. function GetLocalizedDescription: string; override;
  141. end;
  142. const
  143. le = LineEnding;
  144. resourcestring
  145. SBrookIDEItemCategoryName = 'Brook for Free Pascal';
  146. SBrookAppName = 'Full CGI/FastCGI Application';
  147. SBrookAppDesc = 'Create a full CGI or FastCGI application.';
  148. SBrookHttpAppName = 'Embedded server';
  149. SBrookHttpAppDesc = 'Create an embedded HTTP webserver.';
  150. SBrookHttpDaemonName = 'Embedded daemon server';
  151. SBrookHttpDaemonDesc = 'Create an embedded daemon HTTP webserver.';
  152. SBrookSimpleCGIAppName = 'Simple CGI application';
  153. SBrookSimpleCGIAppDesc = 'Create a simple CGI application.';
  154. SBrookSimpleFastCGIAppName = 'Simple FastCGI application';
  155. SBrookSimpleFastCGIAppDesc = 'Create a simple FastCGI application.';
  156. SBrookBrokersName = 'Brokers unit';
  157. SBrookBrokersDesc = 'Create a brokers unit.';
  158. SBrookActionName = 'Action unit';
  159. SBrookActionDesc = 'Create an action unit.';
  160. SBrookDataModuleName = 'Data module';
  161. SBrookDataModuleDesc = 'Create a new unit with a data module.';
  162. procedure Register;
  163. function BrookNewProjectDlg(const AProjectType: Integer): TfrBrookNewProject;
  164. function BrookGetExpertsConfigPath: string;
  165. function BrookGetExpertsConfigFileName: string;
  166. implementation
  167. var
  168. _NewProjectDlg: TfrBrookNewProject;
  169. const
  170. PAGE_404_TPL =
  171. '<html><head><title>Page not found</title><style>body{margin:0;'+
  172. 'padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{mar'+
  173. 'gin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{disp'+
  174. 'lay:inline-block;width:65px;}</style></head><body><h1>404 - Page not '+
  175. 'found</h1><br />Go to <a href="@path">home page</a> ...'+
  176. '</body></html>';
  177. PAGE_500_TPL =
  178. '<html><head><title>Internal server error</title><style>body{margin:0;'+
  179. 'padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{mar'+
  180. 'gin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{disp'+
  181. 'lay:inline-block;width:65px;}</style></head><body><h1>500 - Internal '+
  182. 'server error</h1><br />@error'+
  183. '</body></html>';
  184. procedure Register;
  185. begin
  186. RegisterNewItemCategory(TNewIDEItemCategory.Create(SBrookIDEItemCategoryName));
  187. RegisterProjectDescriptor(TBrookSimpleCGIProjectDescriptor.Create,
  188. SBrookIDEItemCategoryName);
  189. RegisterProjectDescriptor(TBrookSimpleFastCGIProjectDescriptor.Create,
  190. SBrookIDEItemCategoryName);
  191. RegisterProjectDescriptor(TBrookHttpAppProjectDescriptor.Create,
  192. SBrookIDEItemCategoryName);
  193. RegisterProjectDescriptor(TBrookHttpDaemonProjectDescriptor.Create,
  194. SBrookIDEItemCategoryName);
  195. RegisterProjectDescriptor(TBrookProjectDescriptor.Create,
  196. SBrookIDEItemCategoryName);
  197. RegisterProjectFileDescriptor(TBrookDataModuleFileDescPascalUnitWithResource.Create,
  198. SBrookIDEItemCategoryName);
  199. RegisterProjectFileDescriptor(TBrookActionFileDescPascalUnit.Create,
  200. SBrookIDEItemCategoryName);
  201. RegisterProjectFileDescriptor(TBrookBrokersFileDescPascalUnit.Create,
  202. SBrookIDEItemCategoryName);
  203. FormEditingHook.RegisterDesignerBaseClass(TBrookDataModule);
  204. end;
  205. function BrookNewProjectDlg(const AProjectType: Integer): TfrBrookNewProject;
  206. begin
  207. if not Assigned(_NewProjectDlg) then
  208. _NewProjectDlg := TfrBrookNewProject.Create(Application, AProjectType);
  209. Result := _NewProjectDlg;
  210. end;
  211. procedure BrookFreeNewProjectDlg;
  212. begin
  213. FreeAndNil(_NewProjectDlg);
  214. end;
  215. function BrookGetExpertsConfigPath: string;
  216. begin
  217. if Assigned(LazarusIDE) then
  218. Result := IncludeTrailingPathDelimiter(
  219. {$IFDEF MSWINDOWS}Utf8ToAnsi({$ENDIF}LazarusIDE.GetPrimaryConfigPath
  220. {$IFDEF MSWINDOWS}){$ENDIF})
  221. else
  222. Result := '';
  223. end;
  224. function BrookGetExpertsConfigFileName: string;
  225. begin
  226. Result := BrookGetExpertsConfigPath + 'brook.xml';
  227. end;
  228. { TBrookCustomSimpleCGIProjectDescriptor }
  229. constructor TBrookCustomSimpleCGIProjectDescriptor.Create;
  230. begin
  231. inherited Create;
  232. Name := '';
  233. end;
  234. {$PUSH}{$WARN 5024 OFF}
  235. function TBrookCustomSimpleCGIProjectDescriptor.CreateStartFiles(
  236. AProject: TLazProject): TModalResult;
  237. var
  238. VActItem: TBrookActionFileDescPascalUnit;
  239. begin
  240. case FProjectType of
  241. 0:
  242. begin
  243. VActItem := ProjectFileDescriptors.FindByName(
  244. SBrookActionName) as TBrookActionFileDescPascalUnit;
  245. VActItem.FQuiet := True;
  246. VActItem.ActName := 'MyAction';
  247. VActItem.ActPattern := '*';
  248. VActItem.ActDefault := False;
  249. LazarusIDE.DoNewEditorFile(VActItem, '', '',
  250. [nfIsPartOfProject, nfOpenInEditor, nfCreateDefaultSrc]);
  251. end;
  252. 1:
  253. LazarusIDE.DoNewEditorFile(ProjectFileDescriptors.FindByName(
  254. SBrookDataModuleName), '', '', [nfIsPartOfProject, nfOpenInEditor,
  255. nfCreateDefaultSrc]);
  256. end;
  257. Result := mrOK;
  258. end;
  259. {$POP}
  260. function TBrookCustomSimpleCGIProjectDescriptor.DoInitDescriptor: TModalResult;
  261. begin
  262. FProjectType := TfrBrookChooseProjectType.Execute;
  263. if FProjectType > -1 then
  264. Result := mrOK
  265. else
  266. Result := mrCancel;
  267. end;
  268. function TBrookCustomSimpleCGIProjectDescriptor.InitProject(
  269. AProject: TLazProject): TModalResult;
  270. begin
  271. AProject.AddPackageDependency('BrookRT');
  272. AProject.Flags := AProject.Flags - [pfRunnable];
  273. if FProjectType = 0 then
  274. AProject.Flags := AProject.Flags - [pfMainUnitHasCreateFormStatements];
  275. AProject.LazCompilerOptions.Win32GraphicApp := False;
  276. AProject.LazCompilerOptions.TargetFilenameApplyConventions := False;
  277. AProject.SessionStorage := pssInProjectInfo;
  278. AProject.MainFileID := 0;
  279. AProject.Title := '';
  280. Result := mrOK;
  281. end;
  282. function TBrookCustomSimpleCGIProjectDescriptor.GetLocalizedName: string;
  283. begin
  284. Result := '';
  285. end;
  286. function TBrookCustomSimpleCGIProjectDescriptor.GetLocalizedDescription: string;
  287. begin
  288. Result := '';
  289. end;
  290. { TBrookSimpleCGIProjectDescriptor }
  291. constructor TBrookSimpleCGIProjectDescriptor.Create;
  292. begin
  293. inherited Create;
  294. Name := SBrookSimpleCGIAppName;
  295. end;
  296. function TBrookSimpleCGIProjectDescriptor.InitProject(
  297. AProject: TLazProject): TModalResult;
  298. var
  299. VApp: string;
  300. begin
  301. CreateProjectFile(AProject);
  302. Result := inherited InitProject(AProject);
  303. case FProjectType of
  304. 0: VApp := 'BrookApp.Run';
  305. 1: VApp := 'Application.Run';
  306. end;
  307. AProject.MainFile.SetSourceText(
  308. 'program cgi1;'+le+
  309. le+
  310. '{$mode objfpc}{$H+}'+le+
  311. le+
  312. 'uses'+le+
  313. '{$IFDEF UNIX}{$IFDEF UseCThreads}'+le+
  314. ' cthreads,'+le+
  315. '{$ENDIF}{$ENDIF}'+le+
  316. ' BrookApplication, Brokers;'+le+
  317. le+
  318. 'begin'+le+
  319. ' '+VApp+';'+le+
  320. 'end.');
  321. AProject.LazCompilerOptions.TargetFileName := 'cgi1.bf';
  322. end;
  323. function TBrookSimpleCGIProjectDescriptor.CreateStartFiles(
  324. AProject: TLazProject): TModalResult;
  325. var
  326. VBrkItem: TBrookBrokersFileDescPascalUnit;
  327. begin
  328. Result := inherited CreateStartFiles(AProject);
  329. VBrkItem := ProjectFileDescriptors.FindByName(
  330. SBrookBrokersName) as TBrookBrokersFileDescPascalUnit;
  331. ConfigureBrokerItem(VBrkItem);
  332. LazarusIDE.DoNewEditorFile(VBrkItem, '', '',
  333. [nfIsPartOfProject, nfOpenInEditor, nfCreateDefaultSrc]);
  334. end;
  335. procedure TBrookSimpleCGIProjectDescriptor.ConfigureBrokerItem(
  336. AItem: TBrookBrokersFileDescPascalUnit);
  337. begin
  338. AItem.FQuiet := True;
  339. AItem.FFullBrk := False;
  340. AItem.FAppType := 0;
  341. AItem.FAppDefCharset := 0;
  342. end;
  343. procedure TBrookSimpleCGIProjectDescriptor.CreateProjectFile(
  344. AProject: TLazProject);
  345. var
  346. VProject: TLazProjectFile;
  347. begin
  348. VProject := AProject.CreateProjectFile('cgi1.lpr');
  349. VProject.IsPartOfProject := True;
  350. AProject.AddFile(VProject, False);
  351. end;
  352. function TBrookSimpleCGIProjectDescriptor.GetLocalizedName: string;
  353. begin
  354. Result := SBrookSimpleCGIAppName;
  355. end;
  356. function TBrookSimpleCGIProjectDescriptor.GetLocalizedDescription: string;
  357. begin
  358. Result := SBrookSimpleCGIAppDesc;
  359. end;
  360. { TBrookSimpleFastCGIProjectDescriptor }
  361. constructor TBrookSimpleFastCGIProjectDescriptor.Create;
  362. begin
  363. inherited Create;
  364. Name := SBrookSimpleFastCGIAppName;
  365. end;
  366. function TBrookSimpleFastCGIProjectDescriptor.InitProject(
  367. AProject: TLazProject): TModalResult;
  368. begin
  369. Result := inherited InitProject(AProject);
  370. AProject.Flags := AProject.Flags + [pfRunnable];
  371. AProject.LazCompilerOptions.TargetFileName := 'cgi1.fbf';
  372. end;
  373. procedure TBrookSimpleFastCGIProjectDescriptor.ConfigureBrokerItem(
  374. AItem: TBrookBrokersFileDescPascalUnit);
  375. begin
  376. inherited;
  377. AItem.FAppType := 1;
  378. end;
  379. function TBrookSimpleFastCGIProjectDescriptor.GetLocalizedName: string;
  380. begin
  381. Result := SBrookSimpleFastCGIAppName;
  382. end;
  383. function TBrookSimpleFastCGIProjectDescriptor.GetLocalizedDescription: string;
  384. begin
  385. Result := SBrookSimpleFastCGIAppDesc;
  386. end;
  387. { TBrookHttpAppProjectDescriptor }
  388. constructor TBrookHttpAppProjectDescriptor.Create;
  389. begin
  390. inherited Create;
  391. Name := SBrookHttpAppName;
  392. end;
  393. function TBrookHttpAppProjectDescriptor.InitProject(
  394. AProject: TLazProject): TModalResult;
  395. var
  396. VApp: string;
  397. begin
  398. Result := inherited InitProject(AProject);
  399. AProject.Flags := AProject.Flags + [pfRunnable];
  400. AProject.LazCompilerOptions.TargetFilenameApplyConventions := True;
  401. case FProjectType of
  402. 0: VApp := 'BrookApp.Run';
  403. 1: VApp := 'Application.Run';
  404. end;
  405. AProject.MainFile.SetSourceText(
  406. 'program project1;'+le+
  407. le+
  408. '{$mode objfpc}{$H+}'+le+
  409. le+
  410. 'uses'+le+
  411. '{$IFDEF UNIX}{$IFDEF UseCThreads}'+le+
  412. ' cthreads,'+le+
  413. '{$ENDIF}{$ENDIF}'+le+
  414. ' BrookApplication, Brokers;'+le+
  415. le+
  416. 'begin'+le+
  417. ' '+VApp+';'+le+
  418. 'end.');
  419. AProject.LazCompilerOptions.TargetFileName := 'project1';
  420. end;
  421. procedure TBrookHttpAppProjectDescriptor.ConfigureBrokerItem(
  422. AItem: TBrookBrokersFileDescPascalUnit);
  423. begin
  424. inherited;
  425. AItem.FAppType := 2;
  426. end;
  427. procedure TBrookHttpAppProjectDescriptor.CreateProjectFile(
  428. AProject: TLazProject);
  429. var
  430. VProject: TLazProjectFile;
  431. begin
  432. VProject := AProject.CreateProjectFile('project1.lpr');
  433. VProject.IsPartOfProject := True;
  434. AProject.AddFile(VProject, False);
  435. end;
  436. function TBrookHttpAppProjectDescriptor.GetLocalizedName: string;
  437. begin
  438. Result := SBrookHttpAppName;
  439. end;
  440. function TBrookHttpAppProjectDescriptor.GetLocalizedDescription: string;
  441. begin
  442. Result := SBrookHttpAppDesc;
  443. end;
  444. { TBrookHttpDaemonProjectDescriptor }
  445. constructor TBrookHttpDaemonProjectDescriptor.Create;
  446. begin
  447. inherited Create;
  448. Name := SBrookHttpDaemonName;
  449. end;
  450. function TBrookHttpDaemonProjectDescriptor.InitProject(
  451. AProject: TLazProject): TModalResult;
  452. begin
  453. Result := inherited InitProject(AProject);
  454. AProject.LazCompilerOptions.Win32GraphicApp := True;
  455. end;
  456. procedure TBrookHttpDaemonProjectDescriptor.ConfigureBrokerItem(
  457. AItem: TBrookBrokersFileDescPascalUnit);
  458. begin
  459. inherited;
  460. AItem.FAppType := 3;
  461. end;
  462. function TBrookHttpDaemonProjectDescriptor.GetLocalizedName: string;
  463. begin
  464. Result := SBrookHttpDaemonName;
  465. end;
  466. function TBrookHttpDaemonProjectDescriptor.GetLocalizedDescription: string;
  467. begin
  468. Result := SBrookHttpDaemonDesc;
  469. end;
  470. { TBrookProjectDescriptor }
  471. constructor TBrookProjectDescriptor.Create;
  472. begin
  473. inherited Create;
  474. Name := SBrookAppName;
  475. end;
  476. function TBrookProjectDescriptor.CreateStartFiles(
  477. AProject: TLazProject): TModalResult;
  478. procedure CreateHTMLFile(const F, S: string);
  479. begin
  480. if FileExists(F) and (MessageDlg('Confirmation', Format('Replace file "%s"?',
  481. [F]), mtConfirmation, mbYesNo, 0) <> mrYes) then
  482. Exit;
  483. with TFileStream.Create(F, fmCreate) do
  484. try
  485. Write(Pointer(S)^, Length(S));
  486. finally
  487. Free;
  488. end;
  489. end;
  490. var
  491. I: Integer;
  492. VItem: TListItem;
  493. VHTMLsDir: string;
  494. VDlg: TfrBrookNewProject;
  495. VPage404, VPage500: TFileName;
  496. VActItem: TBrookActionFileDescPascalUnit;
  497. VBrkItem: TBrookBrokersFileDescPascalUnit;
  498. begin
  499. VDlg := BrookNewProjectDlg(FProjectType);
  500. VActItem := ProjectFileDescriptors.FindByName(SBrookActionName) as
  501. TBrookActionFileDescPascalUnit;
  502. for I := 0 to Pred(VDlg.lvActions.Items.Count) do
  503. begin
  504. VActItem.FQuiet := True;
  505. VItem := VDlg.lvActions.Items[I];
  506. VActItem.ActName := VItem.Caption;
  507. VActItem.ActPattern := VItem.SubItems[0];
  508. VActItem.ActDefault := StrToBool(VItem.SubItems[1]);
  509. LazarusIDE.DoNewEditorFile(VActItem, '', '',
  510. [nfIsPartOfProject, nfOpenInEditor, nfCreateDefaultSrc]);
  511. end;
  512. if FProjectType = 1 then
  513. LazarusIDE.DoNewEditorFile(ProjectFileDescriptors.FindByName(
  514. SBrookDataModuleName), '', '', [nfIsPartOfProject, nfOpenInEditor,
  515. nfCreateDefaultSrc]);
  516. VBrkItem := ProjectFileDescriptors.FindByName(
  517. SBrookBrokersName) as TBrookBrokersFileDescPascalUnit;
  518. VBrkItem.FQuiet := True;
  519. VBrkItem.FFullBrk := True;
  520. VBrkItem.FAppType := VDlg.rgAppType.ItemIndex;
  521. VBrkItem.FAppDefCharset := VDlg.rgCharset.ItemIndex;
  522. LazarusIDE.DoNewEditorFile(VBrkItem, '', '',
  523. [nfIsPartOfProject, nfOpenInEditor, nfCreateDefaultSrc]);
  524. if VDlg.edPubHTMLDir.Text <> '' then
  525. begin
  526. VHTMLsDir := IncludeTrailingPathDelimiter(VDlg.edPubHTMLDir.Text);
  527. VPage404 := VHTMLsDir + '404.html';
  528. VPage500 := VHTMLsDir + '500.html';
  529. CreateHTMLFile(VPage404, PAGE_404_TPL);
  530. CreateHTMLFile(VPage500, PAGE_500_TPL);
  531. LazarusIDE.DoOpenEditorFile(VPage404, -1, -1,
  532. [ofProjectLoading, ofRegularFile, ofAddToProject]);
  533. LazarusIDE.DoOpenEditorFile(VPage500, -1, -1,
  534. [ofProjectLoading, ofRegularFile, ofAddToProject]);
  535. end;
  536. Result := LazarusIDE.DoOpenEditorFile(AProject.MainFile.FileName, -1, -1,
  537. [ofProjectLoading, ofRegularFile]);
  538. BrookFreeNewProjectDlg;
  539. end;
  540. function TBrookProjectDescriptor.DoInitDescriptor: TModalResult;
  541. begin
  542. FProjectType := TfrBrookChooseProjectType.Execute;
  543. if FProjectType = -1 then
  544. Exit(mrCancel);
  545. Result := BrookNewProjectDlg(FProjectType).ShowModal;
  546. if Result <> mrOK then
  547. BrookFreeNewProjectDlg;
  548. end;
  549. function TBrookProjectDescriptor.InitProject(
  550. AProject: TLazProject): TModalResult;
  551. var
  552. S, VApp: string;
  553. VDlg: TfrBrookNewProject;
  554. VProject: TLazProjectFile;
  555. begin
  556. VDlg := BrookNewProjectDlg(FProjectType);
  557. VProject := AProject.CreateProjectFile(VDlg.edAppName.Text + '.lpr');
  558. VProject.IsPartOfProject := True;
  559. AProject.AddFile(VProject, False);
  560. AProject.AddPackageDependency('BrookRT');
  561. case FProjectType of
  562. 0:
  563. begin
  564. AProject.Flags := AProject.Flags - [pfMainUnitHasCreateFormStatements];
  565. VApp := 'BrookApp.Run';
  566. end;
  567. 1: VApp := 'Application.Run';
  568. end;
  569. AProject.LazCompilerOptions.TargetFileName := VDlg.edAppName.Text;
  570. AProject.LazCompilerOptions.TargetFilenameApplyConventions := False;
  571. AProject.LazCompilerOptions.Win32GraphicApp := False;
  572. case VDlg.rgAppType.ItemIndex of
  573. 0:
  574. begin
  575. AProject.Flags := AProject.Flags - [pfRunnable];
  576. AProject.LazCompilerOptions.TargetFileName := VDlg.edAppName.Text + '.bf';
  577. end;
  578. 1: AProject.LazCompilerOptions.TargetFileName := VDlg.edAppName.Text + '.fbf';
  579. 2: AProject.LazCompilerOptions.TargetFilenameApplyConventions := True;
  580. 3:
  581. begin
  582. AProject.LazCompilerOptions.TargetFilenameApplyConventions := True;
  583. AProject.LazCompilerOptions.Win32GraphicApp := True;
  584. end;
  585. end;
  586. AProject.SessionStorage := pssInProjectInfo;
  587. AProject.MainFileID := 0;
  588. S :=
  589. 'program '+VDlg.edAppName.Text+';'+le+
  590. le+
  591. '{$mode objfpc}{$H+}'+le+
  592. le+
  593. 'uses'+le+
  594. '{$IFDEF UNIX}{$IFDEF UseCThreads}'+le+
  595. ' cthreads,'+le+
  596. '{$ENDIF}{$ENDIF}'+le+
  597. ' BrookApplication, Brokers;'+le+
  598. le+
  599. 'begin'+le+
  600. ' '+VApp+';'+le+
  601. 'end.';
  602. AProject.MainFile.SetSourceText(S);
  603. Result := mrOK;
  604. end;
  605. function TBrookProjectDescriptor.GetLocalizedName: string;
  606. begin
  607. Result := SBrookAppName;
  608. end;
  609. function TBrookProjectDescriptor.GetLocalizedDescription: string;
  610. begin
  611. Result := SBrookAppDesc;
  612. end;
  613. { TBrookFileDescPascalUnitWithResource }
  614. function TBrookFileDescPascalUnitWithResource.GetResourceType: TResourceType;
  615. begin
  616. if LazarusIDE.ActiveProject.Resources is TAbstractProjectResources then
  617. Result := TAbstractProjectResources(
  618. LazarusIDE.ActiveProject.Resources).ResourceType
  619. else
  620. Result := inherited GetResourceType;
  621. end;
  622. { TBrookBrokersFileDescPascalUnit }
  623. constructor TBrookBrokersFileDescPascalUnit.Create;
  624. begin
  625. inherited Create;
  626. Name := SBrookBrokersName;
  627. DefaultFileName := 'brokers.pas';
  628. DefaultSourceName := 'Brokers';
  629. FQuiet := False;
  630. end;
  631. function TBrookBrokersFileDescPascalUnit.Init(var ANewFilename: string;
  632. ANewOwner: TObject; var ANewSource: string; AQuiet: Boolean): TModalResult;
  633. begin
  634. Result := inherited Init(ANewFilename, ANewOwner, ANewSource, AQuiet);
  635. if AQuiet or FQuiet then
  636. Exit;
  637. FAppType := TfrBrookNewBroker.Execute;
  638. if FAppType = -1 then
  639. Result := mrCancel;
  640. end;
  641. {$PUSH}{$WARN 5024 OFF}
  642. function TBrookBrokersFileDescPascalUnit.CreateSource(const AFileName,
  643. ASourceName, AResourceName: string): string;
  644. var
  645. VDlg: TfrBrookNewProject;
  646. VCharset, VBroker, VInitPort: string;
  647. begin
  648. if FFullBrk then
  649. begin
  650. case FAppType of
  651. 0:
  652. begin
  653. VBroker := 'BrookFCLCGIBroker';
  654. VInitPort := '';
  655. end;
  656. 1:
  657. begin
  658. VBroker := 'BrookFCLFCGIBroker';
  659. VInitPort := '';
  660. end;
  661. 2:
  662. begin
  663. VBroker := 'BrookFCLHttpAppBroker';
  664. VInitPort := ' BrookSettings.Port := 8080;'+le;
  665. end;
  666. 3:
  667. begin
  668. VBroker := 'BrookFCLHttpDaemonBroker';
  669. VInitPort := ' BrookSettings.Port := 8080;'+le;
  670. end;
  671. end;
  672. case FAppDefCharset of
  673. 0: VCharset := 'BROOK_HTTP_CHARSET_UTF_8';
  674. 1: VCharset := 'BROOK_HTTP_CHARSET_ISO_8859_1';
  675. end;
  676. VDlg := BrookNewProjectDlg(0);
  677. Result :=
  678. 'unit '+ASourceName+';'+le+
  679. le+
  680. '{$mode objfpc}{$H+}'+le+
  681. le+
  682. 'interface'+le+
  683. le+
  684. 'uses'+le+
  685. ' '+VBroker+', BrookUtils, BrookHttpConsts, Classes, SysUtils;'+le+
  686. le+
  687. 'const'+le+
  688. ' PUBLIC_HTML = '+QuotedStr(IncludeTrailingPathDelimiter(VDlg.edPubHTMLDir.Text))+';'+le+
  689. le+
  690. 'implementation'+le+
  691. le+
  692. 'initialization'+le+
  693. VInitPort+
  694. ' BrookSettings.Charset := ' + VCharset + ';'+le+
  695. ' BrookSettings.Page404File := PUBLIC_HTML + ''404.html'';'+le+
  696. ' BrookSettings.Page500File := PUBLIC_HTML + ''500.html'';'+le+
  697. le+
  698. 'end.';
  699. end
  700. else
  701. begin
  702. case FAppType of
  703. 0:
  704. begin
  705. VBroker := 'BrookFCLCGIBroker';
  706. VInitPort := '';
  707. end;
  708. 1:
  709. begin
  710. VBroker := 'BrookFCLFCGIBroker';
  711. VInitPort := '';
  712. end;
  713. 2:
  714. begin
  715. VBroker := 'BrookFCLHttpAppBroker, BrookUtils';
  716. VInitPort :=
  717. 'initialization'+le+
  718. ' BrookSettings.Port := 8080;'+le+
  719. le;
  720. end;
  721. 3:
  722. begin
  723. VBroker := 'BrookFCLHttpDaemonBroker, BrookUtils';
  724. VInitPort :=
  725. 'initialization'+le+
  726. ' BrookSettings.Port := 8080;'+le+
  727. le;
  728. end;
  729. end;
  730. Result :=
  731. 'unit '+ASourceName+';'+le+
  732. le+
  733. '{$mode objfpc}{$H+}'+le+
  734. le+
  735. 'interface'+le+
  736. le+
  737. 'uses'+le+
  738. ' '+VBroker+';'+le+
  739. le+
  740. 'implementation'+le+
  741. le+
  742. VInitPort+
  743. 'end.';
  744. end;
  745. FAppType := 0;
  746. FAppDefCharset := 0;
  747. FFullBrk := False;
  748. FQuiet := False;
  749. end;
  750. {$POP}
  751. function TBrookBrokersFileDescPascalUnit.GetLocalizedName: string;
  752. begin
  753. Result := SBrookBrokersName;
  754. end;
  755. function TBrookBrokersFileDescPascalUnit.GetLocalizedDescription: string;
  756. begin
  757. Result := SBrookBrokersDesc;
  758. end;
  759. { TBrookActionFileDescPascalUnit }
  760. constructor TBrookActionFileDescPascalUnit.Create;
  761. begin
  762. inherited Create;
  763. Name := SBrookActionName;
  764. end;
  765. function TBrookActionFileDescPascalUnit.Init(var ANewFilename: string;
  766. ANewOwner: TObject; var ANewSource: string; AQuiet: Boolean): TModalResult;
  767. begin
  768. Result := inherited Init(ANewFilename, ANewOwner, ANewSource, AQuiet);
  769. if AQuiet or FQuiet then
  770. Exit;
  771. if not TfrBrookActEdit.Execute('Add', FActName, FActPattern,
  772. FActDefault, True) then
  773. Result := mrCancel;
  774. end;
  775. {$PUSH}{$WARN 5024 OFF}
  776. function TBrookActionFileDescPascalUnit.CreateSource(const AFileName,
  777. ASourceName, AResourceName: string): string;
  778. var
  779. VClassName: ShortString = '';
  780. VActDefault: ShortString = '';
  781. begin
  782. if FActName = '' then
  783. FActName := ASourceName;
  784. FActName := UpperCase(Copy(FActName, 1, 1)) + Copy(FActName, 2, MaxInt);
  785. if FActDefault then
  786. VActDefault := ', True';
  787. VClassName := 'T'+FActName;
  788. Result :=
  789. 'unit '+ASourceName+';'+le+
  790. le+
  791. '{$mode objfpc}{$H+}'+le+
  792. le+
  793. 'interface'+le+
  794. le+
  795. 'uses'+le+
  796. ' BrookAction;'+le+
  797. le+
  798. 'type'+le+
  799. le+
  800. ' { '+VClassName+' }'+le+
  801. le+
  802. ' '+VClassName+' = class(TBrookAction)'+le+
  803. ' public'+le+
  804. ' procedure Get; override;'+le+
  805. ' end;'+le+
  806. le+
  807. 'implementation'+le+
  808. le+
  809. '{ '+VClassName+' }'+le+
  810. le+
  811. 'procedure T'+FActName+'.Get;'+le+
  812. 'begin'+le+
  813. ' Write(''Your content here ...'');'+le+
  814. 'end;'+le+
  815. le+
  816. 'initialization'+le+
  817. ' T'+FActName+'.Register('+QuotedStr(FActPattern)+VActDefault+');'+le+
  818. le+
  819. 'end.';
  820. FActName := '';
  821. FActPattern := '';
  822. FActDefault := False;
  823. FQuiet := False;
  824. end;
  825. {$POP}
  826. function TBrookActionFileDescPascalUnit.GetLocalizedName: string;
  827. begin
  828. Result := SBrookActionName;
  829. end;
  830. function TBrookActionFileDescPascalUnit.GetLocalizedDescription: string;
  831. begin
  832. Result := SBrookActionDesc;
  833. end;
  834. { TBrookDataModuleFileDescPascalUnitWithResource }
  835. constructor TBrookDataModuleFileDescPascalUnitWithResource.Create;
  836. begin
  837. inherited Create;
  838. Name := SBrookDataModuleName;
  839. RequiredPackages := 'BrookRT';
  840. ResourceClass := TBrookDataModule;
  841. UseCreateFormStatements := True;
  842. end;
  843. function TBrookDataModuleFileDescPascalUnitWithResource.GetInterfaceUsesSection: string;
  844. begin
  845. Result := 'BrookClasses';
  846. if GetResourceType = rtLRS then
  847. Result := Result + ', LResources';
  848. Result += ', Classes, SysUtils';
  849. end;
  850. function TBrookDataModuleFileDescPascalUnitWithResource.GetLocalizedName: string;
  851. begin
  852. Result := SBrookDataModuleName;
  853. end;
  854. function TBrookDataModuleFileDescPascalUnitWithResource.GetLocalizedDescription: string;
  855. begin
  856. Result := SBrookDataModuleDesc;
  857. end;
  858. finalization
  859. BrookFreeNewProjectDlg;
  860. end.