From 92b58b25139c905c88fb87fb511fdf9022476027 Mon Sep 17 00:00:00 2001 From: stovak <119924+stovak@users.noreply.github.com> Date: Sun, 13 Jun 2021 16:30:15 -0700 Subject: [PATCH] 3.0-alpha1 --- src/Commands/D9ify/ProcessCommand.php | 33 ++++--- src/Helpers/Site/Directory.php | 61 +++++++++--- .../Site/Sources/SiteSourceInterface.php | 38 +++++++ src/Helpers/Site/Sources/Terminus.php | 99 +++++++++++++++++++ src/Models/Site.php | 9 ++ 5 files changed, 211 insertions(+), 29 deletions(-) create mode 100644 src/Helpers/Site/Sources/SiteSourceInterface.php create mode 100644 src/Helpers/Site/Sources/Terminus.php diff --git a/src/Commands/D9ify/ProcessCommand.php b/src/Commands/D9ify/ProcessCommand.php index 2492ffb2e..d9ce6cddb 100644 --- a/src/Commands/D9ify/ProcessCommand.php +++ b/src/Commands/D9ify/ProcessCommand.php @@ -121,6 +121,7 @@ public function process($sourceSite, $destinationSite = null, $options = []) 'drupal9', ['org' => null] ); + $destinationSiteInfoCommand = $this->getContainer()->get(InfoCommand::class); $destinationSiteInfo = $destinationSiteInfoCommand->info($destinationSite)->getArrayCopy(); $this->getContainer()->add('destinationSiteModel', Site::class) ->addArgument($destinationSiteInfo); @@ -157,21 +158,20 @@ public function process($sourceSite, $destinationSite = null, $options = []) "*************************************************************", ]); - \Kint::dump($this->sites()); - exit(); + $this->copyRepositoriesFromSource(); - $this->updateDestModulesAndThemesFromSource($output); - $this->updateDestEsLibrariesFromSource($output); - $this->writeComposer($output); - $this->destinationComposerInstall($output); - $this->copyCustomCode($output); - $this->copyConfigFiles($output); - $this->downloadDatabase($output); - $this->downloadSourceSiteFilesDirectory($output); + $this->updateDestModulesAndThemesFromSource(); + $this->updateDestEsLibrariesFromSource(); + $this->writeComposer(); + $this->destinationComposerInstall(); + $this->copyCustomCode(); + $this->copyConfigFiles(); + $this->downloadDatabase(); + $this->downloadSourceSiteFilesDirectory(); } catch (D9ifyExceptionBase $d9ifyException) { // TODO: Composer install exception help text - $io->writeln((string)$d9ifyException); + $this->output()->writeln((string)$d9ifyException); } catch (\Exception $e) { // TODO: General help text and how to restart the process $this->output()->writeln("Script ended in Exception state. " . $e->getMessage()); @@ -213,9 +213,9 @@ protected function setSourceDirectory(Directory $sourceDirectory): void * * @param \Symfony\Component\Console\Output\OutputInterface $output */ - protected function copyRepositoriesFromSource(OutputInterface $output) + protected function copyRepositoriesFromSource() { - $output->writeln([ + $this->output()->writeln([ "===> Ensuring source and destination folders exist.", PHP_EOL, "*********************************************************************", @@ -224,12 +224,13 @@ protected function copyRepositoriesFromSource(OutputInterface $output) "*********************************************************************", PHP_EOL, ]); - $this->getSourceDirectory()->ensure(false); - $this->getDestinationDirectory()->ensure(true); + $this->getSourceDirectory()->ensureLocalCopyOfRepo(false); + $this->getDestinationDirectory()->ensureLocalCopyOfRepo(true); $this->destinationDirectory->getComposerObject()->setRepositories( $this->sourceDirectory->getComposerObject()->getOriginal()['repositories'] ?? [] ); - $output->writeln([ + exit(); + $this->output()->writeln([ "*********************************************************************", sprintf("Source Folder: %s", $this->getSourceDirectory()->getClonePath()), sprintf("Destination Folder: %s", $this->getDestinationDirectory()->getClonePath()), diff --git a/src/Helpers/Site/Directory.php b/src/Helpers/Site/Directory.php index b9d51b4ef..14041d672 100644 --- a/src/Helpers/Site/Directory.php +++ b/src/Helpers/Site/Directory.php @@ -4,6 +4,8 @@ use League\Container\ContainerAwareInterface; use League\Container\ContainerAwareTrait; +use Pantheon\Terminus\Exceptions\TerminusException; +use Pantheon\Terminus\Exceptions\TerminusNotFoundException; use Pantheon\Terminus\Helpers\Composer\ComposerFile; use Pantheon\Terminus\Exceptions\ComposerInstallException; use Pantheon\Terminus\Helpers\Traits\CommandExecutorTrait; @@ -30,6 +32,9 @@ class Directory implements ContainerAwareInterface, IOAwareInterface, SiteAwareI use IO; use SiteAwareTrait; + /** + * @var + */ protected $site; /** @@ -51,10 +56,7 @@ class Directory implements ContainerAwareInterface, IOAwareInterface, SiteAwareI */ public function __construct($site) { - if (is_string($site)) { - $site = $this->getSite($site); - } - $this->site = $site; + $this->setSource($site); } /** @@ -77,9 +79,9 @@ public static function delTree($dir): bool /** * @throws \Exception */ - public function ensure(bool $create = false) + public function ensureLocalCopyOfRepo(bool $create = false) { - $valid = $this->getSource()->valid(); + $valid = $this->getSource()->valid() ?? false; if ($valid === false) { // if site doesn't exist if ($create === true) { @@ -91,11 +93,11 @@ public function ensure(bool $create = false) } $this->clonePath = new \SplFileInfo( $this->getDefaultClonePathBase() . - DIRECTORY_SEPARATOR . $this->getSource()->getSiteInfo()->getName() + DIRECTORY_SEPARATOR . $this->getSource()->getName() ); if (!$this->clonePath->isDir()) { // -oStrictHostKeyChecking=no - $this->getSource()->cloneFiles($this->getOutput()); + $this->getSource()->cloneLocalCopy(); } $this->setComposerFile(); @@ -184,12 +186,12 @@ public function install() { is_file($this->clonePath . "/composer.lock") ? unlink($this->clonePath . "/composer.lock") : []; - $this->execute("rm -Rf %s && cd %s && composer upgrade --with-dependencies", [ + $result = $this->execute("rm -Rf %s && cd %s && composer upgrade --with-dependencies", [ $this->clonePath . "/vendor", $this->clonePath ]); if ($this->execResult[0] !== 0) { - throw new ComposerInstallException($result, $output); + throw new ComposerInstallException($result, $this->output()); } return $result; } @@ -239,10 +241,13 @@ public function ensureCustomCodeFoldersExist(InputInterface $input, OutputInterf } } + /** + * @return string + */ public function getDefaultClonePathBase() { // Get path resoltion from default composer file directory - return dirname(\Composer\Factory::getComposerFile()) . "/local-copies"; + return $_SERVER['HOME'] . "/pantheon-local-copies"; } /** @@ -271,8 +276,38 @@ public function ensurePantheonYamlValues(InputInterface $input, OutputInterface yaml_emit_file($yamlFile, $pantheonYaml); } - public function getInfo() : Site + /** + * @return \Pantheon\Terminus\Models\Site|null + */ + public function getSource(): ? Site + { + return $this->site ?? null; + } + + + /** + * @param string|Site $site + */ + public function setSource($site) { - return $this->getSite($this->siteID); + if (is_string($site)) { + $this->site = $this->getSite($site); + } + if ($site instanceof Site) { + $this->site = $site; + } + if (is_object($site)) { + $site = (array)$site; + } + if (is_array($site) && isset($site['id'])) { + $this->site = $this->getSite($site['id']); + } + if (is_array($site) && isset($site['site'])) { + $this->site = $site['site']; + } + if (!$this->site instanceof Site) { + \Kint::dump($site); + throw new TerminusNotFoundException("Site not found: "); + } } } diff --git a/src/Helpers/Site/Sources/SiteSourceInterface.php b/src/Helpers/Site/Sources/SiteSourceInterface.php new file mode 100644 index 000000000..c72f567a3 --- /dev/null +++ b/src/Helpers/Site/Sources/SiteSourceInterface.php @@ -0,0 +1,38 @@ +setSiteInfoFromSiteId($siteID); + $this->referenceEnvironment = $referenceEnvironment; + } + + /** + * @return bool + */ + public function valid(): bool + { + return $this->getSiteInfo()->valid(); + } + + /** + * @return array + */ + public function getConnectionInfo(): array + { + return $this->execute("terminus connection:info %s.dev --format=json", [ + $this->getSiteInfo()->getName() + ]); + } + + /** + * @return string + */ + protected function getClonePath():string + { + return $this->getDefaultClonePathBase() . DIRECTORY_SEPARATOR . $this->getSiteInfo()->getName(); + } + + /** + * @return string + */ + protected function getGitCommand():string + { + return str_replace( + $this->getSiteInfo()->getName(), + $this->getClonePath(), + $this->getConnectionInfo()['git_command'] + ); + } + + /** + * @return bool + * @throws \Exception + */ + public function cloneFiles(OutputInterface $output): bool + { + + $this->execute($this->getGitCommand()); + if ($this->getLastStatus() !== 0) { + throw new \Exception("Cannot clone site with terminus command." . + join(PHP_EOL, $this->execResult)); + } + $output->writeln( + sprintf( + "Site Code Folder: %s", + $this->clonePath->getRealPath() + ) + ); + } +} diff --git a/src/Models/Site.php b/src/Models/Site.php index 13e4b27eb..9a0e02bc1 100755 --- a/src/Models/Site.php +++ b/src/Models/Site.php @@ -478,4 +478,13 @@ public function updateServiceLevel($service_level) throw $e; } } + + public function valid():bool + { + return (bool) $this->id; + } + + public function cloneLocalCopy() + { + } }