Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to set preserve-paths in dependency extras. #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Simply install the plugin with composer: `composer require drupal-composer/prese

## Configuration

For configuring the paths you need to set `preserve-paths` within the `extra` of your root `composer.json`.
For configuring the paths you need to set `preserve-paths` within the `extra` of your `composer.json`.

```json
{
Expand All @@ -31,6 +31,24 @@ For configuring the paths you need to set `preserve-paths` within the `extra` of
}
```

In case you need to set the `preserve-paths` option in a dependent package, you must also set `preserve-paths-as-dependency` in the `composer.json` of the dependency.

```json
{
"extra": {
"preserve-paths-as-dependency": true,
"preserve-paths": [
"web/sites/all/modules/contrib",
"web/sites/all/themes/contrib",
"web/sites/all/libraries",
"web/sites/all/drush"
]
}
}
```

You can disable this setting in your root `composer.json` by setting `preserve-paths-ignore-dependencies` to true.

## Example

An example composer.json using [composer/installers](https://packagist.org/packages/composer/installers):
Expand Down
42 changes: 38 additions & 4 deletions src/PluginWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function prePackage(PackageEvent $event)

$preserver = new PathPreserver(
$paths,
$this->getPreservePaths(),
$this->getPreservePaths($packages),
$this->composer->getConfig()->get('cache-dir'),
$this->filesystem,
$this->io
Expand Down Expand Up @@ -158,13 +158,47 @@ protected function getUniqueNameFromPackages(array $packages)
}

/**
* Get preserve paths from root configuration.
* Get preserve paths.
*
* @param \Composer\Package\PackageInterface[] $packages
*
* @return string[]
*/
protected function getPreservePaths()
protected function getPreservePaths($packages)
{
$extra = $this->composer->getPackage()->getExtra();
$paths = array();

/*
* Root package allows that preserve paths from dependencies are also evaluated.
* Negative check as this feature is developed for situations where the root package
* can not be adjusted, but paths must be preserved.
*/
if (!isset($extra['preserve-paths-ignore-dependencies']) || !$extra['preserve-paths-ignore-dependencies']) {
foreach ($packages as $package) {
$paths = array_merge($paths, $this->extractPathsFromExtra($package->getExtra(), false));
}
}

$paths = array_unique(array_merge($paths, $this->extractPathsFromExtra($extra)));

return $this->absolutePaths($paths);
}

/**
* Extract the paths from the extra data.
*
* @param array $extra
* @param boolean $root
*
* @return string[]
*/
protected function extractPathsFromExtra($extra, $root = true)
{
// Package does not explicitly allow to preserve paths if used as dependency.
if (!$root && (!isset($extra['preserve-paths-as-dependency']) || $extra['preserve-paths-as-dependency'] != true)) {
return array();
}

if (!isset($extra['preserve-paths'])) {
$paths = array();
Expand All @@ -174,7 +208,7 @@ protected function getPreservePaths()
$paths = array_values((array) $extra['preserve-paths']);
}

return $this->absolutePaths($paths);
return $paths;
}

/**
Expand Down