5
5
namespace Codewithkyrian \Transformers \Utils ;
6
6
7
7
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 ;
8
11
9
12
class LibsChecker
10
13
{
11
- public static function check ($ event = null ): void
14
+ protected static ProgressBar $ progressBar ;
15
+
16
+ protected static function getProgressBar ($ filename , $ output ): ProgressBar
12
17
{
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
+
13
36
$ vendorDir = $ event !== null ?
14
37
$ event ->getComposer ()->getConfig ()->get ('vendor-dir ' )
15
38
: 'vendor ' ;
@@ -27,12 +50,12 @@ public static function check($event = null): void
27
50
}
28
51
29
52
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 );
32
55
}
33
56
}
34
57
35
- private static function install (): void
58
+ private static function install (OutputInterface $ output ): void
36
59
{
37
60
$ version = file_get_contents (basePath ('VERSION ' ));
38
61
@@ -56,23 +79,35 @@ private static function install(): void
56
79
$ maxRetries = 10 ;
57
80
$ attempts = 0 ;
58
81
82
+
59
83
do {
60
84
$ 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 " ;
63
87
$ downloadPath = tempnam (sys_get_temp_dir (), 'transformers-php ' ).". $ extension " ;
64
88
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
+ };
66
94
67
95
$ downloadSuccess = false ;
68
96
69
97
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> " );
71
104
} catch (\Exception ) {
105
+ } finally {
106
+ unset($ progressBar );
72
107
}
73
108
74
109
if ($ downloadSuccess ) {
75
- echo " - Installing " . self :: colorize ( " transformersphp- $ version - $ os - $ arch " ). " : Extracting archive \n" ;
110
+ $ output -> writeln ( " - Installing <info> $ filename </info> : Extracting archive" ) ;
76
111
77
112
$ archive = new \PharData ($ downloadPath );
78
113
if ($ extension != 'zip ' ) {
@@ -82,10 +117,10 @@ private static function install(): void
82
117
$ archive ->extractTo (basePath (), overwrite: true );
83
118
@unlink ($ downloadPath );
84
119
85
- echo " TransformersPHP libraries installed \n" ;
120
+ $ output -> writeln ( " ✔ TransformersPHP libraries installed successfully! " ) ;
86
121
return ;
87
122
} 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..." ) ;
89
124
$ version = self ::getLowerVersion ($ version );
90
125
}
91
126
0 commit comments