Skip to content

Commit 03e3f83

Browse files
authored
[make:*] improve output messages for Symfony CLI users (#1238)
1 parent c19f411 commit 03e3f83

File tree

9 files changed

+129
-17
lines changed

9 files changed

+129
-17
lines changed

src/Maker/MakeEntity.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Bundle\MakerBundle\Str;
3030
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
3131
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
32+
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
3233
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
3334
use Symfony\Bundle\MakerBundle\Validator;
3435
use Symfony\Component\Console\Command\Command;
@@ -310,7 +311,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
310311

311312
$this->writeSuccessMessage($io);
312313
$io->text([
313-
'Next: When you\'re ready, create a migration with <info>php bin/console make:migration</info>',
314+
sprintf('Next: When you\'re ready, create a migration with <info>%s make:migration</info>', CliOutputHelper::getCommandPrefix()),
314315
'',
315316
]);
316317
}

src/Maker/MakeMigration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bundle\MakerBundle\DependencyBuilder;
2020
use Symfony\Bundle\MakerBundle\Generator;
2121
use Symfony\Bundle\MakerBundle\InputConfiguration;
22+
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
2223
use Symfony\Component\Console\Application;
2324
use Symfony\Component\Console\Command\Command;
2425
use Symfony\Component\Console\Input\ArgvInput;
@@ -121,7 +122,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
121122

122123
$io->text([
123124
sprintf('Next: Review the new migration <info>%s</info>', $migrationName),
124-
'Then: Run the migration with <info>php bin/console doctrine:migrations:migrate</info>',
125+
sprintf('Then: Run the migration with <info>%s doctrine:migrations:migrate</info>', CliOutputHelper::getCommandPrefix()),
125126
'See <fg=yellow>https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html</>',
126127
]);
127128
}

src/Maker/MakeRegistrationForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
3232
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
3333
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
34+
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
3435
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
3536
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
3637
use Symfony\Bundle\MakerBundle\Validator;
@@ -434,7 +435,7 @@ private function successMessage(ConsoleStyle $io, bool $emailVerification, strin
434435
$closing[] = ' * Customize the last <fg=yellow>redirectToRoute()</> after a successful email verification.';
435436
$closing[] = ' * Make sure you\'re rendering <fg=yellow>success</> flash messages or change the <fg=yellow>$this->addFlash()</> line.';
436437
$closing[] = sprintf('%d) Review and customize the form, controller, and templates as needed.', $index++);
437-
$closing[] = sprintf('%d) Run <fg=yellow>"php bin/console make:migration"</> to generate a migration for the newly added <fg=yellow>%s::isVerified</> property.', $index++, $userClass);
438+
$closing[] = sprintf('%d) Run <fg=yellow>"%s make:migration"</> to generate a migration for the newly added <fg=yellow>%s::isVerified</> property.', $index++, CliOutputHelper::getCommandPrefix(), $userClass);
438439
}
439440

440441
$io->text($closing);

src/Maker/MakeResetPassword.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper;
3131
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
3232
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
33+
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
3334
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
3435
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
3536
use Symfony\Bundle\MakerBundle\Validator;
@@ -384,7 +385,7 @@ private function setBundleConfig(ConsoleStyle $io, Generator $generator, string
384385
private function successMessage(InputInterface $input, ConsoleStyle $io, string $requestClassName): void
385386
{
386387
$closing[] = 'Next:';
387-
$closing[] = sprintf(' 1) Run <fg=yellow>"php bin/console make:migration"</> to generate a migration for the new <fg=yellow>"%s"</> entity.', $requestClassName);
388+
$closing[] = sprintf(' 1) Run <fg=yellow>"%s make:migration"</> to generate a migration for the new <fg=yellow>"%s"</> entity.', CliOutputHelper::getCommandPrefix(), $requestClassName);
388389
$closing[] = ' 2) Review forms in <fg=yellow>"src/Form"</> to customize validation and labels.';
389390
$closing[] = ' 3) Review and customize the templates in <fg=yellow>`templates/reset_password`</>.';
390391
$closing[] = ' 4) Make sure your <fg=yellow>MAILER_DSN</> env var has the correct settings.';

src/Test/MakerTestEnvironment.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,16 @@ public function runCommand(string $command): MakerTestProcess
170170
return MakerTestProcess::create($command, $this->path)->run();
171171
}
172172

173-
public function runMaker(array $inputs, string $argumentsString = '', bool $allowedToFail = false): MakerTestProcess
173+
public function runMaker(array $inputs, string $argumentsString = '', bool $allowedToFail = false, array $envVars = []): MakerTestProcess
174174
{
175175
// Let's remove cache
176176
$this->fs->remove($this->path.'/var/cache');
177177

178178
$testProcess = $this->createInteractiveCommandProcess(
179-
$this->testDetails->getMaker()::getCommandName(),
180-
$inputs,
181-
$argumentsString
179+
commandName: $this->testDetails->getMaker()::getCommandName(),
180+
userInputs: $inputs,
181+
argumentsString: $argumentsString,
182+
envVars: $envVars,
182183
);
183184

184185
$this->runnedMakerProcess = $testProcess->run($allowedToFail);
@@ -329,16 +330,16 @@ public function processReplacement(string $rootDir, string $filename, string $fi
329330
file_put_contents($path, str_replace($find, $replace, $contents));
330331
}
331332

332-
public function createInteractiveCommandProcess(string $commandName, array $userInputs, string $argumentsString = ''): MakerTestProcess
333+
public function createInteractiveCommandProcess(string $commandName, array $userInputs, string $argumentsString = '', array $envVars = []): MakerTestProcess
333334
{
335+
$envVars = array_merge(['SHELL_INTERACTIVE' => '1'], $envVars);
336+
334337
// We don't need ansi coloring in tests!
335338
$process = MakerTestProcess::create(
336-
sprintf('php bin/console %s %s --no-ansi', $commandName, $argumentsString),
337-
$this->path,
338-
[
339-
'SHELL_INTERACTIVE' => '1',
340-
],
341-
10
339+
commandLine: sprintf('php bin/console %s %s --no-ansi', $commandName, $argumentsString),
340+
cwd: $this->path,
341+
envVars: $envVars,
342+
timeout: 10
342343
);
343344

344345
if ($userInputs) {

src/Test/MakerTestRunner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function __construct(
3131
$this->filesystem = new Filesystem();
3232
}
3333

34-
public function runMaker(array $inputs, string $argumentsString = '', bool $allowedToFail = false): string
34+
public function runMaker(array $inputs, string $argumentsString = '', bool $allowedToFail = false, array $envVars = []): string
3535
{
36-
$this->executedMakerProcess = $this->environment->runMaker($inputs, $argumentsString, $allowedToFail);
36+
$this->executedMakerProcess = $this->environment->runMaker($inputs, $argumentsString, $allowedToFail, $envVars);
3737

3838
return $this->executedMakerProcess->getOutput();
3939
}

src/Util/CliOutputHelper.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Util;
13+
14+
/**
15+
* Tools used to enhance maker command output.
16+
*
17+
* For additional context with Symfony CLI EnvVars, see
18+
* https://github.com/symfony-cli/symfony-cli/pull/231
19+
*
20+
* @author Jesse Rushlow <[email protected]>
21+
*
22+
* @internal
23+
*/
24+
final class CliOutputHelper
25+
{
26+
/**
27+
* EnvVars exposed by Symfony's CLI.
28+
*/
29+
public const ENV_VERSION = 'SYMFONY_CLI_VERSION'; // Current CLI Version
30+
public const ENV_BIN_NAME = 'SYMFONY_CLI_BINARY_NAME'; // Name of the binary e.g. "symfony"
31+
32+
/**
33+
* Get the correct command prefix based on Symfony CLI usage.
34+
*/
35+
public static function getCommandPrefix(): string
36+
{
37+
$prompt = 'php bin/console';
38+
39+
$binaryNameEnvVar = getenv(self::ENV_BIN_NAME);
40+
41+
if (false !== $binaryNameEnvVar && false !== getenv(self::ENV_VERSION)) {
42+
$prompt = sprintf('%s console', $binaryNameEnvVar);
43+
}
44+
45+
return $prompt;
46+
}
47+
}

tests/Maker/MakeMigrationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
1616
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails;
1717
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;
18+
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
1819
use Symfony\Component\Finder\Finder;
1920

2021
class MakeMigrationTest extends MakerTestCase
@@ -65,6 +66,28 @@ public function getTestDetails(): \Generator
6566
}),
6667
];
6768

69+
yield 'it_detects_symfony_cli_usage' => [$this->createMakeMigrationTest()
70+
->run(function (MakerTestRunner $runner) {
71+
$output = $runner->runMaker(
72+
inputs: [],
73+
envVars: [CliOutputHelper::ENV_VERSION => '0.0.0', CliOutputHelper::ENV_BIN_NAME => 'symfony']
74+
);
75+
76+
$this->assertStringContainsString('symfony console doctrine:migrations:migrate', $output);
77+
}),
78+
];
79+
80+
yield 'it_detects_symfony_cli_is_not_used' => [$this->createMakeMigrationTest()
81+
->run(function (MakerTestRunner $runner) {
82+
$output = $runner->runMaker(
83+
inputs: [],
84+
envVars: []
85+
);
86+
87+
$this->assertStringContainsString('php bin/console doctrine:migrations:migrate', $output);
88+
}),
89+
];
90+
6891
yield 'it_generates_migration_with_no_changes' => [$this->createMakeMigrationTest()
6992
->run(function (MakerTestRunner $runner) {
7093
// sync so there are no changes

tests/Util/CliOutputHelperTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Tests\Util;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
16+
17+
/**
18+
* @author Jesse Rushlow <[email protected]>
19+
*/
20+
class CliOutputHelperTest extends TestCase
21+
{
22+
protected function tearDown(): void
23+
{
24+
putenv('SYMFONY_CLI_BINARY_NAME');
25+
putenv('SYMFONY_CLI_VERSION');
26+
}
27+
28+
public function testCorrectCommandPrefixReturnedWhenUsingSymfonyBinary(): void
29+
{
30+
self::assertSame('php bin/console', CliOutputHelper::getCommandPrefix());
31+
32+
putenv('SYMFONY_CLI_BINARY_NAME=symfony');
33+
putenv('SYMFONY_CLI_VERSION=0.0.0');
34+
35+
self::assertSame('symfony console', CliOutputHelper::getCommandPrefix());
36+
}
37+
}

0 commit comments

Comments
 (0)