Module: Alphabetize

Defined in:
lib/alphabetize.rb

Overview

The Gemfile is parsed into file_chunks array Each chunk has the following attributes:

:type - :static (don't sort them), :regular, :group (official group, use the header, end correctly)
:header - the line that prepends the chunk
:gem_hash - the hash of gems that belong to this chunk

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.alphabetize_fileObject



13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/alphabetize.rb', line 13

def self.alphabetize_file
  filename = "Gemfile"
  file = File.new(filename)

  lines = file.readlines
  puts "File Lines: #{lines.inspect}"
  file_chunks = make_chunky(lines)

  puts "Chunks: #{file_chunks.inspect}"

  backupFilename = "old_#{filename}"
  %x( mv #{filename} #{backupFilename})

  file = File.open(filename, 'w')
  # file.truncate(0) # clear the file

  file_chunks.each do |chunk|
    if chunk[:type] == :static
      print_gem_hash(file, chunk[:gem_hash])
    elsif chunk[:type] == :group
      file.puts(chunk[:header])
      print_gem_hash(file, chunk[:gem_hash])
      file.puts("end")
    elsif chunk[:type] == :regular
      print_gem_hash(file, chunk[:gem_hash])
    else
      raise "This chunk has some wierd type!"
    end


    # chunk.keys.sort.each do |gem|
    #   line = chunk[gem]
    #   file.puts(line)
    # end
    file.puts("")
  end

  file.close

end