/**
* @param LocalApplication $app
* @param string $sourceDir
* @param string $destination
*
* @return bool
*/
protected function buildApp($app, $sourceDir, $destination)
{
$verbose = $this->output->isVerbose();
$appRoot = $app->getRoot();
$appConfig = $app->getConfig();
$multiApp = $appRoot != $sourceDir;
$appName = $app->getName();
$appId = $app->getId();
$buildName = date('Y-m-d--H-i-s');
if (!empty($this->settings['environmentId'])) {
$buildName .= '--' . $this->settings['environmentId'];
}
if ($multiApp) {
$buildName .= '--' . str_replace('/', '-', $appId);
}
if (!empty($this->settings['projectRoot'])) {
$buildDir = $this->settings['projectRoot'] . '/' . LocalProject::BUILD_DIR . '/' . $buildName;
} else {
$buildDir = $sourceDir . '/' . LocalProject::BUILD_DIR . '/' . $buildName;
}
// Get the configured document root.
$documentRoot = $this->getDocumentRoot($appConfig);
$toolstack = $app->getToolstack();
if (!$toolstack) {
$this->output->writeln("Toolstack not found for application <error>{$appId}</error>");
return false;
}
// Warn about a mismatched PHP version.
if (isset($appConfig['type']) && strpos($appConfig['type'], ':')) {
list($stack, $version) = explode(':', $appConfig['type'], 2);
if ($stack === 'php' && version_compare($version, PHP_VERSION, '>')) {
$this->output->writeln(sprintf('<comment>Warning:</comment> the application <comment>%s</comment> expects PHP %s, but the system version is %s.', $appId, $version, PHP_VERSION));
}
}
$toolstack->setOutput($this->output);
$buildSettings = $this->settings + ['multiApp' => $multiApp, 'appName' => $appName];
$toolstack->prepare($buildDir, $documentRoot, $appRoot, $buildSettings);
$archive = false;
if (empty($this->settings['noArchive']) && empty($this->settings['noCache']) && !empty($this->settings['projectRoot'])) {
$treeId = $this->getTreeId($appRoot);
if ($treeId) {
if ($verbose) {
$this->output->writeln("Tree ID: {$treeId}");
}
$archive = $this->settings['projectRoot'] . '/' . LocalProject::ARCHIVE_DIR . '/' . $treeId . '.tar.gz';
}
}
if ($archive && file_exists($archive)) {
$message = "Extracting archive for application <info>{$appId}</info>";
$this->output->writeln($message);
$this->fsHelper->extractArchive($archive, $buildDir);
} else {
$message = "Building application <info>{$appId}</info>";
if (isset($appConfig['type'])) {
$message .= ' (runtime type: ' . $appConfig['type'] . ')';
}
$this->output->writeln($message);
$toolstack->build();
if ($this->runPostBuildHooks($appConfig, $toolstack->getAppRoot()) === false) {
// The user may not care if build hooks fail, but we should
// not archive the result.
$archive = false;
}
if ($archive && $toolstack->canArchive()) {
$this->output->writeln("Saving build archive");
if (!is_dir(dirname($archive))) {
mkdir(dirname($archive));
}
$this->fsHelper->archiveDir($buildDir, $archive);
}
}
$toolstack->install();
$webRoot = $toolstack->getWebRoot();
// Symlink the built web root ($webRoot) into www or www/appId.
if (!is_dir($webRoot)) {
$this->output->writeln("Web root not found: <error>{$webRoot}</error>");
return false;
}
if ($multiApp) {
$appDir = str_replace('/', '-', $appId);
if (is_link($destination)) {
$this->fsHelper->remove($destination);
}
$destination .= "/{$appDir}";
}
$this->fsHelper->symlink($webRoot, $destination);
$this->output->writeln("Web root: {$destination}");
$message = "Build complete for application <info>{$appId}</info>";
$this->output->writeln($message);
return true;
}