组件化——cocoapods组件化

1.远程索引库

每创建一个组件都会带有一个 xxx.podspec的索引文件。专门用来存放这些索引文件的库就叫做索引库。我们需要将这些索引文件上传到远程索引库才能保证其他的人能够拿来用。私有和开源的创建方式一样。下面以码云为例
我已经创建过了

image.png

2.远程代码库 (代码实际存放的远程仓库)

image.png

3.索引库关联本地

cd ~/.cocoapods/repos
pod repo add LBSpecs https://gitee.com/Gary0726/lbspecs.git

输入gitee账号密码后就会在本地生成

image.png

4.本地创建文件夹

在本地创建一个文件夹用来存放组件代码,并进入到该路径

cd /Users/xxx/Documents/Libs

5.创建本地组件模板

pod lib create LBNetwork
Cloning `https://github.com/CocoaPods/pod-template.git` into `LBNetwork`.
Configuring LBNetwork 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 ]
 > none

Would you like to do view based testing? [ Yes / No ]
 > no

What is your class prefix?
 > LB

Running pod install on your new library.

创建好后会自动打开一个工程,配置xx.podspec文件如下

Pod::Spec.new do |s|
  s.name             = 'LBNetwork'
  s.version          = '0.1.0'
  s.summary          = 'A short description of LBNetwork.'

# 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
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://gitee.com/Gary0726/lbnetwork'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'LuckyBlue' => '[email protected]' }
  s.source           = { :git => 'https://gitee.com/Gary0726/lbnetwork.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '9.0'

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

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

s.homepage=>远程仓库的主页地址

image.png

s.source=>远程仓库组件地址
image.png

如果有依赖别的第三方库就用s.dependency追加上,再到Podfile添加上

use_frameworks!
platform :ios, '9.0'

target 'LBNetwork_Example' do
  pod 'LBNetwork', :path => '../'
  pod 'AFNetworking', '~> 4.0.1'
  pod 'YYModel'
  
  target 'LBNetwork_Tests' do
    inherit! :search_paths
  end
end

pod install安装好即可。

6.添加远端组件仓库

cd /Users/xxx/Documents/Libs/LBNetwork
git remote add origin https://gitee.com/Gary0726/lbnetwork.git 

7.本地组件库代码提交

git push -u origin master //第一次可能会报错可尝试用 git push -u origin master -f 可能会覆盖远程的修改
git add . //记得后面一定要有 .
git commit -m "first commit"
git push -u origin master
git tag '0.1.0' //注意:这里的tag号必须和.podSpec文件的版本号一致
git push --tags

8.组件库验证

//本地验证
pod lib lint --use-libraries --allow-warnings
//远端验证
pod spec lint --use-libraries --allow-warnings

验证时如有遇到fatal: The remote end hung up unexpectedly那可能是网络有问题,自己起初用的github一直试都报这个错,尽管用了git config http.postBuffer 524288000,所以最终才选择用码云...

9.将xxx.spec 文件提交到本地的私有仓库,然后再push到远程仓库

pod repo push LBSpecs LBNetwork.podspec --use-libraries --allow-warnings

10.检查私有库是否成功

pod search LBNetwork

如果发布完成之后使用pod search组件名称却搜不到相关的信息,这个时候需要将cocoaPods的搜索缓存文件清理一下,然后重新使用pod search,就能得到最新的结果。

//进入搜索CocoaPods缓存目录
cd ~/Library/Caches/CocoaPods

//查看缓存文件search_index.json
ls

//删除搜索缓存文件
rm -f search_index.json

可能会用到的命令

//删除本地tag
git tag -d 0.1.0
//删除远程tag
git push origin :refs/tags/0.1.0
//查看本地添加了哪些远程地址
git remote -v
//删除本地指定的远程地址
git remote rm origin

如果公司内部其他成员要使用需要执行下面命令,将私有的远程索引库copy到本地,然后就可以正常使用了。

pod repo add LBSpecs https://gitee.com/Gary0726/lbspecs.git

你可能感兴趣的:(组件化——cocoapods组件化)