CocoaPods 创建私有仓库,使用和维护(2019更新)

前方

随着项目的业务发展扩大,公司决定另外开一个APP,但是会用到现在的一些公共组件,照搬过去用当然没问题,但是有一个问题,如果以后维护公共组件那还不得几个APP都去修改,所以想到了封装成私有库便于维护。

准备工作

1、CocoaPods,Cocoapods是出色的一个iOS依赖管理工具,使用它可以方便的管理和更新项目中所使用到的第三方库,以及将自己的项目中的公共组件交由它去管理。
2、Git版本控制管理,项目中使用的是gitlab,注册或使用现有的账号。

一、创建

1)、创建私有索引库

1、首页创建一个存放版本描述文件的git仓库,去https://gitlab.com/创建一个远程私有仓库(免费)

这里写图片描述

这里写图片描述
这样远程的索引库就创建完成了

2、在终端把远程的私有索引库添加到本地索引

$ pod repo add DRSpecs https://gitlab.com/PrivateSpecs/DRSpecs.git

然后会要求输入账号和密码
这里写图片描述
然后查看本地的索引库

$ pod repo

在这里插入图片描述
在 Finder 目录 ~/.cocoapods/repos,master是cocoaPods上版本库的列表
这里写图片描述

2)、创建代码库

1、这里我是把代码与版本描述文件分离,这样结构就比较清晰

代码库创建与版本库创建是一样的,不同之处就是多了项目工程文件,这里的项目工程是编辑我们的私有版本库文件

这里写图片描述
这里写图片描述

先cd到要创建项目的目录合适位置然后执行

$ pod lib create DRCategories

之后它会问你5个问题:
What language do you want to use?? [ Swift / ObjC ]
Would you like to include a demo application with your library? [ Yes / No ]
Which testing frameworks will you use? [ Specta / Kiwi / None ]
Would you like to do view based testing? [ Yes / No ]
What is your class prefix?
1.你用什么语言?
2.是否需要一个例子工程;
3.选择一个测试框架;
4.是否基于View测试;
5.类的前缀;

5个问题的具体介绍可以去看官方文档,我这里选择的是1.ObjC; 2.yes; 3.Specta/Expecta; 4.yes; 5.DR。 问完这4个问题他会自动执行pod install命令创建项目并生成依赖。

然后本地可以看到
这里写图片描述

提供给外部使用的文件都放在这里,可供外部使用的文件全部放在Classes文件夹中
这里写图片描述

这里替换试一下
这里写图片描述

如果前面的步骤里面选择了添加demo,模板库已经默认帮我们在Podfile中指定了DRCategories.podspec的位置

pod ‘DRCategories’, :path => ‘…/’

那么在Example路径下,执行pod install操作,使得可以安装和测试我们的代码
$ cd /Users/maple_li/DRCategories/Example

$ pod install

到这里,我们可以在模板测试工程中进行修改和操作!如果测试没什么问题,就可以将podspec文件上传到私有索引库,在此之前需要对spec修改。

2、编辑podspec文件,直接双击DRCategories.podspec文件,用Xcode打开进行编辑

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  s.name         = "DRCategories" #名称
  s.version      = "0.1.0"        #版本
  s.summary      = "常用类别"      #简介

  # 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  = "集合了开发中常用的类别,包括NSSting、UIView、NSObject、NSArray、NSDictionary……"  #详细描述

  s.homepage     = "https://gitlab.com/ModulePlugin/DRCategories"   #主页地址
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif" #截图地址


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See http://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = { :type => 'MIT', :file => 'LICENSE' }         #开源协议
  # s.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  s.author             = { "LeeCoach" => "********@yeah.net" }     #作者
  # Or just: s.author    = "LeeCoach"
  # s.authors            = { "LeeCoach" => "DRCategories" }
  # s.social_media_url   = "http://twitter.com/XXX"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # s.platform     = :ios
   s.platform     = :ios, "8.0"  #支持的平台及版本

  #  When using multiple platforms
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "https://gitlab.com/ModulePlugin/DRCategories.git", :tag => s.version.to_s }   #代码地址


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  s.source_files  = "DRCategories", "DRCategories/**/*.{h,m}" #代码源文件地址,**/*表示Classes目录及其子目录下所有文件,如果有多个目录下则用逗号分开,如果需要在项目中分组显示,这里也要做相应的设置
  s.exclude_files = "DRCategories/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # s.resource  = "icon.png"  #资源路径
  # s.resources = "Resources/*.png"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

   s.framework  = "UIKit"  #使用到的系统库
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

   s.requires_arc = true      #是否使用ARC,如果指定具体文件,则具体的问题使用ARC

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"    #依赖关系,该项目所依赖的其他库,如果有多个需要填写多个s.dependency

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

$ pod lib lint

当你看到

 -> DRCategories (0.1.0)

DRCategories passed validation.

时,说明验证通过了

4、因为podspec文件中获取Git版本控制的项目还需要tag号,所以我们要打上一个tag

$ git status
$ git add .
$ git commit -m '编辑spec文件'
$ git remote add origin https://gitlab.com/ModulePlugin/DRCategories.git #添加远端仓库
$ git push origin master  #提交到远端
$ git tag -m "first release" "0.1.0" (要与DRCategories.podspec文件中的tag值保持一致)
$ git push --tags     #推送tag到远端仓库

5、向Spec Repo提交podspec
向Spec Repo提交podspec需要完成两点一个是podspec必须通过验证无误,在一个就是删掉无用的注释(这个不是必须的,为了规范还是删掉吧)。 向我们的私有Spec Repo提交podspec只需要一个命令

$ pod repo push DRSpecs DRCategories.podspec

执行后,当出现下面这样才算是成功了

Validating spec
 -> DRCategories (0.1.0)

Updating the `DRSpecs' repo

From https://gitlab.com/PrivateSpecs/DRSpecs
 * [new branch]      master     -> origin/master

Adding the spec to the `DRSpecs' repo

 - [Add] DRCategories (0.1.0)

Pushing the `DRSpecs' repo

GitLab端
这里写图片描述

本地Repos
这里写图片描述


二、私有库的使用

1、用Xcode打开编辑Podfile文件
这里写图片描述

2、添加私有库索引到本地,先$ cd 到项目路径,然后执行下面语句

$ pod install

加载完成可以看到代码已经整合到我们的项目中了

3、查看本地的pod库索引

$ pod repo 

这里写图片描述

通过$ pod search DRCategories 可以查询到自己维护的私有库
这里写图片描述

4、使用与其它第三方库一样,只是私有库版本更新维护是自己处理


三、私有库维护

1.添加新文件,还是依然放在Classes文件夹下
在这里插入图片描述
2. cd 到 Example文件夹下,即测试的demo目录位置,执行

$ pod install

进行测试代码有无问题

3.修改podspec文件,最重新的版本号
在这里插入图片描述
4.校验podspec文件,本地难文件有效性

$ pod lib lint

或加上代码库名

$ pod lib lint DRCategories.podspec

若要忽略警告

$ pod lib lint DRCategories.podspec --allow-warnings

出现如下,表示验证通过
在这里插入图片描述
5.提交项目到远程代码库

$ git add .
$ git commit -m “更新提交”
//$ git pull origin master //如果是多人协同工作,先拉一下
$ git push origin master

6.打标签

$ git tag -m “更新版本标签” -a 0.1.4
$ git push --tags

查看所有标签 git tag
在这里插入图片描述

7.验证本地和远程文件有效性 ,忽略警告可加--allow-warnings

$ pod spec lint --allow-warnings

验证如果成功如下:
在这里插入图片描述
8.向私有的spec repo远程仓库提交podspec

$ pod repo push DRSpecs DRCategories.podspec

出现如下,说明成功了
在这里插入图片描述

9.查看是否已经成功

$ pod search DRCategories

在这里插入图片描述

10.验证podspec报错

  • WARN | summary: The summary is not meaningful.
    - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use --verbose for more information.
    - NOTE | xcodebuild: note: Using new build system
    - NOTE | [iOS] xcodebuild: note: Planning build
    - NOTE | [iOS] xcodebuild: note: Constructing build description
    - NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target ‘App’)
    - NOTE | [iOS] xcodebuild: fatal error: ‘UIFont+DRFont.h’ file not found
    - NOTE | [iOS] xcodebuild: Headers/Public/DRCategories/DRCategories.h:12:9: fatal error: ‘UIFont+DRFont.h’ file not found

一般都podspec配置没改好,根据提示修改,再验证

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值