Skip to content

Automatically create Docker Compose files when needed #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Configurator/DockerComposeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =

$rootDir = $this->options->get('root-dir');
foreach ($this->normalizeConfig($config) as $file => $extra) {
if (
(null === $dockerComposeFile = $this->findDockerComposeFile($rootDir, $file)) ||
$this->isFileMarked($recipe, $dockerComposeFile)
) {
$dockerComposeFile = $this->findDockerComposeFile($rootDir, $file);
if (null === $dockerComposeFile) {
$dockerComposeFileName = preg_replace('/\.yml$/', '.yaml', $file);
$dockerComposeFile = $rootDir.'/'.$dockerComposeFileName;
file_put_contents($dockerComposeFile, "version: '3'\n");
$this->write(sprintf(' Created <fg=green>"%s"</>', $dockerComposeFileName));
}
if ($this->isFileMarked($recipe, $dockerComposeFile)) {
continue;
}

$this->write(sprintf('Adding Docker Compose definitions to "%s"', $dockerComposeFile));

$offset = 8;
$offset = 2;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default indentation for Docker Compose file is 2 spaces. This was not caught before as tests always used an existing file.

$node = null;
$endAt = [];
$lines = [];
Expand Down
37 changes: 37 additions & 0 deletions tests/Configurator/DockerComposeConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,41 @@ public function testConfigureFileInParentDir()
$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
$this->assertEquals(self::ORIGINAL_CONTENT, file_get_contents($dockerComposeFile));
}

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

$this->configurator->configure($this->recipeDb, self::CONFIG_DB, $this->lock);

$this->assertStringEqualsFile($dockerComposeFile, $defaultContent.<<<'YAML'

services:
###> doctrine/doctrine-bundle ###
db:
image: mariadb:10.3
environment:
- MYSQL_DATABASE=symfony
# You should definitely change the password in production
- MYSQL_PASSWORD=password
- MYSQL_RANDOM_ROOT_PASSWORD=true
- MYSQL_USER=symfony
volumes:
- db-data:/var/lib/mysql:rw
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
# - ./docker/db/data:/var/lib/mysql:rw
###< doctrine/doctrine-bundle ###

volumes:
###> doctrine/doctrine-bundle ###
db-data: {}
###< doctrine/doctrine-bundle ###

YAML
);

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