11<?php namespace xp \lambda ;
22
33use io \Path ;
4- use io \archive \zip \{ZipFile , ZipDirEntry , ZipFileEntry , Compression };
4+ use io \archive \zip \{ZipFile , ZipArchiveWriter , ZipDirEntry , ZipFileEntry , Compression };
55use io \streams \StreamTransfer ;
66use util \cmd \Console ;
77
88class PackageLambda {
99 const COMPRESSION_THRESHOLD = 24 ;
1010
11- private $ target , $ sources , $ base , $ exclude , $ compression ;
11+ private $ target , $ sources , $ exclude , $ compression ;
1212
13- public function __construct (Path $ target , Path $ base , array $ sources , string $ exclude = '#(^|/)(\..+|src/test|src/it)(/|$)# ' ) {
13+ public function __construct (Path $ target , Sources $ sources , string $ exclude = '#(^|/)(\..+|src/test|src/it)(/|$)# ' ) {
1414 $ this ->target = $ target ;
15- $ this ->base = $ base ->asRealpath ();
1615 $ this ->sources = $ sources ;
1716 $ this ->exclude = $ exclude ;
1817
@@ -26,38 +25,38 @@ public function __construct(Path $target, Path $base, array $sources, string $ex
2625 }
2726 }
2827
29- /** Returns ZIP file entries */
30- private function entries ( Path $ path ) {
28+ /** Adds ZIP file entries */
29+ private function add ( ZipArchiveWriter $ zip , Path $ path ) {
3130 if (preg_match ($ this ->exclude , $ path ->toString ('/ ' ))) return ;
3231
33- $ relative = trim ( str_replace ( $ this ->base , '' , $ path ), DIRECTORY_SEPARATOR );
32+ $ relative = $ path -> relativeTo ( $ this ->sources -> base );
3433 if ($ path ->isFile ()) {
35- yield function ($ z ) use ($ relative , $ path ) {
36- $ file = $ z ->add (new ZipFileEntry ($ relative ));
34+ $ file = $ zip ->add (new ZipFileEntry ($ relative ));
3735
38- // See https://stackoverflow.com/questions/46716095/minimum-file-size-for-compression-algorithms
39- if (filesize ($ path ) > self ::COMPRESSION_THRESHOLD ) {
40- $ file ->setCompression ($ this ->compression , 9 );
41- }
42- (new StreamTransfer ($ path ->asFile ()->in (), $ file ->out ()))->transferAll ();
43- return $ file ;
44- };
36+ // See https://stackoverflow.com/questions/46716095/minimum-file-size-for-compression-algorithms
37+ if (filesize ($ path ) > self ::COMPRESSION_THRESHOLD ) {
38+ $ file ->setCompression ($ this ->compression , 9 );
39+ }
40+ (new StreamTransfer ($ path ->asFile ()->in (), $ file ->out ()))->transferAll ();
41+ yield $ file ;
4542 } else {
46- yield function ( $ z ) use ( $ relative ) { return $ z ->add (new ZipDirEntry ($ relative )); } ;
43+ yield $ zip ->add (new ZipDirEntry ($ relative ));
4744 foreach ($ path ->asFolder ()->entries () as $ entry ) {
48- yield from $ this ->entries ( $ entry );
45+ yield from $ this ->add ( $ zip , $ entry );
4946 }
5047 }
5148 }
5249
5350 public function run (): int {
5451 Console::writeLine ('[+] Creating ' , $ this ->target , ' (compression: ' , $ this ->compression , ') ' );
5552 $ z = ZipFile::create ($ this ->target ->asFile ()->out ());
56- foreach ($ this ->sources as $ i => $ source ) {
57- Console::writef ("\e[34m => [%d/%d] " , $ i + 1 , sizeof ($ this ->sources ) + 1 );
53+
54+ $ sources = [...$ this ->sources ];
55+ $ total = sizeof ($ sources ) + 1 ;
56+ foreach ($ sources as $ i => $ source ) {
57+ Console::writef ("\e[34m => [%d/%d] " , $ i + 1 , $ total );
5858 $ entries = 0 ;
59- foreach ($ this ->entries (new Path ($ source )) as $ action ) {
60- $ entry = $ action ($ z );
59+ foreach ($ this ->add ($ z , new Path ($ source )) as $ entry ) {
6160 $ entries ++;
6261 Console::writef ('%-60s %4d%s ' , substr ($ entry ->getName (), -60 ), $ entries , str_repeat ("\x08" , 65 ));
6362 }
@@ -66,7 +65,7 @@ public function run(): int {
6665
6766 $ file = $ z ->add (new ZipFileEntry ('class.pth ' ));
6867 $ file ->out ()->write (preg_replace ($ this ->exclude .'m ' , '?$1 ' , file_get_contents ('class.pth ' )));
69- Console::writeLinef ("\e[34m => [%1 \$d/%1 \$d] class.pth \e[0m " , sizeof ( $ this -> sources ) + 1 );
68+ Console::writeLinef ("\e[34m => [%1 \$d/%1 \$d] class.pth \e[0m " , $ total );
7069
7170 $ z ->close ();
7271 Console::writeLine ();
0 commit comments