Skip to content

Commit

Permalink
Merge pull request #1483 from hydephp/add-open-flag-to-serve-command
Browse files Browse the repository at this point in the history
Add option to open site in the browser when starting the realtime compiler
  • Loading branch information
caendesilva authored Dec 1, 2023
2 parents 66aaf69 + cefc5ff commit e18e2be
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This serves two purposes:
- Adds a new fancy output for the realtime compiler serve command in https://github.com/hydephp/develop/pull/1444
- Added support for dot notation in the Yaml configuration files in https://github.com/hydephp/develop/pull/1478
- Added a config option to customize automatic sidebar navigation group names in https://github.com/hydephp/develop/pull/1481
- Added a new `hyde serve --open` option to automatically open the site in the browser in https://github.com/hydephp/develop/pull/1483

### Changed
- The `docs.sidebar.footer` config option now accepts a Markdown string to replace the default footer in https://github.com/hydephp/develop/pull/1477
Expand Down
24 changes: 24 additions & 0 deletions packages/framework/src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Facades\Process;

use function sprintf;
use function str_replace;
use function class_exists;

/**
Expand All @@ -31,6 +32,7 @@ class ServeCommand extends Command
{--dashboard= : Enable the realtime compiler dashboard. (Overrides config setting)}
{--pretty-urls= : Enable pretty URLs. (Overrides config setting)}
{--play-cdn= : Enable the Tailwind Play CDN. (Overrides config setting)}
{--open : Open the site preview in the browser.}
';

/** @var string */
Expand All @@ -43,6 +45,10 @@ public function safeHandle(): int
$this->configureOutput();
$this->printStartMessage();

if ($this->option('open')) {
$this->openInBrowser();
}

$this->runServerProcess(sprintf('php -S %s:%d %s',
$this->getHostSelection(),
$this->getPortSelection(),
Expand Down Expand Up @@ -135,4 +141,22 @@ protected function checkArgvForOption(string $name): ?string

return null;
}

protected function openInBrowser(): void
{
$command = match (PHP_OS_FAMILY) {
'Windows' => 'start',
'Darwin' => 'open',
'Linux' => 'xdg-open',
default => null
};

$process = $command ? Process::command(sprintf('%s http://%s:%d', $command, $this->getHostSelection(), $this->getPortSelection()))->run() : null;

if (! $process || $process->failed()) {
$this->warn('Unable to open the site preview in the browser on your system:');
$this->line(sprintf(' %s', str_replace("\n", "\n ", $process ? $process->errorOutput() : "Missing suitable 'open' binary.")));
$this->newLine();
}
}
}
92 changes: 92 additions & 0 deletions packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

namespace Hyde\Framework\Testing\Unit;

use Mockery;
use Hyde\Testing\UnitTestCase;
use Hyde\Foundation\HydeKernel;
use Illuminate\Console\OutputStyle;
use Hyde\Console\Commands\ServeCommand;
use Illuminate\Support\Facades\Process;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;

/**
* @covers \Hyde\Console\Commands\ServeCommand
Expand Down Expand Up @@ -196,6 +202,91 @@ public function test_checkArgvForOption()
$_SERVER = $serverBackup;
}

public function testWithOpenArgument()
{
HydeKernel::setInstance(new HydeKernel());

$command = new class(['open' => true]) extends ServeCommandMock
{
public bool $openInBrowserCalled = false;

// Void unrelated methods
protected function configureOutput(): void
{
}

protected function printStartMessage(): void
{
}

protected function runServerProcess(string $command): void
{
}

protected function openInBrowser(): void
{
$this->openInBrowserCalled = true;
}
};

$command->safeHandle();
$this->assertTrue($command->openInBrowserCalled);
}

public function test_openInBrowser()
{
$output = $this->createMock(OutputStyle::class);
$output->expects($this->never())->method('writeln');

$command = $this->getMock(['--open' => true]);
$command->setOutput($output);

$binary = match (PHP_OS_FAMILY) {
'Darwin' => 'open',
'Windows' => 'start',
default => 'xdg-open',
};

Process::shouldReceive('command')->once()->with("$binary http://localhost:8080")->andReturnSelf();
Process::shouldReceive('run')->once()->andReturnSelf();
Process::shouldReceive('failed')->once()->andReturn(false);

$command->openInBrowser();
}

public function test_openInBrowserThatFails()
{
$output = Mockery::mock(OutputStyle::class);
$output->shouldReceive('getFormatter')->andReturn($this->createMock(OutputFormatterInterface::class));

$warning = '<warning>Unable to open the site preview in the browser on your system:</warning>';
$context = ' Missing suitable \'open\' binary.';

$output->shouldReceive('writeln')->once()->with($warning, OutputInterface::VERBOSITY_NORMAL);
$output->shouldReceive('writeln')->once()->with($context, OutputInterface::VERBOSITY_NORMAL);
$output->shouldReceive('newLine')->once();

$command = $this->getMock(['--open' => true]);
$command->setOutput($output);

$binary = match (PHP_OS_FAMILY) {
'Darwin' => 'open',
'Windows' => 'start',
default => 'xdg-open',
};

Process::shouldReceive('command')->once()->with("$binary http://localhost:8080")->andReturnSelf();
Process::shouldReceive('run')->once()->andReturnSelf();
Process::shouldReceive('failed')->once()->andReturn(true);
Process::shouldReceive('errorOutput')->once()->andReturn("Missing suitable 'open' binary.");

$command->openInBrowser();

Mockery::close();

$this->assertTrue(true);
}

protected function getMock(array $options = []): ServeCommandMock
{
return new ServeCommandMock($options);
Expand All @@ -208,6 +299,7 @@ protected function getMock(array $options = []): ServeCommandMock
* @method getEnvironmentVariables
* @method parseEnvironmentOption(string $name)
* @method checkArgvForOption(string $name)
* @method openInBrowser()
*/
class ServeCommandMock extends ServeCommand
{
Expand Down

0 comments on commit e18e2be

Please sign in to comment.