創建GitHub私有庫,製作CocoaPods依賴庫

CocoaPods 是託管在 github 上的,所有的 Pods 也都是託管在 github 上
其實讓自己的工程支持cocoapods很簡單只需要幾步
1.創建自己的私有庫,github上上傳自己的代碼
2.clone倉庫到本地
3.創建並編輯.podspec
4.創建LICENSE(許可證/授權)文件、README.md文件
5.驗證.podspec文件
6.提交修改到GitHub.將自己的項目打成tag
7.註冊CocoaPods
8.發佈

1.創建自己的 github 倉庫

上圖中標識出了6處地方

  1. Repository name: 倉庫名稱,這裏寫上我們的依賴庫名字 O2View ,必填。
  2. Description: 倉庫的描述信息,可選。
  3. Public or Private: 倉庫的公開性,開源的話選 Public 。 如果是公司內部使用的話這裏推薦大家使用私有的倉庫,但是 github 的私有倉庫是要收費的 $7/month 。大部分公司都是有自己的git服務器,不過一般只能在公司內網使用,或者可以選擇Gitlab,Coding,Bitbucket任意一種。
  4. Initialize: 是否默認創建一個 README 文檔,一個完整的庫都會有這個說明文檔,這裏最好勾選一下。不過如果忘記的話也不要緊,後面手動創建也是可以的。
  5. .gitignore: 忽略項文件,記錄一些想忽略的文件類型,凡是該文件包含的文件類型, git 都不會將其納入到版本管理中。看需要選擇就行。
  6. license: 正規的倉庫都有一個 license 文件, Pods 依賴庫對這個文件要求比較嚴格,需要有這個文件。這裏最好讓 github 自動創建一個,不過後續手動創建也行。我們這次先使用 MIT 類型的 license。

2.Clone倉庫到本地
    第一步:先cd到你想在本地存儲的目錄(我保存到桌面上)
    第二步:找到自己創建的工程,然後複製紅色框中的地址,執行clone命令

完整命令:

cd /Users/fjz/Desktop
 
git clone https://github.com/FuJunZhi/FUTabBarController.git


3.創建並編輯.podspec文件

兩種途徑:

  • 執行命令行創建(改起來比較麻煩)。
  • 複製已有的 podspec 文件然後修改對應的參數。

1>執行命令行創建:先cd到你項目的目錄,執行命令,你也可以使用vim創建,只要創建就可以了

// 注 FUTabBarController 這個是你框架的名稱
$ pod spec create FUTabBarController

創建好之後,用文本編輯器打開,內容如下(裏面會有許多沒用的字段):

#
#  Be sure to run `pod spec lint FUTabBarController.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         = "FUTabBarController"
  s.version      = "0.0.1"
  s.summary      = "A short description of FUTabBarController."
  # 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/FUTabBarController"
  # 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      = "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             = { "" => "" }
  # Or just: s.author    = ""
  # s.authors            = { "" => "" }
  # s.social_media_url   = "http://twitter.com/"
 
  # ――― 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/FUTabBarController.git", :tag => "#{s.version}" }
 
  # ――― 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}"
  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"
 
  # 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
  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"
end

2>複製已有的 podspec 文件然後修改對應的參數。

Pod::Spec.new do |s|
    s.name         = "FUTabBarController"
    s.version      = "1.0.2"
    s.summary      = "custom FUTabBar"
    s.homepage     = "https://github.com/FuJunZhi/FUTabBarController"
    s.license      = {:type=>"MIT",:file=>"LICENSE"}
    s.authors      = {"fujunzhi" => "[email protected]"}
    s.platform     = :ios, "7.0"
    s.source       = {:git => "https://github.com/FuJunZhi/FUTabBarController.git", :tag => s.version}
    s.source_files = "FUTabBarController/*.{h,m}"
    s.requires_arc = true
end

s.name:名稱,pod search 搜索的關鍵詞,注意這裏一定要和.podspec的名稱一樣,否則報錯
s.version:版本號
s.ios.deployment_target:支持的pod最低版本
s.summary: 簡介
s.homepage:項目主頁地址
s.license:許可證
s.author:作者
s.social_media_url:社交網址,你的podspec發佈成功後會@你
s.source:項目的地址
s.source_files:需要包含的源文件

" FUTabBarController/*                           “*” 表示匹配所有文件
""FUTabBarController/*.{h,m}"  “*.{h,m}” 表示匹配所有以.h和.m結尾的文件
"FUTabBarContriller/**/*.h"                       
“**” 表示匹配所有子目錄s.resources: 資源文件

s.requires_arc: 是否支持ARC
s.dependency:依賴庫,不能依賴未發佈的庫,如果有多個可以寫多個s.dependency

4.創建LICENSE文件、README.md文件

LICENSE文件
CocoaPods 強制要求所有的 Pods 依賴庫都必須有 license 文件,否則驗證不會通過。 license 文件有很多中,詳情可以參考 tldrlegal。前面我們已經選擇創建了一個 MIT 類型的 license。

MIT License
Copyright (c) 2016 FuJunZhi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

README.md文件
使用 github 的人應該都熟悉這個文件,它使一個成功的 github 倉庫必不可少的一部分,使用 markdown 對倉庫進行詳細說明。

# FUTabBarController
 *Custom FUTabBarController
 包括功能:選中按鈕動畫(縮放/旋轉)、選中字體可以改變大小顏色、所有按鈕都可以凸起、自定義push/模態跳轉、角標的自定義

 ### CocoaPods
   1. Add `pod 'FUTabBarController', '~> 1.0.2'` to your Podfile.
   2. Run `pod install` or `pod update`.
   3. 創建UITabBarController自定義類 繼承FUTabBarController
   4. 具體實現可以看demo`Home->MainTabBarController.m`

 ### 帶動畫的效果圖
 <img src="http://i2.bvimg.com/606664/112328df5cbe8bb9.gif" width="30%" height="30%">


5.驗證.podspec文件
驗證會先測試本地 .podspec 文件是否存在語法錯誤.
到此檢查一下你工程下面的文件, .podspec文件, LICENSE文件,這兩個文件必須要有
 第一種驗證命令:

//1.--allow-warnings 忽略一些警告
pod lib lint --allow-warnings


注:由於我們現在還沒有正式生成 release 版本, github 上並沒有任何 tag,所以我們剛纔填寫 .podspec 文件填寫 git 地址的時候沒有填寫指定 tag ,此時會有警告,解決方法是在命令後加上--allow-warnings


 第二種驗證命令(會打印出詳細信息):

//2. --verbose 如果驗證失敗會報錯誤信息
pod spec lint FUTabBarController.podspec --verbose

 驗證成功後:

        
  
  驗證失敗:

[!] The spec did not pass validation, due to 1 error.

這個過程會出現很多錯誤,這裏不一一列舉,大家按照錯誤信息自行修改,不過一般不會出現錯誤;大部分錯誤都是語法規範的問題(標點是否正確)

6.提交修改到GitHub,並對當前版本打Tag
1>驗證成功之後,我們只要把代碼提交到 github 倉庫,命令:

git add -A && git commit -m "add pod files"
 
git push origin master


2>打Tag:

因爲cocoapods是依賴tag版本的,所以必須打tag,
以後再次更新只需要把你的項目打一個tag
然後修改.podspec文件中的版本接着提交到cocoapods官方就可以了,提交命令請看下面

//爲git打tag
git tag "v1.0.2" 
//將tag推送到遠程倉庫
git push --tags

經過前面的步驟,我們的 CocoaPods 依賴庫就已經準備好了,但是現在你還不能在你的工程中使用它。如果你想成爲一個真正可用的依賴庫,還需要最後一步操作:將剛纔生產的podspec 文件提交到 CocoaPods Specs中。

沒錯,我們平時用的能用 pod search 搜到的依賴庫都會把它上傳到這個倉庫中, 也就是說只有將我們的podspec 文件上傳到這裏,才能成爲一個真正的依賴庫,別人才能用!

按照 github 的規則,要想向別人的庫中添加文件,就要先 fork 別人的倉庫,做相應的修改,再 pullrequest 給倉庫的原作者,等到作者審覈通過,進行 meger 之後就可以了!

7.註冊CocoaPods
     用pod --version命令查看安裝的版本,儘量使用最新版本

如果版本低,需要升級:

sudo gen install cocoapods
pod setup

已經註冊過的不需要註冊(可以跳過註冊步驟),怎麼看自己有沒有註冊

pod trunk me

我的註冊信息

FJZdeiMac:FUTabBarController-master fjz$ pod trunk me

  - Name:     FuJunZhi

  - Email:    [email protected]

  - Since:    September 18th, 2016 02:46

  - Pods:

    - FUCanlendar

    - FUThemeManager

    - FUTableViewIndexBar

    - FUTabBarController

  - Sessions:

    -September 18th, 2016 02:46 -         May 25th, 21:03. IP:

    180.166.66.226

    -August 17th, 19:32         - January 3rd, 2018 19:08. IP:

    218.83.245.210

註冊,如果沒有註冊信息就需要註冊了
        註冊的步驟:

1、pod trunk register 郵箱 name --verbose
2、註冊完之後,會向你郵箱發送一個鏈接,在郵箱中點擊確認一下就可以了

// 加上--verbose可以輸出詳細debug信息,方便出錯時查看。
pod trunk register [email protected] "FuJunZhi" --verbose

8.發佈

 

  • 驗證你的podspec文件是否合法。在trunk方式之前我們一般用“pod lib lint”命令進行驗證。
  • 上傳podspec文件到trunk服務器(其實最終也會自動添加到https://github.com/CocoaPods/Specs中,只是使用trunk方式省去了以前先fork在pull request的繁瑣操作)
  • 將你上傳的podspec文件轉成json格式文件
  • 執行上面的push操作,就相當於你把你的源代碼提交給CocoaPods團隊審覈了,現在CocoaPods審覈只需要幾秒鐘或者幾分鐘就可以完成了。

1>發佈時會驗證 Pod 的有效性,如果你在手動驗證 Pod 時使用了 --use-libraries 或 --allow-warnings 等修飾符,那麼發佈的時候也應該使用相同的字段修飾,否則出現相同的報錯。


// --use-libraries --allow-warnings
pod trunk push FUTabBarController.podspec

如果是公有庫還需要發佈到cocoaPods trunk上。私有庫就不用了。如果私有庫發佈到trunk上會報錯。

[!] Source code for your Pod was not accessible to CocoaPods Trunk. Is it a private repo or behind a username/password on http?

私有庫提交trunk報錯

2>等待......................................
3>最後進行驗證 

在trunk push後,先用"pod search"查找一下你的代碼,有結果的話就歡天喜地;

沒有的話執行"pod setup"進行本地依賴庫更新,再search。

pod search FUTabbarController


依賴庫更新:
        如果不出意外,大多數同學在執行上述命令後會卡在“Setting up CocoaPods master repo”這一句中。我的經驗是一個字:等!不要關閉Terminal,大概半小時到一小時左右就會完成,提示“Setup completed”。(第一次會比較慢,第一次以後只需要幾秒鐘即可完成)爲什麼會卡這麼久呢?pod setup其實在做這麼一件事:Cocoapods在將https://github.com/CocoaPods/Specs的信息下載到你電腦的~/.cocoapods目錄下並進行文件比對,總數據大小大約在100MB左右,再加上服務器在國外,因此速度會比較慢。在執行過程中你也可以新開一個Terminal窗口,cd到~/.cocoapods目錄,用du -sh *來查看下載進度。


4>最後會出現->

   

到此大功告成,同學們趕快讓你的框架支持pod吧
 

 

文章引用:https://blog.csdn.net/fujunzhi1007/article/details/77641779

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章