| 1 | package kateb::Install; | ||
| 2 | $kateb::Install::VERSION = '1.2.1'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use 5.012; | ||
| 7 | |||
| 8 | use kateb::FontInfo; | ||
| 9 | use JSON::PP; | ||
| 10 | use HTTP::Tinyish; | ||
| 11 | use URI (); | ||
| 12 | use File::Spec::Functions qw(catdir catfile tmpdir); | ||
| 13 | use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); | ||
| 14 | use File::Copy; | ||
| 15 | use Term::ANSIColor qw(:constants); | ||
| 16 | |||
| 17 | my $shown; | ||
| 18 | my %c = | ||
| 19 | ( | ||
| 20 | byellow => (BOLD YELLOW), | ||
| 21 | bpurple => (BOLD MAGENTA), | ||
| 22 | bblue => (BOLD BLUE), | ||
| 23 | bgreen => (BOLD GREEN), | ||
| 24 | bold => (BOLD), | ||
| 25 | bred => (BOLD RED), | ||
| 26 | bcyan => (BOLD CYAN), | ||
| 27 | reset => (RESET), | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $class = shift; | ||
| 32 | my @args = @_; | ||
| 33 | my $self; | ||
| 34 | if (not $args[0]) | ||
| 35 | { | ||
| 36 | $self = { | ||
| 37 | error => 1, | ||
| 38 | message => "type font name(s) or use all|-a option", | ||
| 39 | errlist => [], | ||
| 40 | fonts => '', | ||
| 41 | }; | ||
| 42 | } elsif ($args[0] =~ /^all$|^-a$/) | ||
| 43 | { | ||
| 44 | $self = { | ||
| 45 | error => 0, | ||
| 46 | message => "ok!", | ||
| 47 | errlist => [], | ||
| 48 | fonts => _all_fonts(), | ||
| 49 | }; | ||
| 50 | } else | ||
| 51 | { | ||
| 52 | my @compare = _compare(@args); | ||
| 53 | if (@compare) | ||
| 54 | { | ||
| 55 | $self = { | ||
| 56 | error => 2, | ||
| 57 | message => "unknown font(s)", | ||
| 58 | errlist => \@compare, | ||
| 59 | fonts => '', | ||
| 60 | }; | ||
| 61 | } else | ||
| 62 | { | ||
| 63 | $self = { | ||
| 64 | error => 0, | ||
| 65 | message => "ok!", | ||
| 66 | errlist => [], | ||
| 67 | fonts => \@args, | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | bless $self, $class; | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub install { | ||
| 77 | my $self = shift; | ||
| 78 | my $local_data = shift; | ||
| 79 | my @fonts = @{$self->{fonts}}; | ||
| 80 | my $info = kateb::FontInfo->new; | ||
| 81 | |||
| 82 | my $json_file = $local_data->{jsonFile}; | ||
| 83 | my @check; | ||
| 84 | |||
| 85 | foreach my $font_name (@fonts) | ||
| 86 | { | ||
| 87 | my $version = _online_version($info->{$font_name}->{api}); | ||
| 88 | if ($local_data->{installedVersions}->{$font_name}) | ||
| 89 | { | ||
| 90 | printf("$c{bgreen}%10s$c{reset}",$font_name); | ||
| 91 | say " version " . $c{bgreen} . $version . $c{reset} . " is already installed"; | ||
| 92 | |||
| 93 | unless ($version eq $local_data->{installedVersions}->{$font_name}) { | ||
| 94 | push @check, $font_name; | ||
| 95 | say "\tnew version is available: " . | ||
| 96 | $c{bred} . $version . $c{reset}; | ||
| 97 | } | ||
| 98 | next; | ||
| 99 | } | ||
| 100 | _do($font_name, $local_data, $version); | ||
| 101 | } | ||
| 102 | kateb::LocalData->write_data($local_data->{installedVersions}, $json_file); | ||
| 103 | say "\nto update fonts: | ||
| 104 | $c{bblue}kateb update @check$c{reset}" if @check; | ||
| 105 | |||
| 106 | # my @fc_cache = ("fc-cache", "-f", "-v", $local_data->{targetDir}, ">/dev/null"); | ||
| 107 | # qx(@fc_cache); | ||
| 108 | system("fc-cache -f -v $local_data->{targetDir} >/dev/null"); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub update { | ||
| 112 | my $self = shift; | ||
| 113 | my $local_data = shift; | ||
| 114 | my @fonts = @{$self->{fonts}}; | ||
| 115 | my $info = kateb::FontInfo->new; | ||
| 116 | |||
| 117 | my $json_file = $local_data->{jsonFile}; | ||
| 118 | |||
| 119 | foreach my $font_name (@fonts) | ||
| 120 | { | ||
| 121 | if (not $local_data->{installedVersions}->{$font_name}) | ||
| 122 | { | ||
| 123 | printf("$c{bred}%10s$c{reset}",$font_name); | ||
| 124 | print " is not already installed, but can be installed with:\n\n"; | ||
| 125 | say $c{bblue} . "\tkateb install $font_name\n". $c{reset}; | ||
| 126 | next; | ||
| 127 | } | ||
| 128 | |||
| 129 | my $version = _online_version($info->{$font_name}->{api}); | ||
| 130 | |||
| 131 | if ($version eq $local_data->{installedVersions}->{$font_name}) | ||
| 132 | { | ||
| 133 | printf("$c{bgreen}%10s$c{reset}",$font_name); | ||
| 134 | print ", The latest version is already installed: "; | ||
| 135 | say $c{bgreen} . $version . $c{reset}; | ||
| 136 | next; | ||
| 137 | } | ||
| 138 | _do($font_name, $local_data, $version); | ||
| 139 | } | ||
| 140 | kateb::LocalData->write_data($local_data->{installedVersions}, $json_file); | ||
| 141 | |||
| 142 | # my @fc_cache = ("fc-cache", "-f", "-v", $local_data->{targetDir}, ">/dev/null"); | ||
| 143 | # qx(@fc_cache); | ||
| 144 | system("fc-cache -f -v $local_data->{targetDir} >/dev/null"); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub reinstall { | ||
| 148 | my $self = shift; | ||
| 149 | my $local_data = shift; | ||
| 150 | my @fonts = @{$self->{fonts}}; | ||
| 151 | my $info = kateb::FontInfo->new; | ||
| 152 | |||
| 153 | my $json_file = $local_data->{jsonFile}; | ||
| 154 | |||
| 155 | foreach my $font_name (@fonts) | ||
| 156 | { | ||
| 157 | if (not $local_data->{installedVersions}->{$font_name}) | ||
| 158 | { | ||
| 159 | printf("$c{bred}%10s$c{reset}",$font_name); | ||
| 160 | print " is not already installed, but can be installed with:\n\n"; | ||
| 161 | say $c{bblue} . "\tkateb install $font_name\n". $c{reset}; | ||
| 162 | next; | ||
| 163 | } | ||
| 164 | my $version = _online_version($info->{$font_name}->{api}); | ||
| 165 | _do($font_name, $local_data, $version); | ||
| 166 | } | ||
| 167 | kateb::LocalData->write_data($local_data->{installedVersions}, $json_file); | ||
| 168 | |||
| 169 | # my @fc_cache = ("fc-cache", "-f", "-v", $local_data->{targetDir}, ">/dev/null"); | ||
| 170 | # qx(@fc_cache); | ||
| 171 | system("fc-cache -f -v $local_data->{targetDir} >/dev/null"); | ||
| 172 | } | ||
| 173 | |||
| 174 | ############################################ | ||
| 175 | ###### do: install/reinstall/update ###### | ||
| 176 | ############################################ | ||
| 177 | |||
| 178 | sub _do { | ||
| 179 | my $font_name = shift; | ||
| 180 | my $local_data = shift; | ||
| 181 | my $version = shift; | ||
| 182 | my $temp_dir = $local_data->{tempDir}; | ||
| 183 | my $cache_dir = $local_data->{cacheDir}; | ||
| 184 | my $target_dir = $local_data->{targetDir}; | ||
| 185 | |||
| 186 | my $info = kateb::FontInfo->new; | ||
| 187 | print "working on $font_name-$version..."; | ||
| 188 | |||
| 189 | if ($font_name eq 'lalezar') | ||
| 190 | { | ||
| 191 | my $url = $info->$font_name($version); | ||
| 192 | my $archive_file = catfile($cache_dir, "Lalezar-Regular.ttf"); | ||
| 193 | _download( $url, $archive_file ) ? print "\t[Downloaded]\n" : print "\t[Failed]\n"; | ||
| 194 | _copy_fonts( $target_dir, $archive_file ); | ||
| 195 | $local_data->{installedVersions}->{$font_name} = $version; | ||
| 196 | say $c{bgreen} . $font_name . $c{reset} . " installed. version: " . $c{bgreen} . $version . $c{reset}; | ||
| 197 | } elsif ($font_name eq 'nastaliq') | ||
| 198 | { | ||
| 199 | my $url = $info->$font_name($version); | ||
| 200 | my $archive_file = catfile($cache_dir, "IranNastaliq-Web.ttf"); | ||
| 201 | _download( $url, $archive_file ) ? print "\t[Downloaded]\n" : print "\t[Failed]\n"; | ||
| 202 | _copy_fonts( $target_dir, $archive_file ); | ||
| 203 | $local_data->{installedVersions}->{$font_name} = $version; | ||
| 204 | say $c{bgreen} . $font_name . $c{reset} . " installed. version: " . $c{bgreen} . $version . $c{reset}; | ||
| 205 | } else | ||
| 206 | { | ||
| 207 | my $url = $info->$font_name($version); | ||
| 208 | my $archive_file = catfile($temp_dir, "$font_name-$version.zip"); | ||
| 209 | _download( $url, $archive_file ) ? print "\t[Downloaded]\n" : print "\t[Failed]\n"; | ||
| 210 | my @extracted_fonts = _unzip($font_name, $archive_file, $cache_dir); | ||
| 211 | $local_data->{installedVersions}->{$font_name} = $version; | ||
| 212 | _copy_fonts($target_dir, @extracted_fonts) if @extracted_fonts; | ||
| 213 | say $c{bgreen} . $font_name . $c{reset} . " installed. version: " . $c{bgreen} . $version . $c{reset}; | ||
| 214 | } | ||
| 215 | |||
| 216 | if ($font_name eq 'vazir') | ||
| 217 | { | ||
| 218 | my @vazirs = grep { m"Vazir(?!(?:-Code|matn))"g } glob catfile($target_dir, "*.ttf"); | ||
| 219 | unlink foreach @vazirs; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | ############################################ | ||
| 224 | |||
| 225 | sub _compare { | ||
| 226 | my @args = @_; | ||
| 227 | my $info = kateb::FontInfo->new(); | ||
| 228 | my @compare; | ||
| 229 | foreach my $font_name (@args) | ||
| 230 | { | ||
| 231 | if (not $info->{$font_name}) | ||
| 232 | { | ||
| 233 | push @compare, $font_name; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | return @compare; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _all_fonts { | ||
| 240 | my $info = kateb::FontInfo->new(); | ||
| 241 | my @all_fonts = keys %{$info}; | ||
| 242 | return \@all_fonts; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub _online_version { | ||
| 246 | my $api = shift; | ||
| 247 | |||
| 248 | my $http = HTTP::Tinyish->new | ||
| 249 | ( | ||
| 250 | agent => "kateb/$kateb::VERSION", | ||
| 251 | verify_SSL => 1 | ||
| 252 | ); | ||
| 253 | my $res = $http->get($api); | ||
| 254 | |||
| 255 | my $tags = decode_json( $res->{content} ); | ||
| 256 | my $version; | ||
| 257 | eval | ||
| 258 | { | ||
| 259 | $version = $tags->[0]->{tag_name} || $tags->[0]->{name}; | ||
| 260 | }; if ($@) | ||
| 261 | { | ||
| 262 | say "$c{bred}github API rate limit exceeded. This limit is 50 times per hour, plz try again in about an hour$c{reset}"; | ||
| 263 | exit; | ||
| 264 | } | ||
| 265 | return $version; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _copy_fonts { | ||
| 269 | my $target_dir = shift; | ||
| 270 | my @extracted_fonts = @_; | ||
| 271 | |||
| 272 | #my $glob = catfile($cache_dir, "*.ttf"); | ||
| 273 | #my @downloaded_fonts = glob $glob; | ||
| 274 | foreach my $font_file (@extracted_fonts) | ||
| 275 | { | ||
| 276 | my ($volume, $directories, $file) = File::Spec->splitpath( $font_file ); | ||
| 277 | my $target = catfile($target_dir, $file); | ||
| 278 | ########################################################## | ||
| 279 | # say "...copy $file to $target_dir"; | ||
| 280 | ########################################################## | ||
| 281 | |||
| 282 | copy($font_file, $target); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | ################################### | ||
| 287 | ############ unZip ############## | ||
| 288 | ################################### | ||
| 289 | |||
| 290 | sub _unzip { | ||
| 291 | my $font_name = shift; | ||
| 292 | my $archive_file = shift; | ||
| 293 | my $cache_dir = shift; | ||
| 294 | my @extracted_fonts; | ||
| 295 | my $zip = Archive::Zip->new(); | ||
| 296 | unless ( $zip->read( $archive_file ) == AZ_OK ) | ||
| 297 | { | ||
| 298 | die 'read error'; | ||
| 299 | } | ||
| 300 | # unzip fonts | ||
| 301 | if ($font_name eq 'parastoo') | ||
| 302 | { | ||
| 303 | foreach my $file (grep { m{(web/((?!/).)*\.ttf$)|(print/((?!/).)*\.ttf$)}g } $zip->memberNames()) | ||
| 304 | { | ||
| 305 | my ($volume, $directories, $file_name) = File::Spec->splitpath($file); | ||
| 306 | $zip->extractMember( $file, catfile($cache_dir, $file_name) ); | ||
| 307 | push @extracted_fonts, catfile($cache_dir, $file_name); | ||
| 308 | } | ||
| 309 | } elsif ($font_name =~ /^ganjnameh$|^nika$|^mikhak$|^shahab$/) | ||
| 310 | { | ||
| 311 | foreach my $file (grep { m{.*\.ttf$}g } $zip->memberNames()) | ||
| 312 | { | ||
| 313 | my ($volume, $directories, $file_name) = File::Spec->splitpath($file); | ||
| 314 | $zip->extractMember( $file, catfile($cache_dir, $file_name) ); | ||
| 315 | push @extracted_fonts, catfile($cache_dir, $file_name); | ||
| 316 | } | ||
| 317 | } elsif ($font_name =~ /^pfont$/) | ||
| 318 | { | ||
| 319 | foreach my $file (grep { m{(?<!__MACOSX\/)pfont/ttf/Hinted/.*\.ttf$}g } $zip->memberNames()) | ||
| 320 | { | ||
| 321 | my ($volume, $directories, $file_name) = File::Spec->splitpath($file); | ||
| 322 | $zip->extractMember( $file, catfile($cache_dir, $file_name) ); | ||
| 323 | push @extracted_fonts, catfile($cache_dir, $file_name); | ||
| 324 | } | ||
| 325 | } elsif ($font_name =~ /^estedad$/) | ||
| 326 | { | ||
| 327 | foreach my $file (grep { m{Static/ttf/.*\.ttf$}g } $zip->memberNames()) | ||
| 328 | { | ||
| 329 | my ($volume, $directories, $file_name) = File::Spec->splitpath($file); | ||
| 330 | $zip->extractMember( $file, catfile($cache_dir, $file_name) ); | ||
| 331 | push @extracted_fonts, catfile($cache_dir, $file_name); | ||
| 332 | } | ||
| 333 | } elsif ($font_name =~ /^vazir$/) | ||
| 334 | { | ||
| 335 | foreach my $file (grep { m"^fonts/ttf/((?!/).)*\.ttf$"g } $zip->memberNames()) | ||
| 336 | { | ||
| 337 | my ($volume, $directories, $file_name) = File::Spec->splitpath($file); | ||
| 338 | $zip->extractMember( $file, catfile($cache_dir, $file_name) ); | ||
| 339 | push @extracted_fonts, catfile($cache_dir, $file_name); | ||
| 340 | } | ||
| 341 | } elsif ($font_name =~ /^arad$/) | ||
| 342 | { | ||
| 343 | # m{^main/static/ttf/.*\.ttf$} && ! /Dots/ | ||
| 344 | foreach my $file (grep { m{main/static/ttf/(?!.*Dots).*\.ttf$}g } $zip->memberNames()) | ||
| 345 | { | ||
| 346 | my ($volume, $directories, $file_name) = File::Spec->splitpath($file); | ||
| 347 | $zip->extractMember( $file, catfile($cache_dir, $file_name) ); | ||
| 348 | push @extracted_fonts, catfile($cache_dir, $file_name); | ||
| 349 | } | ||
| 350 | } else | ||
| 351 | { | ||
| 352 | foreach my $file (grep { m"^((?!/).)*\.ttf$"g } $zip->memberNames()) | ||
| 353 | { | ||
| 354 | $zip->extractMember( $file, catfile($cache_dir, $file) ); | ||
| 355 | push @extracted_fonts, catfile($cache_dir, $file); | ||
| 356 | } | ||
| 357 | } | ||
| 358 | return @extracted_fonts; | ||
| 359 | } | ||
| 360 | |||
| 361 | ################################### | ||
| 362 | ############ Download ############# | ||
| 363 | ################################### | ||
| 364 | |||
| 365 | sub _download { | ||
| 366 | my $url = shift; | ||
| 367 | my $archive_file = shift; | ||
| 368 | unlink $archive_file if -e $archive_file; | ||
| 369 | # my $file = $archive_file; | ||
| 370 | # $file =~ s{^.*/([^/]*\.zip)}{$1}; | ||
| 371 | |||
| 372 | $url = URI->new($url); | ||
| 373 | my $http = HTTP::Tinyish->new | ||
| 374 | ( | ||
| 375 | agent => "kateb/$kateb::VERSION", | ||
| 376 | verify_SSL => 1 | ||
| 377 | ); | ||
| 378 | my $res = $http->mirror($url, $archive_file); | ||
| 379 | $res->{success} ? return 1 : return 0; | ||
| 380 | } | ||
| 381 | |||
| 382 | 1; |