Cocoapods 源码浅析 - 一些 Tips

栏目: IT技术 · 发布时间: 6年前

内容简介:在整个源码阅读中,发现很多值得学习的地方,放在某一篇中又不合适,全都汇总在这里吧。环境信息Cocoapods 1.9.0.beta.2 (f1293b7)

在整个源码阅读中,发现很多值得学习的地方,放在某一篇中又不合适,全都汇总在这里吧。

环境信息

Cocoapods 1.9.0.beta.2 (f1293b7)

Core 1.9.0.beta.2 (95e133d)

Podfile 解析

最开始在Podfile 解析最佳实践 中,通过自定义的 pod 等方法,解析 Podfile,从而获取 Podfile 中,每个 pod 的信息。当时有一点没做好:关于解析方法的注入。

起初的做法是这样的:将文件分为了三段,分别是 header.rb , Podfile , footer.rb 。定义如下:

# header.rb
$map = {}

def target(target_name = nil, &block = nil)
  yield if block_given?
end

def pod(pod_name, version = nil, **args)
  git = args[:git]
end

def method_missing(m, *args); end

# -----------------------------------------------

#footer.rb
File.open($result_path, "w") do |f|
  f.write(JSON.pretty_generate($map))
end

不难看出,整个解析分为了三个文件,而且最终执行的时候,需要将三个文件按顺序拼接,然后 eval 。除此之外,还有一个弊端:本来解析 Podfile 的脚本就是用 Ruby 写的,最好的通信方式当然是解析完以后,返回一个 map 对象就好。但是在 footer.rb 中可以看到,最后是通过导出文件的形式处理的。

在看 Cocoapods 源码时,发现当初遗漏了一个很重要的点(源码位于 Core/lib/cocoapods-core/podfile.rb 中): include 。源码中,Cocoapods 解析 Podfile 是这样的:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Realm of Racket

Realm of Racket

Matthias Felleisen、Conrad Barski M.D.、David Van Horn、Eight Students Northeastern University of / No Starch Press / 2013-6-25 / USD 39.95

Racket is the noble descendant of Lisp, a programming language renowned for its elegance and power. But while Racket retains the functional goodness of Lisp that makes programming purists drool, it wa......一起来看看 《Realm of Racket》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具