Skip to content

Commit

Permalink
Add a progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Jun 2, 2018
1 parent 5f9fc58 commit 075fd2b
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions src/Console/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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');
Expand All @@ -75,15 +92,19 @@ 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
* for that could make sense.
*/
$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
Expand All @@ -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.
Expand All @@ -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';
/*
Expand All @@ -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:
Expand All @@ -150,16 +180,38 @@ 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
{
$process = new Process($command, '.bref/output');
$process->mustRun();
}

/**
* @param SymfonyStyle $io
* @return ProgressBar
*/
private function createProgressBar(SymfonyStyle $io, int $max) : ProgressBar
{
ProgressBar::setFormatDefinition('bref', "<comment>%message%</comment>\n %current%/%max% [%bar%] %elapsed:6s%\n");

$progressBar = $io->createProgressBar($max);
$progressBar->setFormat('bref');
$progressBar->setBarCharacter('');
$progressBar->setEmptyBarCharacter(' ');

$progressBar->start();

return $progressBar;
}
}

0 comments on commit 075fd2b

Please sign in to comment.