showdpk.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2024 by Michael Van Canneyt ([email protected])
  4. Program to analyse a package file, demo for dpkinfo.pp
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program showdpk;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes, SysUtils, Types, CustApp, dpkinfo;
  15. type
  16. { TDPK2LPKApplication }
  17. TDPK2LPKApplication = class(TCustomApplication)
  18. private
  19. function ConfigInfo(Info: TPackageInfo): TInfoKind;
  20. protected
  21. procedure DoRun; override;
  22. public
  23. constructor Create(TheOwner: TComponent); override;
  24. destructor Destroy; override;
  25. procedure Usage(const Msg: String); virtual;
  26. end;
  27. { TDPK2LPKApplication }
  28. function TDPK2LPKApplication.ConfigInfo(Info : TPackageInfo) : TInfoKind;
  29. procedure OptToStrings(Opt : string; Strings: TSTrings);
  30. begin
  31. begin
  32. if (Opt[1]='@') then
  33. begin
  34. Opt:=Copy(Opt,2);
  35. if FileExists(Opt) then
  36. Strings.LoadFromFile(Opt);
  37. end
  38. else
  39. begin
  40. Strings.Delimiter:=';';
  41. Strings.DelimitedText:=Opt;
  42. end;
  43. end;
  44. end;
  45. var
  46. m,Opt : String;
  47. begin
  48. Opt:=GetOptionValue('k','known');
  49. if (Opt<>'') then
  50. OptToStrings(Opt,Info.KnownPackages);
  51. Opt:=GetOptionValue('d','defines');
  52. if (Opt<>'') then
  53. OptToStrings(Opt,Info.Defines);
  54. info.UseAbsolute:=HasOption('a','absolute');
  55. m:=GetOptionValue('m','mode');
  56. case lowercase(m) of
  57. 'requires' : result:=ikRequires;
  58. 'files' : Result:=ikFiles;
  59. 'paths' : Result:=ikPaths;
  60. else
  61. Usage('Unknown mode: '+m);
  62. Result:=ikUnknown;
  63. end;
  64. end;
  65. procedure TDPK2LPKApplication.DoRun;
  66. const
  67. Short = 'ho:k:m:d:a';
  68. Long : Array of string = ('help','output:','known:','mode:','defines:','absolute');
  69. var
  70. ErrorMsg: String;
  71. Info : TPackageInfo;
  72. Kind : TInfoKind;
  73. aLine,PFN,OFN : String;
  74. FNS : TStringDynArray;
  75. begin
  76. Terminate;
  77. ErrorMsg:=CheckOptions(Short,Long);
  78. if (ErrorMsg<>'') or HasOption('h','help') then
  79. begin
  80. Usage(ErrorMsg);
  81. Exit;
  82. end;
  83. FNS:=GetNonOptions(Short,Long);
  84. if Length(FNS)<>1 then
  85. begin
  86. Usage('Need one input file');
  87. exit;
  88. end;
  89. PFN:=FNS[0];
  90. OFN:=GetOptionValue('o','output');
  91. Info:=TPackageInfo.Create(Self);
  92. try
  93. Kind:=ConfigInfo(Info);
  94. if Kind=ikUnknown then
  95. exit;
  96. Info.ShowInfo(PFN,Kind);
  97. if OFN<>'' then
  98. Info.Output.SaveToFile(OFN)
  99. else
  100. for aLine in Info.Output do
  101. Writeln(aLine);
  102. finally
  103. Info.Free;
  104. end;
  105. end;
  106. constructor TDPK2LPKApplication.Create(TheOwner: TComponent);
  107. begin
  108. inherited Create(TheOwner);
  109. StopOnException:=True;
  110. end;
  111. destructor TDPK2LPKApplication.Destroy;
  112. begin
  113. inherited Destroy;
  114. end;
  115. procedure TDPK2LPKApplication.Usage(Const Msg : String);
  116. begin
  117. if Msg<>'' then
  118. Writeln('Error: ',Msg);
  119. Writeln('Usage: ',ExeName, ' [options] PackageFile');
  120. Writeln('Where [options] is one or more of:');
  121. Writeln('-h --help this help');
  122. Writeln('-k --known=FILE File with known packages, they will not appear in output');
  123. Writeln('-o --output=FILE Write output to file instead of stdout.');
  124. Writeln('-m --mode=MODE Select output mode, MODE is one of:');
  125. Writeln(' requires Show requires in package');
  126. Writeln(' files Show files in package');
  127. Writeln(' paths Show paths for source files in package');
  128. Writeln('-d --defines Semicolon-separated list of defines for parser');
  129. Writeln('-a --absolute Use absolute filenames instead of relative');
  130. ExitCode:=Ord(Msg<>'');
  131. end;
  132. var
  133. Application: TDPK2LPKApplication;
  134. begin
  135. Application:=TDPK2LPKApplication.Create(nil);
  136. Application.Title:='Show package info';
  137. Application.Run;
  138. Application.Free;
  139. end.