Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Oct 4, 2014
1 parent 76d7dff commit bd0300f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
12 changes: 3 additions & 9 deletions Repository/Vcs/GitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Fxp\Composer\AssetPlugin\Repository\Vcs;

use Composer\Cache;
use Composer\Json\JsonFile;
use Composer\Repository\Vcs\GitDriver as BaseGitDriver;

/**
Expand All @@ -36,14 +35,9 @@ public function getComposerInformation($identifier)

if (!isset($this->infoCache[$identifier])) {
$resource = sprintf('%s:%s', escapeshellarg($identifier), $this->repoConfig['filename']);
$this->process->execute(sprintf('git show %s', $resource), $composer, $this->repoDir);

if (!trim($composer)) {
$composer = array('_nonexistent_package' => true);
} else {
$composer = JsonFile::parseJson($composer, $resource);
$composer = Util::addComposerTimeProcessor($composer, $this->process, sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $this->repoDir, '@');
}
$cmdGet = sprintf('git show %s', $resource);
$cmdLog = sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier));
$composer = Util::getComposerInformationProcess($resource, $this->process, $cmdGet, $cmdLog, $this->repoDir, '@');

Util::writeCache($this->cache, $this->repoConfig['asset-type'], $identifier, $composer);
$this->infoCache[$identifier] = $composer;
Expand Down
13 changes: 4 additions & 9 deletions Repository/Vcs/HgDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Fxp\Composer\AssetPlugin\Repository\Vcs;

use Composer\Cache;
use Composer\Json\JsonFile;
use Composer\Repository\Vcs\HgDriver as BaseHgDriver;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
Expand Down Expand Up @@ -51,14 +50,10 @@ public function getComposerInformation($identifier)
$this->infoCache[$identifier] = Util::readCache($this->infoCache, $this->cache, $this->repoConfig['asset-type'], $identifier);

if (!isset($this->infoCache[$identifier])) {
$this->process->execute(sprintf('hg cat -r %s %s', ProcessExecutor::escape($identifier), $this->repoConfig['filename']), $composer, $this->repoDir);

if (!trim($composer)) {
$composer = array('_nonexistent_package' => true);
} else {
$composer = JsonFile::parseJson($composer, $identifier);
$composer = Util::addComposerTimeProcessor($composer, $this->process, sprintf('hg log --template "{date|rfc3339date}" -r %s', ProcessExecutor::escape($identifier)), $this->repoDir);
}
$resource = sprintf('%s %s', ProcessExecutor::escape($identifier), $this->repoConfig['filename']);
$cmdGet = sprintf('hg cat -r %s', $resource);
$cmdLog = sprintf('hg log --template "{date|rfc3339date}" -r %s', ProcessExecutor::escape($identifier));
$composer = Util::getComposerInformationProcess($resource, $this->process, $cmdGet, $cmdLog, $this->repoDir);

Util::writeCache($this->cache, $this->repoConfig['asset-type'], $identifier, $composer);
$this->infoCache[$identifier] = $composer;
Expand Down
27 changes: 26 additions & 1 deletion Repository/Vcs/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ public static function addComposerTime(array $composer, $resourceKey, $resource,
return $composer;
}

/**
* Get composer information with Process Executor.
*
* @param string $resource
* @param ProcessExecutor $process
* @param string $cmdGet
* @param string $cmdLog
* @param string $repoDir
* @param string $datetimePrefix
*
* @return array The composer
*/
public static function getComposerInformationProcess($resource, ProcessExecutor $process, $cmdGet, $cmdLog, $repoDir, $datetimePrefix = '')
{
$process->execute($cmdGet, $composer, $repoDir);

if (!trim($composer)) {
return array('_nonexistent_package' => true);
}

$composer = JsonFile::parseJson($composer, $resource);

return static::addComposerTimeProcess($composer, $process, $cmdLog, $repoDir, $datetimePrefix);
}

/**
* Add time in composer with Process Executor.
*
Expand All @@ -116,7 +141,7 @@ public static function addComposerTime(array $composer, $resourceKey, $resource,
*
* @return array The composer
*/
public static function addComposerTimeProcessor(array $composer, ProcessExecutor $process, $cmd, $repoDir, $datetimePrefix = '')
protected static function addComposerTimeProcess(array $composer, ProcessExecutor $process, $cmd, $repoDir, $datetimePrefix = '')
{
if (!isset($composer['time'])) {
$process->execute($cmd, $output, $repoDir);
Expand Down

0 comments on commit bd0300f

Please sign in to comment.