feat: Converted vcalendar todo events to taskwarrior tasks
This commit is contained in:
@@ -11,32 +11,18 @@ class ConfigBuilder implements ConfigurationInterface {
|
||||
private $configs = [];
|
||||
private $configDir;
|
||||
|
||||
public function __construct($configDir = null) {
|
||||
if (!isset($configDir)) {
|
||||
$this->configDir = $this->getHomeDir() . '~/.config/baikal';
|
||||
} else {
|
||||
$this->configDir = $configDir;
|
||||
}
|
||||
public function __construct($configDir) {
|
||||
$this->configDir = $configDir;
|
||||
$this->processor = new Processor();
|
||||
}
|
||||
|
||||
private function getHomeDir() {
|
||||
if (stristr(PHP_OS, 'WIN')) {
|
||||
return rtrim($_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'], '\\/');
|
||||
} else {
|
||||
return rtrim($_SERVER['HOME'], '/');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function add($config) {
|
||||
$this->configs[] = $config;
|
||||
}
|
||||
|
||||
public function getConfigTreeBuilder() {
|
||||
$treeBuilder = new TreeBuilder('configs');
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('configs');
|
||||
$ref = $rootNode->children();
|
||||
foreach ($this->configs as $config) {
|
||||
$ref = $ref->append($config->get());
|
||||
@@ -46,10 +32,7 @@ class ConfigBuilder implements ConfigurationInterface {
|
||||
}
|
||||
|
||||
public function readContent() {
|
||||
if (!is_dir($this->configDir)) {
|
||||
mkdir($this->configDir, 0755, true);
|
||||
}
|
||||
$contents = sprintf('%s/storage.yml', $this->configDir);
|
||||
$contents = sprintf('%s/storage.yaml', $this->configDir);
|
||||
return file_get_contents($contents);
|
||||
}
|
||||
|
||||
|
@@ -6,12 +6,17 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
|
||||
class TaskwarriorConfig {
|
||||
public function get() {
|
||||
$treeBuilder = new TreeBuilder('taskwarrior');
|
||||
$node = $treeBuilder->getRootNode();
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$node = $treeBuilder->root('taskwarrior');
|
||||
$node->children()
|
||||
->scalarNode('data_dir')
|
||||
->defaultValue('~/.task')
|
||||
->end();
|
||||
->scalarNode('taskdata')
|
||||
->defaultValue('~/.task')
|
||||
->end()
|
||||
->scalarNode('taskrc')
|
||||
->defaultValue('~/.taskrc')
|
||||
->end()
|
||||
->end();
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
@@ -18,14 +18,15 @@ class Console extends AbstractConsole {
|
||||
}
|
||||
}
|
||||
|
||||
public function execute($cmd, $args, $input = null) {
|
||||
public function execute($cmd, $args, $input = null, $envs = []) {
|
||||
$stdin[] = $cmd;
|
||||
$stdin[] = array_merge($stdin, $this->defaultArgs, $args);
|
||||
$stdin = array_merge($stdin, $this->defaultArgs, $args);
|
||||
|
||||
if (isset($input)) {
|
||||
$stdin[] = $this->convertToString($input);
|
||||
$process = new Process($stdin);
|
||||
}
|
||||
$process = new Process(implode(' ', $stdin), $input, $envs);
|
||||
$process->inheritEnvironmentVariables();
|
||||
|
||||
try {
|
||||
$process->mustRun();
|
||||
|
@@ -31,11 +31,6 @@ class Plugin extends ServerPlugin {
|
||||
*/
|
||||
protected $storageManager;
|
||||
|
||||
/**
|
||||
* @var ConfigBuilder
|
||||
*/
|
||||
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Creates the Taskwarrior plugin
|
||||
@@ -43,14 +38,16 @@ class Plugin extends ServerPlugin {
|
||||
* @param CalendarProcessor $TWCalManager
|
||||
*
|
||||
*/
|
||||
function __construct($config = null){
|
||||
if (isset($config)) {
|
||||
$this->config = $config;
|
||||
} else {
|
||||
$this->config = new ConfigBuilder();
|
||||
}
|
||||
$this->storageManager = new StorageManager($this->config);
|
||||
$this->addStorages();
|
||||
function __construct($configDir){
|
||||
$configs = $this->buildConfigurations($configDir);
|
||||
$this->storageManager = new StorageManager($configs);
|
||||
$this->initializeStorages($configDir, $configs);
|
||||
}
|
||||
|
||||
public function buildConfigurations($configDir) {
|
||||
$this->config = new ConfigBuilder($configDir);
|
||||
$this->config->add(new TaskwarriorConfig());
|
||||
return $this->config->loadYaml();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,10 +55,9 @@ class Plugin extends ServerPlugin {
|
||||
*
|
||||
*/
|
||||
|
||||
public function addStorages() {
|
||||
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), new TaskwarriorConfig());
|
||||
public function initializeStorages($configDir, $configs) {
|
||||
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configDir, $configs);
|
||||
$this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior);
|
||||
$this->storageManager->init();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -3,7 +3,6 @@
|
||||
namespace Aerex\BaikalStorage;
|
||||
|
||||
use Sabre\VObject\Component\VCalendar as Calendar;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
|
||||
class StorageManager {
|
||||
|
||||
@@ -15,13 +14,12 @@ class StorageManager {
|
||||
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
* @var array()
|
||||
*/
|
||||
private $configBuilder;
|
||||
private $configs;
|
||||
|
||||
public function __construct($configBuilder){
|
||||
$this->configBuilder = $configBuilder;
|
||||
public function __construct($configs){
|
||||
$this->configs = $configs;
|
||||
}
|
||||
|
||||
public function getStorages() {
|
||||
@@ -33,14 +31,9 @@ class StorageManager {
|
||||
}
|
||||
|
||||
public function addStorage($name, $storage) {
|
||||
$this->configBuilder->add($storage->getConfig());
|
||||
$this->storages[$name] = $storage;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->configs = $this->configBuilder->loadYaml();
|
||||
}
|
||||
|
||||
public function import(Calendar $calendar) {
|
||||
if (!isset($this->configs)) {
|
||||
throw new \Exception('StorageManger was not initialize or configs are not defined');
|
||||
@@ -50,7 +43,6 @@ class StorageManager {
|
||||
if (!isset($storage)){
|
||||
throw new \Exception();
|
||||
}
|
||||
$storage->setRawConfigs($this->configs[$key]);
|
||||
$storage->save($calendar);
|
||||
}
|
||||
}
|
||||
|
@@ -8,5 +8,4 @@ interface IStorage {
|
||||
public function save(Calendar $c);
|
||||
public function refresh();
|
||||
public function getConfig();
|
||||
public function setRawConfigs($rawConfigs);
|
||||
}
|
||||
|
@@ -8,25 +8,22 @@ use Carbon\Carbon;
|
||||
class Taskwarrior implements IStorage {
|
||||
|
||||
private const DATA_FILES = ['pending.data', 'completed.data', 'undo.data'];
|
||||
private $rawConfigs;
|
||||
public const NAME = 'taskwarrior';
|
||||
private $tasks = [];
|
||||
public function __construct($console, $config) {
|
||||
private $configDir;
|
||||
private $configs;
|
||||
public function __construct($console, $configDir, $configs) {
|
||||
$this->console = $console;
|
||||
$this->config = $config;
|
||||
$this->configDir = $configDir;
|
||||
$this->configs = $configs['taskwarrior'];
|
||||
}
|
||||
|
||||
public function getConfig() {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
public function setRawConfigs($rawConfigs) {
|
||||
$this->rawConfigs = $rawConfigs;
|
||||
}
|
||||
|
||||
public function refresh() {
|
||||
$dataDir = $this->rawConfigs['data_dir'];
|
||||
$fp = fopen(sprintf('%s/taskwarrior-baikal-storage.lock', $dataDir), 'a');
|
||||
$fp = fopen(sprintf('%s/taskwarrior-baikal-storage.lock', $this->configDir), 'a');
|
||||
|
||||
if (!$fp || !flock($fp, LOCK_EX | LOCK_NB, $eWouldBlock) || $eWouldBlock) {
|
||||
fputs(STDERR, 'Could not get lock');
|
||||
@@ -35,55 +32,125 @@ class Taskwarrior implements IStorage {
|
||||
$mtime = 0;
|
||||
$tasksUpdated = false;
|
||||
foreach (Taskwarrior::DATA_FILES as $dataFile) {
|
||||
$fmtime = filemtime(sprintf('%s/%s', $this->config['data_dir'], $dataFile));
|
||||
if ($fmtime > $mtime) {
|
||||
$mtime = $fmtime;
|
||||
$tasksUpdated = true;
|
||||
}
|
||||
$fmtime = filemtime(sprintf('%s/%s', $this->configs['taskdata'], $dataFile));
|
||||
if ($fmtime > $mtime) {
|
||||
$mtime = $fmtime;
|
||||
$tasksUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($tasksUpdated) {
|
||||
$tasks = $this->console->execute('task', ['export']);
|
||||
$tasks = json_decode($this->console->execute('task', ['export'], null,
|
||||
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
|
||||
foreach ($tasks as $task) {
|
||||
$this->tasks[$task['uuid']] = $task;
|
||||
}
|
||||
}
|
||||
fclose($fp);
|
||||
unlink(sprintf('%s/taskwarrior-baikal-storage.lock', $dataDir));
|
||||
unlink(sprintf('%s/taskwarrior-baikal-storage.lock', $this->configDir));
|
||||
}
|
||||
|
||||
public function vObjectToTask($vtodo) {
|
||||
if ($this->tasks['uid'] == $vtodo->UID) {
|
||||
if (isset($this->tasks['uid']) && $this->tasks['uid'] == $vtodo->UID) {
|
||||
$task = $this->tasks['uid'];
|
||||
} else {
|
||||
$task = [];
|
||||
$task['uid'] = $vtodo->UID;
|
||||
$task['uid'] = (string)$vtodo->UID;
|
||||
}
|
||||
|
||||
|
||||
if (!isset($vtodo->DESCRIPTION) && isset($vtodo->SUMMARY)){
|
||||
$task['description'] = $vtodo->SUMMARY;
|
||||
$task['description'] = (string)$vtodo->SUMMARY;
|
||||
} else {
|
||||
$task['description'] = $vtodo->DESCRIPTION;
|
||||
$task['description'] = (string)$vtodo->DESCRIPTION;
|
||||
}
|
||||
|
||||
if (isset($vtodo->DTSTAMP)){
|
||||
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
|
||||
if (isset($vtodo->DTSTART)) {
|
||||
$task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
|
||||
if (isset($vtodo->DTEND)){
|
||||
$task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
|
||||
if (isset($vtodo->{'LAST-MODIFIED'})) {
|
||||
$task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
|
||||
if (isset($vtodo->PRIORITY)) {
|
||||
$priority = $vtodo->PRIORITY->getJsonValue();
|
||||
if ($priority < 5) {
|
||||
$task['priority'] = 'H';
|
||||
} else if ($priority === 5) {
|
||||
$task['priority'] = 'M';
|
||||
} else if ($priority > 5 && $priority < 10) {
|
||||
$task['priority'] = 'L';
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($vtodo->DUE)){
|
||||
$task['due'] = new Carbon($vtodo->DUE->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function save(Calendar $c) {
|
||||
if (!isset($c->VTODO)){
|
||||
throw new \Exception('Calendar event does not contain VTODO');
|
||||
if (isset($vtodo->RRULE)) {
|
||||
$rules = $vtodo->RRULE->getParts();
|
||||
if (isset($rules['FREQ'])) {
|
||||
$task['recu'] = $rules['FREQ'];
|
||||
}
|
||||
if (isset($rules['UNTIL'])) {
|
||||
$task['until'] = $rules['UNTIL'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($vtodo->STATUS)) {
|
||||
switch((string)$vtodo->STATUS) {
|
||||
case 'NEEDS-ACTION':
|
||||
$task['status'] = 'pending';
|
||||
break;
|
||||
case 'COMPLETED':
|
||||
$task['status'] = 'completed';
|
||||
if (!isset($task['end'])) {
|
||||
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
break;
|
||||
case 'CANCELED':
|
||||
$task['status'] = 'deleted';
|
||||
if (!isset($task['end'])) {
|
||||
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (isset($vtodo->CATEGORIES)) {
|
||||
$task['tags'] = [];
|
||||
foreach ($vtodo->CATEGORIES as $category) {
|
||||
if (isset($this->configs['project_tag_suffix'])) {
|
||||
$projTagSuffixRegExp = sprintf('/^%s_/', $this->configs['project_tag_suffix']);
|
||||
if (preg_match($category, $projTagSuffixRegExp)) {
|
||||
$task['project'] = preg_replace($projTagSuffixRegExp, '', $category);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$task['tags'] = $category;
|
||||
}
|
||||
}
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function save(Calendar $c) {
|
||||
if (!isset($c->VTODO)){
|
||||
throw new \Exception('Calendar event does not contain VTODO');
|
||||
}
|
||||
$this->refresh();
|
||||
$task = $this->vObjectToTask($c->VTODO);
|
||||
$this->console->execute('task', ['import'], $task,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
}
|
||||
}
|
||||
$this->refresh();
|
||||
$task = $this->vObjectToTask($c->VTODO);
|
||||
$this->console->execute('task', ['import'], $task);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user