22
33namespace Stolt \LeanPackage ;
44
5+ use Stolt \LeanPackage \Exceptions \GitHeadNotAvailable ;
56use Stolt \LeanPackage \Exceptions \GitNotAvailable ;
67use Stolt \LeanPackage \Exceptions \TreeNotAvailable ;
78use Stolt \LeanPackage \Helpers \Str as OsHelper ;
@@ -11,39 +12,50 @@ final class Tree
1112 private Archive $ archive ;
1213
1314 /**
14- * @throws TreeNotAvailable
1515 * @throws GitNotAvailable
1616 */
1717 public function __construct (Archive $ archive )
1818 {
19- if (!$ this ->detectTreeCommand ()) {
20- throw new TreeNotAvailable ();
21- }
22-
2319 $ this ->archive = $ archive ;
2420
2521 if (!$ this ->archive ->isGitCommandAvailable ()) {
2622 throw new GitNotAvailable ();
2723 }
2824 }
2925
26+ /**
27+ * @throws TreeNotAvailable
28+ */
3029 public function getTreeForSrc (string $ directory ): string
3130 {
32- $ command = 'tree -aL 1 --dirsfirst ' . \escapeshellarg ($ directory ) . ' -I .git 2>&1 ' ;
31+ if (!$ this ->detectTreeCommand ()) {
32+ throw new TreeNotAvailable ('Unix tree command is not available. ' );
33+ }
3334
34- if (( new OsHelper ())-> isMacOs ()) {
35- $ command = ' tree -aL 1 --dirsfirst ' . \escapeshellarg ( $ directory ) . ' --gitignore -I .git 2>&1 ' ;
35+ if (! $ this -> detectTreeCommandVersion ()) {
36+ throw new TreeNotAvailable ( ' Required tree command version >=2.0 is not available. ' ) ;
3637 }
3738
39+ $ command = 'tree -aL 1 --dirsfirst ' . \escapeshellarg ($ directory ) . ' --gitignore -I .git 2>&1 ' ;
40+
3841 \exec ($ command , $ output );
3942
4043 $ output [0 ] = '. ' ;
4144
4245 return \implode (PHP_EOL , $ output ) . PHP_EOL ;
4346 }
4447
45- public function getTreeForDistPackage (string $ directory ): string
48+ /**
49+ * @throws TreeNotAvailable
50+ * @throws GitHeadNotAvailable
51+ * @throws GitNotAvailable
52+ */
53+ public function getTreeForDistPackage (): string
4654 {
55+ if (!$ this ->detectTreeCommand ()) {
56+ throw new TreeNotAvailable ('Unix tree command is not available. ' );
57+ }
58+
4759 $ this ->archive ->createArchive ();
4860
4961 $ command = 'tar --list --exclude="*/*" --file ' . \escapeshellarg ($ this ->archive ->getFilename ()) . ' | tree -aL 1 --dirsfirst --fromfile . 2>&1 ' ;
@@ -59,13 +71,27 @@ public function getTreeForDistPackage(string $directory): string
5971 return \implode (PHP_EOL , $ output ) . PHP_EOL ;
6072 }
6173
62- protected function detectTreeCommand (string $ command = ' tree ' ): bool
74+ protected function detectTreeCommand (): bool
6375 {
64- \exec ('where ' . $ command . ' 2>&1 ' , $ output , $ returnValue );
76+ $ command = 'where tree 2>&1 ' ;
77+
6578 if ((new OsHelper ())->isWindows () === false ) {
66- \exec ( ' which ' . $ command . ' 2>&1 ', $ output , $ returnValue ) ;
79+ $ command = ' which tree 2>&1 ' ;
6780 }
6881
82+ \exec ($ command , $ output , $ returnValue );
83+
6984 return $ returnValue === 0 ;
7085 }
86+
87+ protected function detectTreeCommandVersion (): bool
88+ {
89+ \exec ('tree --version 2>&1 ' , $ output );
90+
91+ if (\strpos ($ output [0 ], 'v2 ' )) {
92+ return true ;
93+ }
94+
95+ return false ;
96+ }
7197}
0 commit comments