Create gem group if it doesn't exist and add gems to the group as required
Opened this issue · 0 comments
coolprobn commented
We have a code like this in our generators
insert_into_file "Gemfile", rubocop_gem_content, after: /group :development do/
It's very unlikely that development OR development, test groups are not present in the Gemfile but there is a chance for them not to present. I want to minimize that possibility and add a new helper method like the following (already in work):
def add_gem_content_to_group(*names, &block)
symbolized_group_names = Array.wrap(names).flatten.map(&:to_sym)
formatted_group_names = symbolized_group_names.join(", ")
group_regex = /group #{formatted_group_names} do/
if File.read("Gemfile").match?(group_regex)
&block
else
gem_group *names do
&block
end
end
end
This code will check and add to the group if present. Else create the group and execute the block that has been passed which includes gem related content.
(Code hasn't been tested so it won't work as it is for sure)