Skip to content

Commit 34e8a4e

Browse files
committed
Improve errors when invalid arguments. Add a default config file name.
1 parent 7e8c466 commit 34e8a4e

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/Command/GenerateTypesCommand.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
final class GenerateTypesCommand extends Command
3535
{
36+
private const DEFAULT_CONFIG_FILE = 'schema.yaml';
37+
3638
/**
3739
* {@inheritdoc}
3840
*/
@@ -42,27 +44,60 @@ protected function configure(): void
4244
->setName('generate-types')
4345
->setDescription('Generate types')
4446
->addArgument('output', InputArgument::REQUIRED, 'The output directory')
45-
->addArgument('config', InputArgument::OPTIONAL, 'The config file to use');
47+
->addArgument('config', InputArgument::OPTIONAL, 'The config file to use (default to "schema.yaml" in the current directory, will generate all types if no config file exists)');
4648
}
4749

4850
/**
4951
* {@inheritdoc}
5052
*/
5153
protected function execute(InputInterface $input, OutputInterface $output): void
5254
{
55+
$outputDir = $input->getArgument('output');
56+
if ($dir = realpath($input->getArgument('output'))) {
57+
if (!is_dir($dir)) {
58+
throw new \InvalidArgumentException(sprintf('The file "%s" is not a directory.', $dir));
59+
}
60+
61+
if (!is_writable($dir)) {
62+
throw new \InvalidArgumentException(sprintf('The "%s" directory is not writable.', $dir));
63+
}
64+
65+
$outputDir = $dir;
66+
} elseif (!@mkdir($outputDir, 0777, true)) {
67+
throw new \InvalidArgumentException(sprintf('Cannot create the "%s" directory. Check that the parent directory is writable.', $outputDir));
68+
} else {
69+
$outputDir = realpath($outputDir);
70+
}
71+
5372
$configArgument = $input->getArgument('config');
5473
if ($configArgument) {
74+
if (!file_exists($configArgument)) {
75+
throw new \InvalidArgumentException(sprintf('The file "%s" doesn\'t exist.', $configArgument));
76+
}
77+
78+
if (!is_file($configArgument)) {
79+
throw new \InvalidArgumentException(sprintf('"%s" isn\'t a file.', $configArgument));
80+
}
81+
82+
if (!is_readable($configArgument)) {
83+
throw new \InvalidArgumentException(sprintf('The file "%s" isn\'t readable.', $configArgument));
84+
}
85+
5586
$parser = new Parser();
5687
$config = $parser->parse(file_get_contents($configArgument));
5788
unset($parser);
89+
} elseif (is_readable(self::DEFAULT_CONFIG_FILE)) {
90+
$parser = new Parser();
91+
$config = $parser->parse(file_get_contents(self::DEFAULT_CONFIG_FILE));
92+
unset($parser);
5893
} else {
5994
$config = [];
6095
}
6196

6297
$processor = new Processor();
6398
$configuration = new TypesGeneratorConfiguration();
6499
$processedConfiguration = $processor->processConfiguration($configuration, [$config]);
65-
$processedConfiguration['output'] = realpath($input->getArgument('output'));
100+
$processedConfiguration['output'] = $outputDir;
66101
if (!$processedConfiguration['output']) {
67102
throw new \RuntimeException('The specified output is invalid');
68103
}

0 commit comments

Comments
 (0)