Skip to content

Commit

Permalink
feature #994 feat: support for compose.yml (dunglas)
Browse files Browse the repository at this point in the history
This PR was submitted for the 2.x branch but it was squashed and merged into the 1.x branch instead.

Discussion
----------

feat: support for compose.yml

Docker Compose now recommends using a definition file named `compose.yaml` and also supports `compose.yml` (the legacy `docker-compose.ya?ml` are still supported): https://docs.docker.com/compose/compose-file/03-compose-file/

This PR adds support for `compose.ya?ml` file to Flex, and changes the name of the generated file to `compose.yaml`, according to Docker best practices.

Closes #992.

Commits
-------

5c98476 feat: support for compose.yml
  • Loading branch information
fabpot committed Oct 22, 2023
2 parents 884a881 + 5c98476 commit 5529871
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
34 changes: 25 additions & 9 deletions src/Configurator/DockerComposeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
use Symfony\Flex\Update\RecipeUpdate;

/**
* Adds services and volumes to docker-compose.yml file.
* Adds services and volumes to compose.yaml file.
*
* @author Kévin Dunglas <[email protected]>
* @author Kévin Dunglas <[email protected]>
*/
class DockerComposeConfigurator extends AbstractConfigurator
{
Expand Down Expand Up @@ -145,9 +145,20 @@ public static function shouldConfigureDockerRecipe(Composer $composer, IOInterfa
*/
private function normalizeConfig(array $config): array
{
foreach ($config as $val) {
// Support for the short syntax recipe syntax that modifies docker-compose.yml only
return isset($val[0]) ? ['docker-compose.yml' => $config] : $config;
foreach ($config as $key => $val) {
// Support for the short recipe syntax that modifies compose.yaml only
if (isset($val[0])) {
return ['compose.yaml' => $config];
}

if (!str_starts_with($key, 'docker-')) {
continue;
}

// If the recipe still use the legacy "docker-compose.yml" names, remove the "docker-" prefix and change the extension
$newKey = pathinfo(substr($key, 7), \PATHINFO_FILENAME).'.yaml';
$config[$newKey] = $val;
unset($config[$key]);
}

return $config;
Expand All @@ -159,11 +170,13 @@ private function normalizeConfig(array $config): array
private function findDockerComposeFile(string $rootDir, string $file): ?string
{
if (isset($_SERVER['COMPOSE_FILE'])) {
$filenameToFind = pathinfo($file, \PATHINFO_FILENAME);
$separator = $_SERVER['COMPOSE_PATH_SEPARATOR'] ?? ('\\' === \DIRECTORY_SEPARATOR ? ';' : ':');

$files = explode($separator, $_SERVER['COMPOSE_FILE']);
foreach ($files as $f) {
if ($file !== basename($f)) {
$filename = pathinfo($f, \PATHINFO_FILENAME);
if ($filename !== $filenameToFind && "docker-$filenameToFind" !== $filename) {
continue;
}

Expand All @@ -180,10 +193,13 @@ private function findDockerComposeFile(string $rootDir, string $file): ?string
// COMPOSE_FILE not set, or doesn't contain the file we're looking for
$dir = $rootDir;
do {
// Test with the ".yaml" extension if the file doesn't end up with ".yml".
if (
$this->filesystem->exists($dockerComposeFile = sprintf('%s/%s', $dir, $file)) ||
$this->filesystem->exists($dockerComposeFile = substr($dockerComposeFile, 0, -2).'aml')
// Test with the ".yml" extension if the file doesn't end up with ".yaml"
$this->filesystem->exists($dockerComposeFile = substr($dockerComposeFile, 0, -3).'ml') ||
// Test with the legacy "docker-" suffix if "compose.ya?ml" doesn't exist
$this->filesystem->exists($dockerComposeFile = sprintf('%s/docker-%s', $dir, $file)) ||
$this->filesystem->exists($dockerComposeFile = substr($dockerComposeFile, 0, -3).'ml')
) {
return $dockerComposeFile;
}
Expand Down Expand Up @@ -359,7 +375,7 @@ private static function askDockerSupport(IOInterface $io, Recipe $recipe): strin
$io->writeError(sprintf(' - <warning> %s </> %s', $warning, $recipe->getFormattedOrigin()));
$question = ' The recipe for this package contains some Docker configuration.
This may create/update <comment>docker-compose.yml</comment> or update <comment>Dockerfile</comment> (if it exists).
This may create/update <comment>compose.yaml</comment> or update <comment>Dockerfile</comment> (if it exists).
Do you want to include Docker configuration from recipes?
[<comment>y</>] Yes
Expand Down
43 changes: 29 additions & 14 deletions tests/Configurator/DockerComposeConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Symfony\Flex\Update\RecipeUpdate;

/**
* @author Kévin Dunglas <[email protected]>
* @author Kévin Dunglas <[email protected]>
*/
class DockerComposeConfiguratorTest extends TestCase
{
Expand Down Expand Up @@ -166,18 +166,33 @@ protected function tearDown(): void
putenv('COMPOSER='.$this->originalEnvComposer);

(new Filesystem())->remove([
FLEX_TEST_DIR.'/compose.yml',
FLEX_TEST_DIR.'/docker-compose.yml',
FLEX_TEST_DIR.'/compose.override.yml',
FLEX_TEST_DIR.'/docker-compose.override.yml',
FLEX_TEST_DIR.'/compose.yaml',
FLEX_TEST_DIR.'/docker-compose.yaml',
FLEX_TEST_DIR.'/composer.json',
FLEX_TEST_DIR.'/child/compose.override.yaml',
FLEX_TEST_DIR.'/child/docker-compose.override.yaml',
FLEX_TEST_DIR.'/child',
]);
}

public function testConfigure()
public static function dockerComposerFileProvider(): iterable
{
$dockerComposeFile = FLEX_TEST_DIR.'/docker-compose.yaml';
yield ['compose.yaml'];
yield ['compose.yml'];
yield ['docker-compose.yaml'];
yield ['docker-compose.yml'];
}

/**
* @dataProvider dockerComposerFileProvider
*/
public function testConfigure(string $fileName)
{
$dockerComposeFile = FLEX_TEST_DIR."/$fileName";
file_put_contents($dockerComposeFile, self::ORIGINAL_CONTENT);

$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);
Expand Down Expand Up @@ -205,7 +220,7 @@ public function testConfigure()
###< doctrine/doctrine-bundle ###

YAML
);
);

$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
$this->assertEquals(self::ORIGINAL_CONTENT, file_get_contents($dockerComposeFile));
Expand All @@ -216,7 +231,7 @@ public function testNotConfiguredIfConfigSet()
$this->package->setExtra(['symfony' => ['docker' => false]]);
$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);

$this->assertFileDoesNotExist(FLEX_TEST_DIR.'/docker-compose.yml');
$this->assertFileDoesNotExist(FLEX_TEST_DIR.'/docker-compose.yaml');
}

/**
Expand All @@ -235,9 +250,9 @@ public function testPreferenceAskedInteractively(string $userInput, bool $expect
$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);

if ($expectedIsConfigured) {
$this->assertFileExists(FLEX_TEST_DIR.'/docker-compose.yml');
$this->assertFileExists(FLEX_TEST_DIR.'/compose.yaml');
} else {
$this->assertFileDoesNotExist(FLEX_TEST_DIR.'/docker-compose.yml');
$this->assertFileDoesNotExist(FLEX_TEST_DIR.'/compose.yaml');
}

$composerJsonData = json_decode(file_get_contents($composerJsonPath), true);
Expand Down Expand Up @@ -270,7 +285,7 @@ public function testEnvVarUsedForDockerConfirmation()
$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);
unset($_SERVER['SYMFONY_DOCKER']);

$this->assertFileExists(FLEX_TEST_DIR.'/docker-compose.yml');
$this->assertFileExists(FLEX_TEST_DIR.'/compose.yaml');

$composerJsonData = json_decode(file_get_contents($composerJsonPath), true);
$this->assertArrayHasKey('extra', $composerJsonData);
Expand Down Expand Up @@ -316,7 +331,7 @@ public function testConfigureFileWithExistingVolumes()
###< doctrine/doctrine-bundle ###

YAML
);
);

$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
// Not the same original, we have an extra breaks line
Expand Down Expand Up @@ -411,7 +426,7 @@ public function testConfigureFileWithExistingMarks()
###< doctrine/doctrine-bundle ###

YAML
);
);

$this->configurator->unconfigure($recipe, $config, $this->lock);
$this->assertEquals($originalContent, file_get_contents($dockerComposeFile));
Expand Down Expand Up @@ -526,7 +541,7 @@ public function testConfigureMultipleFiles()
###< doctrine/doctrine-bundle ###

YAML
);
);
}

$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB_MULTIPLE_FILES, $this->lock);
Expand Down Expand Up @@ -571,7 +586,7 @@ public function testConfigureEnvVar()
###< doctrine/doctrine-bundle ###

YAML
);
);
}

$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB_MULTIPLE_FILES, $this->lock);
Expand Down Expand Up @@ -624,7 +639,7 @@ public function testConfigureFileInParentDir()

public function testConfigureWithoutExistingDockerComposeFiles()
{
$dockerComposeFile = FLEX_TEST_DIR.'/docker-compose.yml';
$dockerComposeFile = FLEX_TEST_DIR.'/compose.yaml';
$defaultContent = "version: '3'\n";

$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);
Expand Down Expand Up @@ -653,7 +668,7 @@ public function testConfigureWithoutExistingDockerComposeFiles()
###< doctrine/doctrine-bundle ###

YAML
);
);

$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
$this->assertEquals(trim($defaultContent), file_get_contents($dockerComposeFile));
Expand Down

0 comments on commit 5529871

Please sign in to comment.