mkfpdocproj.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. program mkfpdocproj;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, mgrfpdocproj;
  5. type
  6. { TManageFPDocProjectApplication }
  7. TManageFPDocProjectApplication = class(TCustomApplication)
  8. private
  9. FMGR : TFPDocProjectManager;
  10. FPackageName,
  11. FInputFileName,
  12. FOutputFileName,
  13. FCmd : String;
  14. FCmdArgs,
  15. FCmdOptions: TStrings;
  16. procedure AddDescrFiles;
  17. procedure AddDescriptionDirs;
  18. procedure AddInputDirs;
  19. procedure AddInputFiles;
  20. procedure AddImportFiles;
  21. function CmdNeedsPackage: Boolean;
  22. procedure RemoveInputFiles;
  23. procedure RemoveDescrFiles;
  24. procedure AddPackages;
  25. function CheckCmdOption(C: Char; S: String): Boolean;
  26. function GetCmdOption(C: Char; S: String): String;
  27. procedure SetOptions(Enable: Boolean);
  28. protected
  29. procedure ParseOptions;
  30. Procedure Error(Const Msg : String);
  31. procedure Usage(AExitCode: Integer);
  32. procedure DoRun; override;
  33. public
  34. constructor Create(TheOwner: TComponent); override;
  35. Destructor Destroy; override;
  36. end;
  37. Resourcestring
  38. SErrNeedArgument = 'Option at position %d needs an argument: %s';
  39. { TManageFPDocProjectApplication }
  40. procedure TManageFPDocProjectApplication.Usage(AExitCode : Integer);
  41. Var
  42. FN : String;
  43. I : Integer;
  44. begin
  45. FN:=ChangeFileExt(ExtractFileName(ParamStr(0)),'');
  46. Writeln('Usage ',FN,' [options] command [command-options] command-args');
  47. Writeln('Where options is one of ');
  48. Writeln(' -i --input=file Initialize project from named file.');
  49. Writeln(' -o --output=file Write project to named file. Default is input file.');
  50. Writeln(' -p --package=name Package to perform operation on.');
  51. Writeln('command is one of:');
  52. Writeln(' add-packages');
  53. Writeln(' Add arguments as package definitions to the file.');
  54. Writeln(' add-description-dirs');
  55. Writeln(' Scan directories for XML files to add as descriptions of selected package.');
  56. Writeln(' add-input-dirs');
  57. Writeln(' Scan directories for .pp or .pas files to add as inputs of selected package.');
  58. Writeln(' add-input-files');
  59. Writeln(' Add files as inputs of selected package.');
  60. Writeln(' add-import-files');
  61. Writeln(' Add files (format: "filename,prefix") to imports of selected package.');
  62. Writeln(' add-descr-files');
  63. Writeln(' Add files as description files of selected package.');
  64. Writeln(' expand-macros');
  65. Writeln(' read file and expand macros. Arguments specify macro values as Name=Value pairs');
  66. Writeln(' remove-descr-files');
  67. Writeln(' Remove files from description files of selected package.');
  68. Writeln(' remove-input-files');
  69. Writeln(' Remove files from input files of selected package.');
  70. Writeln(' set-options');
  71. Writeln(' Set named options (true) for project file.');
  72. Writeln(' Valid option names : ');
  73. Writeln(' hide-protected , warn-no-node, show-private, stop-on-parser-error,');
  74. Writeln(' parse-impl, dont-trim');
  75. Writeln(' unset-options');
  76. Writeln(' UnSet named options (false) for project file.');
  77. Halt(AExitCode);
  78. end;
  79. Function CheckOptionStr(O : String;Short : Char;Long : String): Boolean;
  80. begin
  81. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  82. end;
  83. function TManageFPDocProjectApplication.CmdNeedsPackage : Boolean;
  84. begin
  85. Result:=(FCMd<>'expand-macros') and (FCMD<>'set-options') and (FCmd<>'unset-options');
  86. end;
  87. procedure TManageFPDocProjectApplication.ParseOptions;
  88. Function CheckOption(Index : Integer;Short : char;Long : String): Boolean;
  89. begin
  90. Result:=CheckOptionStr(ParamStr(Index),Short,Long);
  91. end;
  92. Function OptionArg(Var Index : Integer) : String;
  93. Var
  94. P : Integer;
  95. begin
  96. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  97. begin
  98. If Index<ParamCount then
  99. begin
  100. Inc(Index);
  101. Result:=Paramstr(Index);
  102. end
  103. else
  104. Error(Format(SErrNeedArgument,[Index,ParamStr(Index)]));
  105. end
  106. else If length(ParamStr(Index))>2 then
  107. begin
  108. P:=Pos('=',Paramstr(Index));
  109. If (P=0) then
  110. Error(Format(SErrNeedArgument,[Index,ParamStr(Index)]))
  111. else
  112. begin
  113. Result:=Paramstr(Index);
  114. Delete(Result,1,P);
  115. end;
  116. end;
  117. end;
  118. Var
  119. I : Integer;
  120. S : String;
  121. begin
  122. I:=0;
  123. // We can't use the TCustomApplication option handling,
  124. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  125. While (I<ParamCount) do
  126. begin
  127. Inc(I);
  128. if (FCmd='') then
  129. begin
  130. if Checkoption(I,'i','input') then
  131. FInputFileName:=OptionArg(i)
  132. else if Checkoption(I,'o','output') then
  133. FOutputFileName:=OptionArg(i)
  134. else if CheckOption(I,'p','package') then
  135. FPackageName:=OptionArg(i)
  136. else if CheckOption(I,'h','help') then
  137. Usage(0)
  138. else if (ParamStr(i)<>'') then
  139. begin
  140. S:=ParamStr(i);
  141. if (S[1]='-') then
  142. Error('Unknown option : '+S)
  143. else
  144. FCmd:=lowercase(S)
  145. end
  146. end
  147. else
  148. begin
  149. S:=ParamStr(I);
  150. if (S<>'') then
  151. if (S[1]<>'-') then
  152. FCmdArgs.Add(S)
  153. else
  154. FCmdOptions.Add(S);
  155. end;
  156. end;
  157. if (FOutputFileName='') then
  158. FOutputFileName:=FInputFileName;
  159. If (FOutputFileName='') then
  160. Error('Need an output filename');
  161. if (FPackageName='') and CmdNeedsPackage then
  162. Error('Need a package name');
  163. if (FCmd='') then
  164. Error('Need a command');
  165. end;
  166. procedure TManageFPDocProjectApplication.Error(Const Msg: String);
  167. begin
  168. Writeln('Error : ',Msg);
  169. Usage(1);
  170. end;
  171. Function TManageFPDocProjectApplication.CheckCmdOption(C : Char; S : String) : Boolean;
  172. Var
  173. I : integer;
  174. begin
  175. I:=0;
  176. Result:=False;
  177. While (Not Result) and (I<FCmdOptions.Count) do
  178. begin
  179. Result:=CheckOptionStr(FCmdOptions[i],C,S);
  180. Inc(I);
  181. end;
  182. end;
  183. Function TManageFPDocProjectApplication.GetCmdOption(C : Char; S : String) : String;
  184. Var
  185. I,P : integer;
  186. B : Boolean;
  187. begin
  188. I:=0;
  189. B:=False;
  190. While (Not B) and (I<FCmdOptions.Count) do
  191. begin
  192. B:=CheckOptionStr(FCmdOptions[i],C,S);
  193. if B then
  194. begin
  195. Result:=FCmdOptions[I];
  196. if (Length(Result)>1) and (Result[2]<>'-') then
  197. begin
  198. If I<FCmdOptions.Count-1 then
  199. begin
  200. Inc(I);
  201. Result:=FCmdOptions[I];
  202. end
  203. else
  204. Error(Format(SErrNeedArgument,[I,Result]));
  205. end
  206. else If length(Result)>2 then
  207. begin
  208. P:=Pos('=',Result);
  209. If (P=0) then
  210. Error(Format(SErrNeedArgument,[I,Result]))
  211. else
  212. Delete(Result,1,P);
  213. end;
  214. end;
  215. Inc(I);
  216. end;
  217. end;
  218. procedure TManageFPDocProjectApplication.AddDescriptionDirs;
  219. Var
  220. Recursive: Boolean;
  221. Mask : String;
  222. I : Integer;
  223. begin
  224. Recursive:=CheckCmdOption('r','recursive');
  225. Mask:=GetCmdOption('m','mask');
  226. if FCmdArgs.Count=0 then
  227. FMGr.AddDescrFilesFromDirectory('',Mask,Recursive)
  228. else
  229. For I:=0 to FCmdArgs.Count-1 do
  230. FMGr.AddDescrFilesFromDirectory(FCmdArgs[i],Mask,Recursive);
  231. end;
  232. procedure TManageFPDocProjectApplication.AddInputDirs;
  233. Var
  234. Recursive: Boolean;
  235. Options,Mask : String;
  236. I : Integer;
  237. begin
  238. Recursive:=CheckCmdOption('r','recursive');
  239. Mask:=GetCmdOption('m','mask');
  240. Options:=GetCmdOption('o','options');
  241. if FCmdArgs.Count=0 then
  242. FMGr.AddInputFilesFromDirectory('',Mask,Options,Recursive)
  243. else
  244. For I:=0 to FCmdArgs.Count-1 do
  245. FMGr.AddInputFilesFromDirectory(FCmdArgs[i],Mask,Options,Recursive);
  246. end;
  247. procedure TManageFPDocProjectApplication.AddInputFiles;
  248. Var
  249. Options : String;
  250. I : Integer;
  251. begin
  252. Options:=GetCmdOption('o','options');
  253. For I:=0 to FCmdArgs.Count-1 do
  254. FMGr.AddInputFile(FCmdArgs[i],Options);
  255. end;
  256. procedure TManageFPDocProjectApplication.AddImportFiles;
  257. Var
  258. I,J : Integer;
  259. F,P : String;
  260. begin
  261. For I:=0 to FCmdArgs.Count-1 do
  262. begin
  263. P:=FCmdArgs[i];
  264. J:=Pos(',',P);
  265. F:=Copy(P,1,J-1);
  266. Delete(P,1,J);
  267. FMGr.AddImportFile(F,P);
  268. end;
  269. end;
  270. procedure TManageFPDocProjectApplication.RemoveInputFiles;
  271. Var
  272. I : Integer;
  273. begin
  274. For I:=0 to FCmdArgs.Count-1 do
  275. FMGr.RemoveInputFile(FCmdArgs[i]);
  276. end;
  277. procedure TManageFPDocProjectApplication.RemoveDescrFiles;
  278. Var
  279. I : Integer;
  280. begin
  281. For I:=0 to FCmdArgs.Count-1 do
  282. FMGr.RemoveDescrFile(FCmdArgs[i]);
  283. end;
  284. procedure TManageFPDocProjectApplication.AddPackages;
  285. var
  286. I : Integer;
  287. begin
  288. For I:=0 to FCmdArgs.Count-1 do
  289. FMgr.AddPackage(FCmdArgs[i]);
  290. end;
  291. procedure TManageFPDocProjectApplication.AddDescrFiles;
  292. Var
  293. I : Integer;
  294. begin
  295. For I:=0 to FCmdArgs.Count-1 do
  296. FMGr.AddDescrFile(FCmdArgs[i]);
  297. end;
  298. procedure TManageFPDocProjectApplication.SetOptions(Enable : Boolean);
  299. Var
  300. I : Integer;
  301. begin
  302. For I:=0 to FCmdArgs.Count-1 do
  303. FMgr.SetOption(FCmdArgs[i],Enable);
  304. end;
  305. procedure TManageFPDocProjectApplication.DoRun;
  306. begin
  307. ParseOptions;
  308. if (FInputFileName='') then
  309. FMGR.AddPackage(FPackageName)
  310. else
  311. begin
  312. if (FCmd='expand-macros') then
  313. begin
  314. FMGR.Macros:=FCmdArgs;
  315. FMGR.ExpandMacros:=true;
  316. FMGR.ReadOptionFile(FInputFileName)
  317. end
  318. else
  319. begin
  320. FMGR.ReadOptionFile(FInputFileName);
  321. if CmdNeedsPackage then
  322. FMGR.SelectPackage(FPackageName);
  323. end
  324. end;
  325. if (FCmd='add-packages') then
  326. AddPackages
  327. else if (FCmd='add-description-dirs') then
  328. AddDescriptionDirs
  329. else if (FCmd='add-input-dirs') then
  330. AddInputDirs
  331. else if (FCmd='add-input-files') then
  332. AddInputFiles
  333. else if (FCmd='add-import-files') then
  334. AddImportFiles
  335. else if (FCmd='add-description-files') then
  336. AddDescrFiles
  337. else if (FCmd='remove-input-files') then
  338. RemoveInputFiles
  339. else if (FCmd='remove-descr-files') then
  340. RemoveDescrFiles
  341. else if (FCmd='set-options') then
  342. SetOptions(True)
  343. else if (FCmd='unset-options') then
  344. SetOptions(False)
  345. else if (FCMd<>'expand-macros') then
  346. Error(Format('Unknown command : "%s"',[FCmd]));
  347. FMgr.WriteOptionFile(FOutputFileName);
  348. Terminate;
  349. end;
  350. constructor TManageFPDocProjectApplication.Create(TheOwner: TComponent);
  351. begin
  352. inherited Create(TheOwner);
  353. StopOnException:=True;
  354. FCmdArgs:=TStringList.Create;
  355. FCmdOptions:=TStringList.Create;
  356. FMGR:=TFPDocProjectManager.Create(Self);
  357. end;
  358. destructor TManageFPDocProjectApplication.Destroy;
  359. begin
  360. FreeAndNil(FMGR);
  361. FreeAndNil(FCmdArgs);
  362. FreeAndNil(FCmdOptions);
  363. inherited Destroy;
  364. end;
  365. var
  366. Application: TManageFPDocProjectApplication;
  367. begin
  368. Application:=TManageFPDocProjectApplication.Create(nil);
  369. Application.Title:='Program to manipulate FPDoc project files';
  370. Application.Run;
  371. Application.Free;
  372. end.