-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c348241
commit 4c77185
Showing
10 changed files
with
158 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\TypeScriptBundle\Tools; | ||
|
||
use Symfony\Component\Process\Process; | ||
|
||
class WatcherBinary | ||
{ | ||
public function __construct( | ||
private string $executablePath, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<string> $extensions | ||
*/ | ||
public function startWatch(string $watchPath, callable $callback, array $extensions = []): Process | ||
{ | ||
$process = new Process([$this->executablePath, $watchPath]); | ||
|
||
$process->start(function ($type, $buffer) use ($callback, $extensions) { | ||
if (Process::ERR === $type) { | ||
throw new \Exception($buffer); | ||
} else { | ||
$lines = explode("\n", $buffer); | ||
$changedFiles = []; | ||
foreach ($lines as $line) { | ||
try { | ||
$entry = json_decode($line, true, 512, \JSON_THROW_ON_ERROR); | ||
if ($extensions && !\in_array(pathinfo($entry['name'], \PATHINFO_EXTENSION), $extensions)) { | ||
continue; | ||
} | ||
$changedFiles[$entry['name']] = $entry['operation']; | ||
} catch (\JsonException) { | ||
continue; | ||
} | ||
} | ||
foreach ($changedFiles as $file => $operation) { | ||
$callback($file, $operation); | ||
} | ||
} | ||
}); | ||
|
||
return $process; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\TypeScriptBundle\Tools; | ||
|
||
class WatcherBinaryFactory | ||
{ | ||
public function getBinaryFromServerSpecs($os): WatcherBinary | ||
{ | ||
$binaryName = self::getBinaryNameFromServerSpecs($os); | ||
$binaryPath = __DIR__.'/watcher/'.$binaryName; | ||
if (!file_exists($binaryPath)) { | ||
throw new \Exception(sprintf('The watcher binary could not be found at the provided path : "%s"', $binaryPath)); | ||
} | ||
|
||
return new WatcherBinary($binaryPath); | ||
} | ||
|
||
public static function getBinaryNameFromServerSpecs($os) | ||
{ | ||
$os = strtolower($os); | ||
if (str_contains($os, 'darwin')) { | ||
return 'watcher-darwin'; | ||
} | ||
if (str_contains($os, 'linux')) { | ||
return 'watcher-linux'; | ||
} | ||
if (str_contains($os, 'win')) { | ||
return 'watcher-windows.exe'; | ||
} | ||
|
||
throw new \Exception(sprintf('Unknown platform or architecture (OS: %s).', $os)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\TypeScriptBundle\Tests\Tools; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sensiolabs\TypeScriptBundle\Tools\WatcherBinaryFactory; | ||
|
||
class WatcherBinaryFactoryTest extends TestCase | ||
{ | ||
private string $watchPath = __DIR__.'/../fixtures/assets/typescript'; | ||
private string $binaryDir = __DIR__.'/../../src/Tools/watcher'; | ||
|
||
public function testGetBinaryFromServerSpecs() | ||
{ | ||
// Test that the binary exists and the process is created with the correct arguments | ||
$binary = (new WatcherBinaryFactory())->getBinaryFromServerSpecs('Linux'); | ||
$process = $binary->startWatch($this->watchPath, fn ($path, $operation) => ''); | ||
$this->assertEquals('\''.realpath($this->binaryDir).'/watcher-linux\' \''.$this->watchPath.'\'', $process->getCommandLine()); | ||
|
||
// Test that an exception is thrown when the platform is not supported | ||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('Unknown platform or architecture (OS: undefined).'); | ||
(new WatcherBinaryFactory())->getBinaryFromServerSpecs('Undefined'); | ||
} | ||
|
||
/** | ||
* @dataProvider provideServerSpecs | ||
*/ | ||
public function testGetBinaryNameFromServerSpecs($os, $expectedBinaryName, $exception = null) | ||
{ | ||
if (null !== $exception) { | ||
$this->expectException($exception); | ||
} | ||
|
||
$this->assertEquals($expectedBinaryName, WatcherBinaryFactory::getBinaryNameFromServerSpecs($os)); | ||
} | ||
|
||
public function provideServerSpecs() | ||
{ | ||
return [ | ||
['Darwin', 'watcher-darwin'], | ||
['Linux', 'watcher-linux'], | ||
['Windows', 'watcher-windows.exe'], | ||
['Undefined', null, \Exception::class], | ||
]; | ||
} | ||
} |
Oops, something went wrong.