cocoapods-prefer解析

hook cocoapods的类

podfile

在podfile.rb文件实现在podfile调用的函数,传递参数给PreferConfig

  def prefer_source_pod(name = nil, *requirements)
    pod(name, *requirements)

    Pod::PreferConfig.instance.prefer_pod(name)
  end

cocoapods_plugin

  • source_provider

在cocoapods_plugin.rb中,实现把在podfile添加的prefer-source,通过hook source_provider传给context。

Pod::HooksManager.register('cocoapods-prefer', :source_provider) do |context, user_options|
    Pod::PreferConfig.instance.prefer_sources().each { |e, source| 
      context.add_source(source)
    }
end

  • pre_install

hook pre_install来初始化podfile指定的prefer_source

def prefer_source_options(user_options)
      dislike = user_options[:dislike_sources]
      unless dislike.nil? || dislike.empty? || !dislike.is_a?(Hash)
        dislike.each { |source_name, url|
          dislike_source(source_name, url)
        }
      end

      sources = user_options[:prefer_sources]
      unless sources.nil? || sources.empty? || !sources.is_a?(Hash)
        sources.each { |source_name, url|
          prefer_source(source_name, url)
        }
      end
end

  • post_install

hook post_install来做一些report打印操作,过滤出prefer_source不存在的pod

    def report
      report_lock_result()
      if installed?
        locked_result_pods = locked_pods()
        result = lock_file_result().reject { |pod_name, url| 
          locked_result_pods.include?(pod_name)
        }

        prefer_urls = prefer_source_urls.values
        prefer_pod_names = prefer_pods()

        result.select { |pod_name, url| 
          prefer_pod_names.include?(pod_name) && !prefer_urls.include?(url)
        }.each do |pod_name, url|
          UI.puts "#{pod_name} using #{url} not in preferred sources".yellow
        end

        dislike_urls = dislike_source_urls.values
        dislike_pod_names = dislike_pods()

        result.select { |pod_name, url| 
          dislike_pod_names.include?(pod_name) && dislike_urls.include?(url)
        }.each do |pod_name, url|
          UI.puts "#{pod_name} using #{url} in disliked sources".yellow
        end
      end
    end

resolver

在resolver.rb重写Resolver的search_for(dependency)方法,查找spec依赖的source,根据prefer-source,优先返回设置prefer source。

  • 对原先的search_for设置别名
alias perfer_search_for search_for

  • 先执行原来的search_for,得到specifications,然后执行prefer_specifications_filter,得到prefer source
alias perfer_search_for search_for
## Specification
## return preferred source if meet requirement 
def search_for(dependency)
  # install_script_phases_for_dependency(dependency.root_name)
  specifications = perfer_search_for(dependency)

  pod_name = dependency.root_name
  specifications = dislike_lock_specifications_filter(pod_name, specifications)
  specifications = prefer_lock_specifications_filter(pod_name, specifications)

  specifications = dislike_specifications_filter(pod_name, specifications)
  specifications = prefer_specifications_filter(pod_name, specifications)
  specifications
end

你可能感兴趣的:(cocoapods-prefer解析)