makedottedfiles.pp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2022 by Michael Van Canneyt, [email protected]
  4. Application to Prefix units in uses clause of a list of programs.
  5. Optionally adapts an fpmake file.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program makedottedfiles;
  13. {$mode objfpc}{$H+}
  14. uses
  15. {$IFDEF UNIX}
  16. cthreads,
  17. {$ENDIF}
  18. types, Classes, SysUtils, CustApp, Prefixer, namespacetool;
  19. type
  20. { TNamespaceCreation }
  21. TNamespaceCreation = class(TCustomApplication)
  22. Private
  23. FTool : TNamespaceTool;
  24. FListFileName: string;
  25. FQuiet : Boolean;
  26. FVerbose : Boolean;
  27. procedure DoLogTool(Sender: TObject; EventType: TEventType; const Msg: String);
  28. function ProcessOptions: Boolean;
  29. protected
  30. procedure DoLog(EventType: TEventType; const Msg: String); override;
  31. procedure DoRun; override;
  32. public
  33. constructor Create(TheOwner: TComponent); override;
  34. destructor Destroy; override;
  35. procedure Usage(const aMsg : String);
  36. end;
  37. { TNamespaceCreation }
  38. procedure TNamespaceCreation.DoLog(EventType: TEventType; const Msg: String);
  39. begin
  40. Writeln('[',EventType,'] ',Msg);
  41. end;
  42. function TNamespaceCreation.ProcessOptions : Boolean;
  43. const
  44. Short = 'qDhbncarl:k:d:p:s:e:m:vu';
  45. Long : Array of string = ('help','list:','known-prefixes:','dirmap:','backup','prefix:','subdir:','dry-run','cased','apply-rule','restart','replace-subdir','ext','fpmake-file:','quiet','verbose','update');
  46. var
  47. aExt,ErrorMsg: String;
  48. NonOpts : Array of string;
  49. begin
  50. Result:=False;
  51. ErrorMsg:=CheckOptions(Short,Long);
  52. if (ErrorMsg<>'') or HasOption('h','help') then
  53. begin
  54. Usage(ErrorMsg);
  55. Exit;
  56. end;
  57. // Options for tool class
  58. aExt:=GetOptionValue('e','ext');
  59. FTool.ForcedExt:=aExt;
  60. FTool.DirMapFileName:=GetOptionValue('d','dirmap');
  61. FTool.PrefixesFileName:=GetOptionValue('k','known-prefixes');
  62. FTool.DefaultPrefix:=GetOptionValue('p','prefix');
  63. FTool.Subdir:=GetOptionValue('s','subdir');
  64. if FTool.SubDir='' then
  65. FTool.SubDir:=DefaultSubdir;
  66. FTool.SubdirMode:=sdmAppend;
  67. if HasOption('D','replace-subdir') then
  68. FTool.SubDirMode:=sdmReplace;
  69. FTool.Backup:=HasOption('b','backup');
  70. FTool.Update:=HasOption('u','update');
  71. FTool.DryRun:=HasOption('n','dry-run');
  72. FTool.Restart:=HasOption('r','restart');
  73. FTool.CasedFiles:=HasOption('c','cased');
  74. FTool.FPMakeNameSpaceFile:=GetoptionValue('m','fpmake-file');
  75. FQuiet:=HasOption('q','quiet');
  76. FVerbose:=HasOption('v','verbose');
  77. if FVerbose then
  78. FQuiet:=False;
  79. FListFileName:=GetOptionValue('l','list');
  80. NonOpts:=GetNonOptions(Short,Long);
  81. if (FListFileName='') and (Length(NonOpts)=1) then
  82. FListFileName:=NonOpts[0];
  83. If (FListFileName='') then
  84. begin
  85. Usage('Need file list filename');
  86. exit;
  87. end;
  88. If (FTool.PrefixesFileName='') and (FTool.DefaultPrefix='') and not HasOption('a','apply-rule') then
  89. begin
  90. Usage('Need prefixes filename or default prefix');
  91. exit;
  92. end;
  93. Result:=True;
  94. end;
  95. procedure TNamespaceCreation.DoLogTool(Sender: TObject; EventType : TEventType; const Msg: String);
  96. var
  97. CanLog : Boolean;
  98. begin
  99. Case EventType of
  100. etDebug : CanLog:=FVerbose;
  101. etError : CanLog:=True;
  102. else
  103. CanLog:=Not FQuiet;
  104. end;
  105. if CanLog then
  106. Log(EventType,Msg);
  107. end;
  108. procedure TNamespaceCreation.DoRun;
  109. begin
  110. Terminate;
  111. if not ProcessOptions then
  112. exit;
  113. FTool.Init;
  114. if HasOption('a','apply-rule') then
  115. FTool.CreateKnown(FListFileName)
  116. else
  117. FTool.HandleFileList(FListFileName);
  118. end;
  119. constructor TNamespaceCreation.Create(TheOwner: TComponent);
  120. begin
  121. inherited Create(TheOwner);
  122. StopOnException:=True;
  123. FTool:=TNamespaceTool.Create(Self);
  124. FTool.OnLog:=@DoLogTool;
  125. end;
  126. destructor TNamespaceCreation.Destroy;
  127. begin
  128. FreeAndNil(FTool);
  129. Inherited;
  130. end;
  131. procedure TNamespaceCreation.Usage(const aMsg: String);
  132. begin
  133. if aMsg<>'' then
  134. Writeln('Error : ',aMsg);
  135. { add your help code here }
  136. Writeln('Usage: ', ExeName, ' [options] list');
  137. Writeln('Where options is one or more of');
  138. Writeln('-h --help This help');
  139. Writeln('-a --apply-rule Apply rule in filelist to construct known file list.');
  140. Writeln(' if -k is not specified, then map file is list file with extension changed to FILE.map)');
  141. Writeln('-b --backup Create backup of files that are written');
  142. Writeln('-c --cased First letter of name is uppercased when creating alias');
  143. Writeln('-d --dirmap=FILE Directory mapping. Old=New, new is relative to subdir');
  144. Writeln('-e --ext=EXT Force extension of created dotted units to EXT. If not set, original extension is taken');
  145. Writeln('-D --replace-subdir Directory mapping. Completely replace dir with mapping from dirmap.');
  146. Writeln('-k --known-prefixes=FILE Namespace mapping. Unit=Namespace');
  147. Writeln('-l --list=FILE Files to handle. One file per line');
  148. Writeln('-m --fpmake-file=FILE Write namespace unit mappping to FILE and add as NameSpaceMap to package definition in fpmake.pp');
  149. Writeln('-n --dry-run Do not execute commands, only write what would be done.');
  150. Writeln('-r --restart Do not load done.lst. Default is to load done.lst and skip files listed in it.');
  151. Writeln(' The file is always updated when done.');
  152. Writeln('-q --quiet Do not produce any output.');
  153. Writeln('-s --subdir=DIRECTORY Directory in which to write files. Default: '+FTool.DefaultPrefix);
  154. Writeln('-u --update Write updated known prefixes file.');
  155. Writeln('-v --verbose Produce debug output (reports on uses clause manipulations).');
  156. end;
  157. var
  158. Application: TNamespaceCreation;
  159. begin
  160. Application:=TNamespaceCreation.Create(nil);
  161. Application.Title:='Namespaced files creation tool';
  162. Application.Run;
  163. Application.Free;
  164. end.