Skip to content

Commit

Permalink
Allow configuring the "extra.symfony.root-dir" of the Symfony app
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Dec 11, 2018
1 parent 4a7cd6d commit 186bee1
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/Configurator/AbstractConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
$this->composer = $composer;
$this->io = $io;
$this->options = $options;
$this->path = new Path(getcwd());
$this->path = new Path($options->get('root-dir'));
}

abstract public function configure(Recipe $recipe, $config, array $options = []);
Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/BundlesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ private function dump(string $file, array $bundles)

private function getConfFile(): string
{
return $this->options->expandTargetDir('%CONFIG_DIR%/bundles.php');
return $this->options->get('root-dir').'/'.$this->options->expandTargetDir('%CONFIG_DIR%/bundles.php');
}
}
4 changes: 2 additions & 2 deletions src/Configurator/ContainerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function configure(Recipe $recipe, $parameters, array $options = [])
public function unconfigure(Recipe $recipe, $parameters)
{
$this->write('Unsetting parameters');
$target = $this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
$target = $this->options->get('root-dir').'/'.$this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
$lines = [];
foreach (file($target) as $line) {
foreach (array_keys($parameters) as $key) {
Expand All @@ -42,7 +42,7 @@ public function unconfigure(Recipe $recipe, $parameters)

private function addParameters(array $parameters)
{
$target = $this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
$target = $this->options->get('root-dir').'/'.$this->options->expandTargetDir('%CONFIG_DIR%/services.yaml');
$endAt = 0;
$isParameters = false;
$lines = [];
Expand Down
6 changes: 3 additions & 3 deletions src/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function configure(Recipe $recipe, $config, array $options = [])
{
$this->write('Setting configuration and copying files');
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
$this->copyFiles($config, $packageDir, getcwd(), $options['force'] ?? false);
$this->copyFiles($config, $packageDir, $this->options->get('root-dir'), $options['force'] ?? false);
}

public function unconfigure(Recipe $recipe, $config)
{
$this->write('Removing configuration and files');
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
$this->removeFiles($config, $packageDir, getcwd());
$this->removeFiles($config, $packageDir, $this->options->get('root-dir'));
}

private function copyFiles(array $manifest, string $from, string $to, bool $overwrite = false)
Expand Down Expand Up @@ -97,7 +97,7 @@ public function copyFile(string $source, string $target, bool $overwrite = false
throw new LogicException(sprintf('File "%s" does not exist!', $source));
}

copy($source, $target);
file_put_contents($target, $this->options->expandTargetDir(file_get_contents($source)));
@chmod($target, fileperms($target) | (fileperms($source) & 0111));
$this->write(sprintf('Created <fg=green>"%s"</>', $this->path->relativize($target)));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class CopyFromRecipeConfigurator extends AbstractConfigurator
public function configure(Recipe $recipe, $config, array $options = [])
{
$this->write('Setting configuration and copying files');
$this->copyFiles($config, $recipe->getFiles(), getcwd(), $options['force'] ?? false);
$this->copyFiles($config, $recipe->getFiles(), $this->options->get('root-dir'), $options['force'] ?? false);
}

public function unconfigure(Recipe $recipe, $config)
{
$this->write('Removing configuration and files');
$this->removeFiles($config, $recipe->getFiles(), getcwd());
$this->removeFiles($config, $recipe->getFiles(), $this->options->get('root-dir'));
}

private function copyFiles(array $manifest, array $files, string $to, bool $overwrite = false)
Expand Down Expand Up @@ -62,7 +62,7 @@ private function copyFile(string $to, string $contents, bool $executable, bool $
mkdir(\dirname($to), 0777, true);
}

file_put_contents($to, $contents);
file_put_contents($to, $this->options->expandTargetDir($contents));
if ($executable) {
@chmod($to, fileperms($to) | 0111);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function configure(Recipe $recipe, $vars, array $options = [])
$this->write('Added environment variable defaults');

$this->configureEnvDist($recipe, $vars);
if (!file_exists(getcwd().'/.env.test')) {
if (!file_exists($this->options->get('root-dir').'/.env.test')) {
$this->configurePhpUnit($recipe, $vars);
}
}
Expand All @@ -37,7 +37,7 @@ public function unconfigure(Recipe $recipe, $vars)
private function configureEnvDist(Recipe $recipe, $vars)
{
foreach (['.env.dist', '.env'] as $file) {
$env = getcwd().'/'.$file;
$env = $this->options->get('root-dir').'/'.$file;
if (!is_file($env)) {
continue;
}
Expand Down Expand Up @@ -69,7 +69,7 @@ private function configureEnvDist(Recipe $recipe, $vars)
private function configurePhpUnit(Recipe $recipe, $vars)
{
foreach (['phpunit.xml.dist', 'phpunit.xml'] as $file) {
$phpunit = getcwd().'/'.$file;
$phpunit = $this->options->get('root-dir').'/'.$file;
if (!is_file($phpunit)) {
continue;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ private function configurePhpUnit(Recipe $recipe, $vars)
private function unconfigureEnvFiles(Recipe $recipe, $vars)
{
foreach (['.env', '.env.dist'] as $file) {
$env = getcwd().'/'.$file;
$env = $this->options->get('root-dir').'/'.$file;
if (!file_exists($env)) {
continue;
}
Expand All @@ -128,7 +128,7 @@ private function unconfigureEnvFiles(Recipe $recipe, $vars)
private function unconfigurePhpUnit(Recipe $recipe, $vars)
{
foreach (['phpunit.xml.dist', 'phpunit.xml'] as $file) {
$phpunit = getcwd().'/'.$file;
$phpunit = $this->options->get('root-dir').'/'.$file;
if (!is_file($phpunit)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Configurator/GitignoreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function configure(Recipe $recipe, $vars, array $options = [])
{
$this->write('Added entries to .gitignore');

$gitignore = getcwd().'/.gitignore';
$gitignore = $this->options->get('root-dir').'/.gitignore';
if ($this->isFileMarked($recipe, $gitignore)) {
return;
}
Expand All @@ -37,7 +37,7 @@ public function configure(Recipe $recipe, $vars, array $options = [])

public function unconfigure(Recipe $recipe, $vars)
{
$file = getcwd().'/.gitignore';
$file = $this->options->get('root-dir').'/.gitignore';
if (!file_exists($file)) {
return;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Configurator/MakefileConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ public function configure(Recipe $recipe, $definitions, array $options = [])
{
$this->write('Added Makefile entries');

$makefile = getcwd().'/Makefile';
$makefile = $this->options->get('root-dir').'/Makefile';
if ($this->isFileMarked($recipe, $makefile)) {
return;
}

$data = $this->markData($recipe, implode("\n", $definitions));
$data = $this->options->expandTargetDir(implode("\n", $definitions));
$data = $this->markData($recipe, $data);

if (!file_exists($makefile)) {
file_put_contents(
getcwd().'/Makefile',
$this->options->get('root-dir').'/Makefile',
<<<EOF
ifndef APP_ENV
include .env
Expand All @@ -45,12 +46,12 @@ public function configure(Recipe $recipe, $definitions, array $options = [])
EOF
);
}
file_put_contents(getcwd().'/Makefile', "\n".ltrim($data, "\r\n"), FILE_APPEND);
file_put_contents($this->options->get('root-dir').'/Makefile', "\n".ltrim($data, "\r\n"), FILE_APPEND);
}

public function unconfigure(Recipe $recipe, $vars)
{
if (!file_exists($makefile = getcwd().'/Makefile')) {
if (!file_exists($makefile = $this->options->get('root-dir').'/Makefile')) {
return;
}

Expand Down
11 changes: 7 additions & 4 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public function update(Event $event, $operations = [])
if ($operations) {
$this->operations = $operations;
}
$cwd = getcwd();
$rootDir = $this->options->get('root-dir');

if (!file_exists("$cwd/.env") && !file_exists("$cwd/.env.local") && file_exists("$cwd/.env.dist") && false === strpos(file_get_contents("$cwd/.env.dist"), '.env.local')) {
copy(getcwd().'/.env.dist', getcwd().'/.env');
if (!file_exists("$rootDir/.env") && !file_exists("$rootDir/.env.local") && file_exists("$rootDir/.env.dist") && false === strpos(file_get_contents("$rootDir/.env.dist"), '.env.local')) {
copy($rootDir.'/.env.dist', $rootDir.'/.env');
}

list($recipes, $vulnerabilities) = $this->fetchRecipes();
Expand Down Expand Up @@ -629,14 +629,17 @@ private function fetchRecipes(): array

private function initOptions(): Options
{
$extra = $this->composer->getPackage()->getExtra();

$options = array_merge([
'bin-dir' => 'bin',
'conf-dir' => 'conf',
'config-dir' => 'config',
'src-dir' => 'src',
'var-dir' => 'var',
'public-dir' => 'public',
], $this->composer->getPackage()->getExtra());
'root-dir' => $extra['symfony']['root-dir'] ?? '.',
], $extra);

return new Options($options, $this->io);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ScriptExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function expandSymfonyCmd(string $cmd)
return null;
}

$console = ProcessExecutor::escape($this->options->get('bin-dir').'/console');
$console = ProcessExecutor::escape($this->options->get('root-dir').'/'.$this->options->get('bin-dir').'/console');
if ($this->io->isDecorated()) {
$console .= ' --ansi';
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Configurator/BundlesConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class BundlesConfiguratorTest extends TestCase
{
public function testConfigure()
{
$config = getcwd().'/config/bundles.php';
$config = FLEX_TEST_DIR.'/config/bundles.php';

$configurator = new BundlesConfigurator(
$this->getMockBuilder('Composer\Composer')->getMock(),
$this->getMockBuilder('Composer\IO\IOInterface')->getMock(),
new Options(['config-dir' => \dirname($config)])
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);

$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
Expand Down
16 changes: 8 additions & 8 deletions tests/Configurator/ContainerConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ContainerConfiguratorTest extends TestCase
public function testConfigure()
{
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$config = getcwd().'/config/services.yaml';
$config = FLEX_TEST_DIR.'/config/services.yaml';
file_put_contents(
$config,
<<<EOF
Expand All @@ -37,7 +37,7 @@ public function testConfigure()
$configurator = new ContainerConfigurator(
$this->getMockBuilder(Composer::class)->getMock(),
$this->getMockBuilder(IOInterface::class)->getMock(),
new Options(['config-dir' => \dirname($config)])
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);
$configurator->configure($recipe, ['locale' => 'en']);
$this->assertEquals(<<<EOF
Expand All @@ -64,7 +64,7 @@ public function testConfigure()
public function testConfigureWithoutParametersKey()
{
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$config = getcwd().'/config/services.yaml';
$config = FLEX_TEST_DIR.'/config/services.yaml';
file_put_contents(
$config,
<<<EOF
Expand All @@ -75,7 +75,7 @@ public function testConfigureWithoutParametersKey()
$configurator = new ContainerConfigurator(
$this->getMockBuilder(Composer::class)->getMock(),
$this->getMockBuilder(IOInterface::class)->getMock(),
new Options(['config-dir' => \dirname($config)])
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);
$configurator->configure($recipe, ['locale' => 'en']);
$this->assertEquals(<<<EOF
Expand All @@ -100,7 +100,7 @@ public function testConfigureWithoutParametersKey()
public function testConfigureWithoutDuplicated()
{
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$config = getcwd().'/config/services.yaml';
$config = FLEX_TEST_DIR.'/config/services.yaml';
file_put_contents(
$config,
<<<EOF
Expand All @@ -114,7 +114,7 @@ public function testConfigureWithoutDuplicated()
$configurator = new ContainerConfigurator(
$this->getMockBuilder(Composer::class)->getMock(),
$this->getMockBuilder(IOInterface::class)->getMock(),
new Options(['config-dir' => \dirname($config)])
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);
$configurator->configure($recipe, ['locale' => 'en']);
$this->assertEquals(<<<EOF
Expand All @@ -139,7 +139,7 @@ public function testConfigureWithoutDuplicated()
public function testConfigureWithComplexContent()
{
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$config = getcwd().'/config/services.yaml';
$config = FLEX_TEST_DIR.'/config/services.yaml';
file_put_contents(
$config,
<<<EOF
Expand All @@ -157,7 +157,7 @@ public function testConfigureWithComplexContent()
$configurator = new ContainerConfigurator(
$this->getMockBuilder(Composer::class)->getMock(),
$this->getMockBuilder(IOInterface::class)->getMock(),
new Options(['config-dir' => \dirname($config)])
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);
$configurator->configure($recipe, ['locale' => 'en', 'foobar' => 'baz']);
$this->assertEquals(<<<EOF
Expand Down
12 changes: 6 additions & 6 deletions tests/Configurator/CopyDirectoryFromPackageConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ protected function setUp()
{
parent::setUp();

$this->sourceDirectory = getcwd().'/package/files';
$this->sourceDirectory = FLEX_TEST_DIR.'/package/files';
$this->sourceFileRelativePath = 'package/files/';
$this->sourceFiles = [
$this->sourceDirectory.'/file1',
$this->sourceDirectory.'/file2',
];

$this->targetDirectory = getcwd().'/public/files';
$this->targetDirectory = FLEX_TEST_DIR.'/public/files';
$this->targetFileRelativePath = 'public/files/';
$this->targetFiles = [
$this->targetDirectory.'/file1',
Expand All @@ -79,7 +79,7 @@ protected function setUp()
$installationManager->expects($this->exactly(1))
->method('getInstallPath')
->with($package)
->willReturn(getcwd())
->willReturn(FLEX_TEST_DIR)
;
$this->composer = $this->getMockBuilder(Composer::class)->getMock();
$this->composer->expects($this->exactly(1))
Expand All @@ -102,13 +102,13 @@ protected function tearDown()

private function createConfigurator(): CopyFromPackageConfigurator
{
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options());
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR]));
}

private function cleanUpTargetFiles()
{
$this->rrmdir(getcwd().'/package');
$this->rrmdir(getcwd().'/public');
$this->rrmdir(FLEX_TEST_DIR.'/package');
$this->rrmdir(FLEX_TEST_DIR.'/public');
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/Configurator/CopyFromPackageConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ protected function setUp()
{
parent::setUp();

$this->sourceDirectory = getcwd().'/package';
$this->sourceDirectory = FLEX_TEST_DIR.'/package';
$this->sourceFileRelativePath = 'package/file';
$this->sourceFile = $this->sourceDirectory.'/file';

$this->targetDirectory = getcwd().'/public';
$this->targetDirectory = FLEX_TEST_DIR.'/public';
$this->targetFileRelativePath = 'public/file';
$this->targetFile = $this->targetDirectory.'/file';

Expand All @@ -137,7 +137,7 @@ protected function setUp()
$installationManager->expects($this->exactly(1))
->method('getInstallPath')
->with($package)
->willReturn(getcwd())
->willReturn(FLEX_TEST_DIR)
;
$this->composer = $this->getMockBuilder(Composer::class)->getMock();
$this->composer->expects($this->exactly(1))
Expand All @@ -158,13 +158,13 @@ protected function tearDown()

private function createConfigurator(): CopyFromPackageConfigurator
{
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options([], $this->io));
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
}

private function cleanUpTargetFiles()
{
@unlink($this->targetFile);
@rmdir(getcwd().'/package');
@rmdir(getcwd().'/public');
@rmdir(FLEX_TEST_DIR.'/package');
@rmdir(FLEX_TEST_DIR.'/public');
}
}
Loading

0 comments on commit 186bee1

Please sign in to comment.