Skip to content

Commit 4b3c729

Browse files
authored
Merge pull request #1174 from veewee/windows-command-locator
Change command locator suffix order to avoid issues on Windows
2 parents d6ba4be + 8615c0a commit 4b3c729

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

spec/Locator/ExternalCommandSpec.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GrumPHP\Exception\RuntimeException;
66
use GrumPHP\Locator\ExternalCommand;
7+
use GrumPHP\Util\Platform;
78
use PhpSpec\ObjectBehavior;
89
use Symfony\Component\Process\ExecutableFinder;
910

@@ -23,25 +24,35 @@ function it_throws_exception_when_external_command_is_not_found(ExecutableFinder
2324
{
2425
$executableFinder->find('test', null, ['bin'])->willReturn(false);
2526
$executableFinder->find('test.phar', null, ['bin'])->willReturn(false);
27+
$executableFinder->find('test.bat', null, ['bin'])->willReturn(false);
2628
$this->shouldThrow(RuntimeException::class)->duringLocate('test');
2729
}
2830

2931
function it_locates_external_commands(ExecutableFinder $executableFinder)
3032
{
33+
$executableFinder->find('test.bat', null, ['bin'])->willReturn(false);
3134
$executableFinder->find('test', null, ['bin'])->willReturn('bin/test');
3235
$this->locate('test')->shouldEqual('bin/test');
3336
}
3437

3538
function it_locates_external_commands_with_a_suffix(ExecutableFinder $executableFinder)
3639
{
40+
$executableFinder->find('test.bat', null, ['bin'])->willReturn(false);
3741
$executableFinder->find('test', null, ['bin'])->willReturn(false);
3842
$executableFinder->find('test.phar', null, ['bin'])->willReturn('bin/test.phar');
3943
$this->locate('test')->shouldEqual('bin/test.phar');
4044
}
4145

4246
function it_locates_external_commands_without_suffix_first(ExecutableFinder $executableFinder) {
47+
$executableFinder->find('test.bat', null, ['bin'])->willReturn('bin/test.bat');
4348
$executableFinder->find('test', null, ['bin'])->willReturn('bin/test');
4449
$executableFinder->find('test.phar', null, ['bin'])->willReturn('bin/test.phar');
50+
51+
if (Platform::isWindows()) {
52+
$this->locate('test')->shouldEqual('bin/test.bat');
53+
return;
54+
}
55+
4556
$this->locate('test')->shouldEqual('bin/test');
4657
}
4758
}

src/Locator/ExternalCommand.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66

77
use GrumPHP\Exception\ExecutableNotFoundException;
88
use GrumPHP\Util\Paths;
9+
use GrumPHP\Util\Platform;
910
use Symfony\Component\Process\ExecutableFinder;
1011

1112
class ExternalCommand
1213
{
13-
/**
14-
* @var list<string>
15-
*/
16-
private $suffixes = ['', '.phar'];
17-
1814
/**
1915
* @var string
2016
*/
@@ -41,9 +37,9 @@ public static function loadWithPaths(Paths $paths, ExecutableFinder $executableF
4137

4238
public function locate(string $command): string
4339
{
44-
foreach ($this->suffixes as $suffix) {
40+
$suffixes = Platform::isWindows() ? ['.bat', '', '.phar'] : ['', '.phar'];
41+
foreach ($suffixes as $suffix) {
4542
$cmdName = $command . $suffix;
46-
// Search executable:
4743
$executable = $this->executableFinder->find($cmdName, null, [$this->binDir]);
4844

4945
if ($executable) {

0 commit comments

Comments
 (0)