Commit 323c9328 authored by Sean Madsen's avatar Sean Madsen

basic setup for adding theme customizations

parent 16e2d7fc
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/book/service_container.html
parameters:
# parameter_name: value
books_dir: %kernel.root_dir%/../books
publish_path_root: %kernel.root_dir%/../web
services:
library:
class: AppBundle\Model\Library
arguments:
- %kernel.root_dir%/../books
- %books_dir%
github.hook.processor:
class: AppBundle\Utils\GitHubHookProcessor
mkdocs:
class: AppBundle\Utils\MkDocs
arguments:
- '@filesystem'
- '@file_locator'
publisher:
class: AppBundle\Utils\Publisher
arguments:
......@@ -21,7 +28,8 @@ services:
- '@filesystem'
- '@library'
- %publisher_repos_dir%
- %kernel.root_dir%/../web
- %publish_path_root%
- '@mkdocs'
publish.logger:
class: Monolog\Logger
......
<?php
namespace AppBundle\Utils;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Config\FileLocatorInterface as FileLocator;
use Symfony\Component\Process\Process;
class MkDocs {
/**
* @var Filesystem
*/
private $fs;
/**
* @var FileLocator
*/
private $fileLocator;
/**
* @var string The full filesystem path to the directory containing the
* markdown files
*/
private $sourcePath;
/**
* @var string $destinationPath The full filesystem path to the directory
* where we want the published content to go
*/
private $destinationPath;
/**
* @var string The full filesystem path to the directory which stores
* different possible theme customizations. Within this directory,
* separate directories should exist, per theme, for the
* customizations, named with the same name as the theme.
*/
private $themeCustomPathRoot;
/**
* @var string The full filesystem path to the directory
*/
private $themeCustomPath;
/**
* @var string The full filesystem location of the mkdocs.yml config file to
* use when building the book. This is the file as it's stored
* after adjustments we make to it.
*/
private $configFile;
/**
* @param Filesystem $fs
* @param FileLocator $fileLocator
*/
public function __construct(Filesystem $fs, FileLocator $fileLocator) {
$this->fs = $fs;
$this->fileLocator = $fileLocator;
$this->themeCustomPathRoot = $this->fileLocator->locate(
'@AppBundle/Resources/theme-customizations');
}
/**
* Reads the mkdocs.yml config file from the book source. Makes some
* customizations to it, and the write the file into the directory where
* we're going to publish the book. Why put it there? It doesn't need to be in
* the publish destination, but it seems like as good a place as any. It just
* needs to be stored somewhere so that mkdocs can read it while building the
* book.
*/
private function customizeConfig() {
// Read config in
$inFile = "{$this->sourcePath}/mkdocs.yml";
$parser = new Parser();
$config = $parser->parse(file_get_contents($inFile));
// If we have a theme-cumstomization directory which matches the theme used
// in the book, then use these theme customizations when building.
$theme = $config['theme'];
$this->themeCustomPath = "{$this->themeCustomPathRoot}/$theme";
if ($this->fs->exists($this->themeCustomPath)) {
$config['theme_dir'] = $this->themeCustomPath;
}
// Dump config out
$dumper = new Dumper();
$this->configFile = dirname($this->destinationPath) . "/"
. basename($this->destinationPath) . "-mkdocs.yml";
$this->fs->dumpFile($this->configFile, $dumper->dump($config, 4));
}
private function getOptions() {
// discard existing build files -- build site from scratch
$opts[] = "--clean";
// abort the build if any errors occur
$opts[] = "--strict";
// use our customized config file
$opts[] = "--config-file {$this->configFile}";
// this is where the finished site should go
$opts[] = "--site-dir {$this->destinationPath}";
return implode(" ", $opts);
}
/**
* Run MkDocs to build a book
*
* @param string $sourcePath The full filesystem path to the directory
* containing the markdown files
*
* @param string $destinationPath The full filesystem path to the directory
* where we want the published content to go
*/
public function build($sourcePath, $destinationPath) {
$this->sourcePath = $sourcePath;
$this->destinationPath = $destinationPath;
$this->customizeConfig();
$buildCommand = "mkdocs build " . $this->getOptions();
$mkdocs = new Process($buildCommand, $this->sourcePath);
$mkdocs->run();
if (!$mkdocs->isSuccessful()) {
throw new \Exception("MkDocs was unable to build the book. "
. "MkDocs command output: "
. $mkdocs->getErrorOutput());
}
}
}
......@@ -102,6 +102,11 @@ class Publisher {
*/
public $repoURL;
/**
* @var \AppBundle\Utils\MkDocs
*/
private $mkDocs;
/**
*
* @param RequestStack $requestStack
......@@ -110,6 +115,7 @@ class Publisher {
* @param Library $library
* @param string $reposPathRoot
* @param string $publishPathRoot
* @param \AppBundle\Utils\MkDocs $mkDocs
*/
public function __construct(
$requestStack,
......@@ -117,12 +123,14 @@ class Publisher {
$fs,
$library,
$reposPathRoot,
$publishPathRoot) {
$publishPathRoot,
$mkDocs) {
$this->logger = $logger;
$this->fs = $fs;
$this->library = $library;
$this->repoPathRoot = realpath($reposPathRoot);
$this->publishPathRoot = realpath($publishPathRoot);
$this->mkDocs = $mkDocs;
if ($requestStack->getCurrentRequest()) {
$this->publishURLBase
= $requestStack->getCurrentRequest()->getUriForPath('');
......@@ -170,7 +178,7 @@ class Publisher {
try {
$this->book->validate();
}
catch (Exception $e) {
catch (\Exception $e) {
$this->addMessage('CRITICAL', "The book settings for {$this->book->name}"
. "failed validation. Validation error is: " . $e->getMessage());
return FALSE;
......@@ -343,22 +351,13 @@ class Publisher {
* @return boolean TRUE if success
*/
private function build() {
$buildCommand = "mkdocs build "
. "--clean --strict --site-dir {$this->publishPath}";
$this->addMessage('INFO', "Running '{$buildCommand}'");
$mkdocs = new Process($buildCommand, $this->repoPath);
$mkdocs->run();
$mkdocsLogMessages = explode("\n", trim($mkdocs->getErrorOutput()));
$this->addMessage('INFO', "mkdocs output: '{$mkdocs->getErrorOutput()}'");
$mkdocsErrors = FALSE;
foreach ($mkdocsLogMessages as $mkdocsLogMessage) {
if (substr($mkdocsLogMessage, 0, 4) != 'INFO') {
$mkdocsErrors = TRUE;
}
try {
$this->mkDocs->build($this->repoPath, $this->publishPath);
}
if ($mkdocsErrors) {
catch (\Exception $e) {
$this->addMessage('CRITICAL',
"MkDocs build errors encountered. Book not published.");
"Build errors encountered. Book not published. Build error message: "
. $e->getMessage());
return FALSE;
}
$this->addMessage('INFO', "Book published successfully at "
......
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