From f713fbb16b9a2528a50ab75362c737c95fb80fef Mon Sep 17 00:00:00 2001 From: Aerex Date: Sat, 1 Dec 2018 20:55:56 -0600 Subject: [PATCH] feat: added modify taskwarrior command --- src/Plugin.php | 2 +- src/Taskwarrior/Commands/TodoStrategy.php | 127 ++++++++++++++-------- src/Taskwarrior/Task.php | 63 +++++++++-- src/Taskwarrior/Taskwarrior.php | 26 +++-- src/Taskwarrior/TaskwarriorManager.php | 25 +++-- 5 files changed, 173 insertions(+), 70 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 0ab834a..6a9b8e5 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -118,7 +118,7 @@ class Plugin extends ServerPlugin { $response->setStatus(200); $response->setBody(''); $response->setHeader('Content-Type', 'text/plain'); - return false; + return true; } /** diff --git a/src/Taskwarrior/Commands/TodoStrategy.php b/src/Taskwarrior/Commands/TodoStrategy.php index f96d15f..13628d5 100644 --- a/src/Taskwarrior/Commands/TodoStrategy.php +++ b/src/Taskwarrior/Commands/TodoStrategy.php @@ -4,57 +4,89 @@ namespace Aerex\TaskwarriorPlugin\Taskwarrior\Commands; use Aerex\TaskwarriorPlugin\Taskwarrior\Task; use Aerex\TaskwarriorPlugin\Config; use Symfony\Component\Process\Process; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Aerex\TaskwarriorPlugin\Exceptions\TaskwarriorCommandException; + + class TodoStrategy implements IStrategy { +private $config; - public function __construct(TaskwarriorConfig $config){ - $this->config = $config; +public function __construct(Config $config){ + $this->config = $config; +} + +public function count($uuid){ + $cmd[] = $this->config->getTaskBin(); + $cmd[] = sprintf('%s count', $uuid); + return $this->executeCommand($cmd); +} + +public function modify($task){ + $cmd[] = $this->config->getTaskBin(); + + $uuid = $task->getUuid(); + $taskJson = $task->convertToArray(); + + // Append modify command + $cmd[] = sprintf(' %s modify ', $uuid); + + $document = $task->getDescription(); + + // Append as first modifier description if set + + if(isset($document)){ + $cmd[] = $document; } - public function add(Task $task){ - $cmd[] = $this->config->getTaskBin(); - $cmd[] = 'add'; - - if($task->getDescription() != null){ - $cmd[] = sprintf('"%s"', (string)$task->getDescription()); + // Append on modifiers + foreach($taskJson as $prop => $value){ + if(isset($value) && $value != null){ + $cmd[] = sprintf(' %s: %s ', $prop, $value); } - - if($task->getCategories() != null){ - $categories = implode(' +', (string)$task->getCategories()); - $cmd[] = $categories; - } - - if($task->getDue() != null){ - $cmd[] = sprintf("due:%s",$task->getDue('Y-m-dTH:i:s')); - } - - if($task->getRecurrence() != null){ - $rrule = $task->getRecurrence()->getParts(); - $cmd[] = sprintf('recur:%s', $rrule['FREQ']); - if(isset($rrule['UNTIL'])){ - $cmd[] = sprintf('until:%s', $rrule['UNTIL']); - } - - } - - if($task->getStatus() != null){ - $cmd[] = sprintf('status:%s', (string)$task->getStatus()); - } - - return $this->executeCommand($cmd); } - public function count($uuid){ - $cmd[] = $this->config->getTaskBin(); - $cmd[] = sprintf('%s count', $uuid); - return $this->executeCommand($cmd); + return $this->executeCommand($cmd); + +} + +public function add(Task $task){ + $cmd[] = $this->config->getTaskBin(); + $cmd[] = 'add'; + + if($task->getDescription() != null){ + $cmd[] = sprintf('"%s"', (string)$task->getDescription()); } + if($task->getCategories() != null){ + $categories = implode(' +', (string)$task->getCategories()); + $cmd[] = $categories; + } + + if($task->getDue() != null){ + $cmd[] = sprintf("due:%s",$task->getDue('Y-m-dTH:i:s')); + } + + if($task->getRecurrence() != null){ + $rrule = $task->getRecurrence()->getParts(); + $cmd[] = sprintf('recur:%s', $rrule['FREQ']); + if(isset($rrule['UNTIL'])){ + $cmd[] = sprintf('until:%s', $rrule['UNTIL']); + } + } + + if($task->getStatus() != null){ + $cmd[] = sprintf('status:%s', (string)$task->getStatus()); + } + + return $this->executeCommand($cmd); +} - private function executeCommand($command){ - $rcOptions = $this->config->getOptions(); + +private function executeCommand($command){ + $rcOptions = $this->config->getOptions(); foreach ($rcOptions as $rcOption) { $command[] = $rcOption; @@ -64,17 +96,20 @@ class TodoStrategy implements IStrategy { echo $cmdString; $process = new Process($cmdString); - $process->run(); + try { + $process->mustRun(); + // clear cmd queue + $this->cmd = []; + return $process->getOutput(); + } catch(ProcessFailedException $error){ + throw new TaskwarriorCommandException($error->getMesage()); + return; - if(!$process->isSuccessful()){ - echo $process; - throw new TaskwarriorCommandLineException($process); } - // clear cmd queue - $this->cmd = []; - return $process->getOutput(); - } } + +} + diff --git a/src/Taskwarrior/Task.php b/src/Taskwarrior/Task.php index b57e977..db35247 100644 --- a/src/Taskwarrior/Task.php +++ b/src/Taskwarrior/Task.php @@ -1,3 +1,4 @@ + uuid; - } + public function getUuid(){ + return $this->uuid; + } + + public function convertToArray(){ + $tagsString = null; + $dependsString = null; + $dueString = null; + + // Process array properties + if(isset($this->tags)){ + $tagsString = implode(',', $this->tags); + } + if(isset($this->depends)){ + $dependsString = implode(',', $this->depends); + } + + // Process date properties + if(isset($this->due)){ + $dueString = $this->due->format('Y-m-dTH:i:s'); + + } + + + + return array( + "status" => $this->status, + "start" => $this->start, + "wait" => $this->wait, + "end" => $this->end, + "entry" => $this->entry, + "priority" => $this->priority, + "project" => $this->project, + "due" => $this->due, + "tags" => $tagsString, + "urgency" => $this->urgency, + "recu" => $this->recur, + "until" => $this->until, + "tags" => $this->annotations, + "modified" => $this->modified, + "depends" => $dependsString + ); + } /** * @@ -140,7 +185,7 @@ public function setDescription(VTodo $component){ if(!isset($component->DESCRIPTION) && isset($component->SUMMARY)){ $this->description = $component->SUMMARY; } else if(!isset($component->DESCRIPTION) && !isset($component->SUMMARY)){ - throw new Exception('Task must have a description or summary'); + throw new Exception("Task must have a description or summary"); } else { $this->description = $component->DESCRIPTION; } @@ -192,7 +237,11 @@ public function setDue(VTodo $document){ $this->due = new Carbon($document->DUE->getDateTime()->format(\DateTime::W3C)); } } -public function getDue(){ +public function getDue($format=null){ + if($format != null){ + return $this->due->format($format); + } + return $this->due; } @@ -236,7 +285,6 @@ public function getRecurrence(){ return $this->recur; } - public function setRecurrence(VTodo $document){ if(isset($document->RRULE)){ $this->recur = $document->RRULE; @@ -252,3 +300,4 @@ public function getStatus(){ } + diff --git a/src/Taskwarrior/Taskwarrior.php b/src/Taskwarrior/Taskwarrior.php index c5dcd63..a4af1a3 100644 --- a/src/Taskwarrior/Taskwarrior.php +++ b/src/Taskwarrior/Taskwarrior.php @@ -8,6 +8,11 @@ use Aerex\TaskwarriorPlugin\Config; class Taskwarrior { + /** + * @var IStrategy + */ + private $strategy; + /** * @var TaskwarriorConfig */ @@ -23,7 +28,6 @@ class Taskwarrior { */ private $taskrc; - /** * @var string */ @@ -34,11 +38,6 @@ class Taskwarrior { */ private $rcOptions; - /** - * @var IStrategy - */ - - private $strategy; /** * @var string */ @@ -49,8 +48,11 @@ class Taskwarrior { } $this->config = $config; } + public function getConfig(){ + return $this->config; + } - public function setStrategy(IStrategy $strategy){ + public function setStrategy($strategy){ $this->strategy = $strategy; } @@ -65,12 +67,18 @@ class Taskwarrior { } public function count($uuid){ - return $this->strategy->cound($uuid); - } + return $this->strategy->count($uuid); + } + public function modify($task){ + return $this->strategy->modify($task); + + } } ?> + + diff --git a/src/Taskwarrior/TaskwarriorManager.php b/src/Taskwarrior/TaskwarriorManager.php index fa489f0..3d1e1b4 100644 --- a/src/Taskwarrior/TaskwarriorManager.php +++ b/src/Taskwarrior/TaskwarriorManager.php @@ -4,28 +4,33 @@ namespace Aerex\TaskwarriorPlugin\Taskwarrior; use Aerex\TaskwarriorPlugin\Taskwarrior\Taskwarrior; use Aerex\TaskwarriorPlugin\Taskwarrior\Task; +use Aerex\TaskwarriorPlugin\Taskwarrior\Commands\TodoStrategy; use Sabre\VObject\Component\VTodo; class TaskwarriorManager { + /** - * @var TodoStrategy + * @var TodoStrategy */ private $todoStrategy; + public function __construct($taskwarrior){ if(!isset($taskwarrior)){ $this->taskwarrior = new Taskwarrior(); } else { $this->taskwarrior = $taskwarrior; } + // Initialize strategies $this->todoStrategy = new TodoStrategy($this->taskwarrior->getConfig()); } public function addTask(VTodo $document){ + $this->taskwarrior->setStrategy($this->todoStrategy); $task = $this->taskwarrior->createTask((string)$document->UID); @@ -38,8 +43,6 @@ class TaskwarriorManager { $task->setCategories($document); - $task->setStatus($document); - $task->setRecurrence($document); return $this->taskwarrior->add($task); @@ -48,11 +51,11 @@ class TaskwarriorManager { public function updateTask(VTodo $document){ - $task = $this->taskwarrior->get($document->UID); + $task = $this->taskwarrior->createTask((string)$document->UID); $task->setDescription($document); - $task->setEntry($document); + $task->setEntryTime($document); $task->setStartTime($document); @@ -74,11 +77,19 @@ class TaskwarriorManager { } public function taskExists($taskUuid){ - $this->taskwarrior->setStrategy($this->$todoStrategy); - return $this->taskwarrior->count((string)$taskUuid); + + $this->taskwarrior->setStrategy($this->todoStrategy); + $exists = $this->taskwarrior->count((string)$taskUuid); + echo("exists " . $exists); + if($exists === "1"){ + return true; + } + return false; } + } ?> +