feat: added modify taskwarrior command

This commit is contained in:
Aerex 2018-12-01 20:55:56 -06:00
parent 68470e3dee
commit f713fbb16b
5 changed files with 173 additions and 70 deletions

View File

@ -118,7 +118,7 @@ class Plugin extends ServerPlugin {
$response->setStatus(200);
$response->setBody('');
$response->setHeader('Content-Type', 'text/plain');
return false;
return true;
}
/**

View File

@ -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();
}
}
}

View File

@ -1,3 +1,4 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior;
@ -13,12 +14,15 @@ class Task {
/**
* @var string
*
* @JMS\Type("string")
*/
private $uuid;
/**
* @var string
*
* @JMS\Type("string")
*/
private $description;
@ -107,11 +111,12 @@ class Task {
private $status;
/**
* @var Task[]|ArrayCollection
*
* @JMS\Type("Depends")
*/
private $depends;
public function __construct($UUID=null){
$validator = new Uuid();
@ -126,10 +131,50 @@ class Task {
}
public function getUuid(){
return $this->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(){
}

View File

@ -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);
}
}
?>

View File

@ -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
*/
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;
}
}
?>