Carthage

Carthage

资源地址:

  • Carthage/Carthage - github地址
  • Carthage Tutorial: Getting Started - 入门教程

其它使用教程:

  • Carthage的安装和使用
  • How to Carthage Efficiently

Carthage只支持dynamic frameworks,也就是说只支持iOS8及以上的版本

安装:

1.通过homebrew安装 brew install carthage

2.下载.pkg安装,地址

安装完成后,在终端输入carthage version,可查看安装的版本
查看版本
使用:

1.创建Cartfile文件

Cartfile文件的说明,参考官方文档

Cartfile文件描述的是依赖,相当于Cocoapods的Podfile

切换到项目的文件夹,使用如下的命令创建一个Cartfile文件:

touch Cartfile

使用Xcode打开来编辑:

open -a Xcode Cartfile

内容一般是如下的形式:

github "Alamofire/Alamofire" == 4.5
github "Alamofire/AlamofireImage" ~> 3.2

格式说明:

1.目前支持三种origin:GitHub、Git、二进制framework
2.依赖的版本

  1. >= 1.0 for “at least version 1.0” 至少是1.0版本
  2. ~> 1.0 for “compatible with version 1.0” 兼容1.0的版本
  3. == 1.0 for “exactly version 1.0”
  4. “some-branch-or-tag-or-commit” for a specific Git object (anything allowed by git rev-parse). Note: This form of requirement is not supported for binary origins.

If you specify ~> 1.7.5, then any version from 1.7.5 up to, but not including 2.0, is considered compatible.
如果你指定的是~> 1.7.5,那么可以是1.7.5以上的任何版本,但不包括2.0版本

构建依赖

在终端中输入如下的命令:

carthage update --platform iOS

该命令会clone carthage中指定的git仓库,并把每个依赖build成framework,终端中会有如下类似的输出

*** Fetching AlamofireImage
*** Fetching Alamofire
*** Checking out Alamofire at "4.5.0"
*** Checking out AlamofireImage at "3.2.0"
*** xcodebuild output can be found in /var/folders/cn/tknd724s0fv8pbdcbkg2sb6w0000gn/T/carthage-xcodebuild.no8ytB.log
*** Building scheme "Alamofire iOS" in Alamofire.xcworkspace
*** Building scheme "AlamofireImage iOS" in AlamofireImage.xcworkspace

--platform iOS 确保只为iOS构建framework,如果你不指定一个平台,默认会为所有的平台(Mac 和 iOS)构建framework

之后,项目中会多了一个Carthage文件夹,包含2个子文件夹:

  • Build
  • Checkouts

Carthage_第1张图片
Carthage_第2张图片

当运行carthage update命令后,Carthage创建了一系列的文件和文件夹,如下:

Carthage_第3张图片

  • Cartfile.resolved - 定义了Carthage安装依赖的版本,建议将该文件提交到版本控制存储库
  • Carthage - 包含2个文件夹
    • Build - 构建好的framework,可以集成到项目中
    • Checkouts - 依赖的源码

Don’t modify any code inside the Checkouts folder because its contents may be overwritten at any time by a future carthage update or carthage checkout command, and your hard work would be gone in the twinkling of an eye.
不要修改Checkouts文件夹中的代码,因为它可被carthage update 后者 carthage checkout 命令所覆盖

If modifications to your dependencies are a must do, you can run carthage update using the --use-submodules option.
如果必须修改依赖,使用-use-submodules选项
With this option, Carthage adds each dependency in the Checkouts folder to your Git repository as a submodule, meaning you can change the dependencies’ source, and commit and push those changes elsewhere without fear of an overwrite.

注意:如果有其它用户要使用你的project,如果你没有在code中提交已构建好的framework,需要运行carthage bootstrap命令

The bootstrap command will download and build the exact versions of your dependencies that are specified in Cartfile.resolved.
bootstrap命令会下载 Cartfile.resolved中指定的版本的依赖
carthage update, on the other hand, would update the project to use the newest compatible versions of each dependency, which may not be desirable.
carthage update会使用最新的兼容的依赖

将Frameworks添加到项目中

选择project->target->General->Linked Frameworks and Libraries

Build\iOS文件下的framework,拖动到Linked Frameworks and Libraries中:

Carthage_第4张图片
之后切换到Build Phases 中,添加一个新的Run Script

/usr/local/bin/carthage copy-frameworks

Input Files中,为每个framework添加一个entry

$(SRCROOT)/Carthage/Build/iOS/Alamofire.framework
$(SRCROOT)/Carthage/Build/iOS/AlamofireImage.framework

结果如下:

Carthage_第5张图片

为什么这么做呢?

Strictly speaking, this build phase isn’t required for your project to run. However, it’s a slick workaround for an App Store submission bug where apps with frameworks that contain binary images for the iOS simulator are automatically rejected.

The carthage copy-frameworks command strips out these extra architectures. w00t!

你可能感兴趣的:(代码管理)