Skip to content

Commit f8b1ab5

Browse files
committed
Add extension .phar to installed tools
Signed-off-by: Anatoliy Melnikov <[email protected]>
1 parent 9dee308 commit f8b1ab5

File tree

7 files changed

+182
-1
lines changed

7 files changed

+182
-1
lines changed

src/commands/help/help.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
_-t, --target_ Set custom target directory for the PHAR
2626

2727
_-c, --copy_ Copy PHAR file instead of using symlink
28+
_-e, --extension_ Add extension ".phar" to installed file. It can be usefull for autocompletion by IDE
29+
in configuration files of installed tools.
2830
_-g, --global_ Install a copy of the PHAR globally (likely requires root privileges)
2931
_--temporary_ Do not add entries in phive.xml for installed PHARs
3032
_--trust-gpg-keys_ Silently import these keys when required (40-digit fingerprint

src/commands/install/InstallCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ protected function getConfig(): InstallCommandConfig {
6363
return $this->config;
6464
}
6565

66+
private function normaliseName(string $pharName): string {
67+
if ($this->config->withExtension() && '.phar' !== substr($pharName, -5)) {
68+
$pharName .= '.phar';
69+
}
70+
71+
return $pharName;
72+
}
73+
6674
private function resolveToRelease(RequestedPhar $requestedPhar): SupportedRelease {
6775
$repository = $this->pharResolver->resolve($requestedPhar);
6876
$releases = $repository->getReleasesByRequestedPhar($requestedPhar);
@@ -80,6 +88,6 @@ private function getDestination(string $pharName, RequestedPhar $requestedPhar,
8088
return $requestedPhar->getLocation();
8189
}
8290

83-
return $destination->file($pharName);
91+
return $destination->file($this->normaliseName($pharName));
8492
}
8593
}

src/commands/install/InstallCommandConfig.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function forceAcceptUnsignedPhars(): bool {
8282
return $this->cliOptions->hasOption('force-accept-unsigned');
8383
}
8484

85+
public function withExtension(): bool {
86+
return $this->cliOptions->hasOption('extension');
87+
}
88+
8589
/**
8690
* @throws ConfiguredPharException
8791
*

src/commands/install/InstallContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ protected function getKnownOptions(): array {
2222
return [
2323
'target' => 't',
2424
'copy' => 'c',
25+
'extension' => 'e',
2526
'global' => 'g',
2627
'temporary' => false,
2728
'trust-gpg-keys' => false,

tests/unit/commands/install/InstallCommandConfigTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,28 @@ public function testDoNotAddToPhiveXml($switch): void {
148148
$this->assertSame($switch, $config->doNotAddToPhiveXml());
149149
}
150150

151+
/**
152+
* @dataProvider boolProvider
153+
*
154+
* @param bool $switch
155+
*/
156+
public function testWithExtension($switch): void {
157+
$options = $this->getOptionsMock();
158+
$options->expects($this->once())
159+
->method('hasOption')
160+
->with('extension')
161+
->willReturn($switch);
162+
163+
$config = new InstallCommandConfig(
164+
$options,
165+
$this->getPhiveXmlConfigMock(),
166+
$this->getEnvironmentMock(),
167+
$this->getTargetDirectoryLocatorMock()
168+
);
169+
170+
$this->assertSame($switch, $config->withExtension());
171+
}
172+
151173
public function testConvertsRequestedPharAliasToLowercase(): void {
152174
$options = $this->getOptionsMock();
153175
$options->expects($this->any())
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of Phive.
4+
*
5+
* Copyright (c) Arne Blankerts <[email protected]>, Sebastian Heuer <[email protected]> and contributors
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
*/
11+
12+
namespace PharIo\Phive;
13+
14+
use PharIo\FileSystem\Directory;
15+
use PharIo\Version\AnyVersionConstraint;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @covers \PharIo\Phive\InstallCommandConfig
20+
*/
21+
class InstallCommandTest extends TestCase
22+
{
23+
use ScalarTestDataProvider;
24+
25+
/**
26+
* @dataProvider boolProvider
27+
*
28+
* @param bool $switch
29+
*/
30+
public function testAddingOfExtension($switch): void
31+
{
32+
$pharName = $expectedPharName = 'some-path';
33+
if ($switch) {
34+
$expectedPharName .= '.phar';
35+
}
36+
37+
$directory = $this->createMock(Directory::class);
38+
$directory
39+
->expects($this->once())
40+
->method('file')
41+
->with($expectedPharName);
42+
43+
$requestedPhars = [
44+
new RequestedPhar(
45+
new PharAlias($pharName),
46+
new AnyVersionConstraint(),
47+
new AnyVersionConstraint(),
48+
null,
49+
true
50+
),
51+
];
52+
53+
$config = $this->createMock(InstallCommandConfig::class);
54+
$config
55+
->method('getTargetDirectory')
56+
->willReturn($directory);
57+
58+
$config
59+
->method('getRequestedPhars')
60+
->willReturn($requestedPhars);
61+
62+
$config
63+
->method('withExtension')
64+
->willReturn($switch);
65+
66+
$installService = $this->createMock(InstallService::class);
67+
$sourceRepository = $this->createMock(SourceRepository::class);
68+
69+
$pharResolver = $this->createMock(RequestedPharResolverService::class);
70+
$pharResolver
71+
->method('resolve')
72+
->willReturn($sourceRepository);
73+
74+
$url = $this->createMock(PharUrl::class);
75+
$url
76+
->method('getPharName')
77+
->willReturn($pharName);
78+
79+
$release = $this->createMock(SupportedRelease::class);
80+
$release
81+
->method('getUrl')
82+
->willReturn($url);
83+
84+
$selector = $this->createMock(ReleaseSelector::class);
85+
$selector
86+
->method('select')
87+
->willReturn($release);
88+
89+
$command = new InstallCommand($config, $installService, $pharResolver, $selector);
90+
$command->execute();
91+
}
92+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of Phive.
4+
*
5+
* Copyright (c) Arne Blankerts <[email protected]>, Sebastian Heuer <[email protected]> and contributors
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
*/
11+
namespace PharIo\Phive;
12+
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @covers \PharIo\Phive\InstallContext
17+
*/
18+
class InstallContextTest extends TestCase {
19+
/**
20+
* @dataProvider knownOptionCharProvider
21+
*
22+
* @param string $optionChar
23+
*/
24+
public function testHasOptionForChar($optionChar): void
25+
{
26+
$context = new InstallContext();
27+
self::assertTrue($context->hasOptionForChar($optionChar));
28+
}
29+
30+
/**
31+
* @dataProvider knowsOptionProvider
32+
*
33+
* @param string $option
34+
*/
35+
public function testKnowsOption($option): void
36+
{
37+
$context = new InstallContext();
38+
self::assertTrue($context->knowsOption($option));
39+
}
40+
41+
public function knowsOptionProvider(): array {
42+
return [
43+
['extension'],
44+
];
45+
}
46+
47+
public function knownOptionCharProvider(): array {
48+
return [
49+
['e'],
50+
];
51+
}
52+
}

0 commit comments

Comments
 (0)