Class: ReadmeExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/readmeExtractor.rb,
lib/readmeExtractor/version.rb

Defined Under Namespace

Classes: Error, FromPathError, GemContentError, GemFileNameError

Constant Summary collapse

VERSION =
'0.1.10'

Instance Method Summary collapse

Instance Method Details

#extract_from_gem_file(gem_path, to, version) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/readmeExtractor.rb', line 51

def extract_from_gem_file(gem_path, to, version)
  output_folder = Pathname(to).join(File.basename(gem_path))
  readme_file = output_folder.join('readme.md').to_s
   = output_folder.join('metadata.gz').to_s
  version_file = output_folder.join('version').to_s
  FileUtils.mkdir_p(output_folder)
  File.write(version_file, version)
  magic = File.binread(gem_path, 2)
  is_gzipped = magic == "\x1F\x8B"
  File.open(gem_path, 'rb') do |file|
    file = Zlib::GzipReader.new(file) if is_gzipped

    Gem::Package::TarReader.new(file) do |tar|
      tar.each do |entry|
        case entry.full_name.downcase
        when 'metadata.gz'
          File.binwrite(, entry.read)
        when 'data.tar.gz'
          data_tar_io = StringIO.new(entry.read)
          Zlib::GzipReader.wrap(data_tar_io) do |data_gz|
            Gem::Package::TarReader.new(data_gz) do |data_tar|
              data_tar.each do |data_entry|
                case data_entry.full_name.downcase
                when 'readme.md'
                  File.write(readme_file, data_entry.read)
                when 'readme.rdoc'
                  next if File.exist? readme_file

                  File.write(readme_file, RDoc::Markup::ToMarkdown.new.convert(data_entry.read))
                end
              end
              raise GemContentError, "#{gem_path} unable to find readme file" unless File.exist? readme_file
            end
          end
        end
      end
      raise GemContentError, "#{gem_path} doesnt have data.tar.gz file" unless File.exist? 
    end
  end
rescue Zlib::GzipFile::Error => e
  puts "Error reading gem file: #{gem_path}, #{e.message}"
rescue GemContentError => e
  puts e.message
  # rubocop :disable Lint/DuplicateBranch
rescue GemFileNameError => e
  puts e.message
end

#gem_list_prepare(from) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/readmeExtractor.rb', line 24

def gem_list_prepare(from)
  raise FromPathError, 'Is not a folder' unless File.directory? from

  gem_paths = Dir[Pathname(from).join('*')]
  gems = {}
  gem_paths.each do |gem_path|
    basename = File.basename gem_path

    match = basename.match(/(?<name>.*?)-(?<version>\d+(?:\.\d+)*)(?:-(?<platform>[^.]+))?.gem/)

    unless match && match[:name] && match[:version]
      puts "#{basename} does not match the expected format."
      next
    end

    name = match[:name]
    version = match[:version]
    gems[name] ||= { version:, gem_path: }
    if Gem::Version.new(version) > Gem::Version.new(gems[name][:version])
      gems[name] = { version:, gem_path: }
    end
  rescue Encoding::CompatibilityError => e
    puts "Encoding::CompatibilityError #{e.message}"
  end
  gems
end

#perform(from, to) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/readmeExtractor.rb', line 16

def perform(from, to)
  gem_list = gem_list_prepare(from)
  gem_list.each_value do |gem_info|
    puts "Extracting #{gem_info[:gem_path]}..."
    extract_from_gem_file(gem_info[:gem_path], to, gem_info[:version])
  end
end