Commit b5d66804 authored by mickadoo's avatar mickadoo

68: Add method to remove directory, cleanup tmp dir after publishing

parent a1ba8a28
......@@ -13,10 +13,9 @@ class FileSystem extends BaseFileSystem {
public function copyDir($source, $target) {
$this->mkdir($target);
$selfFirst = \RecursiveIteratorIterator::SELF_FIRST;
$mode = \RecursiveIteratorIterator::SELF_FIRST;
$directoryIterator = new \RecursiveDirectoryIterator($source);
$iterator = new \RecursiveIteratorIterator($directoryIterator, $selfFirst);
$iterator = new \RecursiveIteratorIterator($directoryIterator, $mode);
foreach ($iterator as $item) {
if ($item->isDir()) {
......@@ -28,4 +27,24 @@ class FileSystem extends BaseFileSystem {
}
}
}
/**
* @param string $dir
*/
public function removeDir($dir) {
$childFirst = \RecursiveIteratorIterator::CHILD_FIRST;
$noDots = \RecursiveDirectoryIterator::SKIP_DOTS;
$directoryIterator = new \RecursiveDirectoryIterator($dir, $noDots);
$iterator = new \RecursiveIteratorIterator($directoryIterator, $childFirst);
foreach ($iterator as $item) {
if ($item->isDir()) {
$this->removeDir($item->getPathname());
} else {
$this->remove($item->getPathname());
}
}
rmdir($dir);
}
}
......@@ -8,13 +8,12 @@ use AppBundle\Model\Version;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;
use Symfony\Component\Filesystem\Filesystem;
use AppBundle\Model\Library;
class Publisher {
/**
* @var Filesystem
* @var FileSystem
*/
protected $fs;
......@@ -185,44 +184,35 @@ class Publisher {
*/
private function publishVersion($book, $language, $versionDescriptor) {
$version = $this->getVersion($book, $language, $versionDescriptor);
$branch = $version->branch;
$bookRepo = $language->repo;
// info text
$this->addMessage('INFO', "Using book: " . $book->name);
$aliasText = "";
if ($version->aliases) {
$aliasList = implode('", "', $version->aliases);
$aliasText = sprintf("with aliases: %s", $aliasList);
}
$this->addMessage('INFO', "Using language: " . $language->getEnglishName());
$msg = sprintf("Using branch: '%s' %s", $version->branch, $aliasText);
$this->addMessage('INFO', $msg);
$fullIdentifier = "{$book->slug}/{$language->code}/" . "{$version->branch}";
$repoURL = $language->repo;
$this->showVersionInfo($book, $language, $version);
$publishPath = $this->paths->getPublishPathRoot() . "/{$fullIdentifier}";
$this->fs->mkdir($publishPath);
$repoRoot = $this->paths->getRepoPathRoot();
$masterRepoDir = sprintf('%s/%s/%s', $repoRoot, $book->slug, $language->code);
$repoPath = $this->paths->getRepoPathRoot() . "/{$book->slug}/" . "{$language->code}";
$this->fs->mkdir($repoPath);
$this->setupLocalRepo($masterRepoDir, $bookRepo);
$tmpRepoDir = $this->makeTmpRepo($book, $language, $masterRepoDir);
$this->initializeRepo($repoPath, $repoURL);
$this->git->checkout($tmpRepoDir, $branch);
$this->git->pull($tmpRepoDir);
$this->addMessage('INFO', sprintf("Checked out branch '%s'", $branch));
$this->git->checkout($repoPath, $version->branch);
$msg = sprintf("Checked out branch '%s'", $version->branch);
$this->addMessage('INFO', $msg);
$msg = sprintf("Running 'git pull' to update '%s' branch.", $repoPath);
$this->addMessage('INFO', $msg);
$this->git->pull($repoPath);
$path = "{$book->slug}/{$language->code}/{$branch}";
$webRoot = $this->paths->getPublishPathRoot() . "/{$path}";
$this->fs->mkdir($webRoot);
$this->build($book, $language, $version, $tmpRepoDir, $webRoot);
$this->build($book, $language, $version, $repoPath, $publishPath);
$format = "<a href='/%s'>Book published successfully</a>.";
$msg = sprintf($format, $fullIdentifier);
$msg = sprintf($format, $path);
$this->addMessage('INFO', $msg);
$this->setupSymlinks($book, $language, $version, $publishPath);
$this->setupRedirects($repoPath, $publishPath);
$this->setupSymlinks($book, $language, $version, $webRoot);
$this->setupRedirects($tmpRepoDir, $webRoot);
$this->fs->removeDir($tmpRepoDir);
// todo change publish dir to tmp dir
}
/**
......@@ -291,22 +281,6 @@ class Publisher {
return $version;
}
/**
* Ensure that we have the repository locally
*
* @param string $repoPath
* @param string $repoURL
*/
private function initializeRepo($repoPath, $repoURL) {
$repoExists = (bool) $this->fs->exists($repoPath . '/.git');
if ($repoExists) {
return;
}
$this->git->clone($repoURL, $repoPath);
}
/**
* Use MkDocs to build a static site for the book
*
......@@ -382,4 +356,57 @@ class Publisher {
}
}
/**
* @param $book
* @param $language
* @param $version
*/
private function showVersionInfo(
Book $book,
Language $language,
Version $version
) {
$this->addMessage('INFO', "Using book: " . $book->name);
$aliasText = "";
if ($version->aliases) {
$aliasList = implode('", "', $version->aliases);
$aliasText = sprintf("with aliases: %s", $aliasList);
}
$this->addMessage('INFO', "Using language: " . $language->getEnglishName());
$msg = sprintf("Using branch: '%s' %s", $version->branch, $aliasText);
$this->addMessage('INFO', $msg);
}
/**
* @param $masterRepoDir
* @param $bookRepo
*/
private function setupLocalRepo($masterRepoDir, $bookRepo) {
if (!$this->fs->exists($masterRepoDir)) {
$this->fs->mkdir($masterRepoDir);
$this->git->clone($bookRepo, $masterRepoDir);
}
$this->git->fetch($masterRepoDir);
}
/**
* @param $book
* @param $language
* @param $masterRepoDir
*
* @return string
*/
private function makeTmpRepo($book, $language, $masterRepoDir): string {
$cacheDir = $this->paths->getCacheDir() . '/publishing';
$this->fs->mkdir($cacheDir);
$prefix = $book->slug . '_' . $language->code . '_' . time();
$tmpRepoDir = $cacheDir . '/' . $prefix;
$this->fs->mkdir($tmpRepoDir);
$this->fs->copyDir($masterRepoDir, $tmpRepoDir);
return $tmpRepoDir;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment