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.rbPodfilefooter.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 是这样的:


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

查看所有标签

猜你喜欢:

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

How to Design Programs

How to Design Programs

Matthias Felleisen、Robert Bruce Findler、Matthew Flatt、Shriram Krishnamurthi / The MIT Press / 2001-2-12 / 71.00美元

This introduction to programming places computer science in the core of a liberal arts education. Unlike other introductory books, it focuses on the program design process. This approach fosters a var......一起来看看 《How to Design Programs》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码