dpk2lpk.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. program dpk2lpk;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, Types, CustApp, dpktolpk;
  5. type
  6. { TDPK2LPKApplication }
  7. TDPK2LPKApplication = class(TCustomApplication)
  8. private
  9. procedure WriteLog(const Msg: String);
  10. protected
  11. FQuiet : Boolean;
  12. procedure DoRun; override;
  13. public
  14. constructor Create(TheOwner: TComponent); override;
  15. destructor Destroy; override;
  16. procedure Usage(const Msg: String); virtual;
  17. end;
  18. { TDPK2LPKApplication }
  19. procedure TDPK2LPKApplication.WriteLog(Const Msg : String);
  20. begin
  21. if FQuiet then Exit;
  22. Writeln(StdErr,Msg);
  23. end;
  24. procedure TDPK2LPKApplication.DoRun;
  25. const
  26. Short = 'ho:k:uq';
  27. Long : Array of string = ('help','output:','known:','update','quiet');
  28. var
  29. ErrorMsg: String;
  30. Cnv : TDPK2LPKConverter;
  31. OFN,PFN,KFN : String;
  32. FNS : TStringDynArray;
  33. begin
  34. Terminate;
  35. ErrorMsg:=CheckOptions(Short,Long);
  36. if (ErrorMsg<>'') or HasOption('h','help') then
  37. begin
  38. Usage(ErrorMsg);
  39. Exit;
  40. end;
  41. FNS:=GetNonOptions(Short,Long);
  42. if Length(FNS)=0 then
  43. begin
  44. Usage('Need one or more input files');
  45. exit;
  46. end;
  47. FQuiet:=HasOption('q','quiet');
  48. OFN:=GetOptionValue('o','output');
  49. if (OFN<>'') and (Length(FNS)>1) then
  50. begin
  51. Usage('Cannot specify output file with more than one input file');
  52. exit;
  53. end;
  54. Cnv:=TDPK2LPKConverter.Create(Self);
  55. try
  56. KFN:=GetOptionValue('k','known');
  57. if (KFN<>'') and FileExists(KFN) then
  58. CNV.KnownPackages.LoadFromFile(KFN);
  59. for PFN in FNS do
  60. begin
  61. if (OFN='') then
  62. OFN:=ChangeFileExt(PFN,'.lpk');
  63. CNV.UpdateKnown:=HasOption('u','update');
  64. WriteLog(Format('Converting %s to %s',[PFN,OFN]));
  65. try
  66. CNV.Convert(PFN,OFN);
  67. except
  68. On E : Exception do
  69. WriteLog(Format('Error %s Converting %s to %s : %s',[E.ClassName,PFN,OFN,E.MEssage]));
  70. end;
  71. OFN:='';
  72. end;
  73. if HasOption('u','update') and (KFN<>'') then
  74. CNV.KnownPackages.SaveToFile(KFN);
  75. finally
  76. Cnv.Free;
  77. end;
  78. end;
  79. constructor TDPK2LPKApplication.Create(TheOwner: TComponent);
  80. begin
  81. inherited Create(TheOwner);
  82. StopOnException:=True;
  83. end;
  84. destructor TDPK2LPKApplication.Destroy;
  85. begin
  86. inherited Destroy;
  87. end;
  88. procedure TDPK2LPKApplication.Usage(Const Msg : String);
  89. begin
  90. if Msg<>'' then
  91. Writeln('Error: ',Msg);
  92. Writeln('Usage: ',ExeName, ' [options] File1 [File2]');
  93. Writeln('Where [options] is one or more of:');
  94. Writeln('-h --help this help');
  95. Writeln('-k --known=FILE File with known packages, which can be added to requires if encountered');
  96. Writeln('-q --quiet Produce less output');
  97. Writeln('-u --update Add processed packages to known packages file');
  98. ExitCode:=Ord(Msg<>'');
  99. end;
  100. var
  101. Application: TDPK2LPKApplication;
  102. begin
  103. Application:=TDPK2LPKApplication.Create(nil);
  104. Application.Title:='Convert Delphi To Lazarus Package';
  105. Application.Run;
  106. Application.Free;
  107. end.