gemat 0.1.3 → 0.2.0

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: fff413b0bfd43a7b693a244946e15ede730a479ec4be7260605e71d4001cc72a
4
- data.tar.gz: 325d18312000d035eda2c45c96feeab44245515e75de9ff6139e7f72661114e8
3
+ metadata.gz: 63a846e7020b19c4f093cf7450fa5a3798f78b3b62ff30341e62c8aa58fcd746
4
+ data.tar.gz: 69dc2ce793be644524db9cd29b624792f0421bd9993dc641ae6022e97dff08fa
5
5
  SHA512:
6
- metadata.gz: bfff75358ea7333d62a15ea928c3d2e4e65742097cde1282ae2f8e1c3113e7480773d0f51dfc9b4619fd75f5c4d332d945313b6b170144be6444a9d3ac23dc86
7
- data.tar.gz: ae0556e9407890301bcd97692aad4008da235ea943a45e5bb9de28768e9ac5de2628b42498e7f46fe3cf2e0d982f16d212331a30e4c74e622084694f8f17b992
6
+ metadata.gz: bf94214e16502f17e97e950c67ee359bca850d13c74487311e759915b4e8e1a52aeea96fd23c39b78a85664e9dfde739842d0573a4dfdeacafb39f6c1a2ec1f9
7
+ data.tar.gz: cbdd66759a759c7f8b236f0fd4f5c3b67abc47dea1a43696ff3607a31f289ad573d7717d96610ed9a7c21185cf7202188823d60267efd113a7f7bb7ba8607b5e
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(gems, output = nil)
8
- @gems = gems
9
- @output = output
10
- @rows = []
11
- gen_rows
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
7
  def gen_rows
28
- @rows << ['gem', 'Repo URL']
8
+ @rows << @columns.map(&:column_name).join(',')
29
9
  @gems.each do |gem|
30
- @rows << [gem.name, gem.repo_url]
10
+ @rows << @columns.map { |dsl| dsl.call(gem) }.join(',')
31
11
  end
32
12
  end
33
13
  end
data/lib/fetcher.rb CHANGED
@@ -11,31 +11,53 @@ module Gemat
11
11
 
12
12
  def run
13
13
  pb = ProgressBar.create(total: @dsl.dependencies.length)
14
- @dsl.dependencies.each do |gem|
15
- sleep 0.1
14
+ @dsl.dependencies.each_with_index do |dependency, idx|
16
15
  pb.increment
17
16
 
18
- response = fetch(gem)
17
+ response = fetch_rubygems(dependency)
19
18
  next unless response
20
19
 
21
- @gems << Gem.new(response)
20
+ create_gem(response, idx)
21
+ sleep 0.2
22
22
  end
23
23
  end
24
24
 
25
25
  private
26
26
 
27
- def fetch(gem)
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 = []
28
48
  client = HTTPClient.new
29
- request = client.get(rubygems_api(gem.name))
49
+ request = client.get(uri)
30
50
  begin
31
51
  response = JSON.parse(request.body)
32
52
  rescue JSON::ParserError
33
- print "\n#{gem.name} is not found on Rubygems..."
53
+ failed << gem.name
34
54
  return
35
55
  end
36
- # puts JSON.pretty_generate(response)
56
+
57
+ print "#{failed.join(',')}: failed fetcing info." unless failed.empty?
37
58
  response
38
59
  end
60
+ # rubocop:enable Metrics/MethodLength
39
61
 
40
62
  def rubygems_api(name)
41
63
  "https://rubygems.org/api/v1/gems/#{name}.json"
data/lib/formatter.rb ADDED
@@ -0,0 +1,49 @@
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
+ set_column_name_length
11
+ gen_rows
12
+ end
13
+
14
+ def run
15
+ if @write_path
16
+ File.open(@write_path, 'w') do |file|
17
+ each_write(@rows) { |string| file << string }
18
+ end
19
+ else
20
+ print "\n\n"
21
+ each_write(@rows) { |string| print string }
22
+ print "\n"
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def each_write(rows, &proc)
29
+ rows.each do |row|
30
+ proc.call(row)
31
+ proc.call("\n")
32
+ end
33
+ end
34
+
35
+ # rubocop:disable Metrics/AbcSize
36
+ def set_column_name_length
37
+ rows = []
38
+ rows << @columns.map(&:column_name)
39
+ @gems.each do |gem|
40
+ rows << @columns.map { |dsl| dsl.call(gem) }
41
+ end
42
+ maxes = rows.transpose.map { |row| row.max { |a, b| a.to_s.length <=> b.to_s.length }.to_s.length }
43
+ maxes.zip(@columns) do |max, column|
44
+ column.max_length = max
45
+ end
46
+ end
47
+ # rubocop:enable Metrics/AbcSize
48
+ end
49
+ end
data/lib/gem.rb CHANGED
@@ -2,19 +2,18 @@
2
2
 
3
3
  module Gemat
4
4
  class Gem
5
- def initialize(response)
6
- @response = response
7
- end
5
+ attr_accessor :index, :rubygems, :github
8
6
 
9
- def name
10
- @response['name']
7
+ def initialize(rubygems)
8
+ @rubygems = rubygems
9
+ @index = 0
11
10
  end
12
11
 
13
- def repo_url
14
- match = github_url_match([@response.dig('metadata', 'homepage_uri'),
15
- @response['homepage_uri'],
16
- @response['bug_tracker_uri'],
17
- @response['source_code_uri']])
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']])
18
17
  return if match.nil?
19
18
 
20
19
  user = match[1]
@@ -24,10 +23,10 @@ module Gemat
24
23
 
25
24
  private
26
25
 
27
- def github_url_match(urls)
28
- reg = %r{https://github.com/([\w\-]+)/([\w\-]+)}
29
- urls.each do |url|
30
- return reg.match(url) if reg.match(url)
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)
31
30
  end
32
31
  end
33
32
  end
data/lib/gemat/cli.rb CHANGED
@@ -1,19 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thor'
4
-
5
3
  module Gemat
6
4
  class Cli < Thor
5
+ class_options input: :string, output: :string, columns: :array, all: :boolean
6
+
7
7
  desc 'csv', 'export Gemfile to CSV file'
8
- method_options input: :string, output: :string
9
8
  def csv
10
- Gemat.run(options) { |gems| CsvFormatter.new(gems, options[:output]).to_csv }
9
+ command(options, __method__)
11
10
  end
12
11
 
13
12
  desc 'md', 'export Gemfile to markdown'
14
- method_options input: :string, output: :string
15
13
  def md
16
- Gemat.run(options) { |gems| MdFormatter.new(gems, options[:output]).to_md }
14
+ command(options, __method__)
15
+ end
16
+
17
+ desc 'org', 'export Gemfile to org'
18
+ def org
19
+ command(options, __method__)
20
+ end
21
+
22
+ no_tasks do
23
+ def command(options, method_name)
24
+ klass = "Gemat::#{method_name.capitalize}Formatter"
25
+ Gemat.run(options) do |gems, columns|
26
+ Object.const_get(klass).new(gems, columns, write_path: options[:output]).run
27
+ end
28
+ end
17
29
  end
18
30
  end
19
31
  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.3'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/gemat.rb CHANGED
@@ -1,14 +1,19 @@
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
 
7
9
  require_relative 'in_dsl'
10
+ require_relative 'out_dsl'
8
11
  require_relative 'fetcher'
9
12
  require_relative 'gem'
13
+ require_relative 'formatter'
10
14
  require_relative 'csv_formatter'
11
15
  require_relative 'md_formatter'
16
+ require_relative 'org_formatter'
12
17
  require_relative 'gemat/version'
13
18
  require_relative 'gemat/cli'
14
19
 
@@ -18,9 +23,10 @@ module Gemat
18
23
  class Gemat
19
24
  def self.run(options = {}, &block)
20
25
  dsl = InDsl.new(options[:input])
26
+ outdsls = OutDsl.create(options[:columns], all: options[:all])
21
27
  fetcher = Fetcher.new(dsl)
22
28
  fetcher.run
23
- block.call(fetcher.gems)
29
+ block.call(fetcher.gems, outdsls)
24
30
  end
25
31
  end
26
32
  end
data/lib/in_dsl.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler'
4
-
5
3
  module Gemat
6
4
  class InDsl
7
5
  attr_accessor :dependencies, :sources, :git_sources, :groups, :gemfiles
data/lib/md_formatter.rb CHANGED
@@ -1,39 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
4
-
5
3
  module Gemat
6
- class MdFormatter
7
- def initialize(gems, output = nil)
8
- @gems = gems
9
- @output = output
10
- @rows = []
11
- gen_rows
12
- end
13
-
14
- def to_md
15
- if @output
16
- File.open(@output, 'w') { |file| each_write(@rows) { |string| file.<< string } }
17
- else
18
- print "\n\n"
19
- each_write(@rows) { |string| print string }
20
- end
21
- end
22
-
4
+ class MdFormatter < Formatter
23
5
  private
24
6
 
25
7
  def gen_rows
26
- @rows << '| gem | Repo URL |'
27
- @rows << '| ---- | ---- |'
8
+ @rows << "| #{@columns.map(&:column_name).join(' | ')} |"
9
+ @rows << "| #{Array.new(@columns.length, '----').join(' | ')} |"
28
10
  @gems.each do |gem|
29
- @rows << "| #{gem.name} | #{gem.repo_url} |"
30
- end
31
- end
32
-
33
- def each_write(rows, &proc)
34
- rows.each do |row|
35
- proc.call(row)
36
- proc.call("\n")
11
+ @rows << "| #{@columns.map { |dsl| dsl.call(gem) }.join(' | ')} |"
37
12
  end
38
13
  end
39
14
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gemat
4
+ class OrgFormatter < Formatter
5
+ # private
6
+
7
+ # rubocop:disable Metrics/AbcSize
8
+ def gen_rows
9
+ @rows << "| #{@columns.map { |column| column.column_name.ljust(column.max_length) }.join(' | ')} |"
10
+ @rows << "|#{@columns.map { |column| '-' * (column.max_length + 2) }.join('+')}|"
11
+ @gems.each do |gem|
12
+ @rows << "| #{@columns.map { |column| column.call(gem).to_s.ljust(column.max_length) }.join(' | ')} |"
13
+ end
14
+ end
15
+ # rubocop:enable Metrics/AbcSize
16
+ end
17
+ 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, :max_length
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.3
4
+ version: 0.2.0
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-27 00:00:00.000000000 Z
11
+ date: 2021-12-02 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:
@@ -63,12 +63,15 @@ files:
63
63
  - exe/gemat
64
64
  - lib/csv_formatter.rb
65
65
  - lib/fetcher.rb
66
+ - lib/formatter.rb
66
67
  - lib/gem.rb
67
68
  - lib/gemat.rb
68
69
  - lib/gemat/cli.rb
69
70
  - lib/gemat/version.rb
70
71
  - lib/in_dsl.rb
71
72
  - lib/md_formatter.rb
73
+ - lib/org_formatter.rb
74
+ - lib/out_dsl.rb
72
75
  homepage: https://github.com/kijimaD/gemat
73
76
  licenses:
74
77
  - GPL3
@@ -94,5 +97,5 @@ requirements: []
94
97
  rubygems_version: 3.1.4
95
98
  signing_key:
96
99
  specification_version: 4
97
- summary: Export Gemfile several formats.
100
+ summary: Gemat is a Ruby gem for curating and exporing Gemfile to several formats!
98
101
  test_files: []