Skip to content

Commit

Permalink
feat: Add wsl-send-notify for WSL based Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo D authored and TheoD02 committed Apr 3, 2024
1 parent a54d0bc commit e6944e6
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes between versions

## Not released yet

* Added wsl-notify-send notifier for Windows Subsystem for Linux

## 2.6.0 (2023-12-03)

* Deprecated Joli\JoliNotif\Util\OsHelper in favor of jolicode/php-os-helper package
Expand Down
Binary file added bin/wsl-notify-send/wsl-notify-send.exe
Binary file not shown.
9 changes: 9 additions & 0 deletions doc/03-notifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ AppleScript can display notification with only a body and a title. AppleScript
don't support to set an icon and will always use instead the icon of the
application sending the notification, in our case, the terminal.

#### WslNotifySendNotifier

This notifier uses the executable `wsl-notify-send`.
It permit to send notification from Windows Subsystem for Linux to Windows.

wsl-notify-send can display notification with a body and a title.

Icon is partially supported by `wsl-notify-send`, but it's not possible to set an icon for now.

### Windows

#### SnoreToastNotifier
Expand Down
69 changes: 69 additions & 0 deletions src/Notifier/WslNotifySendNotifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the JoliNotif project.
*
* (c) Loïck Piera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Joli\JoliNotif\Notifier;

use Joli\JoliNotif\Notification;
use JoliCode\PhpOsHelper\OsHelper;

/*
* This notifier can be used on Windows Subsystem for Linux and provides notifications using the `wsl-notify-send` binary.
*
* @see https://github.com/stuartleeks/wsl-notify-send the source code of the `wsl-notify-send` binary
*/
class WslNotifySendNotifier extends CliBasedNotifier implements BinaryProvider
{
public function getBinary(): string
{
return 'wsl-notify-send';
}

public function getPriority(): int
{
return static::PRIORITY_HIGH;
}

public function canBeUsed(): bool
{
return OsHelper::isWindowsSubsystemForLinux();
}

public function getRootDir(): string
{
return \dirname(__DIR__, 2) . '/bin/wsl-notify-send';
}

public function getEmbeddedBinary(): string
{
return 'wsl-notify-send.exe';
}

public function getExtraFiles(): array
{
return [];
}

protected function getCommandLineArguments(Notification $notification): array
{
$arguments = [
'--appId',
'JoliNotif',
$notification->getBody(),
];

if ($notification->getTitle()) {
$arguments[] = '-c';
$arguments[] = $notification->getTitle();
}

return $arguments;
}
}
2 changes: 2 additions & 0 deletions src/NotifierFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Joli\JoliNotif\Notifier\NullNotifier;
use Joli\JoliNotif\Notifier\SnoreToastNotifier;
use Joli\JoliNotif\Notifier\TerminalNotifierNotifier;
use Joli\JoliNotif\Notifier\WslNotifySendNotifier;
use JoliCode\PhpOsHelper\OsHelper;

class NotifierFactory
Expand Down Expand Up @@ -79,6 +80,7 @@ private static function getUnixNotifiers(): array
new AppleScriptNotifier(),
new KDialogNotifier(),
new NotifySendNotifier(),
new WslNotifySendNotifier(),
];
}

Expand Down
70 changes: 70 additions & 0 deletions tests/Notifier/WslNotifySendNotifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the JoliNotif project.
*
* (c) Loïck Piera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Joli\JoliNotif\tests\Notifier;

use Joli\JoliNotif\Notifier;
use Joli\JoliNotif\Notifier\WslNotifySendNotifier;

class WslNotifySendNotifierTest extends NotifierTestCase
{
use BinaryProviderTestTrait;
use CliBasedNotifierTestTrait;

private const BINARY = 'wsl-notify-send';

public function testGetBinary()
{
$notifier = $this->getNotifier();

$this->assertSame(self::BINARY, $notifier->getBinary());
}

public function testGetPriority()
{
$notifier = $this->getNotifier();

$this->assertSame(Notifier::PRIORITY_HIGH, $notifier->getPriority());
}

protected function getNotifier(): WslNotifySendNotifier
{
return new WslNotifySendNotifier();
}

protected function getExpectedCommandLineForNotification(): string
{
return <<<'CLI'
'wsl-notify-send' '--appId' 'JoliNotif' 'I'\''m the notification body'
CLI;
}

protected function getExpectedCommandLineForNotificationWithATitle(): string
{
return <<<'CLI'
'wsl-notify-send' '--appId' 'JoliNotif' 'I'\''m the notification body' '-c' 'I'\''m the notification title'
CLI;
}

protected function getExpectedCommandLineForNotificationWithAnIcon(): string
{
return <<<'CLI'
'wsl-notify-send' '--appId' 'JoliNotif' 'I'\''m the notification body'
CLI;
}

protected function getExpectedCommandLineForNotificationWithAllOptions(): string
{
return <<<'CLI'
'wsl-notify-send' '--appId' 'JoliNotif' 'I'\''m the notification body' '-c' 'I'\''m the notification title'
CLI;
}
}
3 changes: 3 additions & 0 deletions tests/NotifierFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Joli\JoliNotif\Notifier\NullNotifier;
use Joli\JoliNotif\Notifier\SnoreToastNotifier;
use Joli\JoliNotif\Notifier\TerminalNotifierNotifier;
use Joli\JoliNotif\Notifier\WslNotifySendNotifier;
use Joli\JoliNotif\NotifierFactory;
use Joli\JoliNotif\tests\fixtures\ConfigurableNotifier;
use Joli\JoliNotif\Util\OsHelper;
Expand All @@ -38,6 +39,7 @@ public function testGetDefaultNotifiers()
AppleScriptNotifier::class,
KDialogNotifier::class,
NotifySendNotifier::class,
WslNotifySendNotifier::class,
];
} else {
$expectedNotifierClasses = [
Expand Down Expand Up @@ -66,6 +68,7 @@ public function testCreateUsesDefaultNotifiers()
AppleScriptNotifier::class,
KDialogNotifier::class,
NotifySendNotifier::class,
WslNotifySendNotifier::class,
];
} else {
$expectedNotifierClasses = [
Expand Down

0 comments on commit e6944e6

Please sign in to comment.