Browse Source

Allow parameter configuration on the Task configuration definition.

1.0
Andrés Montañez 12 years ago
parent
commit
969caa0bbb
  1. 8
      Mage/Command/BuiltIn/Deploy.php
  2. 25
      Mage/Config.php
  3. 4
      Mage/Task/BuiltIn/Releases/Rollback.php
  4. 16
      Mage/Task/Factory.php
  5. 15
      Mage/Task/TaskAbstract.php
  6. 3
      docs/example-config/.mage/config/environment/staging.yml
  7. 20
      docs/example-config/.mage/tasks/TaskWithParameters.php

8
Mage/Command/BuiltIn/Deploy.php

@ -57,9 +57,9 @@ class Mage_Command_BuiltIn_Deploy
Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> skipped!', 1, 3); Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> skipped!', 1, 3);
} else { } else {
foreach ($tasksToRun as $taskName) { foreach ($tasksToRun as $taskData) {
$tasks++; $tasks++;
$task = Mage_Task_Factory::get($taskName, $this->getConfig(), false, 'deploy'); $task = Mage_Task_Factory::get($taskData, $this->getConfig(), false, 'deploy');
$task->init(); $task->init();
$runTask = true; $runTask = true;
@ -141,9 +141,9 @@ class Mage_Command_BuiltIn_Deploy
$tasks = 0; $tasks = 0;
$completedTasks = 0; $completedTasks = 0;
foreach ($tasksToRun as $taskName) { foreach ($tasksToRun as $taskData) {
$tasks++; $tasks++;
$task = Mage_Task_Factory::get($taskName, $config, false, $stage); $task = Mage_Task_Factory::get($taskData, $config, false, $stage);
$task->init(); $task->init();
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, 0); Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, 0);

25
Mage/Config.php

@ -57,10 +57,12 @@ class Mage_Config
* @param string $name * @param string $name
* @return mixed * @return mixed
*/ */
public function getParameter($name, $default = null) public function getParameter($name, $default = null, $extraParameters = array())
{ {
if (isset($this->_parameters[$name])) { if (isset($this->_parameters[$name])) {
return $this->_parameters[$name]; return $this->_parameters[$name];
} else if (isset($extraParameters[$name])) {
return $extraParameters[$name];
} else { } else {
return $default; return $default;
} }
@ -107,7 +109,18 @@ class Mage_Config
$tasks = array(); $tasks = array();
$config = $this->_getEnvironmentOption('tasks', array()); $config = $this->_getEnvironmentOption('tasks', array());
if (isset($config[$stage])) { if (isset($config[$stage])) {
$tasks = ($config[$stage] ? (array) $config[$stage] : array()); $tasksData = ($config[$stage] ? (array) $config[$stage] : array());
foreach ($tasksData as $taskName => $taskData) {
if (is_array($taskData)) {
;
$tasks[] = array(
'name' => key($taskData),
'parameters' => current($taskData),
);
} else {
$tasks[] = $taskData;
}
}
} }
return $tasks; return $tasks;
@ -317,7 +330,13 @@ class Mage_Config
if (count($optionValue) == 1) { if (count($optionValue) == 1) {
$this->_parameters[$optionValue[0]] = true; $this->_parameters[$optionValue[0]] = true;
} else if (count($optionValue) == 2) { } else if (count($optionValue) == 2) {
$this->_parameters[$optionValue[0]] = $optionValue[1]; if (strtolower($optionValue[1]) == 'true') {
$this->_parameters[$optionValue[0]] = true;
} else if (strtolower($optionValue[1]) == 'false') {
$this->_parameters[$optionValue[0]] = false;
} else {
$this->_parameters[$optionValue[0]] = $optionValue[1];
}
} }
} else { } else {
$this->_arguments[] = $argument; $this->_arguments[] = $argument;

4
Mage/Task/BuiltIn/Releases/Rollback.php

@ -73,8 +73,8 @@ class Mage_Task_BuiltIn_Releases_Rollback
Mage_Console::output('Deployment to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> skipped!', 1, 3); Mage_Console::output('Deployment to <dark_gray>' . $this->getConfig()->getHost() . '</dark_gray> skipped!', 1, 3);
} else { } else {
foreach ($tasksToRun as $taskName) { foreach ($tasksToRun as $taskData) {
$task = Mage_Task_Factory::get($taskName, $this->getConfig(), true, 'deploy'); $task = Mage_Task_Factory::get($taskData, $this->getConfig(), true, 'deploy');
$task->init(); $task->init();
Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false); Mage_Console::output('Running <purple>' . $task->getName() . '</purple> ... ', 2, false);

16
Mage/Task/Factory.php

@ -4,12 +4,20 @@ class Mage_Task_Factory
/** /**
* *
* *
* @param string $taskName * @param string|array $taskData
* @param boolean $inRollback * @param boolean $inRollback
* @return Mage_Task_TaskAbstract * @return Mage_Task_TaskAbstract
*/ */
public static function get($taskName, Mage_Config $taskConfig, $inRollback = false, $stage = null) public static function get($taskData, Mage_Config $taskConfig, $inRollback = false, $stage = null)
{ {
if (is_array($taskData)) {
$taskName = $taskData['name'];
$taskParameters = $taskData['parameters'];
} else {
$taskName = $taskData;
$taskParameters = array();
}
$instance = null; $instance = null;
$taskName = ucwords(str_replace('-', ' ', $taskName)); $taskName = ucwords(str_replace('-', ' ', $taskName));
$taskName = str_replace(' ', '', $taskName); $taskName = str_replace(' ', '', $taskName);
@ -17,12 +25,12 @@ class Mage_Task_Factory
if (strpos($taskName, '/') === false) { if (strpos($taskName, '/') === false) {
Mage_Autoload::loadUserTask($taskName); Mage_Autoload::loadUserTask($taskName);
$className = 'Task_' . ucfirst($taskName); $className = 'Task_' . ucfirst($taskName);
$instance = new $className($taskConfig, $inRollback, $stage); $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
} else { } else {
$taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName))); $taskName = str_replace(' ', '_', ucwords(str_replace('/', ' ', $taskName)));
$className = 'Mage_Task_BuiltIn_' . $taskName; $className = 'Mage_Task_BuiltIn_' . $taskName;
$instance = new $className($taskConfig, $inRollback, $stage); $instance = new $className($taskConfig, $inRollback, $stage, $taskParameters);
} }
assert($instance instanceOf Mage_Task_TaskAbstract); assert($instance instanceOf Mage_Task_TaskAbstract);

15
Mage/Task/TaskAbstract.php

@ -4,16 +4,18 @@ abstract class Mage_Task_TaskAbstract
protected $_config = null; protected $_config = null;
protected $_inRollback = false; protected $_inRollback = false;
protected $_stage = null; protected $_stage = null;
protected $_parameters = array();
public abstract function getName(); public abstract function getName();
public abstract function run(); public abstract function run();
public final function __construct(Mage_Config $config, $inRollback = false, $stage = null) public final function __construct(Mage_Config $config, $inRollback = false, $stage = null, $parameters = array())
{ {
$this->_config = $config; $this->_config = $config;
$this->_inRollback = $inRollback; $this->_inRollback = $inRollback;
$this->_stage = $stage; $this->_stage = $stage;
$this->_parameters = $parameters;
} }
public function inRollback() public function inRollback()
@ -35,6 +37,17 @@ abstract class Mage_Task_TaskAbstract
{ {
} }
/**
* Return the a parameter
*
* @param string $name
* @return mixed
*/
public function getParameter($name, $default = null)
{
return $this->getConfig()->getParameter($name, $default, $this->_parameters);
}
protected final function _runLocalCommand($command, &$output = null) protected final function _runLocalCommand($command, &$output = null)
{ {
return Mage_Console::executeCommand($command, $output); return Mage_Console::executeCommand($command, $output);

3
docs/example-config/.mage/config/environment/staging.yml

@ -17,5 +17,8 @@ tasks:
- privileges - privileges
- sampleTask - sampleTask
- sampleTaskRollbackAware - sampleTaskRollbackAware
- taskWithParameters:
booleanOption: true
- taskWithParameters
#post-release #post-release
#post-deploy: #post-deploy:

20
docs/example-config/.mage/tasks/TaskWithParameters.php

@ -0,0 +1,20 @@
<?php
class Task_TaskWithParameters
extends Mage_Task_TaskAbstract
{
public function getName()
{
$booleanOption = $this->getParameter('booleanOption', false);
if ($booleanOption) {
return 'A Sample Task With Parameters [booleanOption=true]';
} else {
return 'A Sample Task With Parameters [booleanOption=false]';
}
}
public function run()
{
return true;
}
}
Loading…
Cancel
Save