<commit_before>require 'lock_jar'
require 'lock_jar/registry'
require 'lock_jar/domain/lockfile'
require 'lock_jar/domain/dsl'
require 'lock_jar/domain/gem_dsl'
require 'lock_jar/domain/jarfile_dsl'
require 'lock_jar/domain/dsl_merger'

module LockJar
  #
  class Bundler
    class << self
      attr_accessor :skip_lock

      def load(*groups)
        return if groups.empty? || !File.exist?('Jarfile.lock')

        lockfile = LockJar::Domain::Lockfile.read('Jarfile.lock')

        # expand merged paths to include gem base path
        unless lockfile.merged.empty?
          lockfile.merged = LockJar::Bundler.expand_gem_paths(lockfile.merged)
        end

        LockJar.load(lockfile, groups)

        puts(
          '[LockJar] Loaded Jars for #{groups.inspect}: '\
          "#{LockJar::Registry.instance.loaded_jars.inspect}"
        ) if ENV['DEBUG']
      end

      def expand_gem_paths(merged)
        merged_gem_paths = []
        Gem.path.each do |gem_root|
          merged.each do |merge|
            next unless merge.start_with? 'gem:'

            # merged gems follow the notation: gem:gemname:path
            gem_path = merge.gsub(/^gem:.+:/, '')
            gem_path = File.join(gem_root, gem_path)

            merged_gem_paths << gem_path if File.exist? gem_path
          end
        end

        merged_gem_paths
      end

      # Create a lock file from bundled gems
      def lock!(*opts)
        definition = ::Bundler.definition

        gems_with_jars = []

        jarfile_opt = opts.find { |option| option.is_a? String }

        jarfile = jarfile_opt || 'Jarfile'

        # load local Jarfile
        if File.exist?(jarfile)
          dsl = LockJar::Domain::JarfileDsl.create(File.expand_path(jarfile))
          gems_with_jars << "jarfile:#{jarfile}"

        # Create new Dsl
        else
          dsl = LockJar::Domain::Dsl.new
        end

        definition.groups.each do |group|
          puts '[LockJar] Group #{group}:' if ENV['DEBUG']

          definition.specs_for([group]).each do |spec|
            gem_dir = spec.gem_dir

            jarfile = File.join(gem_dir, 'Jarfile')

            next unless File.exist?(jarfile)

            gems_with_jars << "gem:#{spec.name}"
            puts "[LockJar]   #{spec.name} has Jarfile" if ENV['DEBUG']
            spec_dsl = LockJar::Domain::GemDsl.create(spec, 'Jarfile')
            dsl = LockJar::Domain::DslMerger(dsl, spec_dsl, [group.to_s]).merge
          end

          LockJar::Bundler.skip_lock = true
        end

        puts "[LockJar] Locking Jars for: #{gems_with_jars.inspect}"
        LockJar.lock(*([dsl] + opts))
      end
    end
  end
end
<commit_msg>rubocop is the law :oncoming_police_car:<commit_after>require 'lock_jar'
require 'lock_jar/registry'
require 'lock_jar/domain/lockfile'
require 'lock_jar/domain/dsl'
require 'lock_jar/domain/gem_dsl'
require 'lock_jar/domain/jarfile_dsl'
require 'lock_jar/domain/dsl_merger'

module LockJar
  #
  class Bundler
    class << self
      attr_accessor :skip_lock

      def load(*groups)
        return if groups.empty? || !File.exist?('Jarfile.lock')

        lockfile = LockJar::Domain::Lockfile.read('Jarfile.lock')

        # expand merged paths to include gem base path
        unless lockfile.merged.empty?
          lockfile.merged = LockJar::Bundler.expand_gem_paths(lockfile.merged)
        end

        LockJar.load(lockfile, groups)

        puts(
          '[LockJar] Loaded Jars for #{groups.inspect}: '\
          "#{LockJar::Registry.instance.loaded_jars.inspect}"
        ) if ENV['DEBUG']
      end

      def expand_gem_paths(merged)
        merged_gem_paths = []
        Gem.path.each do |gem_root|
          merged.each do |merge|
            next unless merge.start_with? 'gem:'

            # merged gems follow the notation: gem:gemname:path
            gem_path = merge.gsub(/^gem:.+:/, '')
            gem_path = File.join(gem_root, gem_path)

            merged_gem_paths << gem_path if File.exist? gem_path
          end
        end

        merged_gem_paths
      end

      # Create a lock file from bundled gems
      def lock!(*opts)
        definition = ::Bundler.definition

        gems_with_jars = []

        jarfile_opt = opts.find { |option| option.is_a? String }

        jarfile = jarfile_opt || 'Jarfile'

        # load local Jarfile
        if File.exist?(jarfile)
          dsl = LockJar::Domain::JarfileDsl.create(File.expand_path(jarfile))
          gems_with_jars << "jarfile:#{jarfile}"

        # Create new Dsl
        else
          dsl = LockJar::Domain::Dsl.new
        end

        definition.groups.each do |group|
          puts '[LockJar] Group #{group}:' if ENV['DEBUG']

          definition.specs_for([group]).each do |spec|
            dsl = merge_gem_dsl(dsl, spec, group)
            gems_with_jars << "gem:#{spec.name}" if File.exist? File.join(spec.gem_dir, 'Jarfile')
          end

          LockJar::Bundler.skip_lock = true
        end

        puts "[LockJar] Locking Jars for: #{gems_with_jars.inspect}"
        LockJar.lock(*([dsl] + opts))
      end

      private

      def merge_gem_dsl(dsl, spec, group)
        jarfile = File.join(spec.gem_dir, 'Jarfile')

        return unless File.exist?(jarfile)

        gems_with_jars << "gem:#{spec.name}"
        puts "[LockJar]   #{spec.name} has Jarfile" if ENV['DEBUG']
        spec_dsl = LockJar::Domain::GemDsl.create(spec, 'Jarfile')
        LockJar::Domain::DslMerger(dsl, spec_dsl, [group.to_s]).merge
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11497374615245337, 'fraction_numerical': 0.0, 'mean_word_length': 2.3018529587567245, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 3, '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': '7284477', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1808, 'n_words': 369}