Pod私有库的搭建

2019-10-28  本文已影响0人  Jackie_123

 我在上一篇文章中说完公有库的创建,那么公有库的创建是为了开源自己写的一些工具库或者框架,而私有库的创建就是为了在项目中真正的进行组件化的开发。下面我就说明一下私有库的搭建。
 首先值得注意的一点是,既然是自己项目中使用的,是不想让别人去访问,私有库是需要单独创建一个本地的索引库的。类似于下图:


图 - 0

上图的JNSpec就是我的私有索引库,下面的testOne123就是我的其中一个组件。
那么对于私有库的搭建步骤就是:

一. 在本地创建私有Spec Repo

什么是Spec Repo?它是所有的Pods的一个索引,就是一个容器,所有公开的Pods都在这个里面,它实际是一个Git仓库remote端GitHub上,但是当你使用了Cocoapods后它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。
在上图中的master就是GitHub的索引目录,里面就是存放着公开的pods。我们在这一步做的就是创建一个类似于master这种目录存在我们自己的私有的pods。下面我将以GitHub为例创建私有的podspec仓库。

pod repo add [Private Repo Name] [GitHub HTTPS clone URL]
---------------------------------------------------------
pod repo add JNSpec https://github.com/jniosdeveloper/JNLibaray.git

此时如果成功的话cd到~/.cocoapods/repos目录下就可以看到JNSpec这个目录了。至此第一步创建私有Spec Repo完成。

二.创建Pod项目工程文件

这一步的作用就是放我们自己的组件文件。

pod lib create testOne123

之后你会被问五个问题:

第一个问题:Objective-C 或者 Swift

第二个问题:Making a Demo Application,如果选是就会在你的Xcode工程中新建一个工程。
(如果你想要一个demo或者你需要一个测试单元在你的工程中,你应该选是)

第三个问题:Choosing a Test Framework,你应该测试你的工程,确保别人可用
(如何选择:如果你不能决定,就选Specta/Epecta)

第四个问题:View-based Testing,会根据你选的测试自动生成一个,建议选择NO

第五个问题: 你的前缀是什么,这里我用的JN
#
# Be sure to run `pod lib lint testOne123.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'testOne123'
  s.version          = '0.1.0'
  s.summary          = 'Just Test'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
just oneDemo
                       DESC

  s.homepage         = 'https://github.com/jniosdeveloper/testLib'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '331385257@qq.com' => '331385257@qq.com' }
  s.source           = { :git => 'https://github.com/jniosdeveloper/testLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

  s.source_files = 'testOne123/Classes/**/*'
  
  # s.resource_bundles = {
  #   'testOne123' => ['testOne123/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

编辑完podspec文件后,需要验证一下这个文件是否可用,如果有任何WARNING或者ERROR都是不可以的,它就不能被添加到Spec Repo中,不过xcodeWARNING是可以存在的,验证需要执行以下命令:

 pod lib lint
git add .
git commit -m "Initial Commit of Library"
#推送到远端仓库
git push origin master  
git tag -m "first release" "0.1.0"
git push --tags     #推送tag到远端仓库

三.创建源码仓库

在第二步是把工程创建了出来,但是并没有给我们本地的私有spec仓库绑定。

四.向本地创建的Spec Repo提交podspec文件

pod repo push JNSpec testOne123.podspec  #前面是本地私有Repo名字 后面是工程中podspec的文件名

完了之后我们在单独的工程中引用这个组件看看,编辑你的podfile,添加两个source:

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/jniosdeveloper/JNLibaray.git'
pod 'testOne123'

参考文章:
iOS开发之Cocoapods的使用与私有pod的制作

上一篇 下一篇

猜你喜欢

热点阅读