fontist 1.8.7 → 1.8.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,47 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module CpioExtractor
4
- def cpio_extract(resource)
5
- file = @downloaded ? resource : download_file(resource)
6
-
7
- dir = extract_cpio_file(file)
8
-
9
- largest_file_in_dir(dir)
10
- end
11
-
12
- private
13
-
14
- def extract_cpio_file(archive_path)
15
- archive_file = File.open(archive_path, "rb")
16
- dir = Dir.mktmpdir
17
- extract_cpio_file_to_dir(archive_file, dir)
18
-
19
- dir
20
- end
21
-
22
- def extract_cpio_file_to_dir(archive_file, dir)
23
- cpio_reader_class.new(archive_file).each do |entry, file|
24
- path = File.join(dir, entry.name)
25
- if entry.directory?
26
- FileUtils.mkdir_p(path)
27
- else
28
- File.write(path, file.read, mode: "wb")
29
- end
30
- end
31
- end
32
-
33
- def cpio_reader_class
34
- @cpio_reader_class ||= begin
35
- require "fontist/utils/cpio/cpio"
36
- CPIO::ASCIIReader
37
- end
38
- end
39
-
40
- def largest_file_in_dir(dir)
41
- Dir.glob(File.join(dir, "**/*")).max_by do |path|
42
- File.size(path)
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,75 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module ExeExtractor
4
- def cab_extract(exe_file, download: true, font_ext: /.ttf|.otf|.ttc/i)
5
- download = @downloaded === true ? false : download
6
-
7
- exe_file = download_file(exe_file).path if download
8
-
9
- Fontist.ui.say(%(Installing font "#{formula.key}".))
10
- cab_file = decompressor.search(exe_file)
11
- cabbed_fonts = grep_fonts(cab_file.files) || []
12
- fonts_paths = extract_cabbed_fonts_to_assets(cabbed_fonts)
13
-
14
- block_given? ? yield(fonts_paths) : fonts_paths
15
- end
16
-
17
- def exe_extract(source, subarchive: nil)
18
- cab_file = decompressor.search(download_file(source).path)
19
- subarchive_path = extract_subarchive(cab_file.files, subarchive)
20
- block_given? ? yield(subarchive_path) : subarchive_path
21
- end
22
-
23
- private
24
-
25
- def decompressor
26
- @decompressor ||= (
27
- require "libmspack"
28
- LibMsPack::CabDecompressor.new
29
- )
30
- end
31
-
32
- def grep_fonts(file)
33
- Array.new.tap do |fonts|
34
- while file
35
- fonts.push(file) if font_file?(file.filename)
36
- file = file.next
37
- end
38
- end
39
- end
40
-
41
- def extract_cabbed_fonts_to_assets(cabbed_fonts)
42
- Array.new.tap do |fonts|
43
- cabbed_fonts.each do |font|
44
- target_filename = target_filename(font.filename)
45
- font_path = fonts_path.join(target_filename).to_s
46
- decompressor.extract(font, font_path)
47
-
48
- fonts.push(font_path)
49
- end
50
- end
51
- end
52
-
53
- def extract_subarchive(file, subarchive = nil)
54
- while file
55
- filename = file.filename
56
-
57
- if subarchive_found?(filename, subarchive)
58
- file_path = File.join(Dir.mktmpdir, filename)
59
- decompressor.extract(file, file_path)
60
-
61
- return file_path
62
- end
63
-
64
- file = file.next
65
- end
66
- end
67
-
68
- def subarchive_found?(filename, subarchive)
69
- return subarchive == filename if subarchive
70
-
71
- filename.include?("cab") || filename.include?("msi")
72
- end
73
- end
74
- end
75
- end
@@ -1,24 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module GzipExtractor
4
- def gzip_extract(resource)
5
- file = @downloaded ? resource : download_file(resource)
6
-
7
- extract_gzip_file(file)
8
- end
9
-
10
- private
11
-
12
- def extract_gzip_file(file)
13
- Zlib::GzipReader.open(file) do |gz|
14
- basename = File.basename(file, ".*")
15
- dir = Dir.mktmpdir
16
- path = File.join(dir, basename)
17
- File.write(path, gz.read, mode: "wb")
18
-
19
- path
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,31 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module MsiExtractor
4
- def msi_extract(resource)
5
- file = @downloaded ? resource : download_file(resource)
6
-
7
- cab_content = read_the_largest_file(file)
8
-
9
- cab_file = Tempfile.new(["data", ".cab"], mode: File::BINARY)
10
- cab_file.write(cab_content)
11
-
12
- block_given? ? yield(cab_file.path) : cab_file.path
13
- end
14
-
15
- private
16
-
17
- def read_the_largest_file(file)
18
- ole = storage.open(file)
19
- the_largest_file = ole.dir.entries(".").max_by { |x| ole.file.size(x) }
20
- ole.file.read(the_largest_file)
21
- end
22
-
23
- def storage
24
- @storage ||= begin
25
- require "ole/storage"
26
- Ole::Storage
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,44 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module RpmExtractor
4
- def rpm_extract(resource)
5
- file = download_file(resource)
6
-
7
- extract_rpm_file(file)
8
- end
9
-
10
- private
11
-
12
- def extract_rpm_file(file)
13
- rpm = rpm_class.new(file)
14
- content = rpm.payload.read
15
- path = rpm_target_path(file.path, rpm.tags)
16
- File.write(path, content, mode: "wb")
17
-
18
- path
19
- end
20
-
21
- def rpm_class
22
- @rpm_class ||= begin
23
- require "arr-pm"
24
-
25
- # fix for Ruby 3.0
26
- unless RPM::File::Header::HEADER_MAGIC == [0x8eade801, 0x00000000].pack("NN")
27
- RPM::File::Header.send(:remove_const, "HEADER_MAGIC")
28
- RPM::File::Header.const_set("HEADER_MAGIC", [0x8eade801, 0x00000000].pack("NN"))
29
- end
30
-
31
- RPM::File
32
- end
33
- end
34
-
35
- def rpm_target_path(archive, tags)
36
- basename = File.basename(archive, ".*")
37
- archive_format = tags[:payloadformat]
38
- compression_format = tags[:payloadcompressor] == "gzip" ? "gz" : tags[:payloadcompressor]
39
- filename = basename + "." + archive_format + "." + compression_format
40
- File.join(Dir.mktmpdir, filename)
41
- end
42
- end
43
- end
44
- end
@@ -1,41 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module SevenZipExtractor
4
- def seven_zip_extract(resource, extension: /\.cab$/)
5
- file = download_file(resource)
6
-
7
- extract_seven_zip_file(file, extension)
8
- end
9
-
10
- private
11
-
12
- def extract_seven_zip_file(file, extension)
13
- File.open(file, "rb") do |zip_file|
14
- reader.open(zip_file) do |szr|
15
- path = extract_by_extension(szr, extension)
16
-
17
- return block_given? ? yield(path) : path
18
- end
19
- end
20
- end
21
-
22
- def reader
23
- @reader ||= begin
24
- require "seven_zip_ruby"
25
- SevenZipRuby::Reader
26
- end
27
- end
28
-
29
- def extract_by_extension(szr, extension)
30
- cab_entry = szr.entries.detect do |entry|
31
- entry.file? && entry.path.match(extension)
32
- end
33
-
34
- tmp_dir = Dir.mktmpdir
35
- szr.extract(cab_entry, tmp_dir)
36
- filename = Pathname.new(cab_entry.path).basename
37
- File.join(tmp_dir, filename)
38
- end
39
- end
40
- end
41
- end
@@ -1,61 +0,0 @@
1
- module Fontist
2
- module Utils
3
- module TarExtractor
4
- def tar_extract(resource)
5
- file = @downloaded ? resource : download_file(resource)
6
-
7
- dir = extract_tar_file(file)
8
-
9
- save_fonts(dir)
10
- end
11
-
12
- private
13
-
14
- def extract_tar_file(file)
15
- archive_file = File.open(file, "rb")
16
- dir = Dir.mktmpdir
17
- tar_reader_class.new(archive_file) do |tar|
18
- tar.each do |tarfile|
19
- save_tar_file(tarfile, dir)
20
- end
21
- end
22
-
23
- dir
24
- end
25
-
26
- def tar_reader_class
27
- @tar_reader_class ||= begin
28
- require "rubygems/package"
29
- Gem::Package::TarReader
30
- end
31
- end
32
-
33
- def save_tar_file(file, dir)
34
- path = File.join(dir, file.full_name)
35
-
36
- if file.directory?
37
- FileUtils.mkdir_p(path)
38
- else
39
- File.open(path, "wb") do |f|
40
- f.print(file.read)
41
- end
42
- end
43
- end
44
-
45
- def save_fonts(dir)
46
- Array.new.tap do |fonts_paths|
47
- Dir.glob(File.join(dir, "**/*")).each do |path|
48
- filename = File.basename(path)
49
- next unless font_file?(filename)
50
-
51
- target_filename = target_filename(filename)
52
- font_path = fonts_path.join(target_filename).to_s
53
- FileUtils.mv(path, font_path)
54
-
55
- fonts_paths << font_path
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,52 +0,0 @@
1
- require "zip"
2
- require "pathname"
3
-
4
- module Fontist
5
- module Utils
6
- module ZipExtractor
7
- def zip_extract(resource, download: true, fonts_sub_dir: nil)
8
- zip_file = download_file(resource) if download
9
- zip_file ||= resource.urls.first
10
-
11
- Fontist.ui.say(%(Installing font "#{formula.key}".))
12
- fonts_paths = unzip_fonts(zip_file, fonts_sub_dir)
13
- block_given? ? yield(fonts_paths) : fonts_paths
14
- end
15
-
16
- alias_method :unzip, :zip_extract
17
-
18
- private
19
-
20
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
21
- def unzip_fonts(file, subdir)
22
- Zip.on_exists_proc = true
23
-
24
- Array.new.tap do |fonts|
25
- Zip::File.open(file) do |zip_file|
26
- zip_file.each do |entry|
27
- if entry.name
28
- filename = Pathname.new(entry.name).basename.to_s
29
- if font_directory?(entry.name, subdir) && font_file?(filename)
30
- target_filename = target_filename(filename)
31
- font_path = fonts_path.join(target_filename)
32
- fonts.push(font_path.to_s)
33
-
34
- entry.extract(font_path)
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
41
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
42
-
43
- def font_directory?(path, subdir)
44
- return true unless subdir
45
-
46
- dirname = File.dirname(path)
47
- normalized_pattern = subdir.chomp("/")
48
- File.fnmatch?(normalized_pattern, dirname)
49
- end
50
- end
51
- end
52
- end