Skip to content

Commit

Permalink
POC
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrech committed Jan 29, 2016
1 parent 029133e commit ee0aaa0
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/ChangelogsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Pyrech\ComposerChangelogs\Utils\GitHelper;

class ChangelogsPlugin implements PluginInterface, EventSubscriberInterface
{
Expand All @@ -28,14 +29,24 @@ class ChangelogsPlugin implements PluginInterface, EventSubscriberInterface
/** @var Outputter */
private $outputter;

/** @var Config */
private $config;

/** @var GitHelper */
private $gitHelper;

/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->io = $io;
$this->outputter = Factory::createOutputter();
$this->gitHelper = new GitHelper();

$extra = $composer->getPackage()->getExtra();

$this->setupConfig($extra);
$this->autoloadNeededClasses();
}

Expand Down Expand Up @@ -76,6 +87,18 @@ public function postPackageOperation(PackageEvent $event)
public function postUpdate(Event $event)
{
$this->io->write($this->outputter->getOutput());

switch($this->config->getCommit()) {
case 'never':
return;
case 'ask':
if ($this->io->askConfirmation('<info>Would you like to commit the update? </info>[<comment>no</comment>]: ', false)) {
$this->commit();
}
break;
case 'always':
$this->commit();
}
}

/**
Expand All @@ -99,4 +122,26 @@ private function autoloadNeededClasses()
class_exists($class, true);
}
}

/**
* @param array $extra
*/
private function setupConfig(array $extra)
{
$this->config = Config::createFromExtra(
array_key_exists('composer-changelogs', $extra) ? $extra['composer-changelogs'] : []
);

if (count($this->config->getWarnings()) > 0) {
$this->io->writeError('<error>Invalid config for composer-changelogs plugin:</error>');
foreach ($this->config->getWarnings() as $warning) {
$this->io->writeError(' ' . $warning);
}
}
}

private function commit()
{
$this->gitHelper->commit('Update dependencies', $this->outputter->getOutput());
}
}
94 changes: 94 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the composer-changelogs project.
*
* (c) Loïck Piera <pyrech@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pyrech\ComposerChangelogs;

final class Config
{
private static $validCommitValues = [
'never',
'ask',
'always',
];

/** @var string */
private $commit = 'never';

/** @var string[] */
private $warnings = [];

private function __construct() {}

/**
* @return string
*/
public function getCommit()
{
return $this->commit;
}

/**
* @return string[]
*/
public function getWarnings()
{
return $this->warnings;
}

/**
* @param array $extra
*
* @return Config
*/
public static function createFromExtra(array $extra)
{
$config = new Config();

if (array_key_exists('commit', $extra)) {
if (in_array($extra['commit'], self::$validCommitValues, true)) {
$config->commit = $extra['commit'];
} else {
$config->warnings[] = self::createWarning(
$extra,
'commit',
$config->commit,
sprintf('Valid options are "%s".', implode('", "', self::$validCommitValues))
);
}
}

return $config;
}

/**
* @param array $extra
* @param string $key
* @param mixed $default
* @param string $additionalMessage
*
* @return string
*/
private static function createWarning(array $extra, $key, $default, $additionalMessage = '')
{
$warning = sprintf(
'Invalid value "%s" for option "%s", defaulting to "%s".',
$extra[$key],
$key,
$default
);

if ($additionalMessage) {
$warning .= ' ' . $additionalMessage;
}

return $warning;
}
}
28 changes: 28 additions & 0 deletions src/Utils/GitHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the composer-changelogs project.
*
* (c) Loïck Piera <pyrech@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pyrech\ComposerChangelogs\Utils;

class GitHelper
{
/**
* @param string $title
* @param string $description
*/
public function commit($title, $description)
{
// if (!$this->isGitInstalled()) {
// return;
// }

exec('git commit ' . escapeshellarg($title.PHP_EOL.PHP_EOL.$description));
}
}

0 comments on commit ee0aaa0

Please sign in to comment.