hook Cocoapods Resources

作者: donghuan1 | 来源:发表于2017-07-04 15:16 被阅读526次

在使用Cocoapods组装子Pods的时候,子Pods可能也携带资源,需要在子Pods的podspec中指明:
spec.resources = "xx/xx/a.png"
podspec的resources支持通配符,比如:
"xx/.png"
"xx/
.{png,jpg}"
"xx/*/"

<strong>如果新增或修改了资源,则需要重建组装工程,否则读不到新增的资源文件,这很烦人,这篇就是为了干这事。</strong>

以上为背景------

1、关于Cocoapods拷贝资源文件
在xcode build过程中发现,Cocoapods是在build时把资源文件拷进工程里

image.png

找到了这个脚本的位置 Pods/Target\ Support\ Files/Pods-app/Pods-app-resources.sh (app)为工程名
这个脚本列出了所有podspec中读取的资源文件,并且通过install_resource()对资源进行了拷贝。我们正需要这个脚本。

2、在build阶段执行自己的脚本
在主工程的target->Build Phases下,xcode提供了Run Script,我们可以指定这里调用脚本。

image.png

3、如何hook podspec中的resources
希望不破坏原有podspec的写法情况下,对podspec的resources进行修改:
Ruby有个'cocoapods'包,可以对Pod模块进行扩展我们自己的方法。

#podspec文件
Pod::Spec.new do |s|
  ...
  s.resources_hook(s)
  ...
end
require 'cocoapods'
module Pod
  class Specification
    def resources_hook(spec)
      #无法通过spec.resources取得podspec中设置的resources,我们可以通过下面这种方式
      origin_resources = spec.attributes_hash['resources']
      #other things
    end

拿到了spec中的带通配符的resources,接着就是解析

4、解析带通配符的resources
主要目的是将 "xx/xx.{jpg,png}"
解析成["xx/xx.jpng","xx/xx.png"]
目的就是拿到一个数组,这个数组指向了所需的资源文件路径,这个数组内容支持*这种通配符。

        def wildcard_parser(resources)
            parser_resources = Array.new()
            regex=/(.*)\{(.*)\}/ 
            resources.each{
                |path|
                if regex.match(path)
                    $2.split(',').each{
                        |extension|
                        parser_resources.push("#{$1}#{extension}")
                    }
                else
                    parser_resources.push(path)
                end
            }
            return parser_resources
        end

5、在2中,制定了build时执行我们自己的脚本,在4中,拿到了所有spec制定的资源文件路径,在1中,我们拿到了拷贝资源的源码。把这几个组合一下。
(我是将4中拿到的数据写在文件里,其他方式都行,只要能在2中的脚本里读写到

#这个路径我存储了以子pods名为文件名,里面存储了4的文件。
other_source_link_path="$SRCROOT/../Resources/OtherResources"
for element in `ls $other_source_link_path`
do
    for path in `cat "$other_source_link_path/$element"`
    do 
      for resource in $SRCROOT/../$path 
      do
        if [[ -f $resource ]]; then
          install_resource "$resource"   #这里的install_resources就是1中我们从cocoapods里拷贝出的拷资源文件的方法
        fi
      done
        
    done
done

相关文章

网友评论

    本文标题:hook Cocoapods Resources

    本文链接:https://www.haomeiwen.com/subject/myqxhxtx.html