Skip to content

Instantly share code, notes, and snippets.

@Taluu
Last active October 9, 2019 07:27
Show Gist options
  • Save Taluu/2d9f0d6a52ba6f53513c4e53651e4152 to your computer and use it in GitHub Desktop.
Save Taluu/2d9f0d6a52ba6f53513c4e53651e4152 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
<<<CONFIG
packages:
- "symfony/console": "^4.3"
- "symfony/finder: ^4.3"
- "symfony/filesystem: ^4.3"
- "symfony/yaml": "^4.3"
CONFIG;
use Symfony\Component\Console\Application;
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\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Yaml;
(new Application('redump-yaml', '1.0.0'))
->register('redump-yaml')
->addArgument('directories', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Directory where to look for yaml stuff')
->addOption('inline', null, InputOption::VALUE_REQUIRED, 'From which level it should be inlined', 3)
->addOption('indent', null, InputOption::VALUE_REQUIRED, 'What indent should be used', 4)
->setCode(function (InputInterface $input, OutputInterface $output) {
$parser = new Parser;
$dumper = new Dumper($input->getOption('indent'));
$finder = new Finder;
$filesystem = new Filesystem;
$files = $finder
->files()
->ignoreDotFiles(true)
->name('*.yml')
->name('*.yaml')
;
foreach ($input->getArgument('directories') as $dir) {
$files = $files->in($dir);
}
/** @var SplFileInfo $file */
foreach ($files as $file) {
$yaml = $parser->parseFile($file->getRealPath(), Yaml::PARSE_CUSTOM_TAGS);
if (!is_countable($yaml)) {
continue;
}
$dump = $dumper->dump(
$yaml,
$input->getOption('inline'),
0,
Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK|Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE
);
$filesystem->dumpFile($file->getRealPath(), $dump);
}
})
->getApplication()
->setDefaultCommand('redump-yaml', true)
->run()
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment