1- <?php
2-
3- namespace Oneofftech \LaravelLanguageRecognizer \Commands ;
4-
5- use Exception ;
6- use Illuminate \Console \Command ;
7- use Illuminate \Support \Str ;
8- use Illuminate \Support \Facades \Http ;
9-
10- class InstallLocalRecognizerCommand extends Command
11- {
12- public $ signature = 'language-recognizer:install-local-driver {--path=} ' ;
13-
14- public $ description = 'Install the local Franc binary used to recognize the language of a text ' ;
15-
16-
17- /**
18- * Artifacts urls keyed based on operating system
19- *
20- * @var array
21- */
22- protected static $ urls = [
23- 'linux ' => 'https://github.com/avvertix/franc-bin/releases/download/v1.0.0/franc-bin-linux ' ,
24- 'darwin ' => 'https://github.com/avvertix/franc-bin/releases/download/v1.0.0/franc-bin-macos ' ,
25- 'winnt ' => 'https://github.com/avvertix/franc-bin/releases/download/v1.0.0/franc-bin-win.exe ' ,
26- ];
27-
28-
29- /**
30- * @return int
31- *
32- */
33- public function handle ()
34- {
35- $ this ->comment ('Downloading Franc from binary from https://github.com/avvertix/franc-bin ' );
36-
37- $ os = $ this ->getOs ();
38-
39- $ url = self ::$ urls [$ os ] ?? null ;
40-
41- if (is_null ($ url )){
42- throw new Exception ("Unsupported operating system [ {$ os }] " );
43- }
44-
45- $ this ->info ($ url );
46-
47- $ downloadPath = $ this ->getDownloadPath ();
48-
49- if (!is_dir ($ directory = dirname ($ downloadPath ))){
50- mkdir ($ directory );
51- }
52-
53- Http::withOptions ([
54- 'sink ' => $ downloadPath ,
55- ])->timeout (30 )->get ($ url );
56-
57- $ this ->comment ('All done ' );
58-
59- return 0 ;
60- }
61-
62- protected function getOs ()
63- {
64- return strtolower (PHP_OS );
65- }
66-
67- protected function getDownloadPath ()
68- {
69- $ pathOption = $ this ->option ('path ' );
70-
71- if ($ pathOption ){
72-
73- if ($ this ->getOs () === 'winnt ' && ! Str::endsWith ($ pathOption , '.exe ' )){
74- return $ pathOption .'.exe ' ;
75- }
76-
77- return $ pathOption ;
78- }
79-
80- $ suffixes = [
81- 'winnt ' => '.exe ' ,
82- ];
83-
84- $ configuredPath = config ('language-recognizer.drivers.local.path ' );
85-
86- $ resolvedPath = Str::startsWith ($ configuredPath , '. ' ) ? base_path ($ configuredPath ) : $ configuredPath ;
87-
88- return $ resolvedPath . ($ suffixes [$ this ->getOs ()] ?? '' );
89- }
90- }
1+ <?php
2+
3+ namespace Oneofftech \LaravelLanguageRecognizer \Commands ;
4+
5+ use Exception ;
6+ use Illuminate \Console \Command ;
7+ use Illuminate \Support \Facades \Http ;
8+ use Illuminate \Support \Str ;
9+
10+ class InstallLocalRecognizerCommand extends Command
11+ {
12+ public $ signature = 'language-recognizer:install-local-driver {--path=} ' ;
13+
14+ public $ description = 'Install the local Franc binary used to recognize the language of a text ' ;
15+
16+
17+ /**
18+ * Artifacts urls keyed based on operating system
19+ *
20+ * @var array
21+ */
22+ protected static $ urls = [
23+ 'linux ' => 'https://github.com/avvertix/franc-bin/releases/download/v1.0.0/franc-bin-linux ' ,
24+ 'darwin ' => 'https://github.com/avvertix/franc-bin/releases/download/v1.0.0/franc-bin-macos ' ,
25+ 'winnt ' => 'https://github.com/avvertix/franc-bin/releases/download/v1.0.0/franc-bin-win.exe ' ,
26+ ];
27+
28+ /**
29+ * @return int
30+ *
31+ */
32+ public function handle ()
33+ {
34+ $ this ->comment ('Downloading Franc from binary from https://github.com/avvertix/franc-bin ' );
35+
36+ $ os = $ this ->getOs ();
37+
38+ $ url = self ::$ urls [$ os ] ?? null ;
39+
40+ if (is_null ($ url )) {
41+ throw new Exception ("Unsupported operating system [ {$ os }] " );
42+ }
43+
44+ $ this ->info ($ url );
45+
46+ $ downloadPath = $ this ->getDownloadPath ();
47+
48+ if (! is_dir ($ directory = dirname ($ downloadPath ))) {
49+ mkdir ($ directory );
50+ }
51+
52+ Http::withOptions ([
53+ 'sink ' => $ downloadPath ,
54+ ])->timeout (30 )->get ($ url );
55+
56+ $ this ->comment ('All done ' );
57+
58+ return 0 ;
59+ }
60+
61+ protected function getOs ()
62+ {
63+ return strtolower (PHP_OS );
64+ }
65+
66+ protected function getDownloadPath ()
67+ {
68+ $ pathOption = $ this ->option ('path ' );
69+
70+ if ($ pathOption ) {
71+ if ($ this ->getOs () === 'winnt ' && ! Str::endsWith ($ pathOption , '.exe ' )) {
72+ return $ pathOption .'.exe ' ;
73+ }
74+
75+ return $ pathOption ;
76+ }
77+
78+ $ suffixes = [
79+ 'winnt ' => '.exe ' ,
80+ ];
81+
82+ $ configuredPath = config ('language-recognizer.drivers.local.path ' );
83+
84+ $ resolvedPath = Str::startsWith ($ configuredPath , '. ' ) ? base_path ($ configuredPath ) : $ configuredPath ;
85+
86+ return $ resolvedPath . ($ suffixes [$ this ->getOs ()] ?? '' );
87+ }
88+ }
0 commit comments