Class: GemSort::Sorter
- Inherits:
-
Object
- Object
- GemSort::Sorter
- Defined in:
- lib/gem_sort/sorter.rb
Instance Method Summary collapse
- #extract_blocks!(lines, begin_block_condition, nested = false) ⇒ Object
- #extract_line!(lines, condition) ⇒ Object
- #inject_between(array, divider) ⇒ Object
- #magic_comment ⇒ Object
- #read_gemfile ⇒ Object
- #removal_comment_and_blank(text) ⇒ Object
- #sort! ⇒ Object
- #sort_block_gems(block) ⇒ Object
- #source_gemfile ⇒ Object
- #unwrap_block(block) ⇒ Object
- #wrap_block(block, inside) ⇒ Object
- #write_gemfile(text) ⇒ Object
Instance Method Details
#extract_blocks!(lines, begin_block_condition, nested = false) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/gem_sort/sorter.rb', line 3 def extract_blocks!(lines, begin_block_condition, nested = false) end_block_condition = -> (line) { (nested ? line.strip : line).start_with?("end") } blocks = [] while lines.select(&begin_block_condition).length > 0 begin_block_index = lines.index(&begin_block_condition) block_length = lines .slice(begin_block_index..lines.length) .index(&end_block_condition) block_length += 1 blocks << lines.slice!(begin_block_index, block_length) end blocks end |
#extract_line!(lines, condition) ⇒ Object
20 21 22 23 24 |
# File 'lib/gem_sort/sorter.rb', line 20 def extract_line!(lines, condition) target_line = lines.select(&condition).first lines.delete_if{ |line| line == target_line } if target_line != nil target_line end |
#inject_between(array, divider) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/gem_sort/sorter.rb', line 42 def inject_between(array, divider) array.each_with_index.inject([]) { |acc, (item, i)| acc << item acc << divider if array.last != item acc } end |
#magic_comment ⇒ Object
62 63 64 |
# File 'lib/gem_sort/sorter.rb', line 62 def magic_comment "# frozen_string_literal: true\n" end |
#read_gemfile ⇒ Object
54 55 56 |
# File 'lib/gem_sort/sorter.rb', line 54 def read_gemfile source_gemfile.read.split("\n").select{ |line| line != "" } end |
#removal_comment_and_blank(text) ⇒ Object
58 59 60 |
# File 'lib/gem_sort/sorter.rb', line 58 def removal_comment_and_blank(text) text.gsub(/#.*$/,'').gsub(/\n(\s| )*\n/, "\n") end |
#sort! ⇒ Object
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gem_sort/sorter.rb', line 70 def sort! gemfile = read_gemfile group_blocks = extract_blocks!(gemfile, -> (line) { line.start_with?("group") }).map{ |group_block| sort_block_gems(group_block) } source_blocks = extract_blocks!(gemfile, -> (line) { line.start_with?("source ") && line.end_with?("do") }).map{ |source_block| source_inside = unwrap_block(source_block) inside_group_blocks = extract_blocks!(source_inside, -> (line) { line.strip.start_with?("group") }, true).map{ |inside_group_block| sort_block_gems(inside_group_block) } inside = source_inside.sort + inject_between(inside_group_blocks, nil) wrap_block(source_block, inside) } source_line = extract_line!(gemfile, -> (line) { line.start_with?('source') && !line.end_with?('do') }) ruby_line = extract_line!(gemfile, -> (line) { line.start_with?('ruby') }) rails_line = extract_line!(gemfile, -> (line) { line.start_with?('gem "rails"') || line.start_with?("gem 'rails'") }) sorted_text = inject_between([ source_line, [ruby_line, rails_line], gemfile.sort, inject_between(group_blocks, nil), inject_between(source_blocks, nil) ], nil).flatten.join("\n") write_gemfile(sorted_text) end |
#sort_block_gems(block) ⇒ Object
26 27 28 |
# File 'lib/gem_sort/sorter.rb', line 26 def sort_block_gems(block) wrap_block(block, unwrap_block(block).sort) end |
#source_gemfile ⇒ Object
50 51 52 |
# File 'lib/gem_sort/sorter.rb', line 50 def source_gemfile ::Rails.root.join('Gemfile').open('r+') end |
#unwrap_block(block) ⇒ Object
38 39 40 |
# File 'lib/gem_sort/sorter.rb', line 38 def unwrap_block(block) block[1..block.length - 2] end |
#wrap_block(block, inside) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/gem_sort/sorter.rb', line 30 def wrap_block(block, inside) [ block.first, *inside, block.last ] end |
#write_gemfile(text) ⇒ Object
66 67 68 |
# File 'lib/gem_sort/sorter.rb', line 66 def write_gemfile(text) source_gemfile.write(magic_comment + removal_comment_and_blank(text)) end |