Skip to content

Commit

Permalink
[!!!][TASK] Remove task name compat layer in favor of migrate command
Browse files Browse the repository at this point in the history
  • Loading branch information
helhum committed Jan 7, 2016
1 parent 87999f6 commit 409d05a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 67 deletions.
22 changes: 0 additions & 22 deletions Resources/Private/Scripts/MigrateLegacyTaskNamesToClassNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,3 @@
}
file_put_contents($file, $fileContent);
}

function getTaskIdentifier($className) {
return
substr(
str_replace(
array(
'typo3\\surf',
'\\task',
'\\',
),
array(
'typo3.surf',
'',
':',
),
strtolower($className)
),
0,
-4
);

}
36 changes: 16 additions & 20 deletions Tests/Unit/Domain/Service/TaskManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@ public function executePassesPrefixedTaskOptionsToTask()
$node = new \TYPO3\Surf\Domain\Model\Node('Test node');
$application = new \TYPO3\Surf\Domain\Model\Application('Test application');
$deployment = new \TYPO3\Surf\Domain\Model\Deployment('Test deployment');
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */
$logger = $this->getMock('Psr\Log\LoggerInterface');
$deployment->setLogger($logger);

$task = $this->getMock('TYPO3\Surf\Domain\Model\Task');
/** @var \TYPO3\Surf\Domain\Service\TaskManager|\PHPUnit_Framework_MockObject_MockObject $taskManager */
$taskManager = $this->getMock('TYPO3\Surf\Domain\Service\TaskManager', array('createTaskInstance'));
$taskManager->expects($this->any())->method('createTaskInstance')->with('myvendor.mypackage:taskgroup:mytask')->will($this->returnValue($task));
$taskManager->expects($this->any())->method('createTaskInstance')->with('MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask')->will($this->returnValue($task));

$globalOptions = array(
'myvendor.mypackage:taskgroup:mytask[taskOption]' => 'Foo'
'MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask[taskOption]' => 'Foo'
);
$deployment->setOptions($globalOptions);

$task->expects($this->atLeastOnce())->method('execute')->with(
$this->anything(), $this->anything(), $this->anything(),
$this->logicalOr(
$this->arrayHasKey('taskOption'),
$this->arrayHasKey('myvendor.mypackage:taskgroup:mytask[taskOption]')
)
$this->arrayHasKey('taskOption')
);

$localOptions = array();
$taskManager->execute('myvendor.mypackage:taskgroup:mytask', $node, $application, $deployment, 'test', $localOptions);
$taskManager->execute('MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask', $node, $application, $deployment, 'test', $localOptions);
}

/**
Expand All @@ -51,12 +49,13 @@ public function executePassesNodeOptionsToTask()
$node = new \TYPO3\Surf\Domain\Model\Node('Test node');
$application = new \TYPO3\Surf\Domain\Model\Application('Test application');
$deployment = new \TYPO3\Surf\Domain\Model\Deployment('Test deployment');
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */
$logger = $this->getMock('Psr\Log\LoggerInterface');
$deployment->setLogger($logger);

$task = $this->getMock('TYPO3\Surf\Domain\Model\Task');
/** @var \TYPO3\Surf\Domain\Service\TaskManager|\PHPUnit_Framework_MockObject_MockObject $taskManager */
$taskManager = $this->getMock('TYPO3\Surf\Domain\Service\TaskManager', array('createTaskInstance'));
$taskManager->expects($this->any())->method('createTaskInstance')->with('myvendor.mypackage:taskgroup:mytask')->will($this->returnValue($task));
$taskManager->expects($this->any())->method('createTaskInstance')->with('MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask')->will($this->returnValue($task));

$nodeOptions = array(
'ssh[username]' => 'jdoe'
Expand All @@ -65,13 +64,11 @@ public function executePassesNodeOptionsToTask()

$task->expects($this->atLeastOnce())->method('execute')->with(
$this->anything(), $this->anything(), $this->anything(),
$this->logicalOr(
$this->arrayHasKey('ssh[username]')
)
$this->arrayHasKey('ssh[username]')
);

$localOptions = array();
$taskManager->execute('myvendor.mypackage:taskgroup:mytask', $node, $application, $deployment, 'test', $localOptions);
$taskManager->execute('MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask', $node, $application, $deployment, 'test', $localOptions);
}

/**
Expand All @@ -82,12 +79,13 @@ public function executePassesApplicationOptionsToTask()
$node = new \TYPO3\Surf\Domain\Model\Node('Test node');
$application = new \TYPO3\Surf\Domain\Model\Application('Test application');
$deployment = new \TYPO3\Surf\Domain\Model\Deployment('Test deployment');
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */
$logger = $this->getMock('Psr\Log\LoggerInterface');
$deployment->setLogger($logger);

$task = $this->getMock('TYPO3\Surf\Domain\Model\Task');
/** @var \TYPO3\Surf\Domain\Service\TaskManager|\PHPUnit_Framework_MockObject_MockObject $taskManager */
$taskManager = $this->getMock('TYPO3\Surf\Domain\Service\TaskManager', array('createTaskInstance'));
$taskManager->expects($this->any())->method('createTaskInstance')->with('myvendor.mypackage:taskgroup:mytask')->will($this->returnValue($task));
$taskManager->expects($this->any())->method('createTaskInstance')->with('MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask')->will($this->returnValue($task));

$applicationOptions = array(
'repositoryUrl' => 'ssh://review.typo3.org/foo'
Expand All @@ -96,12 +94,10 @@ public function executePassesApplicationOptionsToTask()

$task->expects($this->atLeastOnce())->method('execute')->with(
$this->anything(), $this->anything(), $this->anything(),
$this->logicalOr(
$this->arrayHasKey('repositoryUrl')
)
$this->arrayHasKey('repositoryUrl')
);

$localOptions = array();
$taskManager->execute('myvendor.mypackage:taskgroup:mytask', $node, $application, $deployment, 'test', $localOptions);
$taskManager->execute('MyVendor\\MyPackage\\Task\\TaskGroup\\MyTask', $node, $application, $deployment, 'test', $localOptions);
}
}
70 changes: 70 additions & 0 deletions src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace TYPO3\Surf\Command;

/* *
* This script belongs to the TYPO3 project "TYPO3 Surf". *
* *
* */

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\Surf\Integration\FactoryAwareInterface;
use TYPO3\Surf\Integration\FactoryAwareTrait;

/**
* Migrate old deployment definitions to new Surf version
*/
class MigrateCommand extends Command implements FactoryAwareInterface
{
use FactoryAwareTrait;

/**
* Configure
*/
protected function configure()
{
$this->setName('migrate')
->addArgument(
'deploymentName',
InputArgument::REQUIRED,
'The deployment name to migrate'
)
->addOption(
'configurationPath',
null,
InputOption::VALUE_OPTIONAL,
'Path for deployment configuration files'
);
}

/**
* Execute
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$configurationPath = $input->getOption('configurationPath');
$deploymentName = $input->getArgument('deploymentName');
$basePath = $this->factory->getDeploymentsBasePath($configurationPath);

$deploymentFileName = $basePath . '/' . $deploymentName . '.php';

$legacyMap = require __DIR__ . '/../../Migrations/Code/LegacyClassMap.php';
$fileContent = file_get_contents($deploymentFileName);
foreach ($legacyMap as $identifier => $className) {
if ($fileContent !== str_ireplace($identifier, str_replace('\\', '\\\\', $className), $fileContent)) {
$output->writeln(sprintf('<warning>Legacy deployment task name or task option "%s" found!</warning>', $identifier));
$fileContent = str_ireplace($identifier, str_replace('\\', '\\\\', $className), $fileContent);
}
}
file_put_contents($deploymentFileName, $fileContent);
$output->writeln('<info>Migrated deployment definition "' . $deploymentName . '"</info>');
}

}
33 changes: 8 additions & 25 deletions src/Domain/Service/TaskManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ class TaskManager
*/
protected $taskHistory = array();

/**
* @var array
*/
protected $legacyClassMap = array();

/**
* TaskManager constructor.
*/
public function __construct()
{
$this->legacyClassMap = require __DIR__ . '/../../../Migrations/Code/LegacyClassMap.php';
}

/**
* Execute a task
*
Expand Down Expand Up @@ -141,13 +128,13 @@ protected function overrideOptions($taskName, Deployment $deployment, Node $node
/**
* Create a task instance from the given task name
*
* @param string $taskIdentifier
* @param string $taskName
* @return \TYPO3\Surf\Domain\Model\Task
* @throws \TYPO3\Surf\Exception\InvalidConfigurationException
*/
protected function createTaskInstance($taskIdentifier)
protected function createTaskInstance($taskName)
{
$taskClassName = $this->calculateTaskClassNameFromTaskIdentifier($taskIdentifier);
$taskClassName = $this->mapTaskNameToTaskClass($taskName);
$task = new $taskClassName();
if ($task instanceof ShellCommandServiceAwareInterface) {
$task->setShellCommandService(new ShellCommandService());
Expand All @@ -156,19 +143,15 @@ protected function createTaskInstance($taskIdentifier)
}

/**
* @param string $taskIdentifier
* @param string $taskName
* @return string
* @throws \TYPO3\Surf\Exception
*/
protected function calculateTaskClassNameFromTaskIdentifier($taskIdentifier)
protected function mapTaskNameToTaskClass($taskName)
{
$lowerCaseTaskIdentifier = strtolower($taskIdentifier);
if (isset($this->legacyClassMap[$lowerCaseTaskIdentifier])) {
return $this->legacyClassMap[$lowerCaseTaskIdentifier];
}
if (class_exists($taskIdentifier)) {
return $taskIdentifier;
if (class_exists($taskName)) {
return $taskName;
}
throw new \TYPO3\Surf\Exception(sprintf('No task found for identifier "%s"', $taskIdentifier), 1451210811);
throw new \TYPO3\Surf\Exception(sprintf('No task found for identifier "%s". Make sure this is a valid class name or a defined task with valid base class name!', $taskName), 1451210811);
}
}
2 changes: 2 additions & 0 deletions src/Integration/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use TYPO3\Surf\Cli\Symfony\Logger\ConsoleHandler;
use TYPO3\Surf\Command\DeployCommand;
use TYPO3\Surf\Command\DescribeCommand;
use TYPO3\Surf\Command\MigrateCommand;
use TYPO3\Surf\Command\ShowCommand;
use TYPO3\Surf\Command\SimulateCommand;
use TYPO3\Surf\Domain\Model\Deployment;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function createCommands()
new SimulateCommand(),
new DescribeCommand(),
new DeployCommand(),
new MigrateCommand(),
);
}

Expand Down

0 comments on commit 409d05a

Please sign in to comment.