8889841cRunner.php000064400000001050150536162340006527 0ustar00module = $module; } /** * Run the given command. * * @param string $command */ public function run($command) { passthru($command); } } Updater.php000064400000004701150536162340006670 0ustar00module->findOrFail($module); chdir(base_path()); $this->installRequires($module); $this->installDevRequires($module); $this->copyScriptsToMainComposerJson($module); } /** * Check if composer should output anything. * * @return string */ private function isComposerSilenced() { return config('modules.composer.composer-output') === false ? ' --quiet' : ''; } /** * @param Module $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}{$this->isComposerSilenced()}"); } } /** * @param Module $module */ 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}{$this->isComposerSilenced()}"); } } /** * @param Module $module */ 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)); } } Installer.php000064400000014407150536162340007225 0ustar00name = $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. * @param \Nwidart\Modules\Contracts\RepositoryInterface $repository * @return $this */ public function setRepository(RepositoryInterface $repository) { $this->repository = $repository; return $this; } /** * Set console command instance. * * @param \Illuminate\Console\Command $console * * @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() )); } }