Module: HappyGemfile
- Defined in:
- lib/happy_gemfile.rb,
lib/happy_gemfile/version.rb
Constant Summary collapse
- VERSION =
"0.1.2"
Class Method Summary collapse
- .all ⇒ Object
- .alphabetize(lines = nil) ⇒ Object
- .gemfile ⇒ Object
- .group_line(group) ⇒ Object
- .group_name(line) ⇒ Object
- .is?(line, type) ⇒ Boolean
-
.is_comment?(line) ⇒ Boolean
HELPERS.
- .organize_groups(lines = nil) ⇒ Object
- .replace_gemfile(lines) ⇒ Object
- .wipe_comments(lines = nil) ⇒ Object
Class Method Details
.all ⇒ Object
5 6 7 8 9 10 |
# File 'lib/happy_gemfile.rb', line 5 def self.all gemfile = wipe_comments gemfile = organize_groups gemfile gemfile = alphabetize gemfile replace_gemfile gemfile end |
.alphabetize(lines = nil) ⇒ Object
12 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 |
# File 'lib/happy_gemfile.rb', line 12 def self.alphabetize lines=nil lines ||= gemfile gem_groups = [[]] gem_indexes = [[]] group_count = 0 in_group = false lines.each_with_index do |line, index| if is?(line, 'gem') unless in_group gem_groups[0] << line gem_indexes[0] << index else gem_groups[group_count] << line gem_indexes[group_count] << index end elsif is?(line, 'group') in_group = true group_count += 1 gem_groups << [] gem_indexes << [] elsif is? line, 'end' in_group = false end end gem_groups.map{|group| group.sort}.each_with_index do |group, group_index| group.each_with_index do |line, line_index| lines[gem_indexes[group_index][line_index]] = line end end lines end |
.gemfile ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/happy_gemfile.rb', line 114 def self.gemfile unless File.exists? "Gemfile" puts "There doesn't appear to be a Gemfile... not sure what to do." return false end File.readlines "Gemfile" end |
.group_line(group) ⇒ Object
110 111 112 |
# File 'lib/happy_gemfile.rb', line 110 def self.group_line group group.to_s.split('_').map{|g| ":#{g}"}.join(', ') end |
.group_name(line) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/happy_gemfile.rb', line 102 def self.group_name line line.match(/group(.*)do/) .to_a[1] .strip.gsub(', ', '_') .gsub(':', '') .to_sym end |
.is?(line, type) ⇒ Boolean
98 99 100 |
# File 'lib/happy_gemfile.rb', line 98 def self.is? line, type line.split(' ').first == type end |
.is_comment?(line) ⇒ Boolean
HELPERS
94 95 96 |
# File 'lib/happy_gemfile.rb', line 94 def self.is_comment? line is? line, '#' end |
.organize_groups(lines = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/happy_gemfile.rb', line 54 def self.organize_groups lines=nil lines ||= gemfile groups = {general: []} current_group = :general lines.each do |line| if is? line, 'gem' groups[current_group] << line.gsub("\n", '') elsif is? line, 'group' current_group = group_name line groups[current_group] ||= [] elsif is? line, 'end' current_group = :general else groups[:not_gems] ||= [] groups[:not_gems] << line.gsub("\n", '') end end groups.each {|key, lines| lines.delete_if {|line| ["\n", ''].include? line} } organized = [] groups[:not_gems].each {|line| organized << line << "\n"} organized << "\n" groups[:general].each {|line| organized << line << "\n"} organized << "\n" (groups.keys - [:general, :not_gems]).each do |group| organized << "group #{group_line(group)} do" << "\n" groups[group].each {|line| organized << "#{line}" << "\n"} organized << 'end' << "\n\n" end organized end |
.replace_gemfile(lines) ⇒ Object
122 123 124 |
# File 'lib/happy_gemfile.rb', line 122 def self.replace_gemfile lines File.open("Gemfile", 'w') { |file| file.write(lines.join('')) } end |
.wipe_comments(lines = nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/happy_gemfile.rb', line 48 def self.wipe_comments lines=nil lines ||= gemfile lines.delete_if{|line| is_comment?(line)} lines end |