Initial Commit

This commit is contained in:
Mark Gregory
2026-04-21 09:12:51 +01:00
commit 6bc3c69c3e
151 changed files with 11917 additions and 0 deletions
+305
View File
@@ -0,0 +1,305 @@
<?php
namespace mpba\Modules\Process;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use mpba\Modules\Contracts\RepositoryInterface;
use Symfony\Component\Process\Process;
class Installer
{
/**
* The module name.
*
* @var string
*/
protected $name;
/**
* The version of module being installed.
*
* @var string
*/
protected $version;
/**
* The module repository instance.
*
* @var mpba\Modules\Contracts\RepositoryInterface
*/
protected $repository;
/**
* The console command instance.
*
* @var \Illuminate\Console\Command
*/
protected $console;
/**
* The destionation path.
*
* @var string
*/
protected $path;
/**
* The process timeout.
*
* @var int
*/
protected $timeout = 3360;
/**
* @var null|string
*/
private $type;
/**
* @var bool
*/
private $tree;
/**
* The constructor.
*
* @param string $name
* @param string $version
* @param string $type
* @param bool $tree
*/
public function __construct($name, $version = null, $type = null, $tree = false)
{
$this->name = $name;
$this->version = $version;
$this->type = $type;
$this->tree = $tree;
}
/**
* Set destination path.
*
* @param string $path
* @return $this
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Set the module repository instance.
*
* @return $this
*/
public function setRepository(RepositoryInterface $repository)
{
$this->repository = $repository;
return $this;
}
/**
* Set console command instance.
*
*
* @return $this
*/
public function setConsole(Command $console)
{
$this->console = $console;
return $this;
}
/**
* Set process timeout.
*
* @param int $timeout
* @return $this
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
return $this;
}
/**
* Run the installation process.
*
* @return \Symfony\Component\Process\Process
*/
public function run()
{
$process = $this->getProcess();
$process->setTimeout($this->timeout);
if ($this->console instanceof Command) {
$process->run(function ($type, $line) {
$this->console->line($line);
});
}
return $process;
}
/**
* Get process instance.
*
* @return \Symfony\Component\Process\Process
*/
public function getProcess()
{
if ($this->type) {
if ($this->tree) {
return $this->installViaSubtree();
}
return $this->installViaGit();
}
return $this->installViaComposer();
}
/**
* Get destination path.
*
* @return string
*/
public function getDestinationPath()
{
if ($this->path) {
return $this->path;
}
return $this->repository->getModulePath($this->getModuleName());
}
/**
* Get git repo url.
*
* @return string|null
*/
public function getRepoUrl()
{
switch ($this->type) {
case 'github':
return "git@github.com:{$this->name}.git";
case 'github-https':
return "https://github.com/{$this->name}.git";
case 'gitlab':
return "git@gitlab.com:{$this->name}.git";
break;
case 'bitbucket':
return "git@bitbucket.org:{$this->name}.git";
default:
// Check of type 'scheme://host/path'
if (filter_var($this->type, FILTER_VALIDATE_URL)) {
return $this->type;
}
// Check of type 'user@host'
if (filter_var($this->type, FILTER_VALIDATE_EMAIL)) {
return "{$this->type}:{$this->name}.git";
}
return;
break;
}
}
/**
* Get branch name.
*
* @return string
*/
public function getBranch()
{
return is_null($this->version) ? 'master' : $this->version;
}
/**
* Get module name.
*
* @return string
*/
public function getModuleName()
{
$parts = explode('/', $this->name);
return Str::studly(end($parts));
}
/**
* Get composer package name.
*
* @return string
*/
public function getPackageName()
{
if (is_null($this->version)) {
return $this->name.':dev-master';
}
return $this->name.':'.$this->version;
}
/**
* Install the module via git.
*
* @return \Symfony\Component\Process\Process
*/
public function installViaGit()
{
return Process::fromShellCommandline(sprintf(
'cd %s && git clone %s %s && cd %s && git checkout %s',
base_path(),
$this->getRepoUrl(),
$this->getDestinationPath(),
$this->getDestinationPath(),
$this->getBranch()
));
}
/**
* Install the module via git subtree.
*
* @return \Symfony\Component\Process\Process
*/
public function installViaSubtree()
{
return Process::fromShellCommandline(sprintf(
'cd %s && git remote add %s %s && git subtree add --prefix=%s --squash %s %s',
base_path(),
$this->getModuleName(),
$this->getRepoUrl(),
$this->getDestinationPath(),
$this->getModuleName(),
$this->getBranch()
));
}
/**
* Install the module via composer.
*
* @return \Symfony\Component\Process\Process
*/
public function installViaComposer()
{
return Process::fromShellCommandline(sprintf(
'cd %s && composer require %s',
base_path(),
$this->getPackageName()
));
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace mpba\Modules\Process;
use mpba\Modules\Contracts\RepositoryInterface;
use mpba\Modules\Contracts\RunableInterface;
class Runner implements RunableInterface
{
protected $module;
public function __construct(RepositoryInterface $module)
{
$this->module = $module;
}
/**
* Run the given command.
*
* @param string $command
*/
public function run($command)
{
passthru($command);
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace mpba\Modules\Process;
use laraveluse mpba\Modules\Module;
class Updater extends Runner
{
/**
* Update the dependencies for the specified module by given the module name.
*
* @param string $module
*/
public function update($module)
{
$module = $this->module->findOrFail($module);
chdir(base_path());
$this->installRequires($module);
$this->installDevRequires($module);
$this->copyScriptsToMainComposerJson($module);
}
private function installRequires(Module $module)
{
$packages = $module->getComposerAttr('require', []);
$concatenatedPackages = '';
foreach ($packages as $name => $version) {
$concatenatedPackages .= "\"{$name}:{$version}\" ";
}
if (! empty($concatenatedPackages)) {
$this->run("composer require {$concatenatedPackages}");
}
}
private function installDevRequires(Module $module)
{
$devPackages = $module->getComposerAttr('require-dev', []);
$concatenatedPackages = '';
foreach ($devPackages as $name => $version) {
$concatenatedPackages .= "\"{$name}:{$version}\" ";
}
if (! empty($concatenatedPackages)) {
$this->run("composer require --dev {$concatenatedPackages}");
}
}
private function copyScriptsToMainComposerJson(Module $module)
{
$scripts = $module->getComposerAttr('scripts', []);
$composer = json_decode(file_get_contents(base_path('composer.json')), true);
foreach ($scripts as $key => $script) {
if (array_key_exists($key, $composer['scripts'])) {
$composer['scripts'][$key] = array_unique(array_merge($composer['scripts'][$key], $script));
continue;
}
$composer['scripts'] = array_merge($composer['scripts'], [$key => $script]);
}
file_put_contents(base_path('composer.json'), json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
}