Skip to content

Commit

Permalink
allow to select the triggers to run on each change
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 19, 2022
1 parent 1a9baf4 commit 8b37b1e
Show file tree
Hide file tree
Showing 17 changed files with 426 additions and 32 deletions.
35 changes: 32 additions & 3 deletions src/Command/Work.php
Expand Up @@ -3,11 +3,18 @@

namespace Innmind\LabStation\Command;

use Innmind\LabStation\Monitor;
use Innmind\LabStation\{
Monitor,
Triggers,
};
use Innmind\CLI\{
Command,
Console,
};
use Innmind\Immutable\{
Str,
Set,
};

final class Work implements Command
{
Expand All @@ -20,14 +27,36 @@ public function __construct(Monitor $monitor)

public function __invoke(Console $console): Console
{
return ($this->monitor)($console);
$triggers = $console
->options()
->maybe('triggers')
->map(Str::of(...))
->map(
static fn($triggers) => $triggers
->split(',')
->map(static fn($trigger) => $trigger->trim()->toString())
->filter(Triggers::allow(...))
->map(Triggers::of(...)),
)
->match(
static fn($triggers) => $triggers->toList(),
static fn() => Triggers::cases(),
);

return ($this->monitor)($console, Set::of(...$triggers));
}

/**
* @psalm-pure
*/
public function usage(): string
{
return 'work --silent --keep-output';
return <<<USAGE
work --silent --keep-output --triggers=
The triggers option can contain a comma separated list of values.
Triggers can contain : cs, composer, docker, psalm and tests
USAGE;
}
}
23 changes: 16 additions & 7 deletions src/Monitor.php
Expand Up @@ -18,6 +18,7 @@
use Innmind\Immutable\{
Maybe,
Str,
Set,
};

final class Monitor
Expand Down Expand Up @@ -52,7 +53,10 @@ public function __construct(
$this->agents = $agents;
}

public function __invoke(Console $console): Console
/**
* @param Set<Triggers> $triggers
*/
public function __invoke(Console $console, Set $triggers): Console
{
$project = $console->workingDirectory();
$manager = $this->manager;
Expand All @@ -66,7 +70,7 @@ public function __invoke(Console $console): Console
return $manager
->start()
->maybe()
->flatMap(fn($agents) => $this->start($agents, $console))
->flatMap(fn($agents) => $this->start($agents, $console, $triggers))
->match(
static fn($console) => $console
->error(Str::of("Terminated\n"))
Expand All @@ -78,21 +82,26 @@ public function __invoke(Console $console): Console
}

/**
* @param Set<Triggers> $triggers
*
* @return Maybe<Console>
*/
private function start(Running $agents, Console $console): Maybe
{
$console = ($this->trigger)(new Activity(Type::start), $console);
private function start(
Running $agents,
Console $console,
Set $triggers,
): Maybe {
$console = ($this->trigger)(new Activity(Type::start), $console, $triggers);

$server = $this->ipc->listen($this->name);

/** @psalm-suppress InvalidArgument */
return $server(
$console,
function($message, $continuation, Console $console) {
function($message, $continuation, Console $console) use ($triggers) {
$activity = $this->protocol->decode($message);
$this->iteration->start();
$console = ($this->trigger)($activity, $console);
$console = ($this->trigger)($activity, $console, $triggers);
$console = $this->iteration->end($console);

return $continuation->continue($console);
Expand Down
10 changes: 9 additions & 1 deletion src/Trigger.php
Expand Up @@ -4,8 +4,16 @@
namespace Innmind\LabStation;

use Innmind\CLI\Console;
use Innmind\Immutable\Set;

interface Trigger
{
public function __invoke(Activity $activity, Console $console): Console;
/**
* @param Set<Triggers> $triggers
*/
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console;
}
10 changes: 7 additions & 3 deletions src/Trigger/All.php
Expand Up @@ -8,6 +8,7 @@
Activity,
};
use Innmind\CLI\Console;
use Innmind\Immutable\Set;

final class All implements Trigger
{
Expand All @@ -22,10 +23,13 @@ public function __construct(Trigger ...$triggers)
$this->triggers = $triggers;
}

public function __invoke(Activity $activity, Console $console): Console
{
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console {
foreach ($this->triggers as $trigger) {
$console = $trigger($activity, $console);
$console = $trigger($activity, $console, $triggers);
}

return $console;
Expand Down
17 changes: 14 additions & 3 deletions src/Trigger/CodingStandard.php
Expand Up @@ -5,6 +5,7 @@

use Innmind\LabStation\{
Trigger,
Triggers,
Activity,
Activity\Type,
Iteration,
Expand All @@ -17,7 +18,10 @@
};
use Innmind\OperatingSystem\Filesystem;
use Innmind\Filesystem\Name;
use Innmind\Immutable\Map;
use Innmind\Immutable\{
Map,
Set,
};

final class CodingStandard implements Trigger
{
Expand All @@ -35,8 +39,15 @@ public function __construct(
$this->iteration = $iteration;
}

public function __invoke(Activity $activity, Console $console): Console
{
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console {
if (!$triggers->contains(Triggers::codingStandard)) {
return $console;
}

return match ($activity->type()) {
Type::sourcesModified => $this->attempt($console),
Type::testsModified => $this->attempt($console),
Expand Down
13 changes: 11 additions & 2 deletions src/Trigger/ComposerUpdate.php
Expand Up @@ -5,6 +5,7 @@

use Innmind\LabStation\{
Trigger,
Triggers,
Activity,
Activity\Type,
};
Expand All @@ -20,6 +21,7 @@
use Innmind\Immutable\{
Map,
Str,
Set,
};

final class ComposerUpdate implements Trigger
Expand All @@ -31,8 +33,15 @@ public function __construct(Processes $processes)
$this->processes = $processes;
}

public function __invoke(Activity $activity, Console $console): Console
{
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console {
if (!$triggers->contains(Triggers::composerUpdate)) {
return $console;
}

return match ($activity->type()) {
Type::start => $this->ask($console),
default => $console,
Expand Down
13 changes: 11 additions & 2 deletions src/Trigger/DockerCompose.php
Expand Up @@ -5,6 +5,7 @@

use Innmind\LabStation\{
Trigger,
Triggers,
Activity,
Activity\Type,
};
Expand All @@ -18,6 +19,7 @@
use Innmind\Immutable\{
Map,
Str,
Set,
};

final class DockerCompose implements Trigger
Expand All @@ -31,8 +33,15 @@ public function __construct(Filesystem $filesystem, Processes $processes)
$this->processes = $processes;
}

public function __invoke(Activity $activity, Console $console): Console
{
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console {
if (!$triggers->contains(Triggers::dockerCompose)) {
return $console;
}

return match ($activity->type()) {
Type::start => $this->attempt($console),
default => $console,
Expand Down
17 changes: 14 additions & 3 deletions src/Trigger/Psalm.php
Expand Up @@ -5,6 +5,7 @@

use Innmind\LabStation\{
Trigger,
Triggers,
Activity,
Activity\Type,
Iteration,
Expand All @@ -17,7 +18,10 @@
};
use Innmind\OperatingSystem\Filesystem;
use Innmind\Filesystem\Name;
use Innmind\Immutable\Map;
use Innmind\Immutable\{
Map,
Set,
};

final class Psalm implements Trigger
{
Expand All @@ -35,8 +39,15 @@ public function __construct(
$this->iteration = $iteration;
}

public function __invoke(Activity $activity, Console $console): Console
{
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console {
if (!$triggers->contains(Triggers::psalm)) {
return $console;
}

return match ($activity->type()) {
Type::sourcesModified => $this->ettempt($console),
Type::testsModified => $this->ettempt($console),
Expand Down
17 changes: 14 additions & 3 deletions src/Trigger/Tests.php
Expand Up @@ -5,6 +5,7 @@

use Innmind\LabStation\{
Trigger,
Triggers,
Activity,
Activity\Type,
Iteration,
Expand All @@ -17,7 +18,10 @@
Process\Output,
};
use Innmind\Filesystem\Name;
use Innmind\Immutable\Map;
use Innmind\Immutable\{
Map,
Set,
};

final class Tests implements Trigger
{
Expand All @@ -35,8 +39,15 @@ public function __construct(
$this->iteration = $iteration;
}

public function __invoke(Activity $activity, Console $console): Console
{
public function __invoke(
Activity $activity,
Console $console,
Set $triggers,
): Console {
if (!$triggers->contains(Triggers::tests)) {
return $console;
}

return match ($activity->type()) {
Type::sourcesModified => $this->attempt($console),
Type::testsModified => $this->attempt($console),
Expand Down
45 changes: 45 additions & 0 deletions src/Triggers.php
@@ -0,0 +1,45 @@
<?php
declare(strict_types = 1);

namespace Innmind\LabStation;

/**
* @psalm-immutable
*/
enum Triggers
{
case codingStandard;
case composerUpdate;
case dockerCompose;
case psalm;
case tests;

/**
* @psalm-pure
*/
public static function of(string $value): self
{
return match ($value) {
'cs', 'codingStandard' => self::codingStandard,
'composer', 'composerUpdate' => self::composerUpdate,
'docker', 'dockerCompose' => self::dockerCompose,
'psalm' => self::psalm,
'tests', 'phpunit' => self::tests,
};
}

/**
* @psalm-pure
*/
public static function allow(string $value): bool
{
return match ($value) {
'cs', 'codingStandard' => true,
'composer', 'composerUpdate' => true,
'docker', 'dockerCompose' => true,
'psalm' => true,
'tests', 'phpunit' => true,
default => false,
};
}
}

0 comments on commit 8b37b1e

Please sign in to comment.