美文网首页
CocoaPods 私有仓库的创建

CocoaPods 私有仓库的创建

作者: 肉身佛陀 | 来源:发表于2017-08-12 17:08 被阅读134次

    基于cocoaPods 1.2.1, Xcode8.3;
    借助MJRefresh作为示例展示:
    参考:
    Cocoapods 入门
    如何编写一个CocoaPods的spec文件
    CocoaPods 私有仓库的创建
    用CocoaPods做iOS程序的依赖管理

    一.首先获取MJRefresh源码

    GitHub 克隆(clone)或下载(download MJRefresh)的源码。克隆会获取连同.git在内的所有文件;下载只下载对应分支的文件,不包含.git文件;

    1.使用Xcode,打开Xcode,选择Check out an existing project后输入https://github.com/CoderMJLee/MJRefresh.git,点击next后选择保存路径下载成功即可。

    2.使用命令行
    //打开Desktop文件夹
    $ cd /Users/thinkjoy/Desktop
    //使用git的clone命令获取文件
    $ git clone https://github.com/CoderMJLee/MJRefresh.git
    
    下载中.png
    下载完成.png

    二.创建私有版本库repos

    1.在码云上创建一个远程版本库
    创建私有版本库1.png
    创建私有版本库2.png
    2.将创建的远程版本库添加到本地
    $ pod repo add MyRepos https://git.oschina.net/ruishengDu/MyRepos.git
    

    在Finder前往~/.cocoapods/repos文件夹查看,如图新增MyRepos文件夹;

    MyRepos
    3.创建代码库,并clone到本地

    码云上创建代码库,并clone到本地;

    $ cd /Users/thinkjoy/Desktop
    $ git clone https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git
    
    创建代码库
    4.导入代码文件,编写.podspec文件

    将clone的MJRefresh文件夹的MJRefresh文件夹拷贝到MJRefreshPodsTest文件夹内,如图:


    clone的MJRefresh文件目录

    其中.podspec文件是代码库的pod描述文件,通过pod命令行创建:

    $ cd /Users/thinkjoy/Desktop/MJRefreshPodsTest
    $ pod spec create MJRefreshPodsTest
    

    运行命令行后,可在MJRefreshPodsTest目录下看到MJRefreshPodsTest.podspec文件,打开如下(添加部分注释):

    #
    #  Be sure to run `pod spec lint MJRefreshPodsTest.podspec' to ensure this is a
    #  valid spec and to remove all comments including this before submitting the spec.
    #
    #  To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
    #  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
    #
    
    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         = "MJRefreshPodsTest"  #项目名称
      s.version      = "0.0.1"      # 版本号
      s.summary      = "A short description of MJRefreshPodsTest."  #项目简介,pod search命令就是根据s.name和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  = <<-DESC
                       DESC
    
      s.homepage     = "http://EXAMPLE/MJRefreshPodsTest"   #代码仓库主页
      # 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'.
      #
      # 开源授权版本,建议使用下面的,检查代码库中是否有FILE_LICENSE文件,默认创建的是LICENSE,直接用LICENSE替代FILE_LICENSE
      s.license      = "MIT (example)"
      # 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             = { "肉身佛陀94" => "1**********@139.com" }  #作者信息
      # Or just: s.author    = "肉身佛陀94"
      # s.authors            = { "肉身佛陀94" => "1**********@139.com" }
      # s.social_media_url   = "http://twitter.com/肉身佛陀94"  #作者主页
    
      # ――― 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, "5.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 => "http://EXAMPLE/MJRefreshPodsTest.git", :tag => "#{s.version}" } #代码仓库地址,#{s.version}是对应版本的Tag标签名,如Tag标签名与版本号不一致,需修改为Tag标签名;例:版本号为“0.0.1”,Tag标签为“V0.0.1”,就需要修改为V0.0.1;
    
    
      # ――― 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  = "Classes", "Classes/**/*.{h,m}" #代码的位置, 前一部分Classes/**/是一个相对目录,目录的层级关系一定要跟代码库的保持一致。最后一部分*.{h,m}是一个类似正则表达式的字符串,表示匹配所有以.h和.m为扩展名的文件
      s.exclude_files = "Classes/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  = "SomeFramework"
      # s.frameworks = "SomeFramework", "AnotherFramework"  #依赖iOS的framework
    
      # s.library   = "iconv"
      # s.libraries = "iconv", "xml2"  #依赖iOS的library
    
    
      # ――― 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
    
      # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
      # s.dependency "JSONKit", "~> 1.4"        #依赖于其他的第三方库
    
    end
    

    删掉注释后最终结果如下:

    Pod::Spec.new do |s|
    
      s.name         = "MJRefreshPodsTest"
      s.version      = "0.0.1"
      s.summary      = "根据MJRefresh作为源码的pods私有库测试"
      s.description  = <<-DESC
                        根据MJRefresh作为源码的pods私有库测试
                       DESC
    
      s.homepage     = "http://git.oschina.net/ruishengDu/MJRefreshPodsTest"   #代码仓库主页
      s.license      = { :type => "MIT", :file => "LICENSE" }
    
      s.author    = "肉身佛陀94"
      s.platform     = :ios, "8.0"  #平台及最低支持版本
      s.source       = { :git => "https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git", :tag => "#{s.version}" }
      s.source_files  = "MJRefresh", "MJRefresh/**/*.{h,m}"
      s.resource     = 'MJRefresh/MJRefresh.bundle'
      s.requires_arc = true #是否支持ARC
    end
    

    配置完成后,我们需要验证下 .podspec 文件的格式正确性,在终端输入

    $ cd /Users/thinkjoy/Desktop/MJRefreshPodsTest
    $ pod lib lint
    

    运行结果可能会有以下警告:

    • - WARN | description: The description is equal to the summary.description和summary相同,修改后即可;
    • - WARN | summary: The summary is not meaningful.summary使用的是默认值;
    • - WARN | description: The description is shorter than the summary.description比summary短;
    • - WARN | url: The URL (http://git.oschina.net/ruishengDu/MJRefreshPodsTest) is not reachable.地址无法访问,原因不知道,可以使用$ pod lib lint --private$ pod lib lint --allow-warnings 忽略警告;
    • [!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a '.swift-version'file to set the version for your Pod. For example to use Swift 2.3, run: 'echo "2.3" > .swift-version'. You can use the '--no-clean' option to inspect any issue.未设置默认支持Swift版本,可以使用$ echo "3.0" > .swift-version命令设置,运行后MJRefreshPodsTest目录下会生成一个.swift-version隐藏文件;

    成功后会有MJRefreshPodsTest passed validation.的提示;

    4.将本地MJRefreshPodsTest目录下的文件推到远程代码库

    执行命令:

    $ cd /Users/thinkjoy/Desktop/MJRefreshPodsTest
    $ git add .
    $ git commit -m '0.0.1完成'
    $ git push https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git
    $ git tag -a 0.0.1 -m 'V0.0.1'
    $ git push --tags
    

    完成后,远程版本库如下图:


    远程代码库
    5.将代码中的pod描述文件推送到版本仓库

    执行命令:

    $ pod repo push MyRepos MJRefreshPodsTest.podspec
    

    成功后通过~/.cocoapods/repos/MyRepos查看,新增的版本库中已添加MJRefreshPodsTest的描述信息,如图:

    ~/.cocoapods/repos/MyRepos目录

    MyRepos远程版本仓库中同样已添加MJRefreshPodsTest的描述信息,如图:

    MyRepos远程版本库

    此时,我们可以在pod 中搜索MJRefreshPodsTest,点击键盘的Q键退出pod search命令

    $ pod search MJRefreshPodsTest
    
    -> MJRefreshPodsTest (0.0.1)
       根据MJRefresh作为源码的pods私有库测试
       pod 'MJRefreshPodsTest', '~> 0.0.1'
       - Homepage: http://git.oschina.net/ruishengDu/MJRefreshPodsTest
       - Source:   https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git
       - Versions: 0.0.1 [MyRepos repo]
    (END)
    

    三.私有版本库的使用

    我们在桌面上创建一个名为PodTestOC工程,在工程目录下创建并编辑好podflie文件,podflie如下:

    source 'https://git.oschina.net/ruishengDu/MyRepos.git' #远程仓库地址
    platform :ios, '8.0'
    inhibit_all_warnings!
    target 'PodTest' do
    pod ‘MJRefreshPodsTest’ #我们的私有库
    end
    

    执行命令行:

    $ cd /Users/thinkjoy/Desktop/PodTest
    $ pod install
    

    运行成功后,PodTest工程目录如下:

    PodTest工程目录

    通过.xcworkspace文件打开项目,查看Pods目录,MJRefreshPodsTest中包含所有我们上传的代码,如图:

    PodTest工程中Pods目录

    此时,MJRefreshPodsTest已添加到项目中,但是直接使用#import "***.h"引用头文件没有提示,方法如下:

    • 可以使用#import <MJRefreshPodsTest/***.h>引用头文件;

    • Target -> Build Settings ->User Header Search Paths条目中,添加${SRCROOT}或者$(PODS_ROOT),并且选择Recursive;

    四.在项目中移除CocoaPods三方库配置文件

    • 删除工程中的Podfile文件Podfile.lock文件.xcworkspace文件Pods文件夹;
    • 删除Target -> Build Settings ->Link Binary With Libraries中的libPods-工程名.a文件;
    • 删除Target -> Build Settings中的[CP] Check Pods Manifest.lock[CP] Embed Pods Frameworks[CP] Copy Pods Resources选项;
    • 删除之前对Target -> Build Settings ->User Header Search Paths设置,没设置的不用理会;

    五.代码库的更新

    当我们的代码需要更新时,例如MJRefreshPodsTest修改了部分代码,更新版本号为0.0.2;

    1.修改.podspec 文件如下
    Pod::Spec.new do |s|
    
      s.name         = "MJRefreshPodsTest"
      s.version      = "0.0.2"
      s.summary      = "根据MJRefresh作为源码的pods私有库测试,V0.0.2"
      s.description  = <<-DESC
                        根据MJRefresh作为源码的pods私有库测试,V0.0.2
                       DESC
    
      s.homepage     = "http://git.oschina.net/ruishengDu/MJRefreshPodsTest"   #代码仓库主页
      s.license      = { :type => "MIT", :file => "LICENSE" }
    
      s.author    = "肉身佛陀94"
      s.platform     = :ios, "8.0"  #平台及最低支持版本
      s.source       = { :git => "https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git", :tag => "#{s.version}" }
      s.source_files  = "MJRefresh", "MJRefresh/**/*.{h,m}"
      s.resource     = 'MJRefresh/MJRefresh.bundle'
      s.requires_arc = true #是否支持ARC
    end
    

    配置完成后,我们需要验证下 .podspec 文件的格式正确性,在终端输入

    $ cd /Users/thinkjoy/Desktop/MJRefreshPodsTest
    $ pod lib lint
    

    成功后会有MJRefreshPodsTest passed validation.的提示;

    2.将本地修改后的MJRefreshPodsTest目录下的文件推到远程代码库

    执行命令:

    $ cd /Users/thinkjoy/Desktop/MJRefreshPodsTest
    $ git add .
    $ git commit -m '0.0.2完成'
    $ git push https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git
    $ git tag -a 0.0.2 -m 'V0.0.2'
    $ git push --tags
    

    完成后,远程版本库如下图:


    远程代码库
    3.将代码中的pod描述文件推送到版本仓库

    执行命令:

    $ pod repo push MyRepos MJRefreshPodsTest.podspec
    

    成功后通过~/.cocoapods/repos/MyRepos查看,新增的版本库中已添加MJRefreshPodsTest的描述信息,如图:


    ~/.cocoapods/repos/MyRepos目录

    MyRepos远程版本仓库中同样已添加MJRefreshPodsTest的描述信息,如图:

    MyRepos远程版本库

    此时,我们可以在pod 中搜索MJRefreshPodsTest,点击键盘的Q键退出pod search命令

    $ pod search MJRefreshPodsTest
    
    -> MJRefreshPodsTest (0.0.2)
       根据MJRefresh作为源码的pods私有库测试,V0.0.2
       pod 'MJRefreshPodsTest', '~> 0.0.2'
       - Homepage: http://git.oschina.net/ruishengDu/MJRefreshPodsTest
       - Source:   https://git.oschina.net/ruishengDu/MJRefreshPodsTest.git
       - Versions: 0.0.2, 0.0.1 [MyRepos repo]
    (END)
    

    代码库更新成功;

    本文档仅用来记录学习的过程,如有错误的地方还望指正,

    相关文章

      网友评论

          本文标题:CocoaPods 私有仓库的创建

          本文链接:https://www.haomeiwen.com/subject/wcjrrxtx.html