Skip to content

Commit

Permalink
[make:migration] Add link to new migration files (#1253)
Browse files Browse the repository at this point in the history
* Add link to new migration files

* fix msg output assertion

Co-authored-by: Jesse Rushlow <jr@rushlow.dev>
  • Loading branch information
nicolas-grekas and jrushlow authored Dec 12, 2022
1 parent 16f26e4 commit 717e12d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/Maker/MakeMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
Expand All @@ -34,8 +35,10 @@ final class MakeMigration extends AbstractMaker implements ApplicationAwareMaker
{
private Application $application;

public function __construct(private string $projectDir)
{
public function __construct(
private string $projectDir,
private ?MakerFileLinkFormatter $makerFileLinkFormatter = null,
) {
}

public static function getCommandName(): string
Expand Down Expand Up @@ -116,13 +119,15 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
return;
}

$this->writeSuccessMessage($io);
$absolutePath = $this->getGeneratedMigrationFilename($migrationOutput);
$relativePath = str_replace($this->projectDir.'/', '', $absolutePath);

$migrationName = $this->getGeneratedMigrationFilename($migrationOutput);
$io->comment('<fg=blue>created</>: '.($this->makerFileLinkFormatter?->makeLinkedPath($absolutePath, $relativePath) ?? $relativePath));

$this->writeSuccessMessage($io);

$io->text([
sprintf('Next: Review the new migration <info>%s</info>', $migrationName),
sprintf('Then: Run the migration with <info>%s doctrine:migrations:migrate</info>', CliOutputHelper::getCommandPrefix()),
sprintf('Review the new migration then run it with <info>%s doctrine:migrations:migrate</info>', CliOutputHelper::getCommandPrefix()),
'See <fg=yellow>https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html</>',
]);
}
Expand All @@ -148,12 +153,12 @@ public function configureDependencies(DependencyBuilder $dependencies)

private function getGeneratedMigrationFilename(string $migrationOutput): string
{
preg_match('#"(.*?)"#', $migrationOutput, $matches);
preg_match('#"<info>(.*?)</info>"#', $migrationOutput, $matches);

if (!isset($matches[0])) {
if (!isset($matches[1])) {
throw new \Exception('Your migration generated successfully, but an error occurred printing the summary of what occurred.');
}

return str_replace($this->projectDir.'/', '', $matches[0]);
return $matches[1];
}
}
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@

<service id="maker.maker.make_migration" class="Symfony\Bundle\MakerBundle\Maker\MakeMigration">
<argument>%kernel.project_dir%</argument>
<argument type="service" id="maker.file_link_formatter" />
<tag name="maker.command" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion tests/Maker/MakeMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getTestDetails(): \Generator
// see that the exact filename is in the output
$iterator = $finder->getIterator();
$iterator->rewind();
$this->assertStringContainsString(sprintf('"%s/%s"', $migrationsDirectoryPath, $iterator->current()->getFilename()), $output);
$this->assertStringContainsString(sprintf('%s/%s', $migrationsDirectoryPath, $iterator->current()->getFilename()), $output);
}),
];

Expand Down

0 comments on commit 717e12d

Please sign in to comment.