Skip to content

Commit

Permalink
fix: actions on Windows machines
Browse files Browse the repository at this point in the history
This commit limit the utilization of some actions on Windows machines.
  • Loading branch information
asciito committed Nov 1, 2023
1 parent 8b3a9bf commit c8628d4
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions app/Commands/DriverManagerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class DriverManagerCommand extends Command
];

protected array $commands = [
'start' => './chromedriver --log-level=ALL --port={port} &',
'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {options}' | awk '{print $2,$13}'",
'start' => './{binary} --log-level=ALL --port={port} &',
'pid' => "ps aux | grep '{binary} --log-level=ALL {options}' | awk '{print $2,$13}'",
'stop' => 'kill -9 {pid}',
];

Expand Down Expand Up @@ -85,11 +85,20 @@ protected function start(string $port): int
return self::SUCCESS;
}


/**
* @throws \Exception if you're on a Windows machine (OS)
*/
public function stop(string $port): int
{
intro("Stopping Google Chrome Driver on port [$port]");

$pid = $this->getProcessID($port);
try {
$pid = $this->getProcessID($port);
} catch (\Exception) {
throw new \Exception('We cannot stop a server on Windows');
}


if (empty($pid)) {
warning("There's no server to stop on port [$port]");
Expand Down Expand Up @@ -129,7 +138,11 @@ protected function status(string $port): int
{
intro("Getting Google Chrome Driver status on port [$port]");

$pid = $this->getProcessID($port);
try {
$pid = $this->getProcessID($port);
} catch (\Exception) {
warning('We are running on windows and we cannot be sure if the server is running, but we will try to check');
}

if (empty($pid)) {
warning("There's no server available on port [$port]");
Expand All @@ -152,11 +165,18 @@ protected function status(string $port): int
return self::SUCCESS;
}

/**
* @throws \Exception if you're on a Windows machine (OS)
*/
protected function list(): int
{
info('Listing all the servers available');

$result = $this->getProcessIDs();
try {
$result = $this->getProcessIDs();
} catch (\Exception) {
throw new \Exception('We cannot list the servers on Windows');
}

if (empty($result)) {
warning("There' no servers available to list");
Expand All @@ -169,9 +189,17 @@ protected function list(): int
return self::SUCCESS;
}


/**
* @throws \Exception if you're on a Windows machine (OS)
*/
protected function kill(): int
{
$pids = $this->getProcessIDs();
try {
$pids = $this->getProcessIDs();
} catch (\Exception) {
throw new \Exception('We cannot kill the servers on Windows');
}

if (empty($pids)) {
warning("There' no servers to kill");
Expand Down Expand Up @@ -199,6 +227,14 @@ protected function kill(): int

protected function command(string $cmd, array $with)
{
$binary = $this->getBinary();

if ($cmd === 'pid') {
$binary = Str::of($binary)->replaceFirst('c', '[c]');
}

$with = array_merge($with, ['{binary}' => $binary]);

return Process::command(
Str::replace(
collect($with)->keys(),
Expand All @@ -210,6 +246,10 @@ protected function command(string $cmd, array $with)

protected function getProcessID(string $port): ?int
{
if ($this->onWindows()) {
throw new \Exception("We cannot get a PID on Windows");
}

$process = $this->command('pid', ['{options}' => '--port='.$port]);

$output = explode(' ', trim($process->output()));
Expand All @@ -219,6 +259,10 @@ protected function getProcessID(string $port): ?int

protected function getProcessIDs(): ?Collection
{
if ($this->onWindows()) {
throw new \Exception("We cannot get a list of PIDs on Windows");
}

$process = $this->command('pid', ['{options}' => '']);

if (empty($process->output())) {
Expand All @@ -239,6 +283,16 @@ protected function getPorts(): Collection
return collect($this->option('port') ? [...$this->option('port')] : $this->port)->unique()->filter();
}

protected function onWindows(): bool
{
return OperatingSystem::onWindows();
}

protected function getBinary(): string
{
return 'chromedriver'.($this->onWindows() ? '.exe' : '');
}

protected function getChromeDriverDirectory(): string
{
return $this->option('path')
Expand Down

0 comments on commit c8628d4

Please sign in to comment.