Skip to content

Commit

Permalink
TASK: Check typo3 console version via composer (#689)
Browse files Browse the repository at this point in the history
* TASK: Check typo3 console version via composer

* Apply php-cs-fixer changes

---------

Co-authored-by: sabbelasichon <sabbelasichon@users.noreply.github.com>
  • Loading branch information
sabbelasichon and sabbelasichon committed Feb 22, 2023
1 parent ba4c9c2 commit ef548c4
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 125 deletions.
4 changes: 4 additions & 0 deletions Resources/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use TYPO3\Surf\Domain\Filesystem\FilesystemInterface;
use TYPO3\Surf\Domain\Model\RollbackWorkflow;
use TYPO3\Surf\Domain\Model\SimpleWorkflow;
use TYPO3\Surf\Domain\Version\ComposerVersionChecker;
use TYPO3\Surf\Domain\Version\VersionCheckerInterface;
use TYPO3\Surf\Integration\Factory;
use TYPO3\Surf\Integration\FactoryInterface;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
Expand Down Expand Up @@ -75,4 +77,6 @@
->args([service(FilesystemInterface::class), service(LoggerInterface::class)]);

$services->alias(FactoryInterface::class, Factory::class);

$services->alias(VersionCheckerInterface::class, ComposerVersionChecker::class);
};
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"myclabs/php-enum": "^1.8",
"neos/utility-files": "^3.0",
"padraic/phar-updater": "1.0.6",
"phar-io/version": "^3.1",
"symfony/config": "^5.0",
"symfony/console": "^5.0",
"symfony/dependency-injection": "^5.0",
Expand Down
104 changes: 52 additions & 52 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/Domain/Version/ComposerVersionChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace TYPO3\Surf\Domain\Version;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;

final class ComposerVersionChecker implements VersionCheckerInterface
{
public function isSatisified(string $packageName, string $constraint): bool
{
return InstalledVersions::satisfies(new VersionParser(), $packageName, $constraint);
}
}
17 changes: 17 additions & 0 deletions src/Domain/Version/VersionCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace TYPO3\Surf\Domain\Version;

interface VersionCheckerInterface
{
public function isSatisified(string $packageName, string $constraint): bool;
}
55 changes: 0 additions & 55 deletions src/Task/TYPO3/CMS/AbstractCliTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace TYPO3\Surf\Task\TYPO3\CMS;

use PharIo\Version\InvalidVersionException;
use PharIo\Version\Version;
use TYPO3\Surf\Application\TYPO3\CMS;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
Expand Down Expand Up @@ -122,59 +120,6 @@ protected function getTypo3ConsoleScriptFileName(Node $node, CMS $application, D
return $options['scriptFileName'];
}

protected function getTypo3CoreVersion(Node $node, CMS $application, Deployment $deployment, array $options): Version
{
$scriptFileName = $this->getTypo3CoreCliFileName($node, $application, $deployment, $options);

$commandArguments = [$scriptFileName, '--version'];

$output = $this->executeCliCommand(
$commandArguments,
$node,
$application,
$deployment,
$options
);

preg_match('/TYPO3 CMS (.*) \(/', $output, $matches);

try {
return new Version($matches[1]);
} catch (InvalidVersionException $e) {
return new Version('0.0.0');
}
}

protected function getTypo3ConsoleVersion(Node $node, CMS $application, Deployment $deployment, array $options): Version
{
$scriptFileName = $this->getTypo3ConsoleScriptFileName($node, $application, $deployment, $options);

$commandArguments = [$scriptFileName, '--version'];

$output = $this->executeCliCommand(
$commandArguments,
$node,
$application,
$deployment,
$options
);

// return version in simulation
if ($output === true) {
return new Version('0.0.0');
}

[$versionLine] = explode("\n", $output);

$version = trim(substr($versionLine, strlen('TYPO3 Console')) ?: '');

try {
return new Version($version);
} catch (InvalidVersionException $e) {
return new Version('0.0.0');
}
}

protected function fileExists(string $pathAndFileName, Node $node, CMS $application, Deployment $deployment, array $options = []): bool
{
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
Expand Down
12 changes: 9 additions & 3 deletions src/Task/TYPO3/CMS/SetUpExtensionsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Domain\Version\VersionCheckerInterface;
use TYPO3\Surf\Exception\InvalidConfigurationException;
use Webmozart\Assert\Assert;

Expand All @@ -27,6 +28,13 @@
*/
class SetUpExtensionsTask extends AbstractCliTask
{
private VersionCheckerInterface $versionChecker;

public function __construct(VersionCheckerInterface $versionChecker)
{
$this->versionChecker = $versionChecker;
}

public function execute(Node $node, Application $application, Deployment $deployment, array $options = []): void
{
/** @var CMS $application */
Expand All @@ -41,11 +49,9 @@ public function execute(Node $node, Application $application, Deployment $deploy

$options = $this->configureOptions($options);

$typo3ConsoleVersion = $this->getTypo3ConsoleVersion($node, $application, $deployment, $options);

$commandArguments = [$scriptFileName];

if ($typo3ConsoleVersion->getMajor()->getValue() >= 7) {
if ($this->versionChecker->isSatisified('helhum/typo3-console', '>= 7.0.0')) {
$commandArguments[] = 'extension:setup';
if (!empty($options['extensionKeys'])) {
foreach ($options['extensionKeys'] as $extensionKey) {
Expand Down
28 changes: 14 additions & 14 deletions tests/Unit/Task/TYPO3/CMS/SetUpExtensionsTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@

namespace TYPO3\Surf\Tests\Unit\Task\TYPO3\CMS;

use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use TYPO3\Surf\Application\TYPO3\CMS;
use TYPO3\Surf\Domain\Version\VersionCheckerInterface;
use TYPO3\Surf\Task\TYPO3\CMS\SetUpExtensionsTask;
use TYPO3\Surf\Tests\Unit\Task\BaseTaskTest;

class SetUpExtensionsTaskTest extends BaseTaskTest
{
/**
* @var ObjectProphecy|VersionCheckerInterface
*/
private $versionChecker;

protected function setUp(): void
{
parent::setUp();
$this->application = new CMS('TestApplication');

$this->node->setDeploymentPath('/home/jdoe/app');
$this->expectTypo3ConsoleVersion('TYPO3 Console 5.8.6');
}

protected function createTask(): SetUpExtensionsTask
{
return new SetUpExtensionsTask();
$this->versionChecker = $this->prophesize(VersionCheckerInterface::class);
$this->versionChecker->isSatisified(Argument::any(), Argument::any())->willReturn(false);
return new SetUpExtensionsTask($this->versionChecker->reveal());
}

/**
Expand Down Expand Up @@ -96,7 +105,7 @@ public function consoleIsFoundInCorrectPathWithWebDirectoryAndSlashesAreTrimmed(
*/
public function consoleIsFoundInCorrectPathWithoutAppDirectoryInVersionEqualOrHigherThanSeven(): void
{
$this->expectTypo3ConsoleVersion('TYPO3 Console 7.0.0');
$this->versionChecker->isSatisified(Argument::any(), Argument::any())->willReturn(true);

$options = [
'scriptFileName' => 'vendor/bin/typo3cms',
Expand All @@ -112,7 +121,7 @@ public function consoleIsFoundInCorrectPathWithoutAppDirectoryInVersionEqualOrHi
*/
public function consoleIsFoundInCorrectPathWithoutAppDirectoryInVersionEqualOrHigherThanSevenButInMultilineFormat(): void
{
$this->expectTypo3ConsoleVersion("TYPO3 Console 7.0.5\nTYPO3 CMS 11.5.7 (Application Context: Production)");
$this->versionChecker->isSatisified(Argument::any(), Argument::any())->willReturn(true);

$options = [
'scriptFileName' => 'vendor/bin/typo3cms',
Expand All @@ -128,7 +137,7 @@ public function consoleIsFoundInCorrectPathWithoutAppDirectoryInVersionEqualOrHi
*/
public function executeWithoutOptionExecutesSetUpInVersionEqualOrHigherThanSeven(): void
{
$this->expectTypo3ConsoleVersion('TYPO3 Console 7.0.0');
$this->versionChecker->isSatisified(Argument::any(), Argument::any())->willReturn(true);

$this->task->execute(
$this->node,
Expand All @@ -145,8 +154,6 @@ public function executeWithoutOptionExecutesSetUpInVersionEqualOrHigherThanSeven
*/
public function executeWithoutOptionAndMissingVersionExecutesSetUpActive(): void
{
$this->expectTypo3ConsoleVersion('');

$this->task->execute(
$this->node,
$this->application,
Expand All @@ -156,11 +163,4 @@ public function executeWithoutOptionAndMissingVersionExecutesSetUpActive(): void

$this->assertCommandExecuted("php 'vendor/bin/typo3cms' 'extension:setupactive'");
}

private function expectTypo3ConsoleVersion(string $typo3ConsoleVersion): void
{
$versionCommand = 'php \'vendor/bin/typo3cms\' \'--version\'';
$this->commands['versionCommand'] = $versionCommand;
$this->responses[$versionCommand] = $typo3ConsoleVersion;
}
}

0 comments on commit ef548c4

Please sign in to comment.