bencagri /
artisan-deployer
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace Bencagri\Artisan\Deployer\Command; |
||
| 5 | |||
| 6 | use Bencagri\Artisan\Deployer\Context; |
||
| 7 | use Bencagri\Artisan\Deployer\Helper\ConfigPathGuesser; |
||
| 8 | use Illuminate\Console\Command; |
||
| 9 | use Symfony\Component\Console\Input\InputArgument; |
||
| 10 | use Symfony\Component\Console\Input\InputInterface; |
||
| 11 | use Symfony\Component\Console\Input\InputOption; |
||
| 12 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 13 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
| 14 | use Symfony\Component\Filesystem\Filesystem; |
||
| 15 | |||
| 16 | class DeployCommand extends Command |
||
| 17 | { |
||
| 18 | |||
| 19 | private $projectDir; |
||
| 20 | private $logDir; |
||
| 21 | private $configFilePath; |
||
| 22 | |||
| 23 | public function __construct() |
||
| 24 | { |
||
| 25 | $this->projectDir = realpath(base_path()); |
||
| 26 | $this->logDir = base_path('storage/logs'); |
||
| 27 | parent::__construct(); |
||
| 28 | } |
||
| 29 | |||
| 30 | protected function configure() |
||
| 31 | { |
||
| 32 | $this |
||
| 33 | ->setName('deploy') |
||
| 34 | ->setDescription('Deploys a Laravel application to one or more remote servers.') |
||
| 35 | ->setHelp('...') |
||
| 36 | ->addArgument('stage', InputArgument::OPTIONAL, 'The stage to deploy to ("production", "staging", etc.)', 'prod') |
||
| 37 | ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Load configuration from the given file path') |
||
| 38 | ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Shows the commands to perform the deployment without actually executing them') |
||
| 39 | ; |
||
| 40 | } |
||
| 41 | |||
| 42 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 43 | { |
||
| 44 | $customConfigPath = $input->getOption('configuration'); |
||
| 45 | if (null !== $customConfigPath && !is_readable($customConfigPath)) { |
||
| 46 | throw new \RuntimeException(sprintf("The given configuration file ('%s') does not exist or it's not readable.", $customConfigPath)); |
||
| 47 | } |
||
| 48 | |||
| 49 | if (null !== $customConfigPath && is_readable($customConfigPath)) { |
||
| 50 | return $this->configFilePath = $customConfigPath; |
||
| 51 | } |
||
| 52 | |||
| 53 | $defaultConfigPath = ConfigPathGuesser::guess($this->projectDir, $input->getArgument('stage')); |
||
| 54 | if (is_readable($defaultConfigPath)) { |
||
| 55 | return $this->configFilePath = $defaultConfigPath; |
||
| 56 | } |
||
| 57 | |||
| 58 | $this->createDefaultConfigFile($input, $output, $defaultConfigPath, $input->getArgument('stage')); |
||
| 59 | } |
||
| 60 | |||
| 61 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 62 | { |
||
| 63 | $logFilePath = sprintf('%s/deploy_%s.log', $this->logDir, $input->getArgument('stage')); |
||
| 64 | $context = new Context($input, $output, $this->projectDir, $logFilePath, true === $input->getOption('dry-run'), $output->isVerbose()); |
||
| 65 | |||
| 66 | $deployer = include $this->configFilePath; |
||
| 67 | $deployer->initialize($context); |
||
| 68 | $deployer->doDeploy(); |
||
| 69 | } |
||
| 70 | |||
| 71 | private function createDefaultConfigFile(InputInterface $input, OutputInterface $output, string $defaultConfigPath, string $stageName) : void |
||
| 72 | { |
||
| 73 | $helper = $this->getHelper('question'); |
||
| 74 | $question = new ConfirmationQuestion(sprintf("\n<bg=yellow> WARNING </> There is no config file to deploy '%s' stage.\nDo you want to create a minimal config file for it? [Y/n] ", $stageName), true); |
||
| 75 | |||
| 76 | if (!$helper->ask($input, $output, $question)) { |
||
| 77 | $output->writeln(sprintf('<fg=green>OK</>, but before running this command again, create this config file: %s', $defaultConfigPath)); |
||
| 78 | } else { |
||
| 79 | (new Filesystem())->copy(__DIR__ . '/../../config/deploy.php.dist', $defaultConfigPath); |
||
| 80 | $output->writeln(sprintf('<fg=green>OK</>, now edit the "%s" config file and run this command again.', $defaultConfigPath)); |
||
| 81 | } |
||
| 82 | |||
| 83 | exit(0); |
||
|
0 ignored issues
–
show
|
|||
| 84 | } |
||
| 85 | } |
||
| 86 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.