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

[WIP] Fixes to GitDriver and Project bugs in Scrutinizer #27

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
64 changes: 43 additions & 21 deletions src/GitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\ParseComposer;

use Composer\Downloader\TransportException;
use Composer\Repository\Vcs\GitDriver as BaseDriver;
use Composer\Package\Version\VersionParser;
use Drupal\ParseComposer\DrupalOrg\DistUrl;
Expand All @@ -19,47 +20,67 @@ class GitDriver extends BaseDriver implements FileFinderInterface
*/
private $versionFactory = false;

/**
* @var string identifier
*/
private $identifier = '';

/**
* @var string drupalProjectName
*/
private $drupalProjectName = '';

/**
* @var bool isCore
*/
private $isCore = false;

/**
* @var string[] paths
*/
private $paths;

/**
* {@inheritDoc}
*/
public function getComposerInformation($identifier)
{
$this->identifier = $identifier;
try {
$composer = parent::getComposerInformation($identifier);
} catch (TransportException $e) {
// There is not composer.json file in the root
}
$composer = is_array($composer) ? $composer : array();

$ref = strlen($this->identifier) == 40
? $this->lookUpRef($this->identifier)
: $this->identifier;
if ($version = $this->getVersion($ref)) {
$core = $version->getCore();
$majorSlug = $this->isCore ? '' : "{$version->getMajor()}.x";
$devBranch = "dev-$core.x-$majorSlug";
$composer['extra']['branch-alias'][$devBranch] = $core.'.'
.($majorSlug ?: 'x').'-dev';
if ($core < 7) {
return [];
}
} else {
return [];
}
// TODO: make configurable?
if ($core < 7) {
return [];

try {
$composer = parent::getComposerInformation($identifier);
} catch (TransportException $e) {
// There is not composer.json file in the root
}
$composer = is_array($composer) ? $composer : array();
foreach (array('replace', 'require', 'suggest') as $link) {
$composer[$link] = isset($composer[$link])
? $composer[$link]
: array();
}
$majorSlug = $this->isCore ? '' : "{$version->getMajor()}.x";
$devBranch = "dev-$core.x-$majorSlug";
$composer['extra']['branch-alias'][$devBranch] = $core.'.'
.($majorSlug ?: 'x').'-dev';
$project = new Project($this->drupalProjectName, $this, $core);
if (null != ($drupalInformation = $project->getDrupalInformation())) {
if (isset($drupalInformation[$this->drupalProjectName])) {
$topInformation = $drupalInformation[$this->drupalProjectName];
} else {
$topInformation = current($drupalInformation);
}
foreach (array('replace', 'require', 'suggest') as $link) {
$composer[$link] = isset($composer[$link])
? $composer[$link]
: array();
}
foreach (array_keys($drupalInformation) as $name) {
if ($name != $this->drupalProjectName) {
$composer['replace']["drupal/$name"] = 'self.version';
Expand Down Expand Up @@ -104,7 +125,6 @@ public function getComposerInformation($identifier)
unset($composer['suggest'][$composer['name']]);
unset($composer['suggest']['drupal/drupal']);
}

return $composer;
}

Expand Down Expand Up @@ -181,9 +201,11 @@ public function getBranches()
if ($branch
&& !preg_match('{^ *[^/]+/HEAD }', $branch)
&& preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)
&& $this->getVersion($name = @end(explode('/', $match[1])))
) {
$branches[$name] = $match[2];
$parts = explode('/', $match[1]);
if ($this->getVersion($name = end($parts))) {
$branches[$name] = $match[2];
}
}
}
$this->branches = $branches;
Expand Down
134 changes: 70 additions & 64 deletions src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
class Project
{

private $makeFiles = [];
private $infoFiles = [];
private $isTheme;
private $hasDrush;
private $makeFiles = [];
private $infoFiles = [];
private $isTheme = false;
private $hasDrush = false;

/**
* @param string $name
Expand Down Expand Up @@ -43,85 +43,91 @@ public function getName()
/**
* Get composer information for Drupal project.
*
* @return array|void
* @return array|null
*/
public function getDrupalInformation()
{
$projectMap = $composerMap = $make = array();
$this->hasDrush = $this->hasModule = false;
$this->finder->pathMatch(
function($path) {
if (strpos($path, 'test') !== false) {
return false;
}
$parts = explode('.', basename($path));
if (end($parts) === 'info'
|| array_slice($parts, -2) == ['info', 'yml']
) {
$this->infoFiles[] = $path;

return true;
} elseif (end($parts) === 'make') {
$this->makeFiles[] = $path;

return true;
} elseif (end($parts) === 'module'
) {
$this->hasModule = true;
} elseif (basename($path) === 'template.php') {
$this->isTheme = true;
} elseif (array_slice($parts, -2) == ['drush', 'inc']
) {
$this->hasDrush = true;
}
}
);
foreach ($this->infoFiles as $infoPath) {
$info = new InfoFile(
basename($infoPath),
$this->finder->fileContents($infoPath),
$this->core
);
$projectMap[$info->getProjectName()] = $info;
}
foreach ($this->makeFiles as $makePath) {
$make[] = new Makefile(
$this->finder->fileContents($makePath)
);
}
if (empty($projectMap) && !$this->hasDrush) {
$this->finder->pathMatch($this->getPathMatcher());
if (empty($this->projectMap) && !$this->hasDrush) {
return;
}
if ('drupal' === $this->name) {
$projectMap['drupal'] = clone($projectMap['system']);
}
foreach ($projectMap as $name => $info) {
$composerMap[$name] = $info->packageInfo();
foreach ($make as $makefile) {
foreach (($makefile->getDrupalProjects()) as $name => $project) {
$composerMap[$this->name]['require']['drupal/'.$name] = $makefile->getConstraint($name);
}
}
foreach ($this->projectMap as $name => $info) {
$composerMap[$name] = $info;
}
$top = isset($composerMap[$this->name])
? $this->name
: current(array_keys($composerMap));
foreach ($this->makeFiles as $makefile) {
foreach (($makefile->getDrupalProjects()) as $name => $project) {
$composerMap[$top]['require']['drupal/'.$name] = $makefile->getConstraint($name);
}
}
if (!isset($composerMap[$top]['name'])) {
$composerMap[$top]['name'] = $this->getName();
}
$composerMap[$top]['type'] = $this->getProjectType($composerMap[$top]);
if ($composerMap[$top]['type'] === 'drupal-drush') {
$composerMap[$top]['require']['drush/drush'] = '>=6';
}

return $composerMap;
}

/**
* @return a function for use with FileFinderInterface::pathMatch()
*/
private function getPathMatcher()
{
return function($path) {
if (strpos($path, 'test') !== false) {
return false;
}
$parts = explode('.', basename($path));
if (end($parts) === 'info'
|| array_slice($parts, -2) == ['info', 'yml']
) {
$info = new InfoFile(
basename($path),
$this->finder->fileContents($path),
$this->core
);
$this->projectMap[$info->getProjectName()] = $info->packageInfo();
} elseif (end($parts) === 'make') {
$this->makeFiles[] = new Makefile(
$this->finder->fileContents($path)
);
} elseif (end($parts) === 'module') {
$this->hasModule = true;
} elseif (basename($path) === 'template.php') {
$this->isTheme = true;
} elseif (array_slice($parts, -2) == ['drush', 'inc']
) {
$this->hasDrush = true;
}
};
}

/**
* @return the type for the composer project
*/
private function getProjectType()
{
if ('drupal' === $this->name) {
$composerMap[$top]['type'] = 'drupal-core';
return 'drupal-core';
} elseif ($releaseInfo = $this->getReleaseInfo($this->core)) {
$composerMap[$top]['type'] = $releaseInfo->getProjectType();
if ($composerMap[$top]['type'] === 'drupal-module'
$type = $releaseInfo->getProjectType();
if ($type === 'drupal-module'
&& !$this->hasModule && !$this->isTheme && $this->hasDrush
) {
if (!isset($composerMap[$top]['name'])) {
$composerMap[$top]['name'] = $this->getName();
}
$composerMap[$top]['type'] = 'drupal-drush';
$composerMap[$top]['require']['drush/drush'] = '>=6';
$type = 'drupal-drush';
}
}

return $composerMap;
return $type;
}
}

/**
Expand Down