-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add better exceptions & improve readability by refactor
- Loading branch information
1 parent
578d9dd
commit 6ffc08e
Showing
8 changed files
with
208 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kauffinger\Pyman\Concerns; | ||
|
||
use Kauffinger\Pyman\DependencyManager; | ||
|
||
trait ManagesDependencies | ||
{ | ||
private DependencyManager $dependencies; | ||
|
||
public function addDependency(string $dependency): self | ||
{ | ||
$this->dependencies->add($dependency); | ||
|
||
return $this; | ||
} | ||
|
||
public function clearDependencies(): self | ||
{ | ||
$this->dependencies->clear(); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function getDependencies(): array | ||
{ | ||
return $this->dependencies->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kauffinger\Pyman; | ||
|
||
use Illuminate\Process\Factory; | ||
use Kauffinger\Pyman\Exceptions\MissingDependencyException; | ||
|
||
class DependencyManager | ||
{ | ||
/** | ||
* Holds list of dependencies that are required for the installation process. | ||
* | ||
* @param array<string> $dependencies | ||
*/ | ||
public function __construct(private readonly Factory $processFactory, private array $dependencies = []) {} | ||
|
||
public function add(string $dependency): void | ||
{ | ||
$this->dependencies[] = $dependency; | ||
} | ||
|
||
public function clear(): void | ||
{ | ||
$this->dependencies = []; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function get(): array | ||
{ | ||
return $this->dependencies; | ||
} | ||
|
||
public function check(): void | ||
{ | ||
foreach ($this->dependencies as $command) { | ||
$result = $this->processFactory->newPendingProcess() | ||
->run("command -v $command"); | ||
|
||
if (! $result->successful()) { | ||
throw new MissingDependencyException("$command is required but not installed."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kauffinger\Pyman\Exceptions; | ||
|
||
class FolderCouldNotBeCreatedException extends PymanException | ||
{ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kauffinger\Pyman\Exceptions; | ||
|
||
class MissingDependencyException extends PymanException | ||
{ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kauffinger\Pyman\Exceptions; | ||
|
||
class VenvManagementException extends PymanException | ||
{ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kauffinger\Pyman; | ||
|
||
use Illuminate\Process\Factory; | ||
use Kauffinger\Pyman\Exceptions\VenvManagementException; | ||
|
||
class RequirementsManager | ||
{ | ||
/** | ||
* Holds list of dependencies that are required for the installation process. | ||
* | ||
* @param array<string> $requirements | ||
*/ | ||
public function __construct( | ||
private readonly Factory $processFactory, | ||
private readonly string $pythonDir, | ||
private array $requirements = [] | ||
) {} | ||
|
||
public function add(string $requirement): void | ||
{ | ||
$this->requirements[] = $requirement; | ||
} | ||
|
||
public function clear(): void | ||
{ | ||
$this->requirements = []; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function get(): array | ||
{ | ||
return $this->requirements; | ||
} | ||
|
||
/** | ||
* Currently only works with the requirements.txt file, but will be extended to be able to use | ||
* passed dependencies as well. | ||
* | ||
* @throws VenvManagementException | ||
*/ | ||
public function install(): void | ||
{ | ||
$requirementsPath = $this->pythonDir.'/requirements.txt'; | ||
|
||
if (! file_exists($requirementsPath)) { | ||
throw new VenvManagementException("requirements.txt not found in {$this->pythonDir}"); | ||
} | ||
|
||
$venvPip = $this->pythonDir.'/venv/bin/pip'; | ||
|
||
$result = $this->processFactory->newPendingProcess() | ||
->path($this->pythonDir) | ||
->timeout(300) | ||
->run([$venvPip, 'install', '-r', 'requirements.txt', '-q']); | ||
|
||
if (! $result->successful()) { | ||
throw new VenvManagementException('Failed to install requirements: '.$result->errorOutput()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters