<commit_before>module Pod
  class DeployTransformer

    attr_accessor :lockfile
    attr_accessor :sandbox

    def initialize(lockfile, sandbox)
      @lockfile = lockfile
      @sandbox = sandbox
    end

    def transform_podfile(podfile)
      internal_hash = podfile.to_hash
      new_hash = transform_internal_hash(internal_hash)

      Podfile.from_hash(new_hash, podfile.defined_in_file)
    end

    def transform_dependency_name(name)
      dependency_hash = transform_dependency(name)
      parse_dependency(dependency_hash)
    end

    private

    def transform_internal_hash(hash)
      targets = hash["target_definitions"]
      targets.map do |target|
        transform_target_definition_hash(target)
      end if targets

      hash
    end

    def transform_target_definition_hash(hash)
      dependencies = hash["dependencies"]
      hash["dependencies"] = dependencies.map do |dep|
        transform_dependency(dep)
      end if dependencies

      dependencies = hash["dependencies"]
      dependencies.each do |dep|
        podspec_dependencies = collect_podspec_dependencies(dep)
        hash["dependencies"] << podspec_dependencies if podspec_dependencies
      end if dependencies && @sandbox

      children = hash["children"]
      hash["children"] = children.map do |target|
        transform_target_definition_hash(target)
      end if children

      hash
    end

    def parse_dependency(name_or_hash)
      if name_or_hash.is_a?(Hash)
        name = name_or_hash.keys.first
        requirements = name_or_hash.values.first
        Dependency.new(name, *requirements)
      else
        Dependency.new(name_or_hash)
      end
    end

    def podspec_url(pod, version)
      "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/#{pod}/#{version}/#{pod}.podspec.json"
    end

    def collect_podspec_dependencies(name_or_hash)
      dependency = parse_dependency(name_or_hash)
      specification = @sandbox.specification(dependency)
      specification.dependencies.map do |dep|
        transform_dependency(dep.name)
      end
    end

    def transform_dependency(name_or_hash)
      dependency = parse_dependency(name_or_hash)

      unless dependency.external_source
        root_pod = dependency.root_name
        pod = dependency.name
        version = @lockfile.version(pod)
        raise Informative, "Missing dependency \"#{pod}\" in Lockfile please run `pod install` or `pod update`." unless version

        ({ "#{pod}" => [{ :podspec => podspec_url(root_pod, version) }] })
      else
        name_or_hash
      end
    end
  end
end
<commit_msg>Fixes infinite loop.<commit_after>module Pod
  class DeployTransformer

    attr_accessor :lockfile
    attr_accessor :sandbox

    def initialize(lockfile, sandbox)
      @lockfile = lockfile
      @sandbox = sandbox
    end

    def transform_podfile(podfile)
      internal_hash = podfile.to_hash
      new_hash = transform_internal_hash(internal_hash)

      Podfile.from_hash(new_hash, podfile.defined_in_file)
    end

    def transform_dependency_name(name)
      dependency_hash = transform_dependency(name)
      parse_dependency(dependency_hash)
    end

    private

    def transform_internal_hash(hash)
      targets = hash["target_definitions"]
      targets.map do |target|
        transform_target_definition_hash(target)
      end if targets

      hash
    end

    def transform_target_definition_hash(hash)
      dependencies = hash["dependencies"]
      hash["dependencies"] = dependencies.map do |dep|
        transform_dependency(dep)
      end if dependencies

      #Duplicate this to prevent infinte loop
      dependencies = hash["dependencies"].dup
      dependencies.each do |dep|
        podspec_dependencies = collect_podspec_dependencies(dep)
        hash["dependencies"] << podspec_dependencies if podspec_dependencies
      end if dependencies && @sandbox

      children = hash["children"]
      hash["children"] = children.map do |target|
        transform_target_definition_hash(target)
      end if children

      hash
    end

    def parse_dependency(name_or_hash)
      if name_or_hash.is_a?(Hash)
        name = name_or_hash.keys.first
        requirements = name_or_hash.values.first
        Dependency.new(name, *requirements)
      else
        Dependency.new(name_or_hash)
      end
    end

    def podspec_url(pod, version)
      "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/#{pod}/#{version}/#{pod}.podspec.json"
    end

    def collect_podspec_dependencies(name_or_hash)
      dependency = parse_dependency(name_or_hash)
      specification = @sandbox.specification(dependency)
      specification.dependencies.map do |dep|
        puts "#{dep}"
        transform_dependency(dep.name)
      end
    end

    def transform_dependency(name_or_hash)
      dependency = parse_dependency(name_or_hash)

      unless dependency.external_source
        root_pod = dependency.root_name
        pod = dependency.name
        version = @lockfile.version(pod)
        raise Informative, "Missing dependency \"#{pod}\" in Lockfile please run `pod install` or `pod update`." unless version

        ({ "#{pod}" => [{ :podspec => podspec_url(root_pod, version) }] })
      else
        name_or_hash
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08130081300813008, 'fraction_numerical': 0.0, 'mean_word_length': 2.9804364183596688, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3942590', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1501, 'n_words': 316}