CocoaPods私有库搭建

简介: 如何创建一个私有的Spec Repo和Pod

创建Spec Repo

Sepc Repo相当于Pods的索引,所有的公开的Pods的引用文件都在上面。实际上它是一个在搭建在Github上的Git仓库。

现在到Git服务器上,创建一个Git仓库。

目前Github的私有库也已经免费了,所以可以直接在Github上创建,比如GinhoorSpecs

然后将这个Spec Repo添加到你的本地CoacoaPods的Repo中

$ pod repo add GinhoorSpecs https://github.com/ginhoor/GinhoorSpecs.git

成功之后,在本地的~/.cocoaPods/repos路径下,就能看到刚才添加的库了,这样第一步创建Spec Repo就成功了。

创建Pod项目

使用命令创建项目

这里推荐用CocoaPods的命令来直接创建项目。

# 进入要创建项目的路径中
$ cd ~/iOS/Workspace

# 创建项目
$ pod lib create GHApplicationMediator

# 执行命令后,根据提示选择选项
Cloning `https://github.com/CocoaPods/pod-template.git` into `GHApplicationMediator`.
Configuring GHApplicationMediator template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide:
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )


What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > Objc

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 >
specta
Would you like to do view based testing? [ Yes / No ]
 > No

What is your class prefix?
 > GH

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `GHApplicationMediator` from `../`
Downloading dependencies
Installing Expecta (1.0.6)
Installing GHApplicationMediator (0.1.0)
Installing Specta (1.0.7)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `GHApplicationMediator.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'GHApplicationMediator/Example/GHApplicationMediator.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.

#命令执行完成后,项目会自动打开。

主要结构

GHApplicationMediator                        # 项目名称
├── Example                                    # Demo项目
├── GHApplicationMediator                   # 组件库
│   ├── Assets                                # 组件库资源文件
│   └── Classes                             # 组件库类文件
├── GHApplicationMediator.podspec            # 主要配置文件
├── README.md                                # 说明文件
└── LICENSE                                    # 开源协议

添加组件库文件

  1. 将组件库文件放入路径/你的库名称/你的库名称/Classes
  2. 在终端上进入路径/你的库文件/Example,在此路径下,执行Pod update,这样Example项目就会加载你的库文件。
  3. 以后每次更新库文件,都需要执行步骤二。

制作Demo

Demo是一个组件的面子,组件做的再牛,没有Demo,大家也还是不会用。

打开Example,写几个简单的使用案例。

写完之后配置下项目的git仓库,commit一下。

$ cd ~/iOS/Workspace/GHApplicationMediator            # 进入组件库根目录
$ git add .
$ git commit -s -m "Initial Commit"
$ git remote add origin https://github.com/ginhoor/GHApplicationMediator.git
                                                    # 添加远端仓库
$ git push origin master                             # 提交

如果你的git仓库已经存在master,则使用下面的命令进行push

$ git pull origin master --allow-unrelated-histories

pod是通过tag来关系组件库版本号的,所以还需要打上tag。

$ git tag -m "first release" "0.1.0"
$ git push --tags

配置podspec

Pod::Spec.new do |s|
  # 组件库名称
  s.name             = 'GHApplicationMediator'
  # 组件库当前版本,也就是tag指定的
  s.version          = '0.1.0'
  # 简介
  s.summary          = 'make AppDelegate lighter.'
  # 详细描述
  s.description      = <<-DESC
                         manage appDelegate modules easier
                       DESC
  # 组件库首页
  s.homepage         = 'https://github.com/ginhoor/GHApplicationMediator'
  # 组件库开源协议
  s.license          = {
    :type => 'Apache License', :file => 'LICENSE' }
  # 作者
  s.author           = {
    'ginhoor' => 'ginhoor@gmail.com' }
  # Git仓库地址
  s.source           = {
    :git => 'https://github.com/ginhoor/GHApplicationMediator.git', :tag => s.version.to_s }
  # 依赖的iOS版本
  s.ios.deployment_target = '10.0'
  # 源文件地址
  s.source_files = 'GHApplicationMediator/Classes/**/*'
  # 依赖资源地址
  # s.resource_bundles = {
   
  #   'GHApplicationMediator' => ['GHApplicationMediator/Assets/*.png']
  # }
  # 头文件地址
  # s.public_header_files = 'Pod/Classes/**/*.h'
  # 依赖系统库
  # s.frameworks = 'UIKit', 'MapKit'
  # 依赖的第三方库
  # s.dependency 'AFNetworking', '~> 2.3'
end

更多设置请参考帮助文档

验证podspec

这里完成以后,需要验证下配置的正确性。

$ pod lib lint

=================================
 -> GHApplicationMediator (0.1.0)

GHApplicationMediator passed validation.
# 验证通过

上传组件库

将组件库上传到你的Spec repo上。

$ pod repo push GinhoorSpecs GHApplicationMediator.podspec

=================================
Validating spec
 -> GHApplicationMediator (0.1.0)

Updating the `GinhoorSpecs' repo

Already up to date.

Adding the spec to the `GinhoorSpecs' repo

 - [Add] GHApplicationMediator (0.1.0)

Pushing the `GinhoorSpecs' repo
# 上传成功

之后就可以通过pod search来找到找你的组件库了。

$ pod search GHApplicationmediator

==================================
-> GHApplicationMediator (0.1.0)
   make AppDelegate lighter.
   pod 'GHApplicationMediator', '~> 0.1.0'
   - Homepage: https://github.com/ginhoor/GHApplicationMediator
   - Source:   https://github.com/ginhoor/GHApplicationMediator.git
   - Versions: 0.1.0 [GinhoorSpecs repo]

引用组件库

由于这个组件库,用了我们自己的Spec Repo,所以在引用的时候需要同时注明官方的Source源与自己的Source源。

source 'https://github.com/ginhoor/GinhoorSpecs.git'
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'

target 'FrameworkDemo' do
    pod 'GHApplicationMediator', '~> 0.1.0'
    inhibit_all_warnings!
end
目录
相关文章
|
3月前
|
Perl
如何在项目中使用CocoaPods和Carthage?
如何在项目中使用CocoaPods和Carthage?
42 4
|
存储 缓存 前端开发
npm私服搭建—verdaccio方案及其最佳实践
如今前端圈已十分繁荣,一个跟得上技术时代脚步的互联网公司必定是前后端分离的(至少在狭义上是分离的),这就导致了前端承受的分工压力会越来越大,很多公司的前端项目变得十分庞杂,因此技术负责人可能会考虑根据业务线进行拆分为几个工程系统,这样又引申出一个问题:这几个系统之间如何共用一套公司内部的组件库呢?
2236 0
|
Java 应用服务中间件 持续交付
Docker+Jenkins+Gitee+Maven构建自动化部署
Docker+Jenkins+Gitee+Maven构建自动化部署
609 0
Docker+Jenkins+Gitee+Maven构建自动化部署
|
10月前
|
Cloud Native jenkins 持续交付
【云原生】使用PyCharm上传代码到Gitlab仓库并在Jenkins构建
【云原生】使用PyCharm上传代码到Gitlab仓库并在Jenkins构建
168 0
|
Docker 容器
CEH 讲义 NPM、PYPI、DockerHub 备份
CEH 讲义 NPM、PYPI、DockerHub 备份
73 0
|
Docker 容器
电脑爱好者合订本 2011~2015 NPM、PYPI、DockerHub 备份
电脑爱好者合订本 2011~2015 NPM、PYPI、DockerHub 备份
71 0
|
Docker 容器
电脑爱好者合订本 2006~2010 NPM、PYPI、DockerHub 备份
电脑爱好者合订本 2006~2010 NPM、PYPI、DockerHub 备份
59 0
|
Docker 容器
电脑爱好者 2016~2020 NPM、PYPI、DockerHub 备份
电脑爱好者 2016~2020 NPM、PYPI、DockerHub 备份
81 0
|
Docker 容器
电脑爱好者合订本 2001~2005 NPM、PYPI、DockerHub 备份
电脑爱好者合订本 2001~2005 NPM、PYPI、DockerHub 备份
65 0
|
监控 jenkins Java
Jenkins部署及使用(安装maven配置阿里云镜像、git工具)
Jenkins部署及使用(安装maven配置阿里云镜像、git工具)
Jenkins部署及使用(安装maven配置阿里云镜像、git工具)