Skip to content

Commit 84acf77

Browse files
committed
Adds a check for the Unix tree command >=v2.*
1 parent fdc37eb commit 84acf77

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,19 @@ Available presets are `PHP`, `Python`, and `Go`. With `PHP` being the default.
197197
#### Tree command
198198

199199
The `tree` command of the lean package validator allows you to inspect the __flat__ `source` and `dist package` structure
200-
of the given project/micro-package. It requires the [tree](https://en.wikipedia.org/wiki/Tree_(command)) command to be installed.
200+
of the given project/micro-package. It requires the [tree](https://en.wikipedia.org/wiki/Tree_(command)) Unix command to
201+
be installed in version `>=2.0`.
201202

202203
``` bash
203204
lean-package-validator tree [<directory>] --src
204205

205206
Package: stolt/lean-package-validator
206207
.
207208
├── bin
208-
├── coverage-reports
209209
├── example
210-
├── .git
211210
├── .github
212-
├── .idea
213-
├── .phpunit.cache
214211
├── src
215212
├── tests
216-
├── vendor
217213
├── box.json.dist
218214
├── CHANGELOG.md
219215
├── composer.json
@@ -230,7 +226,7 @@ Package: stolt/lean-package-validator
230226
├── phpunit.xml.dist
231227
└── README.md
232228

233-
10 directories, 15 files
229+
5 directories, 15 files
234230
```
235231

236232
``` bash

src/Commands/TreeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8181
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
8282

8383
$output->writeln('Package: <info>' . $this->getPackageName() . '</info>');
84-
$output->write($this->tree->getTreeForDistPackage($this->directoryToOperateOn));
84+
$output->write($this->tree->getTreeForDistPackage());
8585

8686
return Command::SUCCESS;
8787
}

src/Tree.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Stolt\LeanPackage;
44

5+
use Stolt\LeanPackage\Exceptions\GitHeadNotAvailable;
56
use Stolt\LeanPackage\Exceptions\GitNotAvailable;
67
use Stolt\LeanPackage\Exceptions\TreeNotAvailable;
78
use 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

Comments
 (0)