CocoaPods私有源搭建

2,638 阅读4分钟

往期文章

寻找IOS相册中相似图片
NSNotification与类对象,实例对象
iCloud-Documents存储

创建Spec索引库

建立远程索引库

首先在内网的git上创立一个中创建一个库名为 MySpecs的库,用来保存CocoaPods在内网中的搜索路径库。

 pod repo add DemoSpecs http://192.168.3.225:4080/mbackupper/mobile/MySpecs.git

将远程索引库关联到本地

使用Pod命令将远程索引库关联到本地

touch ReadMe.md
git add ReadMe.md
git commit -m "私有索引库"
git add remote http://192.168.3.225:4080/mbackupper/mobile/MySpecs.git
git push -u origin master

这里就会在个人电脑的~目录下的.cocoapods文件夹,可以看到repos下面有两个文件夹,一个是master,一个是MySpecs。

master是Cocoapods的官方索引库,而MySpecs是刚才我们建立的私人的索引库,现在里面还什么都没有。而这里面将来会存放内网的一些私有组件,如下图。

MySpecs里存放是相关私有库的索引,而不是具体的私有库

image.png

实际存储私有库的位置

image.png

如果远程索引库没有和本地库相关联,就会产生如下报错

Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.

生成一个私有组件库

使用Pod模板创建一个项目

刚刚生成的远程索引库没有保存任何的的私有库的索引,现在我们就来建立一个私有组件库并向远程索引库添加。

使用模板创建一个AMMediator项目。

pod lib create AMMediator

模板会询问你几个关于创建的问题

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?
 > AM

一般创建完成的模板项目都会放在默认目录 /Users/mac下(MacOS系统)

创建好的目录结构如下所示

image.png

Example 主要存放对应的demo

AMMediator 主要存放代码和需要的资源

podspec文件 则是记录着当前组件的相关信息,包括git地址,对应的描述,需要依赖的库等等

向模板中放入源代码

在AMMediator目录中,Assets用以保存组件库相关的资源,Classes用以保存组件库相关的源代码

企业微信截图_bced38cd-5970-4e29-8410-682f315a080a.png

然后进入Example中,执行

pod install

这样在Example中,我们就成功导入了私有组件库,我们可以再Example中写一些私有组件库使用相关的例子。

到此我们使用模板创建了一个私有的组件库。

将模板保存到远端库

修改模板项目中的AMMediator.podspec配置文件,该配置文件用于索引库的索引任务,索引库库可以根据podspec文件找到其所对应的私有库的实际位置。

image.png

如上图可以看到MySpecs中所保存的索引其实就是每个私有组件库的podspec配置文件

PodSpecs配置结构如下

#
# Be sure to run `pod lib lint AMMediator.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             = 'AMMediator'
  s.version          = '1.1.1'
  s.summary          = 'my module'

# 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://xxxxxxx/AMMediator'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'coderjun' => 'coderjun@foxmail.com' }
  s.source           = { :git => 'http://192.168.3.225:4080/mbackupper/mobile/ios_modules/modulerepos/ammediator.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'

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

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

关键参数

s.source:git : 私有库实际的地址,也就是存放AMMeditor项目的远程git库

s.source:tag : 对应的tag版本号,可以看到这里取得是s.version中的数值

s.source_files : 私有组件源代码保存地,也就是之前所说的Classes目录下。

s.dependency : 私有组件依赖的第三方组件,比如这里我们的私有组件依赖于AFNetworking

s.version : s.version一定要与私有组件库中的git中的tag一致。

修改完podspec文件之后,就可以将本地的私有组件库代码推送到远端库。注意本地git库中打tag的时候,应该与podspec中的version一致。这里建议写一个sh,将私有组件项目中的bundlVersion作为tag,设置podspec中s.version 和 git库中的tag。

这样一来我们只需在项目中设置bundleVersion,然后使用脚本打包上传。这样脚本就会自动同步我们的bundleVersion到podspec文件中,并且在git中给我们打上对应的标签。

作者实现的打包脚本如下

# 获取bundlVersion
function cmd_push(){
	# 获取bundlVersion
	BundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" /Users/mac/Desktop/Modules/MCiCloudDrivePlugin/MCiCloudDrivePlugin.framework/Info.plist)
	# sed替换spec文本内容
	sed -i "" "s/s.version\([ ]\{1,\}\)=\([ ]\{1,\}\)\([\'|\"]\)\([^\"]\{1,\}\([\'|\"]\)\)/s.version = \"$BundleVersion\"/g"  MCiCloudDrivePlugin.podspec
	# git库更新并打上标签
	git add .
	git commit -m "update $BundleVersion"
	git push -u origin master
	git tag $BundleVersion
	git push --tags 
	# push到本地pod库上,发布版本
	pod repo push AMModuleSpecs MCiCloudDrivePlugin.podspec --use-libraries --allow-warnings --sources=http://192.168.3.225:4080/mbackupper/mobile/ios_modules/ammodulespecs.git
	echo "脚本执行完毕"
}

最后将私有库添加到私有索引库中

pod repo push AMModuleSpecs AMMediator.podspec

等待执行完之后,我们就可以看到我们的私有库AMMediator已经被添加到私有索引库当中了。

我们只需要在项目中,给Podfile文件设置好相关的source就可以访问我们自己的私有源了。

source 'https://gitee.com/mirrors/CocoaPods-Specs.git'
source 'http://192.168.3.225:4080/mbackupper/mobile/ios_modules/ammodulespecs.git'
platform :ios, '9.1'
use_frameworks!
target 'mBackup' do
pod 'AMMediator' ,'0.1.1'
end

CocoaPods私有源在一些保密性比较高的项目中,可以实现内网搭建一些私有的组件库供项目组使用,同时又避免了私有的组件库可以被外部访问的问题。