Created
May 23, 2013 09:33
-
-
Save vmoravec/5634927 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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