Skip to content

Commit

Permalink
Properly trim directory separators from theme names
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGallinari committed May 23, 2022
1 parent f28aa9b commit 61d4610
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Command/TemplateChangesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Webgriffe\SyliusUpgradePlugin\Command;

use const DIRECTORY_SEPARATOR;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,7 +23,7 @@ final class TemplateChangesCommand extends Command

public const LEGACY_MODE_OPTION_NAME = 'legacy';

private const TEMPLATES_BUNDLES_SUBDIR = 'templates' . \DIRECTORY_SEPARATOR . 'bundles' . \DIRECTORY_SEPARATOR;
private const TEMPLATES_BUNDLES_SUBDIR = 'templates' . DIRECTORY_SEPARATOR . 'bundles' . DIRECTORY_SEPARATOR;

protected static $defaultName = 'webgriffe:upgrade:template-changes';

Expand All @@ -46,7 +47,7 @@ public function __construct(GitInterface $gitClient, string $rootPath, string $n
parent::__construct($name);

$this->gitClient = $gitClient;
$this->rootPath = rtrim($rootPath, \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR;
$this->rootPath = rtrim($rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}

protected function configure(): void
Expand Down Expand Up @@ -132,7 +133,7 @@ private function getFilesChangedBetweenTwoVersions(): array
}
$diffLineParts = explode(' ', $diffLine);
$changedFileName = substr($diffLineParts[2], 2);
if (strpos($changedFileName, 'Resources' . \DIRECTORY_SEPARATOR . 'views') === false) {
if (strpos($changedFileName, 'Resources' . DIRECTORY_SEPARATOR . 'views') === false) {
continue;
}
$versionChangedFiles[] = $changedFileName;
Expand All @@ -143,8 +144,8 @@ private function getFilesChangedBetweenTwoVersions(): array
static function (string $versionChangedFile): string {
return str_replace(
[
'src' . \DIRECTORY_SEPARATOR . 'Sylius' . \DIRECTORY_SEPARATOR . 'Bundle' . \DIRECTORY_SEPARATOR,
\DIRECTORY_SEPARATOR . 'Resources' . \DIRECTORY_SEPARATOR . 'views',
'src' . DIRECTORY_SEPARATOR . 'Sylius' . DIRECTORY_SEPARATOR . 'Bundle' . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views',
],
[
'Sylius',
Expand Down Expand Up @@ -187,7 +188,7 @@ private function computeTemplateFilesChangedAndOverridden(array $versionChangedF
*/
private function getProjectTemplatesFiles(string $targetDir): array
{
$files = Glob::glob($targetDir . 'Sylius*Bundle' . \DIRECTORY_SEPARATOR . '**' . \DIRECTORY_SEPARATOR . '*.html.twig');
$files = Glob::glob($targetDir . 'Sylius*Bundle' . DIRECTORY_SEPARATOR . '**' . DIRECTORY_SEPARATOR . '*.html.twig');

// from /path_to_project/templates/bundles/SyliusAdminBundle/PaymentMethod/_form.html.twig
// to SyliusAdminBundle/PaymentMethod/_form.html.twig
Expand All @@ -201,7 +202,7 @@ static function (string $file) use ($targetDir): string {

private function computeThemeTemplateFilesChangedAndOverridden(array $versionChangedFiles, string $themeName, bool $legacyMode): void
{
$targetDir = $this->rootPath . $themeName . \DIRECTORY_SEPARATOR;
$targetDir = $this->rootPath . rtrim($themeName, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if (!$legacyMode) {
$targetDir .= self::TEMPLATES_BUNDLES_SUBDIR;
}
Expand Down Expand Up @@ -238,13 +239,13 @@ private function computeThemeTemplateFilesChangedAndOverridden(array $versionCha
*/
private function getProjectLegacyThemeFiles(string $targetDir): array
{
$files = Glob::glob($targetDir . 'Sylius*Bundle' . \DIRECTORY_SEPARATOR . '**' . \DIRECTORY_SEPARATOR . '*.html.twig');
$files = Glob::glob($targetDir . 'Sylius*Bundle' . DIRECTORY_SEPARATOR . '**' . DIRECTORY_SEPARATOR . '*.html.twig');

// from /path_to_project/themes/my-theme/SyliusAdminBundle/views/PaymentMethod/_form.html.twig
// to SyliusAdminBundle/PaymentMethod/_form.html.twig
return array_map(
static function (string $file) use ($targetDir): string {
return str_replace([$targetDir, \DIRECTORY_SEPARATOR . 'views' . \DIRECTORY_SEPARATOR], ['', \DIRECTORY_SEPARATOR], $file);
return str_replace([$targetDir, DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR], ['', DIRECTORY_SEPARATOR], $file);
},
$files
);
Expand Down

0 comments on commit 61d4610

Please sign in to comment.