Skip to content

Commit 1f58904

Browse files
authored
Merge pull request #249 from asgrim/add-working-directory-option-to-pie-install
Add working directory option to pie install
2 parents d212b21 + ba7cf84 commit 1f58904

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/Command/CommandHelper.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ final class CommandHelper
4242
public const OPTION_WITH_PHP_CONFIG = 'with-php-config';
4343
public const OPTION_WITH_PHP_PATH = 'with-php-path';
4444
public const OPTION_WITH_PHPIZE_PATH = 'with-phpize-path';
45+
public const OPTION_WORKING_DIRECTORY = 'working-dir';
4546
private const OPTION_MAKE_PARALLEL_JOBS = 'make-parallel-jobs';
4647
private const OPTION_SKIP_ENABLE_EXTENSION = 'skip-enable-extension';
4748
private const OPTION_FORCE = 'force';
@@ -73,13 +74,16 @@ public static function configurePhpConfigOptions(Command $command): void
7374
);
7475
}
7576

76-
public static function configureDownloadBuildInstallOptions(Command $command): void
77+
public static function configureDownloadBuildInstallOptions(Command $command, bool $withRequestedPackageAndVersion = true): void
7778
{
78-
$command->addArgument(
79-
self::ARG_REQUESTED_PACKAGE_AND_VERSION,
80-
InputArgument::OPTIONAL,
81-
'The PIE package name and version constraint to use, in the format {vendor/package}{?:{?version-constraint}{?@stability}}, for example `xdebug/xdebug:^3.4@alpha`, `xdebug/xdebug:@alpha`, `xdebug/xdebug:^3.4`, etc.',
82-
);
79+
if ($withRequestedPackageAndVersion) {
80+
$command->addArgument(
81+
self::ARG_REQUESTED_PACKAGE_AND_VERSION,
82+
InputArgument::OPTIONAL,
83+
'The PIE package name and version constraint to use, in the format {vendor/package}{?:{?version-constraint}{?@stability}}, for example `xdebug/xdebug:^3.4@alpha`, `xdebug/xdebug:@alpha`, `xdebug/xdebug:^3.4`, etc.',
84+
);
85+
}
86+
8387
$command->addOption(
8488
self::OPTION_MAKE_PARALLEL_JOBS,
8589
'j',
@@ -99,6 +103,13 @@ public static function configureDownloadBuildInstallOptions(Command $command): v
99103
'To attempt to install a version that doesn\'t match the version constraints from the meta-data, for instance to install an older version than recommended, or when the signature is not available.',
100104
);
101105

106+
$command->addOption(
107+
self::OPTION_WORKING_DIRECTORY,
108+
'd',
109+
InputOption::VALUE_REQUIRED,
110+
'The working directory to use, where applicable. If not specified, the current working directory is used. Only used in certain contexts.',
111+
);
112+
102113
self::configurePhpConfigOptions($command);
103114

104115
/**

src/Command/InstallExtensionsForProjectCommand.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
use function array_merge;
3333
use function array_walk;
3434
use function assert;
35+
use function chdir;
3536
use function getcwd;
3637
use function in_array;
38+
use function is_dir;
3739
use function is_string;
3840
use function realpath;
3941
use function sprintf;
@@ -63,32 +65,58 @@ public function configure(): void
6365
{
6466
parent::configure();
6567

66-
CommandHelper::configurePhpConfigOptions($this);
68+
CommandHelper::configureDownloadBuildInstallOptions($this, false);
6769
}
6870

6971
public function execute(InputInterface $input, OutputInterface $output): int
7072
{
7173
$helper = $this->getHelper('question');
7274
assert($helper instanceof QuestionHelper);
7375

76+
$workingDirOption = (string) $input->getOption(CommandHelper::OPTION_WORKING_DIRECTORY);
77+
$restoreWorkingDir = static function (): void {
78+
};
79+
if ($workingDirOption !== '' && is_dir($workingDirOption)) {
80+
$currentWorkingDir = getcwd();
81+
$restoreWorkingDir = static function () use ($currentWorkingDir, $output): void {
82+
chdir($currentWorkingDir);
83+
$output->writeln(
84+
sprintf('Restored working directory to: %s', $currentWorkingDir),
85+
OutputInterface::VERBOSITY_VERBOSE,
86+
);
87+
};
88+
89+
chdir($workingDirOption);
90+
$output->writeln(
91+
sprintf('Changed working directory to: %s', $workingDirOption),
92+
OutputInterface::VERBOSITY_VERBOSE,
93+
);
94+
}
95+
7496
$rootPackage = $this->composerFactoryForProject->rootPackage($input, $output);
7597

7698
if (ExtensionType::isValid($rootPackage->getType())) {
7799
$cwd = realpath(getcwd());
78100
if (! is_string($cwd) || $cwd === '') {
79101
$output->writeln('<error>Failed to determine current working directory.</error>');
80102

103+
$restoreWorkingDir();
104+
81105
return Command::FAILURE;
82106
}
83107

84-
return ($this->installPiePackageFromPath)(
108+
$exit = ($this->installPiePackageFromPath)(
85109
$this,
86110
$cwd,
87111
$rootPackage,
88112
PieJsonEditor::fromTargetPlatform(CommandHelper::determineTargetPlatformFromInputs($input, new NullOutput())),
89113
$input,
90114
$output,
91115
);
116+
117+
$restoreWorkingDir();
118+
119+
return $exit;
92120
}
93121

94122
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);
@@ -201,6 +229,8 @@ static function (array $match): string {
201229

202230
$output->writeln(PHP_EOL . 'Finished checking extensions.');
203231

232+
$restoreWorkingDir();
233+
204234
/**
205235
* @psalm-suppress TypeDoesNotContainType
206236
* @psalm-suppress RedundantCondition

0 commit comments

Comments
 (0)