mgrfpdocproj.pp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. unit mgrfpdocproj;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpdocproj, fpdocxmlopts;
  6. Type
  7. { TFPDocProjectManager }
  8. TFPDocProjectManager = Class(TComponent)
  9. Private
  10. FProject : TFPDocProject;
  11. FPackage : TFPDocPackage;
  12. FExpandMacros: Boolean;
  13. FMacros: TStrings;
  14. procedure SetMacros(AValue: TStrings);
  15. protected
  16. Procedure CheckPackage;
  17. procedure GetItemsFromDirectory(AList: TStrings; ADirectory, AMask: String; ARecurse: Boolean);
  18. procedure DoMacro(Sender: TObject; const TagString: String; TagParams: TStringList; out ReplaceText: String); virtual;
  19. function ExpandMacrosInFile(AFileName: String): TStream; virtual;
  20. Public
  21. Constructor Create(AOwner : TComponent); override;
  22. Destructor Destroy; override;
  23. procedure AddDescrFilesFromDirectory(Const ADirectory, AMask : String; ARecurse: Boolean);
  24. procedure AddInputFilesFromDirectory(Const ADirectory, AMask, AOptions: String; ARecurse: Boolean);
  25. procedure AddInputFile(Const AFile : String; AOptions : String = '');
  26. procedure AddDescrFile(Const AFile : String);
  27. procedure RemoveInputFile(Const AFile : String);
  28. procedure RemoveDescrFile(Const AFile : String);
  29. procedure WriteOptionFile(const AFileName: String);
  30. procedure ReadOptionFile(const AFileName: String);
  31. Procedure Selectpackage(Const APackageName : String);
  32. Procedure AddPackage (Const APackageName : String);
  33. procedure SetOption(Const AOption : String; Enable : Boolean = True);
  34. Property Project : TFPDocProject Read FProject;
  35. Property SelectedPackage : TFPDocPackage Read FPackage;
  36. Property Macros : TStrings Read FMacros Write SetMacros;
  37. Property ExpandMacros : Boolean Read FExpandMacros Write FExpandMacros;
  38. end;
  39. EMgrFPDoc = Class(Exception);
  40. implementation
  41. uses dom,xmlread,fptemplate;
  42. procedure TFPDocProjectManager.SetMacros(AValue: TStrings);
  43. begin
  44. if FMacros=AValue then Exit;
  45. FMacros.Assign(AValue);
  46. end;
  47. procedure TFPDocProjectManager.DoMacro(Sender: TObject; const TagString: String;
  48. TagParams: TStringList; out ReplaceText: String);
  49. begin
  50. ReplaceText:=FMacros.Values[TagString];
  51. end;
  52. Procedure TFPDocProjectManager.GetItemsFromDirectory(AList : TStrings; ADirectory,AMask : String; ARecurse : Boolean);
  53. Var
  54. D : String;
  55. Info : TSearchRec;
  56. begin
  57. D:=ADirectory;
  58. if (D='.') then
  59. D:='';
  60. if (D<>'') then
  61. D:=includeTrailingPathDelimiter(D);
  62. If FindFirst(D+AMask,0,info)=0 then
  63. try
  64. Repeat
  65. if ((Info.Attr and faDirectory)=0) then
  66. AList.add(D+Info.Name);
  67. Until (FindNext(Info)<>0);
  68. finally
  69. FindClose(Info);
  70. end;
  71. If ARecurse and (FindFirst(ADirectory+AMask,0,info)=0) then
  72. try
  73. Repeat
  74. if ((Info.Attr and faDirectory)<>0) then
  75. GetItemsFromDirectory(Alist,IncludeTrailingPathDelimiter(D+Info.Name),AMask,ARecurse);
  76. Until (FindNext(Info)<>0);
  77. finally
  78. FindClose(Info);
  79. end;
  80. end;
  81. constructor TFPDocProjectManager.Create(AOwner: TComponent);
  82. begin
  83. inherited Create(AOwner);
  84. FProject:=TFPDocProject.Create(Self);
  85. FMacros:=TStringList.Create;
  86. end;
  87. destructor TFPDocProjectManager.Destroy;
  88. begin
  89. FreeAndNil(FMacros);
  90. FreeAndNil(FProject);
  91. inherited Destroy;
  92. end;
  93. Function TFPDocProjectManager.ExpandMacrosInFile(AFileName : String) : TStream;
  94. Var
  95. F : TFileStream;
  96. T : TTemplateParser;
  97. begin
  98. F:=TFileStream.Create(AFileName,fmOpenRead or fmShareDenyWrite);
  99. try
  100. Result:=TMemoryStream.Create;
  101. try
  102. T:=TTemplateParser.Create;
  103. try
  104. T.StartDelimiter:='$(';
  105. T.EndDelimiter:=')';
  106. T.AllowTagParams:=true;
  107. T.OnReplaceTag:=@DoMacro;
  108. T.ParseStream(F,Result);
  109. finally
  110. T.Free;
  111. end;
  112. Result.Position:=0;
  113. except
  114. FreeAndNil(Result);
  115. Raise;
  116. end;
  117. finally
  118. F.Free;
  119. end;
  120. end;
  121. Procedure TFPDocProjectManager.AddDescrFilesFromDirectory(const ADirectory,AMask : String; ARecurse : Boolean);
  122. Var
  123. L : TStringList;
  124. M : String;
  125. begin
  126. CheckPackage;
  127. M:=AMask;
  128. if (M='') then
  129. M:='*.xml';
  130. L:=TStringList.Create;
  131. try
  132. GetItemsFromDirectory(L,ADirectory,M,ARecurse);
  133. FPackage.Descriptions.AddStrings(L);
  134. finally
  135. L.Free;
  136. end;
  137. end;
  138. Procedure TFPDocProjectManager.AddInputFilesFromDirectory(Const ADirectory,AMask,AOptions : String; ARecurse : Boolean);
  139. Var
  140. L : TStringList;
  141. I : integer;
  142. M : String;
  143. begin
  144. CheckPackage;
  145. M:=AMask;
  146. if (M='') then
  147. M:='*.pp';
  148. L:=TStringList.Create;
  149. try
  150. GetItemsFromDirectory(L,ADirectory,M,ARecurse);
  151. For I:=0 to L.Count-1 do
  152. AddInputFile(L[i],AOPtions);
  153. finally
  154. L.Free;
  155. end;
  156. end;
  157. procedure TFPDocProjectManager.AddInputFile(const AFile: String; AOptions : String = '');
  158. Var
  159. S : String;
  160. begin
  161. CheckPackage;
  162. S:=AFile;
  163. If (AOptions<>'') then
  164. S:=AOptions+' '+S;
  165. FPackage.Inputs.Add(S);
  166. end;
  167. procedure TFPDocProjectManager.AddDescrFile(const AFile: String);
  168. begin
  169. CheckPackage;
  170. if FPackage.Descriptions.IndexOf(AFile)<>-1 then
  171. Raise EMgrFPDoc.Createfmt('Duplicate description file : "%s"',[AFile]);
  172. FPackage.Descriptions.Add(AFile);
  173. end;
  174. procedure TFPDocProjectManager.RemoveInputFile(const AFile: String);
  175. Var
  176. I : Integer;
  177. begin
  178. I:=FPackage.Inputs.IndexOf(AFile);
  179. If (I<>-1) then
  180. FPackage.Inputs.Delete(I);
  181. end;
  182. procedure TFPDocProjectManager.RemoveDescrFile(const AFile: String);
  183. Var
  184. I : Integer;
  185. begin
  186. I:=FPackage.Descriptions.IndexOf(AFile);
  187. If (I<>-1) then
  188. FPackage.Descriptions.Delete(I);
  189. end;
  190. procedure TFPDocProjectManager.ReadOptionFile(Const AFileName : String);
  191. Var
  192. XML : TXMLDocument;
  193. S : TStream;
  194. begin
  195. With TXMLFPDocOptions.Create(Self) do
  196. try
  197. if not (ExpandMacros) then
  198. LoadOptionsFromFile(FProject,AFileName)
  199. else
  200. begin
  201. S:=ExpandMacrosInFile(AFileName);
  202. try
  203. ReadXMLFile(XML,S,AFileName);
  204. try
  205. LoadFromXml(FProject,XML)
  206. finally
  207. XML.Free;
  208. end;
  209. finally
  210. S.Free;
  211. end;
  212. end;
  213. finally
  214. Free;
  215. end;
  216. end;
  217. procedure TFPDocProjectManager.Selectpackage(const APackageName: String);
  218. begin
  219. FPackage:=FProject.Packages.FindPackage(APackageName);
  220. If (FPackage=Nil) then
  221. Raise EMgrFPDoc.CreateFmt('Unknown package : "%s"',[APackageName]);
  222. end;
  223. procedure TFPDocProjectManager.AddPackage(const APackageName: String);
  224. begin
  225. if FProject.Packages.FindPackage(APackageName)<>Nil then
  226. Raise EMgrFPDoc.CreateFmt('Duplicate package : "%s"',[APackageName]);
  227. FPackage:=FProject.Packages.Add as TFPDocPackage;
  228. FPackage.Name:=APackageName;
  229. end;
  230. procedure TFPDocProjectManager.SetOption(const AOption: String;
  231. Enable: Boolean = true);
  232. Var
  233. O,V : String;
  234. P : Integer;
  235. EO : TEngineOptions;
  236. begin
  237. V:=LowerCase(AOption);
  238. P:=Pos('=',V);
  239. If (P=0) then
  240. P:=Length(V)+1;
  241. O:=Copy(V,1,P-1);
  242. Delete(V,1,P);
  243. EO:=FProject.Options;
  244. Case IndexOfString(o,OptionNames) of
  245. 0 : EO.HideProtected:=Enable;
  246. 1 : EO.WarnNoNode:=Enable;
  247. 2 : EO.ShowPrivate:=Enable;
  248. 3 : EO.StopOnParseError:=Enable;
  249. 4 : EO.ostarget:=v;
  250. 5 : EO.cputarget:=v;
  251. 6 : EO.MoDir:=V;
  252. 7 : EO.InterfaceOnly:=Not Enable;
  253. 8 : EO.Backend:=V;
  254. 9 : EO.Language:=v;
  255. 10 : EO.DefaultPackageName:=V;
  256. 11 : EO.DontTrim:=Enable;
  257. else
  258. EO.BackendOptions.add('--'+O);
  259. EO.BackendOptions.add(v);
  260. end;
  261. end;
  262. procedure TFPDocProjectManager.WriteOptionFile(Const AFileName : String);
  263. begin
  264. With TXMLFPDocOptions.Create(Self) do
  265. try
  266. SaveOptionsToFile(FProject,AFileName);
  267. finally
  268. Free;
  269. end;
  270. end;
  271. procedure TFPDocProjectManager.CheckPackage;
  272. begin
  273. if (FPackage=Nil) then
  274. Raise EMgrFPDoc.Create('Error: No package selected');
  275. end;
  276. end.