Skip to content

Instantly share code, notes, and snippets.

@joshmn
Created August 1, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmn/294a0a02ba3c78b06dbccbbaa5f0fbbc to your computer and use it in GitHub Desktop.
Save joshmn/294a0a02ba3c78b06dbccbbaa5f0fbbc to your computer and use it in GitHub Desktop.
sort your gemfile lol
file = File.read("test.gemfile")
open_group = false
default_groups = [:general]
current_groups = default_groups
gem_groups = {}
gems = {}
file.split("\n").each do |line|
next if line.empty? || line.index("end") # || line.index("gem").nil? || line.index("group").nil?
if line.index("group")
new_groups = line.scan(%r{:(\w*)}).flatten.map(&:to_sym)
current_groups = new_groups
elsif line.index("end")
current_groups = default_groups
end
name = line.scan(%r{gem .([a-zA-Z\-_]*).}).flatten.first
if name
current_groups.each do |g|
gems[name] ||= {:line => line}
gems[name][:groups] ||= [g]
gems[name][:groups] << g
gems[name][:groups].uniq!
end
end
end
puts gems
groups = gems.map { |k,v| v[:groups] }.uniq
groups.each do |group|
gems_in_group = gems.select { |k,v| v[:groups] == group }.sort_by { |k,v| k }
if group == [:general]
puts gems.delete('rails')[:line]
gems_in_group.each do |k,v|
puts v[:line]
next
end
next
end
puts ""
line = "group "
group.each do |g|
line += ":#{g}, "
end
line = line[0...-2]
line += " do"
puts line
gems_in_group.each do |k,v|
puts " " + v[:line]
end
puts "end"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment