grub 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 40e937ffd04fdb2cf67a2c9fc765a402e54142df
4
- data.tar.gz: 4eba708a3e8b9784a9772db961b9310a302cc692
3
+ metadata.gz: 2e00e745fa6f31b1d45dd4cec754330d5e8a00eb
4
+ data.tar.gz: 284f19215ccee5756bc2fec8dee10dd5dad01ac5
5
5
  SHA512:
6
- metadata.gz: f4043ba1b3ac1b489331bed1325011bcfe68333461b52f1819174781bb43ae68801c3b85ec35e6217fdc320fc3dd6d7b3dd624f0c733daaa6ace66ae9f1bdada
7
- data.tar.gz: cca141c3cd9efeab215408db737a827664ada4e456877b7d6a8cd8bc197fd3fec1e2a1752076412900b3383f297d6c6ebdc252755da2159aa6b9c76281a7b4b6
6
+ metadata.gz: 3d043f4f9d76d84fec7d7a4063fe6fe81095721d0ae4b48aac92ea06ae01df7fb98eeb7757d041384f97a4af9792ce855fbfbd885efe26f93a1db50e2977f55e
7
+ data.tar.gz: be5d9fa170ef7e7899554fc8ac4f13dc8f4960f3c24091b3980b08094f7de5df47f84c466f7da1395a7297f08bbd4ecb3243a65dd560a655a5a93fdbfb5a5e47
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ vendor
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Grub::VERSION
9
9
  spec.authors = ["Ivan Tse"]
10
10
  spec.email = ["ivan.tse1@gmail.com"]
11
- spec.summary = %q{Add comments to your Gemfile with each dependency's description.}
11
+ spec.summary = Grub::DESCRIPTION
12
12
  spec.description = <<-DESCRIPTION.gsub(/^\s+/, "").gsub(/\n/, " ")
13
13
  Clarify your dependencies by adding a detailed comment to each line in Gemfile specifying the
14
14
  gem's summary and its website if any.
@@ -1,13 +1,32 @@
1
+ require "grub/options"
2
+
1
3
  module Grub
2
4
  class CLI
3
- def run(*args)
5
+ def run(args)
6
+ options = Options.new.parse!(args)
7
+ if args.empty?
8
+ run_for_gemfile(options)
9
+ else
10
+ run_for_gem(args.pop, options)
11
+ end
12
+ end
13
+
14
+ def run_for_gemfile(options = {})
4
15
  Bundler.configure
5
- gemfile = Gemfile.new
16
+ gemfile = Gemfile.new(Bundler.default_gemfile, options)
6
17
  gemfile.parse
7
18
  unless gemfile.gem_lines.empty?
8
- SpecFinder.new(gemfile.gem_lines).find_specs
19
+ SpecFinder.find_specs_for(gemfile.gem_lines)
9
20
  gemfile.write_comments
10
21
  end
11
22
  end
23
+
24
+ def run_for_gem(gem_name, options = {})
25
+ gem_line = GemLine.new(name: gem_name, options: options)
26
+ SpecFinder.find_specs_for(gem_line)
27
+ info = gem_line.info
28
+ info = "No information to show" if info.strip.empty?
29
+ puts info
30
+ end
12
31
  end
13
32
  end
@@ -1,34 +1,63 @@
1
1
  module Grub
2
2
  class GemLine
3
3
 
4
- attr_accessor :original_line, :location, :prev_line_comment, :name, :spec
4
+ attr_accessor :name, :original_line, :location, :prev_line_comment, :spec, :options
5
5
 
6
- def initialize(name:, original_line:, location:, prev_line_comment: nil)
6
+ def initialize(name:, original_line: nil, location: nil, prev_line_comment: nil, options: {})
7
7
  @name = name
8
8
  @original_line = original_line
9
9
  @location = location
10
10
  @prev_line_comment = prev_line_comment
11
+ @options = options
11
12
  end
12
13
 
13
14
  def comment
14
- leading_spaces_count = original_line.length - original_line.lstrip.length
15
15
  leading_spaces = original_line[0..leading_spaces_count - 1] if leading_spaces_count > 0
16
- comment = "# #{description}"
17
- comment << " (#{website})" unless website.nil? || website.empty?
18
- comment << "\n"
19
- "#{leading_spaces}#{comment}"
16
+ comment = "#{leading_spaces}# #{info}"
17
+ end
18
+
19
+ def info
20
+ output = if options[:website_only]
21
+ website
22
+ elsif options[:description_only]
23
+ description
24
+ else
25
+ description_and_website
26
+ end
27
+ output << "\n"
20
28
  end
21
29
 
22
30
  def should_insert?
23
- prev_line_comment.nil? || !prev_line_comment.include?(comment)
31
+ !info.empty? && !already_added_comment && !existing_comment_option
32
+ end
33
+
34
+ private
35
+
36
+ def already_added_comment
37
+ prev_line_comment && prev_line_comment.include?(comment)
38
+ end
39
+
40
+ # if there exists a prev_line_comment and the user has specified new_comments_only
41
+ def existing_comment_option
42
+ prev_line_comment && options[:new_comments_only]
43
+ end
44
+
45
+ def leading_spaces_count
46
+ original_line.length - original_line.lstrip.length
24
47
  end
25
48
 
26
49
  def description
27
- spec.summary if spec
50
+ "#{spec.summary}" if spec
28
51
  end
29
52
 
30
53
  def website
31
- spec.homepage if spec
54
+ "#{spec.homepage.to_s}" if spec
55
+ end
56
+
57
+ def description_and_website
58
+ output = "#{description}"
59
+ output << " (#{website})" unless website.empty?
60
+ output
32
61
  end
33
62
 
34
63
  end
@@ -1,26 +1,29 @@
1
1
  module Grub
2
2
  class Gemfile
3
3
 
4
- GEM_LINE_REGEX = /\A\s*gem[\s(]+["']([^'"]*)["']/.freeze
4
+ GEM_LINE_REGEX = /\A\s*gem[\s(]+["'](?<name>[^'"]*)["']/.freeze
5
5
 
6
- attr_accessor :gemfile_path, :gem_lines, :source
6
+ attr_accessor :gemfile_path, :gem_lines, :source, :options
7
7
 
8
- def initialize(gemfile_path = Bundler.default_gemfile)
8
+ def initialize(gemfile_path, options = {})
9
9
  @gemfile_path = gemfile_path
10
10
  @source = []
11
11
  @gem_lines = []
12
+ @options = options
12
13
  end
13
14
 
14
15
  def parse
15
16
  self.source = File.readlines(gemfile_path)
16
17
  source.each_with_index do |line, i|
17
- if (match = GEM_LINE_REGEX.match(line))
18
- prev_line_comment = source[i - 1].strip.start_with?("#") ? source[i - 1] : nil
18
+ if match = GEM_LINE_REGEX.match(line)
19
+ prev_line = source[i - 1]
20
+ prev_line_comment = prev_line if is_line_a_comment?(prev_line)
19
21
  self.gem_lines << GemLine.new(
20
- name: match[1],
22
+ name: match[:name],
21
23
  original_line: line,
22
24
  location: i,
23
- prev_line_comment: prev_line_comment
25
+ prev_line_comment: prev_line_comment,
26
+ options: options
24
27
  )
25
28
  end
26
29
  end
@@ -32,5 +35,12 @@ module Grub
32
35
  end
33
36
  File.write(gemfile_path, source.join)
34
37
  end
38
+
39
+ private
40
+
41
+ def is_line_a_comment?(line)
42
+ line.strip.start_with?("#")
43
+ end
44
+
35
45
  end
36
46
  end
@@ -0,0 +1,66 @@
1
+ require "optparse"
2
+
3
+ module Grub
4
+ class Options
5
+
6
+ attr_accessor :options, :options_parser
7
+
8
+ def initialize
9
+ @options = {}
10
+ @options_parser = OptionParser.new do |opts|
11
+ add_banner(opts)
12
+ add_tail_options(opts)
13
+ add_info_options(opts)
14
+ add_gemfile_comment_options(opts)
15
+ end
16
+ end
17
+
18
+ def parse!(args)
19
+ options_parser.parse!(args)
20
+ validate_options
21
+ options
22
+ end
23
+
24
+ private
25
+
26
+ def validate_options
27
+ info_flags = options[:website_only] && options[:description_only]
28
+ raise ArgumentError, "Cannot set both --website-only and --description-only flags" if info_flags
29
+ end
30
+
31
+ def add_info_options(opts)
32
+ opts.on("--website-only", "Only output the website") do
33
+ options[:website_only] = true
34
+ end
35
+ opts.on("--description-only", "Only output the description") do
36
+ options[:description_only] = true
37
+ end
38
+ end
39
+
40
+ def add_gemfile_comment_options(opts)
41
+ opts.on("--new-comments-only", "Only add a comment to gemfile if there isn't a comment already") do
42
+ options[:new_comments_only] = true
43
+ end
44
+ end
45
+
46
+ def add_banner(opts)
47
+ opts.banner = <<-BANNER.gsub(/\A\s{8}/, '')
48
+ #{Grub::DESCRIPTION}
49
+ Usage: #{opts.program_name} [options]
50
+ #{opts.program_name} [gem name]
51
+ BANNER
52
+ end
53
+
54
+ def add_tail_options(opts)
55
+ opts.on_tail("-h", "--help", "Show this message") do
56
+ puts opts
57
+ exit
58
+ end
59
+ opts.on_tail("-v", "--version", "Show version") do
60
+ puts Grub::VERSION
61
+ exit
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -1,32 +1,36 @@
1
1
  module Grub
2
- class SpecFinder
2
+ module SpecFinder
3
+ extend self
3
4
 
4
- attr_reader :gem_lines
5
-
6
- def initialize(gem_lines)
7
- @gem_lines = gem_lines
5
+ def find_specs_for(gem_lines)
6
+ gem_lines = Array(gem_lines)
7
+ find_matching_specs_for(gem_lines)
8
+ gems_to_fetch = gem_lines.select { |gem_line| gem_line.spec.nil? }
9
+ fetch_specs_for(gems_to_fetch) if gems_to_fetch.any?
8
10
  end
9
11
 
10
- def find_specs
12
+ def find_matching_specs_for(gem_lines)
11
13
  gem_lines.each do |line|
12
14
  matching_specs = Gem::Dependency.new(line.name).matching_specs
13
15
  line.spec = matching_specs.first if matching_specs.any?
14
16
  end
17
+ end
15
18
 
16
- gems_to_fetch = gem_lines.select { |gem_line| gem_line.spec.nil? }
17
-
18
- if gems_to_fetch.any?
19
- fetcher = Bundler::Fetcher.new(Gem.sources.first.uri)
20
- versions, _ = fetcher.send(:fetch_dependency_remote_specs, gems_to_fetch.collect(&:name))
21
- gems_to_fetch.each do |gem_line|
22
- print "."
23
- version = versions.find{ |v| v.first == gem_line.name }[1]
24
- spec = fetcher.fetch_spec([gem_line.name, version])
25
- gem_line.spec = spec
26
- end
27
- print "\n"
19
+ def fetch_specs_for(gem_lines)
20
+ print "Fetching gem metadata..."
21
+ fetcher = Bundler::Fetcher.new(Gem.sources.first.uri)
22
+ versions, _ = fetcher.send(:fetch_dependency_remote_specs, gem_lines.collect(&:name))
23
+ gem_lines.each do |gem_line|
24
+ print "."
25
+ gem_versions = versions.select { |v| v.first == gem_line.name }
26
+ version = find_latest_version(gem_versions)
27
+ gem_line.spec = fetcher.fetch_spec([gem_line.name, version])
28
28
  end
29
+ print "\n"
29
30
  end
30
31
 
32
+ def find_latest_version(versions)
33
+ versions.sort_by { |v| v[1] }.last[1]
34
+ end
31
35
  end
32
36
  end
@@ -1,3 +1,4 @@
1
1
  module Grub
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2".freeze
3
+ DESCRIPTION = "Add comments to your Gemfile with each dependency's description.".freeze
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Tse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,6 +58,7 @@ files:
58
58
  - lib/grub/cli.rb
59
59
  - lib/grub/gem_line.rb
60
60
  - lib/grub/gemfile.rb
61
+ - lib/grub/options.rb
61
62
  - lib/grub/spec_finder.rb
62
63
  - lib/grub/version.rb
63
64
  homepage: https://github.com/ivantsepp/grub