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
This commit is contained in:
Ingo Schommer 2008-10-16 20:40:24 +00:00
parent f1f76b22b7
commit 5b91ea9d0a
3 changed files with 95 additions and 21 deletions

View File

@ -11,12 +11,44 @@
* @package sapphire * @package sapphire
* @subpackage dev * @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); abstract function run($request);
public function isDisabled() { public function isEnabled() {
return (property_exists($this, 'Disabled')) ? true : false; 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;
} }
} }

View File

@ -7,7 +7,11 @@
* @package sapphire * @package sapphire
* @subpackage dev * @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) { function run($request) {
if ($request->param('Direction') == 'down') { if ($request->param('Direction') == 'down') {
@ -17,9 +21,9 @@ abstract class MigrationTask extends BuildTask {
} }
} }
abstract function up(); function up() {}
abstract function down(); function down() {}
} }

View File

@ -11,35 +11,73 @@ class TaskRunner extends Controller {
); );
function index() { function index() {
$tasks = ClassInfo::subclassesFor('BuildTask'); $tasks = $this->getTasks();
if(Director::is_cli()) {
echo "Tasks available:\n\n"; // Web mode
foreach($tasks as $task) echo " * $task: sake dev/tasks/$task\n"; if(!Director::is_cli()) {
} else { $renderer = new DebugView();
echo "<h1>Tasks available</h1>\n"; $renderer->writeHeader();
$renderer->writeInfo("Sapphire Development Tools: Tasks", Director::absoluteBaseURL());
$base = Director::baseURL();
echo "<ul>"; echo "<ul>";
foreach($tasks as $task) { foreach($tasks as $task) {
echo "<li><a href=\"$task\">$task</a></li>\n"; echo "<li>";
echo "<a href=\"" . $task['class'] . "\">" . $task['title'] . "</a><br />";
echo "<span class=\"description\">" . $task['description'] . "</span>";
echo "</li>\n";
} }
echo "</ul>"; echo "</ul>";
$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) { function runTask($request) {
$TaskName = $request->param('TaskName'); $taskName = $request->param('TaskName');
if (class_exists($TaskName) && is_subclass_of($TaskName, 'BuildTask')) { if (class_exists($taskName) && is_subclass_of($taskName, 'BuildTask')) {
if(Director::is_cli()) echo "Running task '$TaskName'...\n\n"; $title = singleton($taskName)->getTitle();
else echo "<h1>Running task '$TaskName'...</h1>\n"; if(Director::is_cli()) echo "Running task '$title'...\n\n";
else echo "<h1>Running task '$title'...</h1>\n";
$task = new $TaskName(); $task = new $taskName();
if (!$task->isDisabled()) $task->run($request); if ($task->isEnabled()) $task->run($request);
} else { } else {
echo "Build task '$TaskName' not found."; echo "Build task '$taskName' not found.";
if(class_exists($TaskName)) echo " It isn't a subclass of BuildTask."; if(class_exists($taskName)) echo " It isn't a subclass of BuildTask.";
echo "\n"; 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;
}
} }
?> ?>