pkgarchive.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. unit pkgarchive;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes,SysUtils,pkghandler;
  6. type
  7. { TUnzipArchive }
  8. TUnzipArchive = Class(TPackagehandler)
  9. Private
  10. Procedure UnzipArchive;
  11. Public
  12. Function Execute(const Args:TActionArgs):boolean;override;
  13. end;
  14. { TCreateArchive }
  15. TCreateArchive = Class(TPackagehandler)
  16. Private
  17. Procedure CreateArchive;
  18. Public
  19. Function Execute(const Args:TActionArgs):boolean;override;
  20. end;
  21. implementation
  22. uses
  23. fprepos,
  24. fpxmlrep,
  25. zipper,
  26. uriparser,
  27. pkgglobals,
  28. pkgmessages;
  29. { TUnzipArchive }
  30. Procedure TUnzipArchive.UnzipArchive;
  31. Var
  32. BuildDir : string;
  33. ArchiveFile : String;
  34. begin
  35. ArchiveFile:=PackageLocalArchive;
  36. BuildDir:=PackageBuildPath;
  37. { Download file if it doesn't exists yet }
  38. if not FileExists(ArchiveFile) then
  39. ExecuteAction(CurrentPackage,'downloadpackage');
  40. { Create builddir, remove it first if needed }
  41. if DirectoryExists(BuildDir) then
  42. DeleteDir(BuildDir);
  43. ForceDirectories(BuildDir);
  44. SetCurrentDir(BuildDir);
  45. { Unzip Archive }
  46. With TUnZipper.Create do
  47. try
  48. Log(vCommands,SLogUnzippping,[ArchiveFile]);
  49. OutputPath:=PackageBuildPath;
  50. UnZipAllFiles(ArchiveFile);
  51. Finally
  52. Free;
  53. end;
  54. end;
  55. function TUnzipArchive.Execute(const Args:TActionArgs):boolean;
  56. begin
  57. {$warning TODO Check arguments}
  58. UnzipArchive;
  59. result:=true;
  60. end;
  61. { TCreateArchive }
  62. procedure TCreateArchive.CreateArchive;
  63. var
  64. P : TFPPackage;
  65. PS : TFPPackages;
  66. X : TFPXMLRepositoryHandler;
  67. SL : TStringList;
  68. begin
  69. if assigned(CurrentPackage) then
  70. Error(SErrOnlyLocalDir);
  71. { Generate manifest.xml if it doesn't exists yet }
  72. if not FileExists(PackageManifestFile) then
  73. ExecuteAction(CurrentPackage,'fpmakemanifest');
  74. { Load manifest.xml }
  75. PS:=TFPPackages.Create(TFPPackage);
  76. X:=TFPXMLRepositoryHandler.Create;
  77. With X do
  78. try
  79. LoadFromXml(PS,PackageManifestFile);
  80. finally
  81. Free;
  82. end;
  83. { Create archive, currently support only 1 file per package, this
  84. can be enhanced in the future if needed }
  85. if PS.Count<>1 then
  86. Error('Only one package supported per manifest');
  87. P:=PS[0];
  88. { Unzip Archive }
  89. With TZipper.Create do
  90. try
  91. Log(vCommands,SLogZippping,[P.FileName]);
  92. {$warning TODO replace with files from manifest}
  93. try
  94. SL:=TStringList.Create;
  95. SearchFiles(SL,AllFiles);
  96. if SL.Count=0 then
  97. Error('No files found');
  98. ZipFiles(P.FileName,SL);
  99. finally
  100. SL.Free;
  101. end;
  102. Finally
  103. Free;
  104. end;
  105. P.Free;
  106. end;
  107. function TCreateArchive.Execute(const Args: TActionArgs): boolean;
  108. begin
  109. CreateArchive;
  110. Result:=true;
  111. end;
  112. initialization
  113. RegisterPkgHandler('unziparchive',TUnzipArchive);
  114. RegisterPkgHandler('createarchive',TCreateArchive);
  115. end.