Skip to content

Commit b446619

Browse files
author
Anton
authored
Merge pull request #37 from bluzphp/develop
Added `--force` option to file generators
2 parents 6e16590 + fad4f3a commit b446619

9 files changed

Lines changed: 43 additions & 15 deletions

File tree

bin/bluzman

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,23 @@ ini_set('display_errors', 1);
3535
ini_set('log_errors', 0);
3636
ini_set('html_errors', 0);
3737

38-
// Get project info from composer.json
39-
$composerConfig = json_decode(
40-
file_get_contents(PATH_VENDOR .DS. 'bluzphp' .DS. 'bluzman' .DS. 'composer.json'),
41-
true
42-
);
43-
require_once PATH_VENDOR . '/autoload.php';
44-
45-
$application = new Bluzman\Application\Application(
46-
$composerConfig['description'],
47-
$composerConfig['version'] ?? 'dev'
48-
);
49-
$application->init();
50-
$application->run();
38+
try {
39+
// Get project info from composer.json
40+
$composerConfig = json_decode(
41+
file_get_contents(PATH_VENDOR . DS . 'bluzphp' . DS . 'bluzman' . DS . 'composer.json'),
42+
true
43+
);
44+
require_once PATH_VENDOR . '/autoload.php';
45+
46+
$application = new Bluzman\Application\Application(
47+
$composerConfig['description'],
48+
$composerConfig['version'] ?? 'dev'
49+
);
50+
$application->init();
51+
$application->run();
52+
} catch (\Throwable $e) {
53+
echo "Error #{$e->getCode()}:\n\n";
54+
echo "\t{$e->getFile()}:{$e->getLine()}\n";
55+
echo "\t{$e->getMessage()}\n";
56+
echo "\n";
57+
}

src/Command/Generate/AbstractGenerateCommand.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Bluzman\Generator\GeneratorException;
1515
use Bluzman\Input\InputArgument;
1616
use Bluzman\Input\InputException;
17+
use Bluzman\Input\InputOption;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920

@@ -31,6 +32,17 @@ abstract class AbstractGenerateCommand extends AbstractCommand
3132
*/
3233
abstract public function verify(InputInterface $input, OutputInterface $output) : void;
3334

35+
/**
36+
* Add Force Option
37+
*
38+
* @return void
39+
*/
40+
protected function addForceOption() : void
41+
{
42+
$force = new InputOption('--force', '-f', InputOption::VALUE_NONE, 'Rewrite previously generated files');
43+
$this->getDefinition()->addOption($force);
44+
}
45+
3446
/**
3547
* Add Model Argument
3648
*
@@ -142,8 +154,9 @@ protected function getTemplate($class)
142154
*/
143155
protected function generateFile($class, $file, array $data = []) : void
144156
{
145-
if (file_exists($file)) {
157+
if (file_exists($file) && !$this->getInput()->getOption('force')) {
146158
$this->comment(" |> File <info>$file</info> already exists");
159+
return;
147160
}
148161

149162
$template = $this->getTemplate($class);

src/Command/Generate/ControllerCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected function configure()
3434

3535
$this->addModuleArgument();
3636
$this->addControllerArgument();
37+
$this->addForceOption();
3738
}
3839

3940
/**

src/Command/Generate/CrudCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ protected function configure()
3131
->setDescription('Generate a CRUD for model')
3232
// the full command description shown when running the command with
3333
// the "--help" option
34-
->setHelp('This command allows you to generate CRUD files');
34+
->setHelp('This command allows you to generate CRUD files')
35+
;
3536

3637
$this->addModelArgument();
3738
$this->addModuleArgument(InputArgument::OPTIONAL);
39+
$this->addForceOption();
3840
}
3941

4042
/**

src/Command/Generate/GridCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function configure()
3636

3737
$this->addModelArgument();
3838
$this->addModuleArgument(InputArgument::OPTIONAL);
39+
$this->addForceOption();
3940
}
4041

4142
/**

src/Command/Generate/ModelCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535

3636
$this->addModelArgument();
3737
$this->addTableArgument();
38+
$this->addForceOption();
3839
}
3940

4041
/**

src/Command/Generate/ModuleCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function configure()
3333
;
3434

3535
$this->addModuleArgument();
36+
$this->addForceOption();
3637
}
3738

3839
/**

src/Command/Generate/RestCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function configure()
3535

3636
$this->addModelArgument();
3737
$this->addModuleArgument();
38+
$this->addForceOption();
3839
}
3940

4041
/**

src/Command/Generate/ScaffoldCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function configure()
3636
$this->addModelArgument();
3737
$this->addTableArgument();
3838
$this->addModuleArgument();
39+
$this->addForceOption();
3940
}
4041

4142
/**

0 commit comments

Comments
 (0)