Skip to content

Commit

Permalink
Merge pull request #36 from webmozart/fix
Browse files Browse the repository at this point in the history
Fixed execution of Puli plugin
  • Loading branch information
webmozart committed Jan 5, 2016
2 parents 0f22ff8 + 2c3156c commit eb2b2e0
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 152 deletions.
111 changes: 59 additions & 52 deletions src/PuliPlugin.php
Expand Up @@ -20,6 +20,7 @@
use Composer\Script\CommandEvent;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Exception;
use RuntimeException;
use Webmozart\PathUtil\Path;

Expand Down Expand Up @@ -98,49 +99,6 @@ public function __construct(PuliRunner $puliRunner = null)
*/
public function activate(Composer $composer, IOInterface $io)
{
// Verify if Puli has the right version
try {
$versionString = $this->puliRunner->run('-V');
} catch (PuliRunnerException $e) {
$this->printWarning($io, 'Could not determine Puli version', $e);

return;
}

if (!preg_match('~\d+\.\d+\.\d+(-\w+)?~', $versionString, $matches)) {
$this->printWarning($io, sprintf(
'Could not determine Puli version. "puli -V" returned: %s',
$versionString
));

return;
}

if (version_compare($matches[0], self::MIN_CLI_VERSION, '<')) {
$this->printWarning($io, sprintf(
'Found an unsupported version of the Puli CLI: %s. Please '.
'upgrade to version %s or higher. You can also install the '.
'puli/cli dependency at version %s in your project.',
$matches[0],
self::MIN_CLI_VERSION,
self::MIN_CLI_VERSION
));

return;
}

if (version_compare($matches[0], self::MAX_CLI_VERSION, '>')) {
$this->printWarning($io, sprintf(
'Found an unsupported version of the Puli CLI: %s. Please '.
'downgrade to a lower version. You can also install the '.
'puli/cli dependency at a lower version than %s in your project.',
$matches[0],
self::MAX_CLI_VERSION
));

return;
}

$composer->getEventDispatcher()->addSubscriber($this);
}

Expand Down Expand Up @@ -267,16 +225,23 @@ private function initialize(Composer $composer, IOInterface $io)
$this->initialized = true;

// Keep the manually set runner
if ($this->puliRunner) {
return;
if (!$this->puliRunner) {
try {
// Add Composer's bin directory in case the "puli" executable is
// installed with Composer
$this->puliRunner = new PuliRunner($composer->getConfig()->get('bin-dir'));
} catch (RuntimeException $e) {
$this->printWarning($io, 'Plugin initialization failed', $e);
$this->runPostAutoloadDump = false;
$this->runPostInstall = false;
}
}

// Use the runner to verify if Puli has the right version
try {
// Add Composer's bin directory in case the "puli" executable is
// installed with Composer
$this->puliRunner = new PuliRunner($composer->getConfig()->get('bin-dir'));
$this->verifyPuliVersion();
} catch (RuntimeException $e) {
$io->writeError('<warning>'.$e->getMessage().'</warning>');
$this->printWarning($io, 'Version check failed', $e);
$this->runPostAutoloadDump = false;
$this->runPostInstall = false;
}
Expand Down Expand Up @@ -654,14 +619,23 @@ private function renamePackage($name, $newName)
));
}

private function printWarning(IOInterface $io, $message, PuliRunnerException $exception = null)
/**
* @param IOInterface $io
* @param $message
* @param Exception|null $exception
*/
private function printWarning(IOInterface $io, $message, Exception $exception = null)
{
if (!$exception) {
$reasonPhrase = '';
} elseif ($io->isVerbose()) {
$reasonPhrase = $exception->getFullError();
$reasonPhrase = $exception instanceof PuliRunnerException
? $exception->getFullError()
: $exception->getMessage()."\n\n".$exception->getTraceAsString();
} else {
$reasonPhrase = $exception->getShortError();
$reasonPhrase = $exception instanceof PuliRunnerException
? $exception->getShortError()
: $exception->getMessage();
}

$io->writeError(sprintf(
Expand Down Expand Up @@ -703,4 +677,37 @@ private function filterProdPackageNames(array $composerPackages, PackageInterfac

return $result;
}

private function verifyPuliVersion()
{
$versionString = $this->puliRunner->run('-V');

if (!preg_match('~\d+\.\d+\.\d+(-\w+)?~', $versionString, $matches)) {
throw new RuntimeException(sprintf(
'Could not determine Puli version. "puli -V" returned: %s',
$versionString
));
}

if (version_compare($matches[0], self::MIN_CLI_VERSION, '<')) {
throw new RuntimeException(sprintf(
'Found an unsupported version of the Puli CLI: %s. Please '.
'upgrade to version %s or higher. You can also install the '.
'puli/cli dependency at version %s in your project.',
$matches[0],
self::MIN_CLI_VERSION,
self::MIN_CLI_VERSION
));
}

if (version_compare($matches[0], self::MAX_CLI_VERSION, '>')) {
throw new RuntimeException(sprintf(
'Found an unsupported version of the Puli CLI: %s. Please '.
'downgrade to a lower version than %s. You can also install '.
'the puli/cli dependency in your project.',
$matches[0],
self::MAX_CLI_VERSION
));
}
}
}

0 comments on commit eb2b2e0

Please sign in to comment.