Skip to content

Commit

Permalink
Add driver for python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
robbplo committed Jan 10, 2022
1 parent 8d4b454 commit 7b5d869
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
1 change: 0 additions & 1 deletion autopilot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php',
}
}

use Autopilot\Apps\TextEditor;
use Autopilot\Drivers\Concerns\RequiresRunning;
use Autopilot\Drivers\Concerns\RequiresSetup;
use Autopilot\ProjectClassifier;
Expand Down
23 changes: 23 additions & 0 deletions src/Drivers/PythonScriptDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Autopilot\Drivers;

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

class PythonScriptDriver extends Driver implements RequiresRunning
{
public function matches(): bool
{
return $this->repository->dir()->find()
->name('*.py')
->count() > 0;
}

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

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

private $repository;
Expand Down
42 changes: 42 additions & 0 deletions src/Tasks/Python/RunScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Autopilot\Tasks\Python;

use Autopilot\Repository;
use Autopilot\Tasks\Task;

class RunScript extends Task
{
private $primaryFile;

public function __construct(Repository $repository)
{
parent::__construct($repository);

$this->primaryFile = $this->getPrimaryFile();
}

public function run()
{
$path = $this->repository()->dir()->getPath($this->primaryFile);

chdir($this->repository()->dir()->getPath());
passthru("python $path"); // @todo does not work with pythons `input` function
}

public function message(): string
{
return "Guessed primary file: $this->primaryFile. Executing..." . PHP_EOL . PHP_EOL;
}

private function getPrimaryFile(): string
{
$finder = $this->repository()->dir()->find()
->depth(0)
->name('*.py');

foreach ($finder as $file) {
return $file->getFilename();
}
}
}
21 changes: 21 additions & 0 deletions tests/Drivers/PythonScriptDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Drivers;

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

class PythonScriptDriverTest extends TestCase
{
/** @test */
public function it_matches()
{
$repository = new Repository("[email protected]:ceae8dbb-4b83-11ec-a0c6-4213e7ee7fac/2c811487-1082-11ec-a7a6-4213e7ee7fac/Hello-world-ca0aef52-63206bf1.git");
$repository->clone();

$driver = new PythonScriptDriver($repository);

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

0 comments on commit 7b5d869

Please sign in to comment.