Skip to content

Commit

Permalink
Merge pull request #320 from pjcdawkins/support-new-toolstack-config-…
Browse files Browse the repository at this point in the history
…format

Support new config format for type/flavor
  • Loading branch information
pjcdawkins committed Jul 11, 2015
2 parents a13fca8 + 70ccb38 commit 35abdac
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/Exception/InvalidConfigException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Platformsh\Cli\Exception;

class InvalidConfigException extends \InvalidArgumentException
{
protected $code = 4;
}
74 changes: 68 additions & 6 deletions src/Local/LocalBuild.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
namespace Platformsh\Cli\Local;

use Platformsh\Cli\Exception\InvalidConfigException;
use Platformsh\Cli\Helper\FilesystemHelper;
use Platformsh\Cli\Helper\GitHelper;
use Platformsh\Cli\Helper\ShellHelper;
use Platformsh\Cli\Local\Toolstack\ToolstackInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;

class LocalBuild
Expand Down Expand Up @@ -160,13 +162,53 @@ public function getAppConfig($appRoot)
{
$config = array();
if (file_exists($appRoot . '/.platform.app.yaml')) {
$parser = new Parser();
$config = (array) $parser->parse(file_get_contents($appRoot . '/.platform.app.yaml'));
try {
$parser = new Parser();
$config = (array) $parser->parse(file_get_contents($appRoot . '/.platform.app.yaml'));
}
catch (ParseException $e) {
throw new InvalidConfigException(
"Parse error in file '$appRoot/.platform.app.yaml'. \n" . $e->getMessage()
);
}
}

return $this->normalizeConfig($config);
}

/**
* Normalize an application's configuration.
*
* @param array $config
*
* @return array
*/
public function normalizeConfig(array $config)
{
if (isset($config['toolstack'])) {
$this->deprecationWarning('2015.7');
if (!strpos($config['toolstack'], ':')) {
throw new InvalidConfigException("Invalid value for 'toolstack'");
}
list($config['type'], $config['build']['flavor']) = explode(':', $config['toolstack'], 2);
}

return $config;
}

/**
* Show a warning about deprecated configuration.
*
* @param string $version
* @param string $file
*/
public function deprecationWarning($version, $file = '.platform.app.yaml')
{
$this->output->writeln("The format of your <comment>$file</comment> configuration file is deprecated.");
$this->output->writeln(sprintf("See how to upgrade at: https://docs.platform.sh/reference/upgrade/#changes-in-%s", $version));
$this->output->writeln("");
}

/**
* Get the toolstack for a particular application.
*
Expand All @@ -180,9 +222,22 @@ public function getAppConfig($appRoot)
public function getToolstack($appRoot, array $appConfig = array())
{
$toolstackChoice = false;
if (isset($appConfig['toolstack'])) {
$toolstackChoice = $appConfig['toolstack'];

// For now, we reconstruct a toolstack string based on the 'type' and
// 'build.flavor' config keys.
if (isset($appConfig['type'])) {
$toolstackChoice = sprintf(
'%s:%s',
$appConfig['type'],
!empty($appConfig['build']['flavor']) ? $appConfig['build']['flavor'] : 'default'
);

// Alias php:default to php:composer.
if ($toolstackChoice === 'php:default') {
$toolstackChoice = 'php:composer';
}
}

foreach (self::getToolstacks() as $toolstack) {
$key = $toolstack->getKey();
if ((!$toolstackChoice && $toolstack->detect($appRoot))
Expand Down Expand Up @@ -319,8 +374,15 @@ protected function buildApp($appRoot, $sourceDir, $destination, array $appConfig
$this->fsHelper->extractArchive($archive, $buildDir);
} else {
$message = "Building application <info>$appIdentifier</info>";
if ($key = $toolstack->getKey()) {
$message .= " using the toolstack <info>$key</info>";
$info = array();
if (isset($appConfig['type'])) {
$info[] = 'runtime type: ' . $appConfig['type'];
}
if (!empty($treeId)) {
$info[] = "tree: " . substr($treeId, 0, 7);
}
if (!empty($info)) {
$message .= ' (' . implode(', ', $info) . ')';
}
$this->output->writeln($message);

Expand Down
4 changes: 4 additions & 0 deletions src/Local/Toolstack/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

class Composer extends ToolstackBase
{
public function getKey()
{
return 'php:composer';
}

public function detect($appRoot)
{
Expand Down
8 changes: 8 additions & 0 deletions tests/Local/Toolstack/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public function testBuildFakeSymfony()
{
$this->assertBuildSucceeds('tests/data/apps/fake-symfony');
}

/**
* Test the deprecated config file format still works.
*/
public function testBuildDeprecatedConfig()
{
$this->assertBuildSucceeds('tests/data/apps/deprecated-config');
}
}
2 changes: 2 additions & 0 deletions tests/data/apps/deprecated-config/.platform.app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: deprecated-config
toolstack: php:symfony
4 changes: 3 additions & 1 deletion tests/data/apps/drupal/project-yaml/.platform.app.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
toolstack: "php:drupal"
type: php
build:
flavor: drupal
4 changes: 3 additions & 1 deletion tests/data/apps/drupal/project/.platform.app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
toolstack: "php:drupal"
type: php
build:
flavor: drupal

# Test build hook
hooks:
Expand Down
4 changes: 3 additions & 1 deletion tests/data/apps/fake-symfony/.platform.app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
# composer.json file.

name: fake-symfony
toolstack: "php:symfony"
type: php
build:
flavor: symfony
5 changes: 3 additions & 2 deletions tests/data/repositories/multiple/drupal/.platform.app.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: drupal

toolstack: "php:drupal"
type: php
build:
flavor: drupal

0 comments on commit 35abdac

Please sign in to comment.