Misc fixes and improvements

* Added request logging to com.noccy.apiclient
* Added plugin com.noccy.watcher
* Added pipe command and filter support
* Fixes and stubs
This commit is contained in:
2021-12-14 23:01:25 +01:00
parent 8cc1eac7a4
commit 30dfd4889b
22 changed files with 648 additions and 3 deletions

View File

@ -0,0 +1,56 @@
<?php
namespace SparkPlug\Com\Noccy\Watcher\Commands;
use Spark\Commands\Command;
use SparkPlug;
use SparkPlug\Com\Noccy\ApiClient\Api\Method;
use SparkPlug\Com\Noccy\ApiClient\ApiClientPlugin;
use SparkPlug\Com\Noccy\ApiClient\Log\RequestData;
use SparkPlug\Com\Noccy\ApiClient\Request\RequestBuilder;
use SparkPlug\Com\Noccy\Watcher\Rule;
use SparkPlug\Com\Noccy\Watcher\WatcherPlug;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class WatchCommand extends Command
{
protected function configure()
{
$this->setName("watch")
->setDescription("Watch files and take action when they are modified")
->addOption("interval", "N", InputOption::VALUE_REQUIRED, "Interval between polls", 5)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var WatcherPlug $plugin */
$plugin = get_plugin('com.noccy.watcher');
$config = read_config('watchers.json');
$iv = max(1, (int)$input->getOption('interval'));
if (!($plugin && $config)) {
$output->writeln("<error>Missing or bad config file watchers.json?</>");
return Command::FAILURE;
}
$watcher = $plugin->getFileWatcher();
foreach ($config['watchers'] as $ruleconf) {
$rule = Rule::createFromConfig($ruleconf);
$watcher->addRule($rule);
}
while (true) {
$watcher->loop();
sleep($iv);
}
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,53 @@
<?php // "name":"Watch files and act when they are changed", "author":"Noccy"
namespace SparkPlug\Com\Noccy\Watcher;
use Spark\Environment\ScriptRunner;
use SparkPlug\Com\Noccy\Watcher\Monitor\MonitorInterface;
use SparkPlug\Com\Noccy\Watcher\Monitor\MtimeMonitor;
use SparkPlug\Com\Noccy\Watcher\Monitor\InotifyMonitor;
class FileWatcher {
private MonitorInterface $monitor;
private ScriptRunner $scriptRunner;
private array $rules = [];
public function __construct()
{
if (extension_loaded('inotify')) {
$this->monitor = new MtimeMonitor();
//$this->monitor = new InotifyMonitor();
} else {
$this->monitor = new MtimeMonitor();
}
$this->scriptRunner = get_environment()->getScriptRunner();
}
public function addRule(Rule $rule)
{
if ($rule->getInitialTrigger()) {
$this->triggerRule($rule);
}
$this->rules[] = $rule;
$this->monitor->add($rule);
}
private function triggerRule(Rule $rule)
{
$actions = $rule->getActions();
$this->scriptRunner->evaluate($actions);
}
public function loop()
{
$this->monitor->loop();
$modified = $this->monitor->getModified();
foreach ($modified as $rule) {
$this->triggerRule($rule);
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace SparkPlug\Com\Noccy\Watcher\Monitor;
use SparkPlug\Com\Noccy\Watcher\Rule;
interface MonitorInterface
{
/**
* Add a rule to be watched for changes
*/
public function add(Rule $rule);
/**
* Return a list of modified filenames
*/
public function getModified(): array;
/**
* Return a list of watched filenames
*/
public function getWatched(): array;
/**
* Called periodically to refresh monitors
*/
public function loop();
}

View File

@ -0,0 +1,77 @@
<?php
namespace SparkPlug\Com\Noccy\Watcher\Monitor;
use SparkPlug\Com\Noccy\Watcher\Rule;
class MtimeMonitor implements MonitorInterface
{
private array $rules = [];
private array $watched = [];
private array $modified = [];
/**
* {@inheritDoc}
*/
public function add(Rule $rule)
{
$this->rules[] = $rule;
}
/**
* {@inheritDoc}
*/
public function getModified(): array
{
$mod = $this->modified;
$this->modified = [];
return $mod;
}
/**
* {@inheritDoc}
*/
public function getWatched(): array
{
return [];
}
public function loop()
{
foreach ($this->rules as $rule) {
$this->checkRule($rule);
}
}
private function checkRule(Rule $rule)
{
clearstatcache();
$paths = $rule->getWatchedFiles();
$check = [];
foreach ($paths as $path) {
if (str_contains($path, '*')) {
$check = array_merge($check, glob($path));
} else {
$check[] = $path;
}
}
foreach ($check as $path) {
if (empty($this->watched[$path])) {
$this->watched[$path] = filemtime($path);
} else {
$mtime = filemtime($path);
if ($mtime > $this->watched[$path]) {
printf("* modified: %s (%s)\n", $path, $rule->getName());
$this->watched[$path] = $mtime;
if (!in_array($rule, $this->modified)) {
$this->modified[] = $rule;
}
}
}
}
}
}

View File

@ -0,0 +1,37 @@
# Watcher Plugin for Spark
Note: While the plugin currently supports wildcards, it does not scale well.
Keep the watched files to a minimum or increase the interval if you experience
issues.
## Usage
$ spark watch
## Installation
1. Install Spark with global plugins
2. Initialize your project: `spark init`
3. Enable the plugin with `spark plugin --enable com.noccy.watcher`
4. Configure your `.spark/watchers.json` file
## Configuration
*watchers.json*
```json
{
"watchers": [
{
"name": "name-of-rule",
"watch": [ "file1", "dir1/*" ],
"initial-trigger": true,
"actions": [
"@build"
]
}
]
}
```
The `initial-trigger` key controls whether the rule is triggered on startup.

View File

@ -0,0 +1,53 @@
<?php // "name":"Watch files and act when they are changed", "author":"Noccy"
namespace SparkPlug\Com\Noccy\Watcher;
class Rule
{
private array $filenames = [];
private array $actions = [];
private bool $initialTrigger = false;
private string $name;
public function __construct()
{
$this->name = "unnamed rule";
}
public static function createFromConfig(array $config)
{
$rule = new Rule();
$rule->filenames = (array)$config['watch'];
$rule->initialTrigger = ((bool)$config['initial-trigger'])??false;
$rule->actions = $config['actions']??[];
$rule->name = $config['name']??$rule->name;
return $rule;
}
public function getName(): string
{
return $this->name;
}
public function getInitialTrigger(): bool
{
return $this->initialTrigger;
}
public function getWatchedFiles(): array
{
return $this->filenames;
}
public function getActions(): array
{
return $this->actions;
}
}

View File

@ -0,0 +1,24 @@
<?php // "name":"Watch files and act when they are changed", "author":"Noccy"
namespace SparkPlug\Com\Noccy\Watcher;
class WatcherPlug extends \SparkPlug {
private ?FileWatcher $watcher = null;
public function load()
{
register_command(new Commands\WatchCommand());
//register_command(new PdoExecCommand());
}
public function getFileWatcher(): FileWatcher
{
if (!$this->watcher) {
$this->watcher = new FileWatcher();
}
return $this->watcher;
}
}
register_plugin("com.noccy.watcher", new WatcherPlug);