|
5 | 5 | namespace Codewithkyrian\Transformers\Commands;
|
6 | 6 |
|
7 | 7 | use Codewithkyrian\Transformers\Transformers;
|
| 8 | +use GuzzleHttp\Client; |
| 9 | +use GuzzleHttp\Exception\GuzzleException; |
| 10 | +use GuzzleHttp\Psr7\Utils; |
8 | 11 | use OnnxRuntime\Exception;
|
9 | 12 | use OnnxRuntime\Vendor;
|
| 13 | +use PharData; |
10 | 14 | use Symfony\Component\Console\Attribute\AsCommand;
|
11 | 15 | use Symfony\Component\Console\Command\Command;
|
| 16 | +use Symfony\Component\Console\Helper\ProgressBar; |
12 | 17 | use Symfony\Component\Console\Input\InputInterface;
|
13 | 18 | use Symfony\Component\Console\Input\InputOption;
|
14 | 19 | use Symfony\Component\Console\Output\OutputInterface;
|
@@ -52,48 +57,90 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
52 | 57 |
|
53 | 58 | ensureDirectory(Transformers::$cacheDir);
|
54 | 59 |
|
55 |
| - echo "◌ Downloading ONNX Runtime...\n"; |
| 60 | + echo "✔ Initializing Transformers...\n"; |
56 | 61 |
|
57 | 62 | $file = Transformers::platform('file');
|
58 | 63 | $ext = Transformers::platform('ext');
|
59 | 64 |
|
60 | 65 | $urlTemplate = "https://github.com/microsoft/onnxruntime/releases/download/v{{version}}/$file.$ext";
|
61 |
| - $url = str_replace('{{version}}', Transformers::ONNX_VERSION, $urlTemplate); |
| 66 | + $url = str_replace('{{version}}', Vendor::VERSION, $urlTemplate); |
62 | 67 |
|
63 |
| - $contents = @file_get_contents($url); |
| 68 | + $client = new Client(); |
| 69 | + $tempDest = tempnam(sys_get_temp_dir(), 'onnxruntime') . '.' . $ext; |
64 | 70 |
|
65 |
| - if (!$contents) { |
66 |
| - throw new \Exception("Something went wrong"); |
67 |
| - } |
| 71 | + ProgressBar::setFormatDefinition('hub', '%filename% : [%bar%] %percent:3s%%'); |
| 72 | + |
| 73 | + $progressBar = new ProgressBar($output, 100); |
| 74 | + $progressBar->setFormat('hub'); |
| 75 | + $progressBar->setBarCharacter('<fg=green>•</>'); |
| 76 | + $progressBar->setEmptyBarCharacter("<fg=red>⚬</>"); |
| 77 | + $progressBar->setProgressCharacter('<fg=green>➤</>'); |
| 78 | + $progressBar->setMessage("✔ Downloading Libraries", 'filename'); |
| 79 | + |
| 80 | + $client->get($url, ['sink' => $tempDest, 'progress' => self::onProgress($progressBar)]); |
| 81 | + |
| 82 | + $contents = @file_get_contents($tempDest); |
68 | 83 |
|
69 | 84 | $checksum = hash('sha256', $contents);
|
| 85 | + |
70 | 86 | if ($checksum != Transformers::platform('checksum')) {
|
71 | 87 | throw new Exception("Bad checksum: $checksum");
|
72 | 88 | }
|
73 | 89 |
|
74 |
| - $tempDest = tempnam(sys_get_temp_dir(), 'onnxruntime') . '.' . $ext; |
75 |
| - |
76 |
| - file_put_contents($tempDest, $contents); |
| 90 | + $archive = new PharData($tempDest); |
77 | 91 |
|
78 |
| - $archive = new \PharData($tempDest); |
79 | 92 | if ($ext != 'zip') {
|
80 | 93 | $archive = $archive->decompress();
|
81 | 94 | }
|
82 | 95 |
|
83 | 96 | $archive->extractTo(Transformers::$cacheDir);
|
84 | 97 |
|
| 98 | + echo "\n"; // New line to since Symphony ProgressBar doesn't add a new line. |
85 | 99 | $output->writeln('✔ Initialized Transformers successfully.');
|
86 | 100 |
|
87 | 101 | $this->askToStar($input, $output);
|
88 | 102 |
|
89 | 103 | return Command::SUCCESS;
|
| 104 | + } catch (GuzzleException $e) { |
| 105 | + $output->writeln($e->getMessage()); |
| 106 | + |
| 107 | + return Command::FAILURE; |
90 | 108 | } catch (Exception $e) {
|
91 | 109 | $output->writeln($e->getMessage());
|
92 | 110 |
|
93 | 111 | return Command::FAILURE;
|
94 | 112 | }
|
95 | 113 | }
|
96 | 114 |
|
| 115 | + /** |
| 116 | + * @param resource $stream |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + public function calculateHash($stream): string |
| 120 | + { |
| 121 | + $ctx = hash_init('sha256'); |
| 122 | + |
| 123 | + while (!feof($stream)) { |
| 124 | + $buffer = fread($stream, 8192); // Read in 8KB chunks |
| 125 | + hash_update($ctx, $buffer); |
| 126 | + } |
| 127 | + |
| 128 | + $hash = hash_final($ctx); |
| 129 | + fclose($stream); |
| 130 | + |
| 131 | + return $hash; |
| 132 | + } |
| 133 | + |
| 134 | + private static function onProgress(ProgressBar $progressBar): callable |
| 135 | + { |
| 136 | + return function ($totalDownload, $downloadedBytes) use ($progressBar) { |
| 137 | + if ($totalDownload == 0) return; |
| 138 | + |
| 139 | + $percent = round(($downloadedBytes / $totalDownload) * 100, 2); |
| 140 | + $progressBar->setProgress((int)$percent); |
| 141 | + }; |
| 142 | + } |
| 143 | + |
97 | 144 |
|
98 | 145 | protected function askToStar(InputInterface $input, OutputInterface $output): void
|
99 | 146 | {
|
|
0 commit comments