From 5b91ea9d0af10b28d9b2d8fadfaa0ef06fdca642 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 16 Oct 2008 20:40:24 +0000 Subject: [PATCH] ENHANCEMENT Improved TaskRunner and BuildTask metadata and styling git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64446 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- dev/BuildTask.php | 38 ++++++++++++++++++++++-- dev/MigrationTask.php | 10 +++++-- dev/TaskRunner.php | 68 +++++++++++++++++++++++++++++++++---------- 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/dev/BuildTask.php b/dev/BuildTask.php index 1ee8a6820..019c92658 100644 --- a/dev/BuildTask.php +++ b/dev/BuildTask.php @@ -11,12 +11,44 @@ * @package sapphire * @subpackage dev */ -abstract class BuildTask { +abstract class BuildTask extends Object { + + /** + * @var bool $enabled If set to FALSE, keep it from showing in the list + * and from being executable through URL or CLI. + */ + protected $enabled = true; + + /** + * @var string $title Shown in the overview on the {@link TaskRunner} + * HTML or CLI interface. Should be short and concise, no HTML allowed. + */ + protected $title; + + /** + * @var string $description Describe the implications the task has, + * and the changes it makes. Accepts HTML formatting. + */ + protected $description = 'No description available'; abstract function run($request); - public function isDisabled() { - return (property_exists($this, 'Disabled')) ? true : false; + public function isEnabled() { + return $this->enabled; + } + + /** + * @return string + */ + public function getTitle() { + return ($this->title) ? $this->title : $this->class; + } + + /** + * @return string HTML formatted description + */ + public function getDescription() { + return $this->description; } } diff --git a/dev/MigrationTask.php b/dev/MigrationTask.php index 91d443bd3..26a00ef57 100644 --- a/dev/MigrationTask.php +++ b/dev/MigrationTask.php @@ -7,7 +7,11 @@ * @package sapphire * @subpackage dev */ -abstract class MigrationTask extends BuildTask { +class MigrationTask extends BuildTask { + + protected $title = "Database Migrations"; + + protected $description = "Provide atomic database changes (not implemented yet)"; function run($request) { if ($request->param('Direction') == 'down') { @@ -17,9 +21,9 @@ abstract class MigrationTask extends BuildTask { } } - abstract function up(); + function up() {} - abstract function down(); + function down() {} } diff --git a/dev/TaskRunner.php b/dev/TaskRunner.php index c76b37a7e..198f7b840 100644 --- a/dev/TaskRunner.php +++ b/dev/TaskRunner.php @@ -11,35 +11,73 @@ class TaskRunner extends Controller { ); function index() { - $tasks = ClassInfo::subclassesFor('BuildTask'); - if(Director::is_cli()) { - echo "Tasks available:\n\n"; - foreach($tasks as $task) echo " * $task: sake dev/tasks/$task\n"; - } else { - echo "

Tasks available

\n"; + $tasks = $this->getTasks(); + + // Web mode + if(!Director::is_cli()) { + $renderer = new DebugView(); + $renderer->writeHeader(); + $renderer->writeInfo("Sapphire Development Tools: Tasks", Director::absoluteBaseURL()); + $base = Director::baseURL(); + echo ""; + + $renderer->writeFooter(); + // CLI mode + } else { + echo "SAPPHIRE DEVELOPMENT TOOLS: Tasks\n--------------------------\n\n"; + foreach($tasks as $task) { + echo " * $task: sake dev/tasks/" . $task['class'] . "\n"; + } } } function runTask($request) { - $TaskName = $request->param('TaskName'); - if (class_exists($TaskName) && is_subclass_of($TaskName, 'BuildTask')) { - if(Director::is_cli()) echo "Running task '$TaskName'...\n\n"; - else echo "

Running task '$TaskName'...

\n"; + $taskName = $request->param('TaskName'); + if (class_exists($taskName) && is_subclass_of($taskName, 'BuildTask')) { + $title = singleton($taskName)->getTitle(); + if(Director::is_cli()) echo "Running task '$title'...\n\n"; + else echo "

Running task '$title'...

\n"; - $task = new $TaskName(); - if (!$task->isDisabled()) $task->run($request); + $task = new $taskName(); + if ($task->isEnabled()) $task->run($request); } else { - echo "Build task '$TaskName' not found."; - if(class_exists($TaskName)) echo " It isn't a subclass of BuildTask."; + echo "Build task '$taskName' not found."; + if(class_exists($taskName)) echo " It isn't a subclass of BuildTask."; echo "\n"; } } + /** + * @return array Array of associative arrays for each task (Keys: 'class', 'title', 'description') + */ + protected function getTasks() { + $availableTasks = array(); + + $taskClasses = ClassInfo::subclassesFor('BuildTask'); + // remove the base class + array_shift($taskClasses); + + if($taskClasses) foreach($taskClasses as $class) { + if(!singleton($class)->isEnabled()) continue; + $desc = (Director::is_cli()) ? Convert::html2raw(singleton($class)->getDescription()) : singleton($class)->getDescription(); + $availableTasks[] = array( + 'class' => $class, + 'title' => singleton($class)->getTitle(), + 'description' => $desc, + ); + } + + return $availableTasks; + } + } ?> \ No newline at end of file