Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions e2e/scenario13/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/actual.txt
/composer.lock
/vendor/
/vendor-bin/*/composer.lock
/vendor-bin/*/vendor/
1 change: 1 addition & 0 deletions e2e/scenario13/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test explicitly configured forwarded commands are forwarded.
23 changes: 23 additions & 0 deletions e2e/scenario13/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"repositories": [
{
"type": "path",
"url": "../../"
}
],
"require-dev": {
"bamarni/composer-bin-plugin": "dev-master"
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
},
"extra": {
"bamarni-bin": {
"bin-links": false,
"forward-command": true,
"forwarded-commands": ["update", "validate"]
}
}
}
62 changes: 62 additions & 0 deletions e2e/scenario13/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
composer update:
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking bamarni/composer-bin-plugin (dev-hash)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing bamarni/composer-bin-plugin (dev-hash): Symlinking from ../..
Generating autoload files
[bamarni-bin] The command is being forwarded.
[bamarni-bin] Checking namespace vendor-bin/ns1
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
[bamarni-bin] Checking namespace vendor-bin/ns2
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files

composer install:
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Nothing to install, update or remove
Generating autoload files

composer validate:
./composer.json is valid for simple usage with Composer but has
strict errors that make it unable to be published as a package
See https://getcomposer.org/doc/04-schema.md for details on the schema
# General warnings
- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
# Publish warnings
- name : The property name is required
- description : The property description is required
[bamarni-bin] The command is being forwarded.
[bamarni-bin] Checking namespace vendor-bin/ns1
./composer.json is valid for simple usage with Composer but has
strict errors that make it unable to be published as a package
See https://getcomposer.org/doc/04-schema.md for details on the schema
# General warnings
- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
# Publish warnings
- name : The property name is required
- description : The property description is required
[bamarni-bin] Checking namespace vendor-bin/ns2
./composer.json is valid for simple usage with Composer but has
strict errors that make it unable to be published as a package
See https://getcomposer.org/doc/04-schema.md for details on the schema
# General warnings
- No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
# Publish warnings
- name : The property name is required
- description : The property description is required
36 changes: 36 additions & 0 deletions e2e/scenario13/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

set -Eeuo pipefail

# Set env variables in order to experience a behaviour closer to what happens
# in the CI locally. It should not hurt to set those in the CI as the CI should
# contain those values.
export CI=1
export COMPOSER_NO_INTERACTION=1

readonly ORIGINAL_WORKING_DIR=$(pwd)

trap "cd ${ORIGINAL_WORKING_DIR}" err exit

# Change to script directory
cd "$(dirname "$0")"

# Ensure we have a clean state
rm -rf actual.txt || true
rm -rf composer.lock || true
rm -rf vendor || true
rm -rf vendor-bin/*/composer.lock || true
rm -rf vendor-bin/*/vendor || true

(
echo "composer update:"
composer update || true
echo

echo "composer install:"
composer install || true
echo

echo "composer validate:"
composer validate --no-check-publish || true
) 2>&1 | tee >> actual.txt
1 change: 1 addition & 0 deletions e2e/scenario13/vendor-bin/ns1/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions e2e/scenario13/vendor-bin/ns2/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 1 addition & 5 deletions src/BamarniBinPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
*/
class BamarniBinPlugin implements PluginInterface, Capable, EventSubscriberInterface
{
private const FORWARDED_COMMANDS = ['install', 'update'];

/**
* @var Composer
*/
Expand Down Expand Up @@ -130,9 +128,7 @@ private function onEvent(
}
}

if ($config->isCommandForwarded()
&& in_array($commandName, self::FORWARDED_COMMANDS, true)
) {
if ($config->isCommandForwarded($commandName)) {
return $this->onForwardedCommand($input, $output);
}

Expand Down
32 changes: 27 additions & 5 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ final class Config
public const BIN_LINKS_ENABLED = 'bin-links';
public const TARGET_DIRECTORY = 'target-directory';
public const FORWARD_COMMAND = 'forward-command';
public const FORWARDED_COMMANDS = 'forwarded-commands';

private const DEFAULT_CONFIG = [
self::BIN_LINKS_ENABLED => true,
self::TARGET_DIRECTORY => 'vendor-bin',
self::FORWARD_COMMAND => false,
self::FORWARDED_COMMANDS => ['install', 'update'],
];

/**
Expand All @@ -37,9 +39,9 @@ final class Config
private $targetDirectory;

/**
* @var bool
* @var list<string>
*/
private $forwardCommand;
private $forwardedCommands;

/**
* @var list<string>
Expand Down Expand Up @@ -126,9 +128,24 @@ public function __construct(array $extra)
);
}

$forwardedCommands = $config[self::FORWARDED_COMMANDS];

if (!is_array($forwardedCommands)) {
throw new InvalidBamarniComposerExtraConfig(
sprintf(
'Expected setting "extra.%s.%s" to be an array value. Got "%s".',
self::EXTRA_CONFIG_KEY,
self::FORWARDED_COMMANDS,
gettype($forwardCommand)
)
);
}

$forwardedCommandsSetExplicitly = array_key_exists(self::FORWARDED_COMMANDS, $userExtra);

$this->binLinks = $binLinks;
$this->targetDirectory = $targetDirectory;
$this->forwardCommand = $forwardCommand;
$this->forwardedCommands = $forwardCommand ? $forwardedCommands : [];
}

public function binLinksAreEnabled(): bool
Expand All @@ -141,9 +158,14 @@ public function getTargetDirectory(): string
return $this->targetDirectory;
}

public function isCommandForwarded(): bool
public function isCommandForwarded(?string $name = null): bool
{
return $this->forwardCommand;
// Trigger deprecation.
if ($name === null) {
return false;
}

return in_array($name, $this->forwardedCommands, true);
}

/**
Expand Down
40 changes: 38 additions & 2 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ public function test_it_can_be_instantiated(
array $extra,
bool $expectedBinLinksEnabled,
string $expectedTargetDirectory,
bool $expectedForwardCommand,
bool $expectedForwardInstallCommand,
bool $expectedForwardUpdateCommand,
array $expectedDeprecations
): void {
$config = new Config($extra);

self::assertSame($expectedBinLinksEnabled, $config->binLinksAreEnabled());
self::assertSame($expectedTargetDirectory, $config->getTargetDirectory());
self::assertSame($expectedForwardCommand, $config->isCommandForwarded());
self::assertSame($expectedForwardInstallCommand, $config->isCommandForwarded('install'));
self::assertSame($expectedForwardUpdateCommand, $config->isCommandForwarded('update'));
self::assertSame($expectedDeprecations, $config->getDeprecations());
}

Expand All @@ -44,6 +46,7 @@ public static function provideExtraConfig(): iterable
true,
'vendor-bin',
false,
false,
[
$binLinksEnabledDeprecationMessage,
$forwardCommandDeprecationMessage,
Expand All @@ -55,6 +58,7 @@ public static function provideExtraConfig(): iterable
true,
'vendor-bin',
false,
false,
[
$binLinksEnabledDeprecationMessage,
$forwardCommandDeprecationMessage,
Expand All @@ -71,6 +75,7 @@ public static function provideExtraConfig(): iterable
true,
'vendor-bin',
false,
false,
[],
];

Expand All @@ -85,6 +90,37 @@ public static function provideExtraConfig(): iterable
false,
'tools',
true,
true,
[],
];

yield 'only forward install command' => [
[
Config::EXTRA_CONFIG_KEY => [
Config::BIN_LINKS_ENABLED => false,
Config::FORWARD_COMMAND => true,
Config::FORWARDED_COMMANDS => ['install'],
],
],
false,
'vendor-bin',
true,
false,
[],
];

yield 'do not forward install command' => [
[
Config::EXTRA_CONFIG_KEY => [
Config::BIN_LINKS_ENABLED => false,
Config::FORWARD_COMMAND => true,
Config::FORWARDED_COMMANDS => ['about', 'update'],
],
],
false,
'vendor-bin',
false,
true,
[],
];
}
Expand Down