<commit_before>require "bundler"
require "fileutils"
require "mixlib/shellout"
require "tempfile"
require "pp"

module Appbundler

  class AppbundlerError < StandardError; end

  class InaccessibleGemsInLockfile < AppbundlerError; end

  class App

    BINSTUB_FILE_VERSION = 1

    attr_reader :bundle_path
    attr_reader :target_bin_dir
    attr_reader :name

    def initialize(bundle_path, target_bin_dir, name)
      @bundle_path = bundle_path
      @target_bin_dir = target_bin_dir
      @name = name
    end

    def gemfile_path
      "#{app_dir}/Gemfile"
    end

    def safe_resolve_local_gem(s)
      Gem::Specification.find_by_name(s.name, s.version)
    rescue Gem::MissingSpecError
      nil
    end

    def requirement_to_str(req)
      req.as_list.map { |r| "\"#{r}\"" }.join(", ")
    end

    def requested_dependencies(without)
      Bundler.settings.temporary(without: without) do
        definition = Bundler::Definition.build(gemfile_path, gemfile_lock, nil)
        definition.send(:requested_dependencies)
      end
    end

    # This is a blatant ChefDK 2.0 hack.  We need to audit all of our Gemfiles, make sure
    # that github_changelog_generator is in its own group and exclude that group from all
    # of our appbundle calls.  But to ship ChefDK 2.0 we just do this.
    SHITLIST = [
      "github_changelog_generator",
    ]

    def external_lockfile?
      app_dir != bundle_path
    end

    def local_gemfile_lock_specs
      gemfile_lock_specs.map do |s|
        #if SHITLIST.include?(s.name)
        #  nil
        #else
          safe_resolve_local_gem(s)
        #end
      end.compact
    end

    def write_merged_lockfiles(without: [])
      # just return we don't have an external lockfile
      return unless external_lockfile?

      # handle external lockfile
      Tempfile.open(".appbundler-gemfile", app_dir) do |t|
        t.puts "source 'https://rubygems.org'"

        locked_gems = {}

        gemfile_lock_specs.each do |s|
          #next if SHITLIST.include?(s.name)
          spec = safe_resolve_local_gem(s)<commit_msg>add back missing Gemfile.lock from 0.10.0 compat mode<commit_after>require "bundler"
require "fileutils"
require "mixlib/shellout"
require "tempfile"
require "pp"

module Appbundler

  class AppbundlerError < StandardError; end

  class InaccessibleGemsInLockfile < AppbundlerError; end

  class App

    BINSTUB_FILE_VERSION = 1

    attr_reader :bundle_path
    attr_reader :target_bin_dir
    attr_reader :name

    def initialize(bundle_path, target_bin_dir, name)
      @bundle_path = bundle_path
      @target_bin_dir = target_bin_dir
      @name = name
    end

    def gemfile_path
      "#{app_dir}/Gemfile"
    end

    def safe_resolve_local_gem(s)
      Gem::Specification.find_by_name(s.name, s.version)
    rescue Gem::MissingSpecError
      nil
    end

    def requirement_to_str(req)
      req.as_list.map { |r| "\"#{r}\"" }.join(", ")
    end

    def requested_dependencies(without)
      Bundler.settings.temporary(without: without) do
        definition = Bundler::Definition.build(gemfile_path, gemfile_lock, nil)
        definition.send(:requested_dependencies)
      end
    end

    # This is a blatant ChefDK 2.0 hack.  We need to audit all of our Gemfiles, make sure
    # that github_changelog_generator is in its own group and exclude that group from all
    # of our appbundle calls.  But to ship ChefDK 2.0 we just do this.
    SHITLIST = [
      "github_changelog_generator",
    ]

    def external_lockfile?
      app_dir != bundle_path
    end

    def local_gemfile_lock_specs
      gemfile_lock_specs.map do |s|
        #if SHITLIST.include?(s.name)
        #  nil
        #else
          safe_resolve_local_gem(s)
        #end
      end.compact
    end

    # Copy over any .bundler and Gemfile.lock files to the target gem
    # directory.  This will let us run tests from under that directory.
    def copy_bundler_env
      gem_path = installed_spec.gem_dir
      # If we're already using that directory, don't copy (it won't work anyway)
      return if gem_path == File.dirname(gemfile_lock)
      FileUtils.install(gemfile_lock, gem_path, :mode => 0644)
      if File.exist?(dot_bundle_dir) && File.directory?(dot_bundle_dir)
        FileUtils.cp_r(dot_bundle_dir, gem_path)
        FileUtils.chmod_R("ugo+rX", File.join(gem_path, ".bundle"))
      end
    end

    def write_merged_lockfiles(without: [])
      unless external_lockfile?
        copy_bundler_env
        return
      end

      # handle external lockfile
      Tempfile.open(".appbundler-gemfile", app_dir) do |t|
        t.puts "source 'https://rubygems.org'"

        locked_gems = {}

        gemfile_lock_specs.each do |s|
          #next if SHITLIST.include?(s.name)
          spec = safe_resolve_local_gem(s)
{'fraction_non_alphanumeric': 0.081738768718802, 'fraction_numerical': 0.0037437603993344427, 'mean_word_length': 2.7806603773584904, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '1640451', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1543, 'n_words': 390}