Skip to content

Commit

Permalink
Add virtual environtment driver
Browse files Browse the repository at this point in the history
  • Loading branch information
robbplo committed Jan 10, 2022
1 parent e6134f7 commit fa4ee5b
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Drivers/PhpCliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Autopilot\Drivers;

use Autopilot\Drivers\Concerns\RequiresRunning;
use Autopilot\Tasks\General\ChdirToRepository;
use Autopilot\Tasks\Php\RunFileInCli;

class PhpCliDriver extends Driver implements RequiresRunning
Expand All @@ -15,6 +16,7 @@ public function matches(): bool
public function runningTasks(): array
{
return [
ChdirToRepository::class,
RunFileInCli::class,
];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Drivers/PhpWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Autopilot\Drivers;

use Autopilot\Drivers\Concerns\RequiresRunning;
use Autopilot\Tasks\General\ChdirToRepository;
use Autopilot\Tasks\Php\ServePhp;

class PhpWebDriver extends Driver implements RequiresRunning
Expand All @@ -28,6 +29,7 @@ public function matches(): bool
public function runningTasks(): array
{
return [
ChdirToRepository::class,
ServePhp::class,
];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Drivers/PythonScriptDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Autopilot\Drivers;

use Autopilot\Drivers\Concerns\RequiresRunning;
use Autopilot\Tasks\General\ChdirToRepository;
use Autopilot\Tasks\Python\RunScript;

class PythonScriptDriver extends Driver implements RequiresRunning
Expand All @@ -17,6 +18,7 @@ public function matches(): bool
public function runningTasks(): array
{
return [
ChdirToRepository::class,
RunScript::class,
];
}
Expand Down
34 changes: 34 additions & 0 deletions src/Drivers/PythonVirtualEnvDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Autopilot\Drivers;

use Autopilot\Drivers\Concerns\RequiresRunning;
use Autopilot\Drivers\Concerns\RequiresSetup;
use Autopilot\Tasks\General\ChdirToRepository;
use Autopilot\Tasks\Python\InstallPythonDependencies;
use Autopilot\Tasks\Python\RunScriptInVirtualEnvironment;
use Autopilot\Tasks\Python\SetupVirtualEnvironment;

class PythonVirtualEnvDriver extends Driver implements RequiresSetup, RequiresRunning
{
public function matches(): bool
{
return $this->repository->dir()->contains('requirements.txt');
}

public function setupTasks(): array
{
return [
ChdirToRepository::class,
SetupVirtualEnvironment::class,
InstallPythonDependencies::class,
];
}

public function runningTasks(): array
{
return [
RunScriptInVirtualEnvironment::class,
];
}
}
2 changes: 2 additions & 0 deletions src/ProjectClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Autopilot\Drivers\PhpCliDriver;
use Autopilot\Drivers\PhpWebDriver;
use Autopilot\Drivers\PythonScriptDriver;
use Autopilot\Drivers\PythonVirtualEnvDriver;

class ProjectClassifier
{
private static $drivers = [
LaravelDriver::class,
PhpWebDriver::class,
PhpCliDriver::class,
PythonVirtualEnvDriver::class,
PythonScriptDriver::class,
];

Expand Down
18 changes: 18 additions & 0 deletions src/Tasks/General/ChdirToRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Autopilot\Tasks\General;

use Autopilot\Tasks\Task;

class ChdirToRepository extends Task
{
public function run()
{
chdir($this->repository()->dir()->getPath());
}

public function message(): string
{
return 'Setting working directory';
}
}
1 change: 0 additions & 1 deletion src/Tasks/Php/RunFileInCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function run()
{
$path = $this->repository()->dir()->getPath($this->primaryFile);

chdir($this->repository()->dir()->getPath());
passthru("php $path");
}

Expand Down
1 change: 0 additions & 1 deletion src/Tasks/Php/ServePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ServePhp extends Task
public function run()
{
$url = "localhost:8000";
chdir($this->repository()->dir()->getPath());
exec("python -m webbrowser http://$url");
passthru("php -S {$url}");
}
Expand Down
20 changes: 20 additions & 0 deletions src/Tasks/Python/InstallPythonDependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Autopilot\Tasks\Python;

use Autopilot\Tasks\Task;

class InstallPythonDependencies extends Task
{
public function run()
{
$executable = $this->repository()->dir()->getPath('venv/bin/pip');

exec("$executable install -r requirements.txt");
}

public function message(): string
{
return 'Installing dependencies from requirements.txt';
}
}
4 changes: 1 addition & 3 deletions src/Tasks/Python/RunScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class RunScript extends Task
{
private $primaryFile;
protected $primaryFile;

public function __construct(Repository $repository)
{
Expand All @@ -20,8 +20,6 @@ public function run()
{
$path = $this->repository()->dir()->getPath($this->primaryFile);

chdir($this->repository()->dir()->getPath());

// -u is to prevent input calls from buffering until after the script is executed
passthru("python -u $path");
}
Expand Down
15 changes: 15 additions & 0 deletions src/Tasks/Python/RunScriptInVirtualEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Autopilot\Tasks\Python;

class RunScriptInVirtualEnvironment extends RunScript
{
public function run()
{
$executable = $this->repository()->dir()->getPath('venv/bin/python');
$path = $this->repository()->dir()->getPath($this->primaryFile);

// -u is to prevent input calls from buffering until after the script is executed
passthru("$executable -u $path");
}
}
18 changes: 18 additions & 0 deletions src/Tasks/Python/SetupVirtualEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Autopilot\Tasks\Python;

use Autopilot\Tasks\Task;

class SetupVirtualEnvironment extends Task
{
public function run()
{
exec('virtualenv venv');
}

public function message(): string
{
return 'Setting up virtual environment';
}
}
22 changes: 22 additions & 0 deletions tests/Drivers/PythonVirtualEnvDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Drivers;

use Autopilot\Drivers\PythonVirtualEnvDriver;
use Autopilot\Repository;
use PHPUnit\Framework\TestCase;

class PythonVirtualEnvDriverTest extends TestCase
{

/** @test */
public function it_matches()
{
$repository = new Repository('[email protected]:1f92ce4c-4b84-11ec-a0c6-4213e7ee7fac/e2a0997b-4219-11ec-a0c6-4213e7ee7fac/Bieps-Per-Minute-11e57750-fcd48921.git');
$repository->clone();

$driver = new PythonVirtualEnvDriver($repository);

$this->assertTrue($driver->matches());
}
}

0 comments on commit fa4ee5b

Please sign in to comment.