2017-02-10 16:48:16 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of the Magallanes package.
|
|
|
|
*
|
|
|
|
* (c) Andrés Montañez <andres@andresmontanez.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mage\Task\BuiltIn\Composer;
|
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
use Mage\Task\Exception\SkipException;
|
2017-02-10 16:48:16 +01:00
|
|
|
use Symfony\Component\Process\Process;
|
2017-02-11 05:48:26 +01:00
|
|
|
use DateTime;
|
2017-02-10 16:48:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Composer Task - Self update
|
|
|
|
*
|
|
|
|
* @author Yanick Witschi <https://github.com/Toflar>
|
|
|
|
*/
|
2017-04-14 20:24:39 +02:00
|
|
|
class SelfUpdateTask extends AbstractComposerTask
|
2017-02-10 16:48:16 +01:00
|
|
|
{
|
|
|
|
public function getName()
|
|
|
|
{
|
2017-02-11 05:48:26 +01:00
|
|
|
return 'composer/self-update';
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription()
|
|
|
|
{
|
2017-02-11 05:48:26 +01:00
|
|
|
return '[Composer] Self Update';
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$options = $this->getOptions();
|
2017-02-11 05:48:26 +01:00
|
|
|
$cmdVersion = sprintf('%s --version', $options['path']);
|
2017-02-10 16:48:16 +01:00
|
|
|
/** @var Process $process */
|
2017-02-11 05:48:26 +01:00
|
|
|
$process = $this->runtime->runCommand(trim($cmdVersion));
|
2017-02-10 16:48:16 +01:00
|
|
|
if (!$process->isSuccessful()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
$buildDate = $this->getBuildDate($process->getOutput());
|
|
|
|
if (!$buildDate instanceof DateTime) {
|
|
|
|
return false;
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
$compareDate = $this->getCompareDate();
|
|
|
|
if ($buildDate >= $compareDate) {
|
|
|
|
throw new SkipException();
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 06:01:49 +01:00
|
|
|
$cmdUpdate = sprintf('%s self-update', $options['path']);
|
2017-02-11 05:48:26 +01:00
|
|
|
/** @var Process $process */
|
|
|
|
$process = $this->runtime->runCommand(trim($cmdUpdate));
|
2017-02-10 16:48:16 +01:00
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
return $process->isSuccessful();
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
protected function getBuildDate($output)
|
2017-02-10 16:48:16 +01:00
|
|
|
{
|
2017-02-11 05:48:26 +01:00
|
|
|
$buildDate = null;
|
2018-11-20 14:06:12 +01:00
|
|
|
$output = explode("\n", $output);
|
2017-02-11 05:48:26 +01:00
|
|
|
foreach ($output as $row) {
|
|
|
|
if (strpos($row, 'Composer version ') === 0) {
|
|
|
|
$buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19));
|
|
|
|
}
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
return $buildDate;
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 05:48:26 +01:00
|
|
|
protected function getCompareDate()
|
2017-02-10 16:48:16 +01:00
|
|
|
{
|
2017-02-11 05:48:26 +01:00
|
|
|
$options = $this->getOptions();
|
|
|
|
$compareDate = new DateTime();
|
|
|
|
$compareDate->modify(sprintf('now -%d days', $options['days']));
|
|
|
|
return $compareDate;
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-14 20:24:39 +02:00
|
|
|
protected function getComposerOptions()
|
2017-02-10 16:48:16 +01:00
|
|
|
{
|
2017-04-14 20:24:39 +02:00
|
|
|
return ['days' => 60];
|
2017-02-10 16:48:16 +01:00
|
|
|
}
|
|
|
|
}
|