diff --git a/src/Console/Deployer.php b/src/Console/Deployer.php index fbdccbf21..e58b0de46 100644 --- a/src/Console/Deployer.php +++ b/src/Console/Deployer.php @@ -5,6 +5,7 @@ use Joli\JoliNotif\Notification; use Joli\JoliNotif\NotifierFactory; +use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; @@ -26,7 +27,13 @@ public function __construct() */ public function invoke(SymfonyStyle $io, string $function, ?string $data, bool $raw) : string { - $this->generateArchive($io); + $progress = $this->createProgressBar($io, 7); + + $this->generateArchive($io, $progress); + + $progress->setMessage('Invoking the lambda'); + $progress->display(); + $progress->finish(); $parameters = array_filter([ '-f' => $function, @@ -53,11 +60,17 @@ function ($value, $key) { public function deploy(SymfonyStyle $io) : void { - $this->generateArchive($io); + $progress = $this->createProgressBar($io, 8); - $io->writeln('Uploading the lambda'); + $this->generateArchive($io, $progress); + + $progress->setMessage('Uploading the lambda'); + $progress->display(); $this->runLocally('serverless deploy'); + $progress->setMessage('Deployment success'); + $progress->finish(); + // Trigger a desktop notification $notifier = NotifierFactory::create(); $notification = (new Notification) @@ -66,7 +79,11 @@ public function deploy(SymfonyStyle $io) : void $notifier->send($notification); } - private function generateArchive(SymfonyStyle $io) : void + /** + * @param ProgressBar $progress The progress bar will advance of 7 steps. + * @throws \Exception + */ + private function generateArchive(SymfonyStyle $io, ProgressBar $progress) : void { if (!$this->fs->exists('serverless.yml') || !$this->fs->exists('bref.php')) { throw new \Exception('The files `bref.php` and `serverless.yml` are required to deploy, run `bref init` to create them'); @@ -75,6 +92,8 @@ private function generateArchive(SymfonyStyle $io) : void // Parse .bref.yml $projectConfig = []; if ($this->fs->exists('.bref.yml')) { + $progress->setMessage('Reading `.bref.yml`'); + $progress->display(); /* * TODO validate the content of the config, for example we should * error if there are unknown keys. Using the Symfony Config component @@ -82,8 +101,10 @@ private function generateArchive(SymfonyStyle $io) : void */ $projectConfig = Yaml::parse(file_get_contents('.bref.yml')); } + $progress->advance(); - $io->writeln('Building the project in the `.bref/output` directory'); + $progress->setMessage('Building the project in the `.bref/output` directory'); + $progress->display(); /* * TODO Mirror the directory instead of recreating it from scratch every time * Blocked by https://github.com/symfony/symfony/pull/26399 @@ -106,6 +127,7 @@ private function generateArchive(SymfonyStyle $io) : void ]); } } + $progress->advance(); // Cache PHP's binary in `.bref/bin/php` to avoid downloading it // on every deploy. @@ -116,8 +138,9 @@ private function generateArchive(SymfonyStyle $io) : void * php: * version: 7.2.2 */ + $progress->setMessage('Downloading PHP in the `.bref/bin/` directory'); + $progress->display(); if (!$this->fs->exists('.bref/bin/php/php-' . PHP_TARGET_VERSION . '.tar.gz')) { - $io->writeln('Downloading PHP in the `.bref/bin/` directory'); $this->fs->mkdir('.bref/bin/php'); $defaultUrl = 'https://s3.amazonaws.com/bref-php/bin/php-' . PHP_TARGET_VERSION . '.tar.gz'; /* @@ -130,17 +153,24 @@ private function generateArchive(SymfonyStyle $io) : void (new Process("curl -sSL $url -o .bref/bin/php/php-" . PHP_TARGET_VERSION . ".tar.gz")) ->mustRun(); } + $progress->advance(); - $io->writeln('Installing the PHP binary'); + $progress->setMessage('Installing the PHP binary'); + $progress->display(); $this->fs->mkdir('.bref/output/.bref/bin'); (new Process('tar -xzf .bref/bin/php/php-' . PHP_TARGET_VERSION . '.tar.gz -C .bref/output/.bref/bin')) ->mustRun(); + $progress->advance(); - $io->writeln('Installing `handler.js`'); + $progress->setMessage('Installing `handler.js`'); + $progress->display(); $this->fs->copy(__DIR__ . '/../../template/handler.js', '.bref/output/handler.js'); + $progress->advance(); - $io->writeln('Installing composer dependencies'); + $progress->setMessage('Installing composer dependencies'); + $progress->display(); $this->runLocally('composer install --no-dev --classmap-authoritative --no-scripts'); + $progress->advance(); /* * TODO Edit the `serverless.yml` copy (in `.bref/output` to deploy these files: @@ -150,11 +180,15 @@ private function generateArchive(SymfonyStyle $io) : void */ // Run build hooks defined in .bref.yml + $progress->setMessage('Running build hooks'); + $progress->display(); $buildHooks = $projectConfig['hooks']['build'] ?? []; foreach ($buildHooks as $buildHook) { - $io->writeln('Running ' . $buildHook); + $progress->setMessage('Running build hook: ' . $buildHook); + $progress->display(); $this->runLocally($buildHook); } + $progress->advance(); } private function runLocally(string $command) : void @@ -162,4 +196,22 @@ private function runLocally(string $command) : void $process = new Process($command, '.bref/output'); $process->mustRun(); } + + /** + * @param SymfonyStyle $io + * @return ProgressBar + */ + private function createProgressBar(SymfonyStyle $io, int $max) : ProgressBar + { + ProgressBar::setFormatDefinition('bref', "%message%\n %current%/%max% [%bar%] %elapsed:6s%\n"); + + $progressBar = $io->createProgressBar($max); + $progressBar->setFormat('bref'); + $progressBar->setBarCharacter('░'); + $progressBar->setEmptyBarCharacter(' '); + + $progressBar->start(); + + return $progressBar; + } }