From fa4ee5bbe023af6b9c270e84b440cec8919965d7 Mon Sep 17 00:00:00 2001 From: Robbin Ploeger Date: Mon, 10 Jan 2022 17:06:45 +0100 Subject: [PATCH] Add virtual environtment driver --- src/Drivers/PhpCliDriver.php | 2 ++ src/Drivers/PhpWebDriver.php | 2 ++ src/Drivers/PythonScriptDriver.php | 2 ++ src/Drivers/PythonVirtualEnvDriver.php | 34 +++++++++++++++++++ src/ProjectClassifier.php | 2 ++ src/Tasks/General/ChdirToRepository.php | 18 ++++++++++ src/Tasks/Php/RunFileInCli.php | 1 - src/Tasks/Php/ServePhp.php | 1 - .../Python/InstallPythonDependencies.php | 20 +++++++++++ src/Tasks/Python/RunScript.php | 4 +-- .../Python/RunScriptInVirtualEnvironment.php | 15 ++++++++ src/Tasks/Python/SetupVirtualEnvironment.php | 18 ++++++++++ tests/Drivers/PythonVirtualEnvDriverTest.php | 22 ++++++++++++ 13 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 src/Drivers/PythonVirtualEnvDriver.php create mode 100644 src/Tasks/General/ChdirToRepository.php create mode 100644 src/Tasks/Python/InstallPythonDependencies.php create mode 100644 src/Tasks/Python/RunScriptInVirtualEnvironment.php create mode 100644 src/Tasks/Python/SetupVirtualEnvironment.php create mode 100644 tests/Drivers/PythonVirtualEnvDriverTest.php diff --git a/src/Drivers/PhpCliDriver.php b/src/Drivers/PhpCliDriver.php index 78928eb..409129c 100644 --- a/src/Drivers/PhpCliDriver.php +++ b/src/Drivers/PhpCliDriver.php @@ -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 @@ -15,6 +16,7 @@ public function matches(): bool public function runningTasks(): array { return [ + ChdirToRepository::class, RunFileInCli::class, ]; } diff --git a/src/Drivers/PhpWebDriver.php b/src/Drivers/PhpWebDriver.php index 2e0eff6..4d5690b 100644 --- a/src/Drivers/PhpWebDriver.php +++ b/src/Drivers/PhpWebDriver.php @@ -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 @@ -28,6 +29,7 @@ public function matches(): bool public function runningTasks(): array { return [ + ChdirToRepository::class, ServePhp::class, ]; } diff --git a/src/Drivers/PythonScriptDriver.php b/src/Drivers/PythonScriptDriver.php index 09d807f..78a7a19 100644 --- a/src/Drivers/PythonScriptDriver.php +++ b/src/Drivers/PythonScriptDriver.php @@ -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 @@ -17,6 +18,7 @@ public function matches(): bool public function runningTasks(): array { return [ + ChdirToRepository::class, RunScript::class, ]; } diff --git a/src/Drivers/PythonVirtualEnvDriver.php b/src/Drivers/PythonVirtualEnvDriver.php new file mode 100644 index 0000000..522911d --- /dev/null +++ b/src/Drivers/PythonVirtualEnvDriver.php @@ -0,0 +1,34 @@ +repository->dir()->contains('requirements.txt'); + } + + public function setupTasks(): array + { + return [ + ChdirToRepository::class, + SetupVirtualEnvironment::class, + InstallPythonDependencies::class, + ]; + } + + public function runningTasks(): array + { + return [ + RunScriptInVirtualEnvironment::class, + ]; + } +} diff --git a/src/ProjectClassifier.php b/src/ProjectClassifier.php index 5d80132..d985690 100644 --- a/src/ProjectClassifier.php +++ b/src/ProjectClassifier.php @@ -8,6 +8,7 @@ use Autopilot\Drivers\PhpCliDriver; use Autopilot\Drivers\PhpWebDriver; use Autopilot\Drivers\PythonScriptDriver; +use Autopilot\Drivers\PythonVirtualEnvDriver; class ProjectClassifier { @@ -15,6 +16,7 @@ class ProjectClassifier LaravelDriver::class, PhpWebDriver::class, PhpCliDriver::class, + PythonVirtualEnvDriver::class, PythonScriptDriver::class, ]; diff --git a/src/Tasks/General/ChdirToRepository.php b/src/Tasks/General/ChdirToRepository.php new file mode 100644 index 0000000..be7175d --- /dev/null +++ b/src/Tasks/General/ChdirToRepository.php @@ -0,0 +1,18 @@ +repository()->dir()->getPath()); + } + + public function message(): string + { + return 'Setting working directory'; + } +} diff --git a/src/Tasks/Php/RunFileInCli.php b/src/Tasks/Php/RunFileInCli.php index 62c511f..6ebbb3b 100644 --- a/src/Tasks/Php/RunFileInCli.php +++ b/src/Tasks/Php/RunFileInCli.php @@ -20,7 +20,6 @@ public function run() { $path = $this->repository()->dir()->getPath($this->primaryFile); - chdir($this->repository()->dir()->getPath()); passthru("php $path"); } diff --git a/src/Tasks/Php/ServePhp.php b/src/Tasks/Php/ServePhp.php index 383b2c0..c082405 100644 --- a/src/Tasks/Php/ServePhp.php +++ b/src/Tasks/Php/ServePhp.php @@ -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}"); } diff --git a/src/Tasks/Python/InstallPythonDependencies.php b/src/Tasks/Python/InstallPythonDependencies.php new file mode 100644 index 0000000..480a61c --- /dev/null +++ b/src/Tasks/Python/InstallPythonDependencies.php @@ -0,0 +1,20 @@ +repository()->dir()->getPath('venv/bin/pip'); + + exec("$executable install -r requirements.txt"); + } + + public function message(): string + { + return 'Installing dependencies from requirements.txt'; + } +} diff --git a/src/Tasks/Python/RunScript.php b/src/Tasks/Python/RunScript.php index 688c2de..1bbd673 100644 --- a/src/Tasks/Python/RunScript.php +++ b/src/Tasks/Python/RunScript.php @@ -7,7 +7,7 @@ class RunScript extends Task { - private $primaryFile; + protected $primaryFile; public function __construct(Repository $repository) { @@ -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"); } diff --git a/src/Tasks/Python/RunScriptInVirtualEnvironment.php b/src/Tasks/Python/RunScriptInVirtualEnvironment.php new file mode 100644 index 0000000..04f979d --- /dev/null +++ b/src/Tasks/Python/RunScriptInVirtualEnvironment.php @@ -0,0 +1,15 @@ +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"); + } +} diff --git a/src/Tasks/Python/SetupVirtualEnvironment.php b/src/Tasks/Python/SetupVirtualEnvironment.php new file mode 100644 index 0000000..b308564 --- /dev/null +++ b/src/Tasks/Python/SetupVirtualEnvironment.php @@ -0,0 +1,18 @@ +clone(); + + $driver = new PythonVirtualEnvDriver($repository); + + $this->assertTrue($driver->matches()); + } +}