Module: HappyGemfile
- Defined in:
- lib/happy_gemfile.rb,
lib/happy_gemfile/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
- .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
.alphabetize(lines = nil) ⇒ Object
4 5 6 7 8 9 10 11 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 |
# File 'lib/happy_gemfile.rb', line 4 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
106 107 108 109 110 111 112 |
# File 'lib/happy_gemfile.rb', line 106 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
102 103 104 |
# File 'lib/happy_gemfile.rb', line 102 def self.group_line group group.to_s.split('_').map{|g| ":#{g}"}.join(', ') end |
.group_name(line) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/happy_gemfile.rb', line 94 def self.group_name line line.match(/group(.*)do/) .to_a[1] .strip.gsub(', ', '_') .gsub(':', '') .to_sym end |
.is?(line, type) ⇒ Boolean
90 91 92 |
# File 'lib/happy_gemfile.rb', line 90 def self.is? line, type line.split(' ').first == type end |
.is_comment?(line) ⇒ Boolean
HELPERS
86 87 88 |
# File 'lib/happy_gemfile.rb', line 86 def self.is_comment? line is? line, '#' end |
.organize_groups(lines = nil) ⇒ Object
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 75 76 77 78 79 80 81 82 |
# File 'lib/happy_gemfile.rb', line 46 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
114 115 116 |
# File 'lib/happy_gemfile.rb', line 114 def self.replace_gemfile lines File.open("Gemfile", 'w') { |file| file.write(lines.join('')) } end |
.wipe_comments(lines = nil) ⇒ Object
40 41 42 43 44 |
# File 'lib/happy_gemfile.rb', line 40 def self.wipe_comments lines=nil lines ||= gemfile lines.delete_if{|line| is_comment?(line)} lines end |