CocoaPods私有仓库的创建和使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Maple_ROSI/article/details/79314588

前方

随着项目的业务发展扩大,公司决定另外开一个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

然后会要求输入账号和密码
这里写图片描述

在 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命令创建项目并生成依赖。

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

提供给外部使用的文件都放在这里
这里写图片描述

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

在回到Example路径下,重新执行pod install操作
$ cd /Users/maple_li/DRCategories/Example

$ pod install

到这里,我们可以在模板测试工程中进行修改和操作!

2、通过Cocoapods创建出来的目录本身就在本地的Git管理下,我们需要做的就是给它添加远端仓库,同样去GitLab拿到HTTP地址,然后cd到DRCategories目录

$ git add.
$ git commit -s -m "Initial Commit of Library"

3、编辑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

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

$ pod lib lint

当你看到

 -> DRCategories (0.1.0)

DRCategories passed validation.

时,说明验证通过了

5、因为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到远端仓库

6、向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、使用与其它第三方库一样,只是私有库版本更新维护是自己处理


三、私有库维护

持续更新……

猜你喜欢

转载自blog.csdn.net/Maple_ROSI/article/details/79314588