Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create gem group if it doesn't exist and add gems to the group as required #115

Open
coolprobn opened this issue Jun 8, 2024 · 0 comments

Comments

@coolprobn
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant