Merge pull request #1189 from ajshort/namespaced-tasks

Support running namespaced build tasks.
This commit is contained in:
Ingo Schommer 2013-02-17 11:47:26 -08:00
commit 5a4d5e10d2

View File

@ -43,7 +43,7 @@ class TaskRunner extends Controller {
echo "<ul>"; echo "<ul>";
foreach($tasks as $task) { foreach($tasks as $task) {
echo "<li><p>"; echo "<li><p>";
echo "<a href=\"{$base}dev/tasks/" . $task['class'] . "\">" . $task['title'] . "</a><br />"; echo "<a href=\"{$base}dev/tasks/" . $task['segment'] . "\">" . $task['title'] . "</a><br />";
echo "<span class=\"description\">" . $task['description'] . "</span>"; echo "<span class=\"description\">" . $task['description'] . "</span>";
echo "</p></li>\n"; echo "</p></li>\n";
} }
@ -54,28 +54,41 @@ class TaskRunner extends Controller {
} else { } else {
echo "SILVERSTRIPE DEVELOPMENT TOOLS: Tasks\n--------------------------\n\n"; echo "SILVERSTRIPE DEVELOPMENT TOOLS: Tasks\n--------------------------\n\n";
foreach($tasks as $task) { foreach($tasks as $task) {
echo " * $task[title]: sake dev/tasks/" . $task['class'] . "\n"; echo " * $task[title]: sake dev/tasks/" . $task['segment'] . "\n";
} }
} }
} }
public function runTask($request) { public function runTask($request) {
$taskName = $request->param('TaskName'); $name = $request->param('TaskName');
if (class_exists($taskName) && is_subclass_of($taskName, 'BuildTask')) { $tasks = $this->getTasks();
$title = singleton($taskName)->getTitle();
if(Director::is_cli()) echo "Running task '$title'...\n\n";
elseif(!Director::is_ajax()) echo "<h1>Running task '$title'...</h1>\n";
$task = Injector::inst()->create($taskName); $title = function ($content) {
if ($task->isEnabled()) $task->run($request); printf(Director::is_cli() ? "%s\n\n" : '<h1>%s</h1>', $content);
else echo "<p>{$title} is disabled</p>"; };
} else {
echo "Build task '$taskName' not found."; $message = function ($content) {
if(class_exists($taskName)) echo " It isn't a subclass of BuildTask."; printf(Director::is_cli() ? "%s\n" : '<p>%s</p>', $content);
echo "\n"; };
foreach ($tasks as $task) {
if ($task['segment'] == $name) {
$inst = Injector::inst()->create($task['class']);
$title(sprintf('Running Task %s', $inst->getTitle()));
if (!$inst->isEnabled()) {
$message('The task is disabled');
return;
}
$inst->run($request);
return;
}
} }
$message(sprintf('The build task "%s" could not be found', $name));
} }
/** /**
* @return array Array of associative arrays for each task (Keys: 'class', 'title', 'description') * @return array Array of associative arrays for each task (Keys: 'class', 'title', 'description')
*/ */
@ -95,13 +108,14 @@ class TaskRunner extends Controller {
$availableTasks[] = array( $availableTasks[] = array(
'class' => $class, 'class' => $class,
'title' => singleton($class)->getTitle(), 'title' => singleton($class)->getTitle(),
'segment' => str_replace('\\', '-', $class),
'description' => $desc, 'description' => $desc,
); );
} }
return $availableTasks; return $availableTasks;
} }
} }