Skip to content

Commit

Permalink
3.0-alpha1
Browse files Browse the repository at this point in the history
  • Loading branch information
stovak committed Jun 13, 2021
1 parent 6cbe4eb commit 92b58b2
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 29 deletions.
33 changes: 17 additions & 16 deletions src/Commands/D9ify/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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,
"*********************************************************************",
Expand All @@ -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()),
Expand Down
61 changes: 48 additions & 13 deletions src/Helpers/Site/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +32,9 @@ class Directory implements ContainerAwareInterface, IOAwareInterface, SiteAwareI
use IO;
use SiteAwareTrait;

/**
* @var
*/
protected $site;

/**
Expand All @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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";
}

/**
Expand Down Expand Up @@ -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: ");
}
}
}
38 changes: 38 additions & 0 deletions src/Helpers/Site/Sources/SiteSourceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Pantheon\D9ify\Site\Sources;

use Pantheon\D9ify\Site\InfoInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Interface SiteSourceInterface
*
* @package D9ify\Site\Sources
*/
interface SiteSourceInterface
{


/**
* @return bool
*/
public function valid(): bool;

/**
* @param \D9ify\Site\Sources\OutputInterface $output
*
* @return bool
*/
public function cloneFiles(OutputInterface $output): bool;

/**
* @return \D9ify\Site\InfoInterface
*/
public function getSiteInfo(): InfoInterface;

/**
* @return array
*/
public function getConnectionInfo(): array;
}
99 changes: 99 additions & 0 deletions src/Helpers/Site/Sources/Terminus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Pantheon\D9ify\Site\Sources;

use CzProject\GitPhp\GitRepository;
use Pantheon\D9ify\Traits\CommandExecutorTrait;
use Pantheon\D9ify\Traits\DefaultClonePathTrait;
use Pantheon\D9ify\Traits\SiteInfoTrait;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class Terminus
* @package D9ify\Site\Sources
*/
class Terminus implements SiteSourceInterface
{

use CommandExecutorTrait;
use SiteInfoTrait;
use DefaultClonePathTrait;

/**
* @var string
*/
protected string $siteID;
/**
* @var string|mixed
*/
protected string $referenceEnvironment;

/**
* Terminus constructor.
* @param $siteID
* @param string $referenceEnvironment
*/
public function __construct($siteID, $referenceEnvironment = "live")
{
$this->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()
)
);
}
}
9 changes: 9 additions & 0 deletions src/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,13 @@ public function updateServiceLevel($service_level)
throw $e;
}
}

public function valid():bool
{
return (bool) $this->id;
}

public function cloneLocalCopy()
{
}
}

0 comments on commit 92b58b2

Please sign in to comment.