Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
|
|
|
<?php
|
|
|
|
|
2019-08-22 02:45:53 +00:00
|
|
|
namespace Drupal\Composer\Plugin\Scaffold;
|
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
|
|
|
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage the .gitignore file.
|
|
|
|
*/
|
|
|
|
class ManageGitIgnore {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Composer's I/O service.
|
|
|
|
*
|
|
|
|
* @var \Composer\IO\IOInterface
|
|
|
|
*/
|
|
|
|
protected $io;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The directory where the project is located.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $dir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ManageGitIgnore constructor.
|
|
|
|
*
|
|
|
|
* @param string $dir
|
|
|
|
* The directory where the project is located.
|
|
|
|
*/
|
|
|
|
public function __construct(IOInterface $io, $dir) {
|
|
|
|
$this->io = $io;
|
|
|
|
$this->dir = $dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages gitignore files.
|
|
|
|
*
|
2019-08-22 02:45:53 +00:00
|
|
|
* @param \Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldResult[] $files
|
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
|
|
|
* A list of scaffold results, each of which holds a path and whether
|
|
|
|
* or not that file is managed.
|
2019-08-22 02:45:53 +00:00
|
|
|
* @param \Drupal\Composer\Plugin\Scaffold\ScaffoldOptions $options
|
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
|
|
|
* Configuration options from the composer.json extras section.
|
|
|
|
*/
|
|
|
|
public function manageIgnored(array $files, ScaffoldOptions $options) {
|
|
|
|
if (!$this->managementOfGitIgnoreEnabled($options)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate entries to add to .gitignore, sorted into buckets based on the
|
|
|
|
// location of the .gitignore file the entry should be added to.
|
|
|
|
$add_to_git_ignore = [];
|
|
|
|
foreach ($files as $scaffoldResult) {
|
|
|
|
$path = $scaffoldResult->destination()->fullPath();
|
|
|
|
$is_ignored = Git::checkIgnore($this->io, $path, $this->dir);
|
|
|
|
if (!$is_ignored) {
|
|
|
|
$is_tracked = Git::checkTracked($this->io, $path, $this->dir);
|
|
|
|
if (!$is_tracked && $scaffoldResult->isManaged()) {
|
|
|
|
$dir = realpath(dirname($path));
|
|
|
|
$name = basename($path);
|
|
|
|
$add_to_git_ignore[$dir][] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write out the .gitignore files one at a time.
|
|
|
|
foreach ($add_to_git_ignore as $dir => $entries) {
|
|
|
|
$this->addToGitIgnore($dir, $entries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether we should manage gitignore files.
|
|
|
|
*
|
2019-08-22 02:45:53 +00:00
|
|
|
* @param \Drupal\Composer\Plugin\Scaffold\ScaffoldOptions $options
|
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
|
|
|
* Configuration options from the composer.json extras section.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* Whether or not gitignore files should be managed.
|
|
|
|
*/
|
|
|
|
protected function managementOfGitIgnoreEnabled(ScaffoldOptions $options) {
|
|
|
|
// If the composer.json stipulates whether gitignore is managed or not, then
|
|
|
|
// follow its recommendation.
|
|
|
|
if ($options->hasGitIgnore()) {
|
|
|
|
return $options->gitIgnore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not manage .gitignore if there is no repository here.
|
|
|
|
if (!Git::isRepository($this->io, $this->dir)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the composer.json did not specify whether or not .gitignore files
|
2019-10-05 12:10:26 +00:00
|
|
|
// should be managed, then manage them if the vendor directory is ignored.
|
|
|
|
return Git::checkIgnore($this->io, 'vendor', $this->dir);
|
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a set of entries to the specified .gitignore file.
|
|
|
|
*
|
|
|
|
* @param string $dir
|
|
|
|
* Path to directory where gitignore should be written.
|
|
|
|
* @param string[] $entries
|
|
|
|
* Entries to write to .gitignore file.
|
|
|
|
*/
|
|
|
|
protected function addToGitIgnore($dir, array $entries) {
|
|
|
|
sort($entries);
|
|
|
|
$git_ignore_path = $dir . '/.gitignore';
|
|
|
|
$contents = '';
|
|
|
|
|
|
|
|
// Appending to existing .gitignore files.
|
|
|
|
if (file_exists($git_ignore_path)) {
|
|
|
|
$contents = file_get_contents($git_ignore_path);
|
|
|
|
if (!empty($contents) && substr($contents, -1) != "\n") {
|
|
|
|
$contents .= "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$contents .= implode("\n", $entries);
|
|
|
|
file_put_contents($git_ignore_path, $contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|