Skip to content

Commit

Permalink
feat: Show output progress when downloading the shared libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeWithKyrian committed Aug 29, 2024
1 parent 563c0f5 commit d613a0d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/Commands/DownloadModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} elseif ($type === 'complete_download') {
$progressBar = $this->getProgressBar($filename, $output);
$progressBar->finish();
$output->writeln('');
$progressBar->clear();
$output->writeln("✔ Downloaded <info>$filename</info>");
}
};

Expand All @@ -115,22 +116,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return Command::SUCCESS;
} catch (Exception $e) {
$output->writeln(''.$e->getMessage());
$output->writeln("<error>✘ {$e->getMessage()}</error>");
return Command::FAILURE;
}
}

protected function getProgressBar(string $filename, OutputInterface $output): ProgressBar
{
ProgressBar::setFormatDefinition('hub', '%filename% : [%bar%] %percent:3s%%');
ProgressBar::setFormatDefinition('hub', '✔ Downloading <info>%message%</info> : [%bar%] %percent:3s%%');

if (!isset($this->progressBars[$filename])) {
$progressBar = new ProgressBar($output, 100);
$progressBar->setFormat('hub');
$progressBar->setBarCharacter('<fg=green>•</>');
$progressBar->setEmptyBarCharacter("<fg=red>⚬</>");
$progressBar->setProgressCharacter('<fg=green>➤</>');
$progressBar->setMessage("✔ Downloading $filename", 'filename');
$progressBar->setMessage($filename);
$this->progressBars[$filename] = $progressBar;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function configure(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
LibsChecker::check();
LibsChecker::check(output: $output);

$this->askToStar($input, $output);

Expand Down
57 changes: 46 additions & 11 deletions src/Utils/LibsChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,34 @@
namespace Codewithkyrian\Transformers\Utils;

use Codewithkyrian\TransformersLibsLoader\Library;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

class LibsChecker
{
public static function check($event = null): void
protected static ProgressBar $progressBar;

protected static function getProgressBar($filename, $output): ProgressBar
{
ProgressBar::setFormatDefinition('hub', ' - Downloading <info>%message%</info> : [%bar%] %percent:3s%%');

if (!isset(self::$progressBar)) {
self::$progressBar = new ProgressBar($output, 100);
self::$progressBar->setFormat('hub');
self::$progressBar->setBarCharacter('<fg=green>•</>');
self::$progressBar->setEmptyBarCharacter("<fg=red>⚬</>");
self::$progressBar->setProgressCharacter('<fg=green>➤</>');
self::$progressBar->setMessage($filename);
}

return self::$progressBar;
}

public static function check($event = null, OutputInterface $output = null): void
{
$output ??= new ConsoleOutput();

$vendorDir = $event !== null ?
$event->getComposer()->getConfig()->get('vendor-dir')
: 'vendor';
Expand All @@ -27,12 +50,12 @@ public static function check($event = null): void
}

if ($installationNeeded) {
echo self::colorize("Installing TransformersPHP libraries...")."\n";
self::install();
$output->writeln("<info>Installing TransformersPHP libraries...</info>");
self::install($output);
}
}

private static function install(): void
private static function install(OutputInterface $output): void
{
$version = file_get_contents(basePath('VERSION'));

Expand All @@ -56,23 +79,35 @@ private static function install(): void
$maxRetries = 10;
$attempts = 0;


do {
$baseUrl = "https://github.com/CodeWithKyrian/transformers-php/releases/download/$version";
$downloadFile = "transformersphp-$version-$os-$arch.$extension";
$downloadUrl = "$baseUrl/$downloadFile";
$filename = "transformersphp-$version-$os-$arch";
$downloadUrl = "$baseUrl/$filename.$extension";
$downloadPath = tempnam(sys_get_temp_dir(), 'transformers-php').".$extension";

echo " - Downloading ".self::colorize("transformersphp-$version-$os-$arch")."\n";
$onProgress = function ($downloadSize, $downloaded, $uploadSize, $uploaded) use ($output, $filename) {
$progressBar = self::getProgressBar($filename, $output);
$percent = round(($downloaded / $downloadSize) * 100, 2);
$progressBar->setProgress((int)$percent);
};

$downloadSuccess = false;

try {
$downloadSuccess = Downloader::download($downloadUrl, $downloadPath);
$downloadSuccess = Downloader::download($downloadUrl, $downloadPath, onProgress: $onProgress);

$progressBar = self::getProgressBar($filename, $output);
$progressBar->finish();
$progressBar->clear();
$output->writeln(" - Downloading <info>$filename</info>");
} catch (\Exception) {
} finally {
unset($progressBar);
}

if ($downloadSuccess) {
echo " - Installing ".self::colorize("transformersphp-$version-$os-$arch")." : Extracting archive\n";
$output->writeln(" - Installing <info>$filename</info> : Extracting archive");

$archive = new \PharData($downloadPath);
if ($extension != 'zip') {
Expand All @@ -82,10 +117,10 @@ private static function install(): void
$archive->extractTo(basePath(), overwrite: true);
@unlink($downloadPath);

echo "TransformersPHP libraries installed\n";
$output->writeln("TransformersPHP libraries installed successfully!");
return;
} else {
echo " - Failed to download ".self::colorize("transformersphp-$version-$os-$arch").", trying a lower version...\n";
$output->writeln(" - Failed to download <info>$filename</info> trying a lower version...");
$version = self::getLowerVersion($version);
}

Expand Down

0 comments on commit d613a0d

Please sign in to comment.