Skip to content

Commit

Permalink
Merge pull request #101 from TheoD02/feat/add_wsl-send-notify
Browse files Browse the repository at this point in the history
feat: Add wsl-send-notify for WSL based Linux
  • Loading branch information
pyrech authored Apr 24, 2024
2 parents a54d0bc + 62d9b4b commit 18aa721
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 3 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
21 changes: 21 additions & 0 deletions bin/wsl-notify-send/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Stuart Leeks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions bin/wsl-notify-send/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# wsl-notify-send

These wsl-notify-send binary files come from the [stuartleeks/wsl-notify-send](https://github.com/stuartleeks/wsl-notify-send)
project. All credits for this Windows WSL application goes to [Stuart Leeks](https://github.com/stuartleeks).

Only the required files were extracted here. If you want to fully test it,
please have a look at the github project instead.
Binary file added bin/wsl-notify-send/wsl-notify-send.exe
Binary file not shown.
17 changes: 14 additions & 3 deletions doc/03-notifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,24 @@ 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 permits 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

This notifier uses the Windows application called SnoreToastNotifier. It works on Windows
8 and higher. Because SnoreToastNotifier is probably not installed on your system,
JoliNotif embed the binaries inside the [bin/snoreToast](bin/snoreToast) directory.
This notifier uses the Windows application called SnoreToastNotifier. It works
on Windows 8 and higher. Because SnoreToastNotifier is probably not installed
on your system, JoliNotif embed the binaries inside the [bin/snoreToast](bin/snoreToast)
directory.

When you use JoliNotif inside a phar archive, we take care to extract those
binaries in the system temp directory to be able to execute them.
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 18aa721

Please sign in to comment.