Skip to content

Commit d613a0d

Browse files
feat: Show output progress when downloading the shared libraries
1 parent 563c0f5 commit d613a0d

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

src/Commands/DownloadModelCommand.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9797
} elseif ($type === 'complete_download') {
9898
$progressBar = $this->getProgressBar($filename, $output);
9999
$progressBar->finish();
100-
$output->writeln('');
100+
$progressBar->clear();
101+
$output->writeln("✔ Downloaded <info>$filename</info>");
101102
}
102103
};
103104

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

116117
return Command::SUCCESS;
117118
} catch (Exception $e) {
118-
$output->writeln(''.$e->getMessage());
119+
$output->writeln("<error>✘ {$e->getMessage()}</error>");
119120
return Command::FAILURE;
120121
}
121122
}
122123

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

127128
if (!isset($this->progressBars[$filename])) {
128129
$progressBar = new ProgressBar($output, 100);
129130
$progressBar->setFormat('hub');
130131
$progressBar->setBarCharacter('<fg=green>•</>');
131132
$progressBar->setEmptyBarCharacter("<fg=red>⚬</>");
132133
$progressBar->setProgressCharacter('<fg=green>➤</>');
133-
$progressBar->setMessage("✔ Downloading $filename", 'filename');
134+
$progressBar->setMessage($filename);
134135
$this->progressBars[$filename] = $progressBar;
135136
}
136137

src/Commands/InstallCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function configure(): void
2828
protected function execute(InputInterface $input, OutputInterface $output): int
2929
{
3030
try {
31-
LibsChecker::check();
31+
LibsChecker::check(output: $output);
3232

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

src/Utils/LibsChecker.php

+46-11
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,34 @@
55
namespace Codewithkyrian\Transformers\Utils;
66

77
use Codewithkyrian\TransformersLibsLoader\Library;
8+
use Symfony\Component\Console\Helper\ProgressBar;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
10+
use Symfony\Component\Console\Output\OutputInterface;
811

912
class LibsChecker
1013
{
11-
public static function check($event = null): void
14+
protected static ProgressBar $progressBar;
15+
16+
protected static function getProgressBar($filename, $output): ProgressBar
1217
{
18+
ProgressBar::setFormatDefinition('hub', ' - Downloading <info>%message%</info> : [%bar%] %percent:3s%%');
19+
20+
if (!isset(self::$progressBar)) {
21+
self::$progressBar = new ProgressBar($output, 100);
22+
self::$progressBar->setFormat('hub');
23+
self::$progressBar->setBarCharacter('<fg=green>•</>');
24+
self::$progressBar->setEmptyBarCharacter("<fg=red>⚬</>");
25+
self::$progressBar->setProgressCharacter('<fg=green>➤</>');
26+
self::$progressBar->setMessage($filename);
27+
}
28+
29+
return self::$progressBar;
30+
}
31+
32+
public static function check($event = null, OutputInterface $output = null): void
33+
{
34+
$output ??= new ConsoleOutput();
35+
1336
$vendorDir = $event !== null ?
1437
$event->getComposer()->getConfig()->get('vendor-dir')
1538
: 'vendor';
@@ -27,12 +50,12 @@ public static function check($event = null): void
2750
}
2851

2952
if ($installationNeeded) {
30-
echo self::colorize("Installing TransformersPHP libraries...")."\n";
31-
self::install();
53+
$output->writeln("<info>Installing TransformersPHP libraries...</info>");
54+
self::install($output);
3255
}
3356
}
3457

35-
private static function install(): void
58+
private static function install(OutputInterface $output): void
3659
{
3760
$version = file_get_contents(basePath('VERSION'));
3861

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

82+
5983
do {
6084
$baseUrl = "https://github.com/CodeWithKyrian/transformers-php/releases/download/$version";
61-
$downloadFile = "transformersphp-$version-$os-$arch.$extension";
62-
$downloadUrl = "$baseUrl/$downloadFile";
85+
$filename = "transformersphp-$version-$os-$arch";
86+
$downloadUrl = "$baseUrl/$filename.$extension";
6387
$downloadPath = tempnam(sys_get_temp_dir(), 'transformers-php').".$extension";
6488

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

6795
$downloadSuccess = false;
6896

6997
try {
70-
$downloadSuccess = Downloader::download($downloadUrl, $downloadPath);
98+
$downloadSuccess = Downloader::download($downloadUrl, $downloadPath, onProgress: $onProgress);
99+
100+
$progressBar = self::getProgressBar($filename, $output);
101+
$progressBar->finish();
102+
$progressBar->clear();
103+
$output->writeln(" - Downloading <info>$filename</info>");
71104
} catch (\Exception) {
105+
} finally {
106+
unset($progressBar);
72107
}
73108

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

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

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

0 commit comments

Comments
 (0)