Skip to content
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

FEATURE: Add reorganize command for Neos 7.2 subfolders #3

Merged
merged 1 commit into from
Oct 8, 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
56 changes: 55 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,61 @@ have the standard naming and others will get names like `NodeTypes.Override.Some

#### Example

./yaml-splitter.phar split --dry-run --package-key MyVendor path/to/MyVendor.NodeTypes.yaml path/to/package
```console
./yaml-splitter.phar split --dry-run --package-key MyVendor path/to/MyVendor.NodeTypes.yaml path/to/package
```


### Reorganize the nodetypes in a Configuration folder into Neos 7.2+ nodetype subfolders

With Neos 7.2 it's possible to organize nodetypes into separate folders than `Configuration`.
It also allows you to use subfolders.

With the following command you can move all `NodeTypes.*.yaml` from a `Configuration` folder into
another folder. They will automatically be put into subfolders based on their naming scheme.

So for example you have the following files in your `Configuration` folder of your site package:

```console
Configuration
├── NodeTypes.Content.Image.yaml
├── NodeTypes.Content.Text.yaml
├── NodeTypes.Document.Abstract.Page.yaml
├── NodeTypes.Document.Home.yaml
├── NodeTypes.Document.Page.yaml
├── NodeTypes.Override.Content.Popup.yaml
├── NodeTypes.Override.Mixin.Document.yaml
└── NodeTypes.Override.Mixin.MarginMixin.yaml

```

Now you run the `reorganize` command:

```console
./yaml-splitter.phar reorganize path/to/sitepackage/Configuration path/to/sitepackage/NodeTypes
```

After you execute the command you will have the following structure:

```console
NodeTypes
├── Content
│ ├── Image.yaml
│ └── Text.yaml
├── Document
│ ├── Abstract
│ │ └── Page.yaml
│ ├── Home.yaml
│ └── Page.yaml
└── Override
├── Content
│ └── Popup.yaml
└── Mixin
├── Document.yaml
└── MarginMixin.yaml
```

**Note:** If you still have multiple nodetypes inside one file, it's recommended to first run the `split` command and then `reorganize`.

## Contributing

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"scripts": {
"compile": "box compile",
"test": "rm -rf ./output/* && ./yaml-splitter.php split --package-key MyVendor.Website examples/NodeTypes.Example.yaml output",
"test-folders": "rm -rf ./output/* && ./yaml-splitter.php split --package-key MyVendor.Website --use-folders examples/NodeTypes.Example.yaml output"
"test-split": "rm -rf ./output/* && ./yaml-splitter.php split --package-key MyVendor.Website examples/split/NodeTypes.Example.yaml output",
"test-split-into-folders": "rm -rf ./output/* && ./yaml-splitter.php split --package-key MyVendor.Website --use-folders examples/split/NodeTypes.Example.yaml output",
"test-reorganize": "rm -rf ./output/* && ./yaml-splitter.php --copy reorganize examples/reorganize output"
}
}
1 change: 1 addition & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function init(InputInterface $input, OutputInterface $output): void
$output->getFormatter()->setStyle('bold', $outputStyle);

$output->writeln([
'',
'==============================================================',
'====**** Neos CMS node type YAML Splitter Console App ****====',
'==============================================================',
Expand Down
135 changes: 135 additions & 0 deletions src/ReorganizeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);

namespace Console;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class ReorganizeCommand extends Command
{

public function configure(): void
{
$this->setName('reorganize')
->setDescription('Reorganizes the nodetype file in a Configuration folder into a subfolder based structure for Neos 7.2+')
->setHelp('Don\'t panic')
->addArgument(
'path',
InputArgument::REQUIRED,
'The path to the YAML files that should be reorganized'
)
->addArgument(
'output-path',
InputArgument::OPTIONAL,
'The path where the output files should be written to',
getcwd()
)
->addOption(
'dry-run',
'd',
InputOption::VALUE_NONE,
'Just simulate the split'
)
->addOption(
'copy',
'c',
InputOption::VALUE_NONE,
'Copy files instead of moving them'
);
}

public function interact(InputInterface $input, OutputInterface $output): void
{
parent::interact($input, $output);

$helper = $this->getHelper('question');

if (!$input->getArgument('path')) {
$question = new Question('Please provide the path to a YAML file with Neos node types: ');
$input->setArgument('path', $helper->ask($input, $output, $question));
}
}

public function execute(InputInterface $input, OutputInterface $output): int
{
$this->init($input, $output);

$path = $input->getArgument('path');
$outputPath = $input->getArgument('output-path');
$dryRun = (bool)$input->getOption('dry-run');
$copy = (bool)$input->getOption('copy');

$output->writeln(sprintf('<info>Selected path: %s</info>', $path));
$output->writeln(sprintf('<info>Output path: %s</info>', $outputPath));
if ($dryRun) {
$output->writeln('<info>Dry run...</info>');
}

$path = rtrim($path, '/');
$outputPath = rtrim($outputPath, '/');

$nodeTypeFiles = glob($path . '/NodeTypes.*.yaml');

$this->reorganizeNodeTypeFiles($nodeTypeFiles, $outputPath, $dryRun, $copy, $output);

return Command::SUCCESS;
}

protected function reorganizeNodeTypeFiles(array $nodeTypeFiles, string $outputPath, bool $dryRun, bool $copy, OutputInterface $output): void
{
foreach ($nodeTypeFiles as $filepath) {
$filename = basename($filepath, '.yaml');
$parts = explode('.', $filename);

// Remove 'NodeTypes' from the beginning
array_shift($parts);

// Build target path where the file will be moved to
$targetPath = $outputPath . '/' . implode('/', $parts) . '.yaml';

if (file_exists(getcwd() . '/' . $targetPath)) {
$output->writeln(sprintf(
'File <bold>%s</bold> already exists, skipping',
$targetPath
));
continue;
}

if ($dryRun) {
$output->writeln(sprintf(
'Would move <bold>%s</bold> to <bold>%s</bold>',
$filepath,
$targetPath
));
} else {
$output->writeln(sprintf(
'Moving <bold>%s</bold> to <bold>%s</bold>',
$filepath,
$targetPath
));

$finalDirectory = dirname($targetPath);

// Generate folders if necessary
/** @noinspection MkdirRaceConditionInspection */
if (!is_dir($finalDirectory) && !mkdir($finalDirectory, 0777, true)) {
$output->writeln(sprintf(
'Could not create directory <bold>%s</bold>',
$finalDirectory
));
continue;
}

if ($copy) {
copy($filepath, $targetPath);
} else {
rename($filepath, $targetPath);
}
}
}
}
}
34 changes: 18 additions & 16 deletions src/SplitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ protected function splitNodeTypesByPackageKey(array $nodeTypes, string $packageK
}

protected function writeNodeTypesToFiles(
string $packageKey,
array $nodeTypes,
string $outputPath,
bool $dryRun,
bool $useFolders,
int $indentation,
string $packageKey,
array $nodeTypes,
string $outputPath,
bool $dryRun,
bool $useFolders,
int $indentation,
OutputInterface $output
): void {
): void
{
foreach ($nodeTypes as $nodeType => $nodeTypeConfig) {
$isAbstract = isset($nodeTypeConfig['abstract']) && $nodeTypeConfig['abstract'];
$filename = $this->generateFileNameFromNodeType($packageKey, $nodeType, $isAbstract, $useFolders);
Expand Down Expand Up @@ -167,25 +168,25 @@ protected function generateFileNameFromNodeType(string $packageKey, string $node
$pathParts = $useFolders ? [] : ['NodeTypes'];

if ($packageKey !== $nodeTypePackageKey) {
$pathParts[]= 'Override';
$pathParts[] = 'Override';
}

// Check for the occurrence of the main types.
// Sometimes NodeTypes are label like My.Vendor:TextMixin, therefore we have to do a string comparison
if (strpos($nodeTypeName, 'Mixin') !== false) {
$pathParts[]= 'Mixin';
$pathParts[] = 'Mixin';
} elseif (strpos($nodeTypeName, 'Document') !== false) {
$pathParts[]= 'Document';
$pathParts[] = 'Document';
} elseif (strpos($nodeTypeName, 'Content') !== false) {
$pathParts[]= 'Content';
$pathParts[] = 'Content';
}

if ($isAbstract) {
$pathParts[]= 'Abstract';
$pathParts[] = 'Abstract';
}

// Add last part of nodetype name as main identifier
$pathParts[]= array_pop($nodeTypeNameParts);
$pathParts[] = array_pop($nodeTypeNameParts);

return implode($useFolders ? '/' : '.', $pathParts) . '.yaml';
}
Expand All @@ -194,9 +195,10 @@ protected function writeNodeTypeToYaml(
string $filename,
string $outputPath,
string $nodeType,
array $nodeTypeConfig,
int $indentation
): bool {
array $nodeTypeConfig,
int $indentation
): bool
{
$finalPath = $outputPath . '/' . $filename;
$finalDirectory = dirname($finalPath);

Expand Down
2 changes: 2 additions & 0 deletions yaml-splitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

require_once __DIR__ . '/vendor/autoload.php';

use Console\ReorganizeCommand;
use Console\SplitCommand;
use Symfony\Component\Console\Application;

$app = new Application();
$app->add(new SplitCommand());
$app->add(new ReorganizeCommand());
$app->run();