<commit_before>require 'thor'

module Bundler
  module Reorganizer
    class CLI < Thor::Group
      DEFAULT_GROUP = [:default]
      attr_accessor :sources, :rubies, :groups, :current_group

      desc "reorganize Gemfile into groups of gems"
      argument :gemfile_path, :banner=>"path/to/Gemfile"
      class_option :output, desc: 'path to write output of reorganized Gemfile', aliases: '-o'
      def reorganize
        @sources = []
        @rubies = []
        @groups = {}

        parse_gemfile
        output_gemfile
      end

      private

      def output_gemfile
        output_path = options[:output] || gemfile_path
        output_buffer = File.open(output_path, 'w')
        say "Writing reorganized Gemfile to: #{output_path}"

        output_buffer << sources.map {|s| "source #{stringify_arg(*s)}"}.join("\n")
        output_buffer << rubies.map {|s| "\nruby #{stringify_arg(*s)}"}.join("\n")

        groups.each do |group, gems|
          output_buffer << "\n"
          indent = group == DEFAULT_GROUP ? '' : '  '
          output_buffer << "\ngroup #{stringify_args(*group)} do" unless group == DEFAULT_GROUP
          gems.sort_by!(&:first)
          gems.each do |gem_args|
            output_buffer << "\n#{indent}gem #{stringify_args(*gem_args.reject(&:empty?))}"
          end
          output_buffer << "\nend" unless group == DEFAULT_GROUP
        end
        output_buffer << "\n"
      end

      # pretty print arguments as ruby parsable string
      def stringify_args(*args)
        args.map {|a| stringify_arg(a) }.join(', ')
      end

      def stringify_arg(arg)
        arg.inspect
      end

      def source(*args)
        sources << args
      end

      def ruby(*args)
        rubies << args
      end

      def gem(*args)
        options = args.last.is_a?(Hash) ? args.last : {}
        group = current_group || options.delete(:group) || DEFAULT_GROUP
        group = wrap_in_array(group).sort
        groups[group] ||= []
        groups[group] << args
      end

      def group(*args, &block)
        self.current_group = args
        yield
      ensure
        current_group = nil
      end

      def wrap_in_array(object)
        object.is_a?(Array) ? object : [object]
      end

      def parse_gemfile
        say "Parsing Gemfile: #{gemfile_path}"<commit_msg>Fixing variable scoping issue to properly clear current_group.<commit_after>require 'thor'

module Bundler
  module Reorganizer
    class CLI < Thor::Group
      DEFAULT_GROUP = [:default]
      attr_accessor :sources, :rubies, :groups, :current_group

      desc "reorganize Gemfile into groups of gems"
      argument :gemfile_path, :banner=>"path/to/Gemfile"
      class_option :output, desc: 'path to write output of reorganized Gemfile', aliases: '-o'
      def reorganize
        @sources = []
        @rubies = []
        @groups = {}

        parse_gemfile
        output_gemfile
      end

      private

      def output_gemfile
        output_path = options[:output] || gemfile_path
        output_buffer = File.open(output_path, 'w')
        say "Writing reorganized Gemfile to: #{output_path}"

        output_buffer << sources.map {|s| "source #{stringify_arg(*s)}"}.join("\n")
        output_buffer << rubies.map {|s| "\nruby #{stringify_arg(*s)}"}.join("\n")

        groups.each do |group, gems|
          output_buffer << "\n"
          indent = group == DEFAULT_GROUP ? '' : '  '
          output_buffer << "\ngroup #{stringify_args(*group)} do" unless group == DEFAULT_GROUP
          gems.sort_by!(&:first)
          gems.each do |gem_args|
            output_buffer << "\n#{indent}gem #{stringify_args(*gem_args.reject(&:empty?))}"
          end
          output_buffer << "\nend" unless group == DEFAULT_GROUP
        end
        output_buffer << "\n"
      end

      # pretty print arguments as ruby parsable string
      def stringify_args(*args)
        args.map {|a| stringify_arg(a) }.join(', ')
      end

      def stringify_arg(arg)
        arg.inspect
      end

      def source(*args)
        sources << args
      end

      def ruby(*args)
        rubies << args
      end

      def gem(*args)
        options = args.last.is_a?(Hash) ? args.last : {}
        group = current_group || options.delete(:group) || DEFAULT_GROUP
        group = wrap_in_array(group).sort
        groups[group] ||= []
        groups[group] << args
      end

      def group(*args, &block)
        self.current_group = args
        yield
      ensure
        self.current_group = nil
      end

      def wrap_in_array(object)
        object.is_a?(Array) ? object : [object]
      end

      def parse_gemfile
        say "Parsing Gemfile: #{gemfile_path}"
{'fraction_non_alphanumeric': 0.1190273037542662, 'fraction_numerical': 0.0, 'mean_word_length': 2.207250341997264, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '99911', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1385, 'n_words': 344}