gemat 0.1.2 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39618b03f87eed4eb3200c82212940c320262f6518932a5df96973f10f204bb2
4
- data.tar.gz: c14fe77e39bec43590dc0c00c8e444e1ff7f6fc97e3d1cb0c1fe009955c75533
3
+ metadata.gz: 422b5c9ef99e71ec0193e7748adff36049c26b88478cdc494c3f5178720f5e70
4
+ data.tar.gz: 386d6d07fb0e91e762f1846cc2395a77748351579e2fa0f277e2da5e115af049
5
5
  SHA512:
6
- metadata.gz: 379fbabf51a3abf620a46628501ec81596fcfcc93526a5b31657e4a0ffcfcfac468a27ff8e100b340f2781485b88fae8ee7030374985b82d7c2f1545100b1fdb
7
- data.tar.gz: 83b49c68fead8652d7492380fa3ad56b65ea93b8c64de8e9d575a187f7454c5456acfd404c5c409b6ba1fa873088a212365a82208d1581d836620e03e68de1b8
6
+ metadata.gz: e79ee792b7f9e98ef3bb7d39f3865410aa3958d2e8d6a394914bcac012db41ff641df84e73c1fcc62e050dc9363a810083792a55346c24da073490d2c3f9d809
7
+ data.tar.gz: febf34bccfadc363376033573ce0fbd1bf43ffae2bcdbe83d9cfc7ccdd112976b81c869dbe3b8407d1f6d9988455296caf483e9dc8d35b95de08333c4323a15b
data/lib/csv_formatter.rb CHANGED
@@ -1,33 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'csv'
4
-
5
3
  module Gemat
6
- class CsvFormatter
7
- def initialize(url, output = nil)
8
- @url = url
9
- @output = output
10
- @rows = []
11
- gen_array
12
- end
13
-
14
- def to_csv
15
- if @output
16
- CSV.open(@output, 'w') do |csv|
17
- @rows.each { |row| csv << row }
18
- end
19
- else
20
- print "\n\n"
21
- @rows.each { |row| print row.to_csv }
22
- end
23
- end
24
-
4
+ class CsvFormatter < Formatter
25
5
  private
26
6
 
27
- def gen_array
28
- @rows << ['gem', 'Repo URL']
29
- @url.urls.each do |k, v|
30
- @rows << [k, v]
7
+ def gen_rows
8
+ @rows << @columns.map(&:column_name).join(',')
9
+ @gems.each do |gem|
10
+ @rows << @columns.map { |dsl| dsl.call(gem) }.join(',')
31
11
  end
32
12
  end
33
13
  end
data/lib/fetcher.rb ADDED
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gemat
4
+ class Fetcher
5
+ attr_accessor :gems
6
+
7
+ def initialize(dsl)
8
+ @dsl = dsl
9
+ @gems = []
10
+ end
11
+
12
+ def run
13
+ pb = ProgressBar.create(total: @dsl.dependencies.length)
14
+ @dsl.dependencies.each_with_index do |dependency, idx|
15
+ pb.increment
16
+
17
+ response = fetch_rubygems(dependency)
18
+ next unless response
19
+
20
+ create_gem(response, idx)
21
+ sleep 0.2
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def create_gem(rubygems, idx)
28
+ gem = Gem.new(rubygems)
29
+ gem.github = fetch_github(gem)
30
+ gem.index = idx
31
+ @gems << gem
32
+ end
33
+
34
+ def fetch_rubygems(gem)
35
+ fetch(rubygems_api(gem.name))
36
+ end
37
+
38
+ def fetch_github(gem, uri = nil)
39
+ uri ||= gem.repo_uri.gsub(/github.com/, 'api.github.com/repos')
40
+ response = fetch(uri)
41
+ response = fetch_github(gem, response['url']) if response['message'] == 'Moved Permanently'
42
+ response
43
+ end
44
+
45
+ # rubocop:disable Metrics/MethodLength
46
+ def fetch(uri)
47
+ failed = []
48
+ client = HTTPClient.new
49
+ request = client.get(uri)
50
+ begin
51
+ response = JSON.parse(request.body)
52
+ rescue JSON::ParserError
53
+ failed << gem.name
54
+ return
55
+ end
56
+
57
+ print "#{failed.join(',')}: failed fetcing info." unless failed.empty?
58
+ response
59
+ end
60
+ # rubocop:enable Metrics/MethodLength
61
+
62
+ def rubygems_api(name)
63
+ "https://rubygems.org/api/v1/gems/#{name}.json"
64
+ end
65
+ end
66
+ end
data/lib/formatter.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gemat
4
+ class Formatter
5
+ def initialize(gems, columns, write_path: nil)
6
+ @gems = gems
7
+ @columns = columns
8
+ @write_path = write_path
9
+ @rows = []
10
+ gen_rows
11
+ end
12
+
13
+ def run
14
+ if @write_path
15
+ File.open(@write_path, 'w') do |file|
16
+ each_write(@rows) { |string| file << string }
17
+ end
18
+ else
19
+ print "\n\n"
20
+ each_write(@rows) { |string| print string }
21
+ print "\n"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def each_write(rows, &proc)
28
+ rows.each do |row|
29
+ proc.call(row)
30
+ proc.call("\n")
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/gem.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gemat
4
+ class Gem
5
+ attr_accessor :index, :rubygems, :github
6
+
7
+ def initialize(rubygems)
8
+ @rubygems = rubygems
9
+ @index = 0
10
+ end
11
+
12
+ def repo_uri
13
+ match = github_uri_match([@rubygems.dig('metadata', 'homepage_uri'),
14
+ @rubygems['homepage_uri'],
15
+ @rubygems['bug_tracker_uri'],
16
+ @rubygems['source_code_uri']])
17
+ return if match.nil?
18
+
19
+ user = match[1]
20
+ repo = match[2]
21
+ "https://github.com/#{user}/#{repo}"
22
+ end
23
+
24
+ private
25
+
26
+ def github_uri_match(uris)
27
+ reg = %r{github.com/([\w\-]+)/([\w\-]+)}
28
+ uris.each do |uri|
29
+ return reg.match(uri) if reg.match(uri)
30
+ end
31
+ end
32
+ end
33
+ end
data/lib/gemat/cli.rb CHANGED
@@ -1,17 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thor'
4
-
5
3
  module Gemat
6
4
  class Cli < Thor
7
5
  desc 'csv', 'export Gemfile to CSV file'
6
+ method_options input: :string, output: :string, columns: :array, all: :boolean
8
7
  def csv
9
- Gemat.run { |url| CsvFormatter.new(url).to_csv }
8
+ command(options, __method__)
10
9
  end
11
10
 
12
11
  desc 'md', 'export Gemfile to markdown'
12
+ method_options input: :string, output: :string, columns: :array, all: :boolean
13
13
  def md
14
- Gemat.run { |url| MdFormatter.new(url).to_md }
14
+ command(options, __method__)
15
+ end
16
+
17
+ no_tasks do
18
+ def command(options, method_name)
19
+ klass = "Gemat::#{method_name.capitalize}Formatter"
20
+ Gemat.run(options) do |gems, columns|
21
+ Object.const_get(klass).new(gems, columns, write_path: options[:output]).run
22
+ end
23
+ end
15
24
  end
16
25
  end
17
26
  end
data/lib/gemat/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gemat
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.6'
5
5
  end
data/lib/gemat.rb CHANGED
@@ -1,25 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'bundler'
3
4
  require 'httpclient'
4
5
  require 'json'
5
6
  require 'ruby-progressbar'
7
+ require 'thor'
6
8
 
9
+ require_relative 'in_dsl'
10
+ require_relative 'out_dsl'
11
+ require_relative 'fetcher'
12
+ require_relative 'gem'
13
+ require_relative 'formatter'
7
14
  require_relative 'csv_formatter'
8
15
  require_relative 'md_formatter'
9
- require_relative 'dsl'
10
16
  require_relative 'gemat/version'
11
17
  require_relative 'gemat/cli'
12
- require_relative 'get_url'
13
18
 
14
19
  module Gemat
15
20
  class Error < StandardError; end
16
21
 
17
22
  class Gemat
18
- def self.run(&block)
19
- dsl = Dsl.new
20
- url = GetUrl.new(dsl)
21
- url.run
22
- block.call(url)
23
+ def self.run(options = {}, &block)
24
+ dsl = InDsl.new(options[:input])
25
+ outdsls = OutDsl.create(options[:columns], all: options[:all])
26
+ fetcher = Fetcher.new(dsl)
27
+ fetcher.run
28
+ block.call(fetcher.gems, outdsls)
23
29
  end
24
30
  end
25
31
  end
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler'
4
-
5
3
  module Gemat
6
- class Dsl
7
- attr_accessor :dependencies, :sources, :git_sources, :groups, :gemspecs
4
+ class InDsl
5
+ attr_accessor :dependencies, :sources, :git_sources, :groups, :gemfiles
8
6
 
9
7
  # rubocop:disable Metrics/MethodLength
10
8
  def initialize(gemfile = nil)
@@ -13,14 +11,14 @@ module Gemat
13
11
  dsl.eval_gemfile(gemfile)
14
12
 
15
13
  class << dsl
16
- attr_accessor :sources, :git_sources, :groups, :gemspecs
14
+ attr_accessor :sources, :git_sources, :groups, :gemfiles
17
15
  end
18
16
 
19
17
  @dependencies = dsl.dependencies
20
18
  @sources = dsl.sources
21
19
  @git_sources = dsl.git_sources
22
20
  @groups = dsl.groups
23
- @gemspecs = dsl.gemspecs
21
+ @gemfiles = dsl.gemfiles
24
22
  end
25
23
  # rubocop:enable Metrics/MethodLength
26
24
  end
data/lib/md_formatter.rb CHANGED
@@ -1,19 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
4
-
5
3
  module Gemat
6
- class MdFormatter
7
- def initialize(url)
8
- @url = url
9
- end
4
+ class MdFormatter < Formatter
5
+ private
10
6
 
11
- def to_md
12
- print "\n\n"
13
- puts '| gem | Repo URL |'
14
- puts '| ---- | ---- |'
15
- @url.urls.each do |k, v|
16
- puts "| #{k} | #{v} |"
7
+ def gen_rows
8
+ @rows << "| #{@columns.map(&:column_name).join(' | ')} |"
9
+ @rows << "| #{Array.new(@columns.length, '----').join(' | ')} |"
10
+ @gems.each do |gem|
11
+ @rows << "| #{@columns.map { |dsl| dsl.call(gem) }.join(' | ')} |"
17
12
  end
18
13
  end
19
14
  end
data/lib/out_dsl.rb ADDED
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gemat
4
+ class OutDsl
5
+ # gem
6
+ INDEX = 'index'
7
+ NAME = 'name'
8
+ REPO_URI = 'repo_uri'
9
+ GEM_URI = 'gem_uri'
10
+
11
+ # Rubygems
12
+ DOC_URI = 'documentation_uri'
13
+ LATEST_VERSION = 'version'
14
+ AUTHORS = 'authors'
15
+
16
+ # GitHub
17
+ DESCRIPTION = 'description'
18
+ CREATED_AT = 'created_at'
19
+ UPDATED_AT = 'updated_at'
20
+ SIZE = 'size'
21
+ STAR = 'stargazers_count'
22
+ WATCH = 'watchers_count'
23
+ FORKS = 'forks'
24
+ LANGUAGE = 'language'
25
+ ARCHIVED = 'archived'
26
+ DISABLED = 'disabled'
27
+ OPEN_ISSUES_COUNT = 'open_issues_count'
28
+ NETWORK_COUNT = 'network_count'
29
+ SUBSCRIBERS = 'subscribers_count'
30
+
31
+ DEFAULT_COLUMNS = [INDEX, NAME, REPO_URI, DOC_URI, SIZE, STAR, CREATED_AT, UPDATED_AT].freeze
32
+
33
+ RUBYGEMS_RESPONSE_SOURCES = [NAME, DOC_URI, GEM_URI, LATEST_VERSION, AUTHORS].freeze
34
+ GITHUB_RESPONSE_SOURCES = [DESCRIPTION, CREATED_AT, UPDATED_AT, SIZE, STAR, WATCH,
35
+ FORKS, LANGUAGE, ARCHIVED, DISABLED, OPEN_ISSUES_COUNT,
36
+ NETWORK_COUNT, SUBSCRIBERS].freeze
37
+ GEM_SOURCES = [INDEX, REPO_URI].freeze
38
+ ALL_SOURCES = [].concat(GEM_SOURCES, RUBYGEMS_RESPONSE_SOURCES, GITHUB_RESPONSE_SOURCES).freeze
39
+
40
+ attr_accessor :column_name
41
+
42
+ def self.new_by_array(columns)
43
+ columns.map { |column| new(column) }
44
+ end
45
+
46
+ def self.create(columns, all: nil)
47
+ if columns
48
+ new_by_array(columns)
49
+ elsif all
50
+ new_by_array(ALL_SOURCES)
51
+ else
52
+ new_by_array(DEFAULT_COLUMNS)
53
+ end
54
+ end
55
+
56
+ def initialize(column_name)
57
+ @column_name = column_name
58
+
59
+ set_lambda
60
+ end
61
+
62
+ def call(gem)
63
+ @lambda.call(gem)
64
+ end
65
+
66
+ # rubocop:disable Metrics/MethodLength
67
+ def set_lambda
68
+ case @column_name
69
+ when *RUBYGEMS_RESPONSE_SOURCES
70
+ @lambda = rubygems_response(@column_name)
71
+ when *GITHUB_RESPONSE_SOURCES
72
+ @lambda = github_response(@column_name)
73
+ when *GEM_SOURCES
74
+ @lambda = gem_method(@column_name)
75
+ else
76
+ msg = <<-ERROR.gsub(/^\s+/, '')
77
+ Contain invalid column name `#{@column_name}`!
78
+ valid columns hint: [#{ALL_SOURCES}]
79
+ ERROR
80
+ raise StandardError, msg
81
+ end
82
+ end
83
+ # rubocop:enable Metrics/MethodLength
84
+
85
+ def rubygems_response(column)
86
+ ->(gem) { gem.rubygems[column] }
87
+ end
88
+
89
+ def github_response(column)
90
+ ->(gem) { gem.github[column] }
91
+ end
92
+
93
+ def gem_method(column)
94
+ ->(gem) { gem.send(column) }
95
+ end
96
+ end
97
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
- - kijimaD
7
+ - Kijima Daigo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-26 00:00:00.000000000 Z
11
+ date: 2021-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Export Gemfile several formats. CSV, Markdown etc.
55
+ description: Gemat is a Ruby gem for curating and exporing Gemfile to several formats!
56
56
  email:
57
57
  - norimaking777@gmail.com
58
58
  executables:
@@ -62,12 +62,15 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - exe/gemat
64
64
  - lib/csv_formatter.rb
65
- - lib/dsl.rb
65
+ - lib/fetcher.rb
66
+ - lib/formatter.rb
67
+ - lib/gem.rb
66
68
  - lib/gemat.rb
67
69
  - lib/gemat/cli.rb
68
70
  - lib/gemat/version.rb
69
- - lib/get_url.rb
71
+ - lib/in_dsl.rb
70
72
  - lib/md_formatter.rb
73
+ - lib/out_dsl.rb
71
74
  homepage: https://github.com/kijimaD/gemat
72
75
  licenses:
73
76
  - GPL3
@@ -93,5 +96,5 @@ requirements: []
93
96
  rubygems_version: 3.1.4
94
97
  signing_key:
95
98
  specification_version: 4
96
- summary: Export Gemfile several formats.
99
+ summary: Gemat is a Ruby gem for curating and exporing Gemfile to several formats!
97
100
  test_files: []
data/lib/get_url.rb DELETED
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Gemat
4
- class GetUrl
5
- def initialize(dsl)
6
- @dsl = dsl
7
- @urls = {}
8
- end
9
-
10
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
11
- def run
12
- pb = ProgressBar.create(total: @dsl.dependencies.length)
13
- @dsl.dependencies.each do |gem|
14
- sleep 0.1
15
-
16
- client = HTTPClient.new
17
- request = client.get(rubygems_api(gem))
18
- begin
19
- response = JSON.parse(request.body)
20
- rescue JSON::ParserError
21
- print "not found\n"
22
- next
23
- end
24
- # puts JSON.pretty_generate(response)
25
-
26
- match = github_url_match(response.dig('metadata', 'homepage_uri')) ||
27
- github_url_match(response['homepage_uri']) ||
28
- github_url_match(response['bug_tracker_uri']) ||
29
- github_url_match(response['source_code_uri'])
30
- next if match.nil?
31
-
32
- user = match[1]
33
- repo = match[2]
34
- gh_url = "https://github.com/#{user}/#{repo}"
35
- @urls[gem.name] = gh_url
36
-
37
- pb.increment
38
- end
39
- end
40
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
41
-
42
- def github_url_match(url)
43
- reg = %r{https://github.com/([\w\-]+)/([\w\-]+)}
44
- reg.match(url)
45
- end
46
-
47
- def rubygems_api(gem)
48
- "https://rubygems.org/api/v1/gems/#{gem.name}.json"
49
- end
50
-
51
- def urls
52
- @urls.sort.to_h
53
- end
54
- end
55
- end