<commit_before>require 'cocoa_pods/specification/set'

module Pod
  class Specification
    def self.from_podfile(path)
      if File.exist?(path)
        spec = new
        spec.instance_eval(File.read(path))
        spec.defined_in_file = path
        spec
      end
    end

    def self.from_podspec(pathname)
      spec = eval(File.read(pathname), nil, pathname.to_s)
      spec.defined_in_file = pathname
      spec
    end

    attr_accessor :defined_in_file

    def initialize(&block)
      @dependencies = []
      instance_eval(&block) if block_given?
    end

    # Attributes

    def read(name)
      instance_variable_get("@#{name}")
    end

    def name(name)
      @name = name
    end

    def version(version)
      @version = Version.new(version)
    end

    def authors(*names_and_email_addresses)
      list = names_and_email_addresses
      unless list.first.is_a?(Hash)
        authors = list.last.is_a?(Hash) ? list.pop : {}
        list.each { |name| authors[name] = nil }
      end
      @authors = authors || list
    end
    alias_method :author, :authors

    def homepage(url)
      @homepage = url
    end

    def summary(summary)
      @summary = summary
      @description ||= summary
    end

    def description(description)
      @description = description
    end

    def part_of(name, *version_requirements)
      #@part_of = Dependency.new(name, *version_requirements)
      @part_of = dependency(name, *version_requirements)
      @part_of.part_of_other_pod = true
    end

    def source_files(*patterns)
      @source_files = patterns
    end

    def source(remote)
      @source = remote
    end

    attr_reader :dependencies
    def dependency(name, *version_requirements)
      dep = Dependency.new(name, *version_requirements)
      @dependencies << dep
      dep
    end

    #def xcconfig(path)
      #@xcconfig = path
    #end

    def frameworks(*frameworks)
      @frameworks = frameworks.map { |f| Pathname.new(f) }
    end

    def header_search_paths(*search_paths)
      @header_search_paths = search_paths
    end

    # Not attributes

    # Returns the specification for the pod that this pod's source is a part of.
    def part_of_specification
      if @part_of
        Set.by_specification_name(@part_of.name).podspec
      end
    end

    # This also includes those that are only part of other specs, but are not
    # actually being used themselves.
    def resolved_dependent_specification_sets
      @resolved_dependent_specifications_sets ||= Resolver.new(self).resolve
    end

    def install_dependent_specifications!
      sets = resolved_dependent_specification_sets
      sets.each do |set|
        # In case the set is only part of other pods we don't need to install
        # the pod itself.
        next if set.only_part_of_other_pod?
        set.podspec.install!
      end
    end

    def create_static_library_project!
      puts "==> Creating static library project"
      project = XcodeProject.static_library
      resolved_dependent_specification_sets.each do |set|
        # In case the set is only part of other pods we don't need to build
        # the pod itself.
        next if set.only_part_of_other_pod?
        spec = set.podspec

        spec.read(:source_files).each do |pattern|
          pattern = spec.pod_destroot + pattern
          pattern = pattern + '*.{h,m,mm,c,cpp}' if pattern.directory?
          Dir.glob(pattern.to_s).each do |file|
            file = Pathname.new(file)
            file = file.relative_path_from(config.project_pods_root)
            project.add_source_file(file)
          end
        end

        if frameworks = spec.read(:frameworks)
          frameworks.each { |framework| project.add_framework(framework) }
        end

        if search_paths = spec.read(:header_search_paths)
          search_paths.each { |path| project.add_header_search_path(path) }
        end
      end
      project.create_in(config.project_pods_root)
    end

    include Config::Mixin

    def pod_destroot<commit_msg>Add a quick and dirty way to create a xcconfig file that the app bases its target configurations on.<commit_after>require 'cocoa_pods/specification/set'

module Pod
  class Specification
    def self.from_podfile(path)
      if File.exist?(path)
        spec = new
        spec.instance_eval(File.read(path))
        spec.defined_in_file = path
        spec
      end
    end

    def self.from_podspec(pathname)
      spec = eval(File.read(pathname), nil, pathname.to_s)
      spec.defined_in_file = pathname
      spec
    end

    attr_accessor :defined_in_file

    def initialize(&block)
      @dependencies = []
      instance_eval(&block) if block_given?
    end

    # Attributes

    def read(name)
      instance_variable_get("@#{name}")
    end

    def name(name)
      @name = name
    end

    def version(version)
      @version = Version.new(version)
    end

    def authors(*names_and_email_addresses)
      list = names_and_email_addresses
      unless list.first.is_a?(Hash)
        authors = list.last.is_a?(Hash) ? list.pop : {}
        list.each { |name| authors[name] = nil }
      end
      @authors = authors || list
    end
    alias_method :author, :authors

    def homepage(url)
      @homepage = url
    end

    def summary(summary)
      @summary = summary
      @description ||= summary
    end

    def description(description)
      @description = description
    end

    def part_of(name, *version_requirements)
      #@part_of = Dependency.new(name, *version_requirements)
      @part_of = dependency(name, *version_requirements)
      @part_of.part_of_other_pod = true
    end

    def source_files(*patterns)
      @source_files = patterns
    end

    def source(remote)
      @source = remote
    end

    attr_reader :dependencies
    def dependency(name, *version_requirements)
      dep = Dependency.new(name, *version_requirements)
      @dependencies << dep
      dep
    end

    def xcconfig(hash)
      @xcconfig = hash
    end

    # Not attributes

    # Returns the specification for the pod that this pod's source is a part of.
    def part_of_specification
      if @part_of
        Set.by_specification_name(@part_of.name).podspec
      end
    end

    # TODO move this all to an Installer class

    # This also includes those that are only part of other specs, but are not
    # actually being used themselves.
    def resolved_dependent_specification_sets
      @resolved_dependent_specifications_sets ||= Resolver.new(self).resolve
    end

    def install_dependent_specifications!
      sets = resolved_dependent_specification_sets
      sets.each do |set|
        # In case the set is only part of other pods we don't need to install
        # the pod itself.
        next if set.only_part_of_other_pod?
        set.podspec.install!
      end
    end

    def create_static_library_project!
      puts "==> Creating static library project"
      xcconfigs = []
      project = XcodeProject.static_library
      resolved_dependent_specification_sets.each do |set|
        # In case the set is only part of other pods we don't need to build
        # the pod itself.
        next if set.only_part_of_other_pod?
        spec = set.podspec

        spec.read(:source_files).each do |pattern|
          pattern = spec.pod_destroot + pattern
          pattern = pattern + '*.{h,m,mm,c,cpp}' if pattern.directory?
          Dir.glob(pattern.to_s).each do |file|
            file = Pathname.new(file)
            file = file.relative_path_from(config.project_pods_root)
            project.add_source_file(file)
          end
        end

        if xcconfig = spec.read(:xcconfig)
          xcconfigs << xcconfig
        end
      end
      project.create_in(config.project_pods_root)

      # TODO the static library needs an extra xcconfig which sets the values from issue #1.
      # Or we could edit the project.pbxproj file, but that seems like more work...

      merged_xcconfig = {
        # in a workspace this is where the static library headers should be found
        'USER_HEADER_SEARCH_PATHS' => '$(BUILT_PRODUCTS_DIR)',
        # search the user headers
        'ALWAYS_SEARCH_USER_PATHS' => 'YES',
      }
      xcconfigs.each do |xcconfig|
        xcconfig.each do |key, value|
          if existing_value = merged_xcconfig[key]
            merged_xcconfig[key] = "#{existing_value} #{value}"
          else
            merged_xcconfig[key] = value
          end
        end
      end
      (config.project_pods_root + 'Pods.xcconfig').open('w') do |file|
        merged_xcconfig.each do |key, value|
          file.puts "#{key} = #{value}"
        end
      end
    end

    include Config::Mixin

    def pod_destroot
{'fraction_non_alphanumeric': 0.07928505957836847, 'fraction_numerical': 0.00011457378551787351, 'mean_word_length': 2.4217953743629947, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4416937', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2611, 'n_words': 678}