Class: GemSorter::Sorter

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_sorter.rb

Instance Method Summary collapse

Constructor Details

#initialize(task_config) ⇒ Sorter

Returns a new instance of Sorter.



18
19
20
21
22
23
24
# File 'lib/gem_sorter.rb', line 18

def initialize(task_config)
  @config = task_config
  @filepath = task_config.gemfile_path
  @content = File.read(task_config.gemfile_path)
  @versions = nil
  @version_updates = []
end

Instance Method Details

#sortObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gem_sorter.rb', line 26

def sort
  ruby_line = extract_ruby_line(@content)
  
  parts = @content.split(/^group/)
  main_section = parts.shift
  group_sections = parts

  source_line, gems, _ = process_main_section(main_section)

  update_gem_summaries(gems) if @config.update_comments
  if @config.force_update
    force_update_versions(gems)
  elsif @config.update_versions
    update_version_text(gems)
  end
  remove_versions(gems) if @config.remove_versions
  sorted_gems = sort_gem_blocks(gems)

  result = []
  result << source_line
  if ruby_line
    result << ''
    result << ruby_line
  end
  result << ''
  result.concat(sorted_gems)
  result << ''

  group_sections.each do |section|
    group_gems = process_group_section(section)
    update_gem_summaries(group_gems) if @config.update_comments
    if @config.force_update
      force_update_versions(group_gems)
    elsif @config.update_versions
      update_version_text(group_gems)
    end
    remove_versions(group_gems) if @config.remove_versions
    result << "group#{section.split("\n").first}"
    result.concat(sort_gem_blocks(group_gems).map { |line| "  #{line}" })
    result << 'end'
    result << ''
  end

  result = transform_to_double_quotes(result) if @config.use_double_quotes

  print_version_updates if @config.force_update && !@version_updates.empty?

  result.join("\n")
end