建立CocoaPods私人库
前言
平时公司做项目积累了一些自己封装的类,每次新建项目总要拖进去总感觉很low,所以决定弄个pod私人库,顺便了解一下组件化开发,以下仅是记录下创建和使用过程和遇到的问题。
过程
创建两个仓库
首先要在代码托管平台(CODING,码云,github,BitBucket等)创建两个仓库,一个是放Spec Repo的,一个放你的代码库。
我使用的是码云,命名根据个人习惯,看的懂就好:
创建私有Spec Repo
什么Spec Repo呢?
Spec Repo是所有的Pods的一个索引,就是一个容器,所有公开的Pods都在这个里面,它实际是一个Git仓库remote端在GitHub上,但是当你使用了Cocoapods后它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到master文件夹就是这个官方的Spec Repo了。
怎么创建呢?
到Terminal执行:
12# pod repo add [Private Repo Name] [GitHub HTTPS clone URL]$ pod repo add JNUIKitComponentSpec https://gitee.com/jacknehc/JNUIKitComponentSpec.git执行完到~/.cocoapods/repos目录,看到JNUIKitComponentSpec就是成功了。
创建Pod项目工程文件
可以使用Cocoapods提供的一个工具直接创建代码库,cd到自己存放代码地方执行:
1pod lib create JNUIKitComponent创建成功会出现如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051Cloning `https://github.com/CocoaPods/pod-template.git` into `JNUIKitComponent`.Configuring JNUIKitComponent 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 ]> iOSWhat language do you want to use?? [ Swift / ObjC ]> ObjCWould you like to include a demo application with your library? [ Yes / No ]> YesWhich testing frameworks will you use? [ Specta / Kiwi / None ]> NoneWould you like to do view based testing? [ Yes / No ]> NoWhat is your class prefix?> JNRunning pod install on your new library.Analyzing dependenciesFetching podspec for `JNUIKitComponent` from `../`Downloading dependenciesInstalling JNUIKitComponent (0.1.0)Generating Pods projectIntegrating client project[!] Please close any current Xcode sessions and use `JNUIKitComponent.xcworkspace` for this project from now on.Sending statsPod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.[!] Automatically assigning platform ios with version 9.3 on target JNUIKitComponent_Example because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.Ace! you're ready to go!We will start you off by opening your project in Xcodeopen 'JNUIKitComponent/Example/JNUIKitComponent.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`.他会询问几个问题,根据实际使用情况自己选择就好。
存放代码
把你自己的代码放到刚建的Pod项目文件工程的这个目录下:
修改.podspec文件
修改Pod项目文件工程根目录下的.podspec文件,一般改这几个参数:
修改后要验证下,cd到.podspec文件目录下,执行:
1pod lib lint --allow-warnings因为会有警告,所以我一般都加上–allow-warnings
提交代码到代码仓库
可能git使用的不熟,遇到点小问题,首先cd到Pod项目文件工程目录下,执行以下提交代码:
12345$ git add .$ git commit -s -m "Initial Commit of Library"# 添加远端仓库$ git remote add origin git@gitee.com:jacknehc/JNUIKitComponent.gitgit@gitee.com:jacknehc/JNUIKitComponent.git是你存放的代码寄放平台获取的ssh地址,通过ssh协议来拉取代码,所以你需要在本地生成公钥私钥:
12# -C后面填写你的邮箱ssh-keygen -t rsa -C 'xxx@xxx.com'然后到~/.ssh/查看id_rsa.pub,复制里面的内容到代码寄放平台的公钥里就可以了,然后接下来提交代码:
12# 提交到远端仓库$ git push origin master这时候报错提示:
12345678To gitee.com:jacknehc/JNUIKitComponent.git! [rejected] master -> master (fetch first)error: failed to push some refs to 'git@gitee.com:jacknehc/JNUIKitComponent.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.这是要你先pull更新下远程的内容
1git pull origin master --allow-unrelated-histories不加
--allow-unrelated-histories
会报1fatal: refusing to merge unrelated histories的错,执行完后又有错:
1234567From gitee.com:jacknehc/JNUIKitComponent* branch master -> FETCH_HEADAuto-merging README.mdCONFLICT (add/add): Merge conflict in README.mdAuto-merging .gitignoreCONFLICT (add/add): Merge conflict in .gitignoreAutomatic merge failed; fix conflicts and then commit the result.执行:
1git status提示:
123456789101112On branch masterYou have unmerged paths.(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)Unmerged paths:(use "git add <file>..." to mark resolution)both added: .gitignoreboth added: README.mdno changes added to commit (use "git add" and/or "git commit -a")所以我执行了以下三句命令:
123git add .gitignoregit add README.mdgit commit然后在push:
1git push origin master终于成功了/(ㄒoㄒ)/~~,然后接下来一步是重点,要到你代码寄存平台打tag,或者终端git命令也行,这个tag要跟第5步修改的.podspec文件的
s.version
参数所写的标签一样,以后更新也是如此,代码仓库的tag要和.podspec文件的版本对的上。向Spec Repo提交.podspec
当然要cd到你的.podspec目录下,
执行以下命令:12# 前面是本地Repo名字 后面是podspec名字$ pod repo push JNUIKitComponentSpec JNUIKitComponent.podspec成功后会出现:
123456789101112131415161718192021222324Counting objects: 99, done.Delta compression using up to 8 threads.Compressing objects: 100% (90/90), done.Writing objects: 100% (99/99), 37.02 KiB | 0 bytes/s, done.Total 99 (delta 25), reused 0 (delta 0)remote: Powered by Gitee.comTo gitee.com:jacknehc/JNUIKitComponent.gitb5560cf..858258c master -> masterjacknehc ~/Documents/My/MyPods/Private/JNUIKitComponent master pod repo push JNUIKitComponentSpec JNUIKitComponent.podspec --allow-warningsValidating spec-> JNUIKitComponent (0.1.0)- WARN | summary: The summary is not meaningful.- WARN | url: The URL (https://gitee.com/jacknehc/JNUIKitComponent) is not reachable.Updating the `JNUIKitComponentSpec' repoAlready up-to-date.Adding the spec to the `JNUIKitComponentSpec' repo- [Add] JNUIKitComponent (0.1.0)Pushing the `JNUIKitComponentSpec' repo
使用
至此,私人库就制作完成了,使用的时候profile要这样填写:
参考
原文作者: JackNehc
原文链接: https://jacknehc.github.io/2018/06/22/建立CocoaPods私人库/
许可协议: 知识共享署名-非商业性使用 4.0 国际许可协议