Skip to content

Instantly share code, notes, and snippets.

@vmoravec
Created May 23, 2013 09:33
Show Gist options
  • Save vmoravec/5634927 to your computer and use it in GitHub Desktop.
Save vmoravec/5634927 to your computer and use it in GitHub Desktop.
namespace :gemfile do
desc "filters the gemfile, leaving only gems belonging to specific groups"
task :filter => :environment do
groups = (ENV['groups'] || "").split(',')
if groups.empty?
puts "! Invalid usage"
puts
puts "Please specify the groups you want to filter. Example:"
puts
puts " rake gemfile:filter groups=development,staging"
puts
exit
end
task = FilterGemfile.new
task.run(*groups)
require_relative "gemfile_parser"
code = File.read(Rails.root.join("Gemfile"))
parser = GemfileParser.new(groups, code)
File.open(gemfile, 'w') {|f| f.write(parser.parse) }
end
end
class GemfileParser
def initialize(groups, code)
@groups = groups
@code = code
end
def parse
@output = ""
instance_eval(@code)
@output
end
def source(source, options = {})
@output += "source #{source.inspect}"
if options.any?
@output += ", " + options.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")
end
@output += "\n"
end
def gem(name, *args)
@output += "gem #{name.inspect}"
if args.any?
@output += ", " + args.map { |arg| arg.inspect }.join(", ")
end
@output += "\n"
end
def group(*groups, &blk)
intersection = groups.map(&:to_s) & @groups.map(&:to_s)
yield if intersection.any?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment