readmeExtractor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'readmeExtractor/version'
4
+ require 'rubygems/package'
5
+ require 'zlib'
6
+ require 'fileutils'
7
+ require 'pathname'
8
+ require 'rdoc'
9
+ require 'stringio'
10
+ class ReadmeExtractor
11
+ class Error < StandardError; end
12
+ class GemFileNameError < StandardError; end
13
+ class GemContentError < StandardError; end
14
+ class FromPathError < StandardError; end
15
+
16
+ def perform(from, to)
17
+ gem_list = gem_list_prepare(from)
18
+ gem_list.each_value do |gem_info|
19
+ extract_from_gem_file(gem_info[:gem_path], to, gem_info[:version])
20
+ end
21
+ end
22
+
23
+ def gem_list_prepare(from)
24
+ raise FromPathError, 'Is not a folder' unless File.directory? from
25
+
26
+ gem_paths = Dir[Pathname(from).join('*')]
27
+ gems = {}
28
+ gem_paths.each do |gem_path|
29
+ basename = File.basename gem_path
30
+ match = basename.match(/(?<name>.*?)-(?<version>\d+(?:\.\d+)*)(?:-(?<platform>[^.]+))?.gem/)
31
+ raise GemFileNameError, "#{basename} does not match the expected format." unless match && match[:name] && match[:version]
32
+
33
+ name = match[:name]
34
+ version = match[:version]
35
+ gems[name] ||= { version:, gem_path: }
36
+ if Gem::Version.new(version) > Gem::Version.new(gems[name][:version])
37
+ gems[name] = { version:, gem_path: }
38
+ end
39
+ end
40
+ gems
41
+ end
42
+
43
+ def extract_from_gem_file(gem_path, to, version)
44
+ output_folder = Pathname(to).join(File.basename(gem_path))
45
+ readme_file = output_folder.join('readme.md').to_s
46
+ metadata_file = output_folder.join('metadata.gz').to_s
47
+ version_file = output_folder.join('version').to_s
48
+ FileUtils.mkdir_p(output_folder)
49
+ File.write(version_file, version)
50
+ magic = File.binread(gem_path, 2)
51
+ is_gzipped = magic == "\x1F\x8B"
52
+ File.open(gem_path, 'rb') do |file|
53
+ file = Zlib::GzipReader.new(file) if is_gzipped
54
+
55
+ Gem::Package::TarReader.new(file) do |tar|
56
+ tar.each do |entry|
57
+ case entry.full_name.downcase
58
+ when 'metadata.gz'
59
+ File.binwrite(metadata_file, entry.read)
60
+ when 'data.tar.gz'
61
+ data_tar_io = StringIO.new(entry.read)
62
+ Zlib::GzipReader.wrap(data_tar_io) do |data_gz|
63
+ Gem::Package::TarReader.new(data_gz) do |data_tar|
64
+ data_tar.each do |data_entry|
65
+ case data_entry.full_name.downcase
66
+ when 'readme.md'
67
+ File.write(readme_file, data_entry.read)
68
+ when 'readme.rdoc'
69
+ next if File.exist? readme_file
70
+
71
+ File.write(readme_file, RDoc::Markup::ToMarkdown.new.convert(data_entry.read))
72
+ end
73
+ end
74
+ raise GemContentError, "#{gem_path} unable to find readme file" unless File.exist? readme_file
75
+ end
76
+ end
77
+ end
78
+ end
79
+ raise GemContentError, "#{gem_path} doesnt have data.tar.gz file" unless File.exist? metadata_file
80
+ end
81
+ end
82
+ rescue Zlib::GzipFile::Error => e
83
+ puts "Error reading gem file: #{gem_path}, #{e.message}"
84
+ rescue GemContentError => e
85
+ puts e.message
86
+ # rubocop :disable Lint/DuplicateBranch
87
+ rescue GemFileNameError => e
88
+ puts e.message
89
+ end
90
+ # rubocop:enable Lint/DuplicateBranch
91
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/readmeExtractor/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'readmeExtractor'
7
+ spec.version = ReadmeExtractor::VERSION
8
+ spec.authors = ['Viktor C']
9
+ spec.email = ['vchekryzhov@ya.ru']
10
+
11
+ spec.summary = 'Simple gem to extract md and rdoc files from .gem'
12
+ spec.description = 'Simple gem to extract md and rdoc files from .gem'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 3.3.0'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(__dir__) do
19
+ %x(git ls-files -z).split("\x0").reject do |f|
20
+ (File.expand_path(f) == __FILE__) ||
21
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
22
+ end
23
+ end
24
+ spec.bindir = 'bin'
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ spec.executables << 'readmeExtractor'
28
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: readmeExtractor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Viktor C
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple gem to extract md and rdoc files from .gem
14
+ email:
15
+ - vchekryzhov@ya.ru
16
+ executables:
17
+ - readmeExtractor
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".idea/inspectionProfiles/Project_Default.xml"
22
+ - ".idea/misc.xml"
23
+ - ".idea/readmeExtractor.iml"
24
+ - ".idea/vcs.xml"
25
+ - ".idea/workspace.xml"
26
+ - ".rspec"
27
+ - ".rubocop.yml"
28
+ - ".ruby-version"
29
+ - CHANGELOG.md
30
+ - CODE_OF_CONDUCT.md
31
+ - LICENSE.txt
32
+ - README.md
33
+ - Rakefile
34
+ - bin/readmeExtractor
35
+ - lib/readmeExtractor.rb
36
+ - lib/readmeExtractor/version.rb
37
+ - readmeExtractor.gemspec
38
+ homepage:
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.3.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.5.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Simple gem to extract md and rdoc files from .gem
61
+ test_files: []