iOS--創(chuàng)建私有庫

以創(chuàng)建一個(gè)名為 MAMonitor 的庫為準(zhǔn)。

一、創(chuàng)建兩個(gè) git 庫

  1. MAMonitor(私有庫項(xiàng)目名稱:https://github.com/xxx/MAMonitor

  2. MAMonitorSpec(私有庫索引庫名稱:https://github.com/mayuee/MAMonitorSpec
    這個(gè)庫用來保存私有庫的 podspec文件,一般起名xxxSpec。這個(gè)庫不存放代碼,而是包名、版本號分門別類的存放所有的有關(guān)私有庫的配置。

復(fù)制倉庫 git 地址 執(zhí)行命令:
pod repo add MAMonitorSpec https://github.com/xxx/MAMonitorSpec
查看是否添加成功:
pod repo list

image.png

二、創(chuàng)建本地私有庫

cd xxx/MAMonitor         //自定義要?jiǎng)?chuàng)建的(本地存放)私有庫的文件夾
pod lib create MAMonitor      //MAMonitor :私有庫項(xiàng)目名稱

執(zhí)行如下,按需選擇

 Press return to continue.
1.平臺
What platform do you want to use?? [ iOS / macOS ]
 > iOS

2.語言
What language do you want to use?? [ Swift / ObjC ]
 > ObjC

3. 是否集成Demo為自己的模塊庫?
Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

4. 是否集成測試框架?
Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

5. 是否基于View的做測試?
Would you like to do view based testing? [ Yes / No ]
 > Yes

6. 類的前綴
What is your class prefix?
 > M

執(zhí)行完畢后創(chuàng)建了一個(gè)工程


image.png

將Classes文件夾下面的ReplaceMe.m文件刪除掉,替換成自己要上傳的私有庫的代碼,然后更新一下這個(gè)工程的pod庫

  1. cd到Example文件下
  2. 執(zhí)行 pod install

修改podspec文件
這里是:MAMonitor項(xiàng)目下的MAMonitorSpec.podspec 文件

#
# Be sure to run `pod lib lint MAMonitor.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'MAMonitor'
  s.version          = '0.1.0'
  s.summary          = 'A short description of MAMonitor.'

# 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
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/xxx/MAMonitor'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'xxx' => 'xxx@163.com' }
  s.source           = { :git => 'https://github.com/xxx/MAMonitor.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '10.0'

#  “*” 表示匹配所有文件,“**” 表示匹配所有子目錄
  s.source_files = 'MAMonitor/Classes/**/*'
  
  # s.resource_bundles = {
  #   'MAMonitor' => ['MAMonitor/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

需要修改的項(xiàng)

  1. 修改版本號
  2. 修改項(xiàng)目的簡單概述和詳細(xì)描述
  3. 修改homepage
    s.homepage = 'https://github.com/xxx/MAMonitor'
    改為
    s.homepage = 'https://github.com/gitxxx/MAMonitor.git'
    注意:s.homepage需要設(shè)置剛創(chuàng)建的私有代碼倉庫的地址, 不是私有索引庫的地址?。?!
  4. 修改source地址
    s.source = { :git => 'https://github.com/xxx/MAMonitor.git', :tag => s.version.to_s }
    改為
    s.source = { :git => 'https://github.com/gitxxx/MAMonitor.git', :tag => s.version.to_s }
    注意:s.source 需要設(shè)置的是私有代碼倉庫的源地址(選擇使用HTTPS地址)?。。?/code>
  5. 添加依賴庫,這里添加 UIKit
    s.frameworks = 'UIKit'

三、將私有庫push到遠(yuǎn)程倉庫

編譯通過后,提交代碼到私有庫的git倉庫地址 并打tag

git status -- 查看當(dāng)前git存了什么文件
git add . -- 將所有文件緩存到待提交文件區(qū)域
git commit -m "上傳組件" -- 提交文件,寫上備注
git remote add origin <私有庫git倉庫地址(https://github.com/gitxxx/MAMonitor.git)>
git pull origin master // 把遠(yuǎn)程倉庫的文件更新到本地
git push -u origin master -- 將代碼推送到遠(yuǎn)程私有庫git倉庫的master分支
git tag <版本號(例如git tag 0.1.0)> --這里的版本號必須和podspec里面寫的版本號一致)
git push -- tags

四、本地校驗(yàn)

到本地私有庫目錄下:cd <本地私有庫目錄下(例如:cd MAMonitor)>
執(zhí)行驗(yàn)證:pod lib lint --allow-warnings

若組件依賴第三方庫,需要將第三方庫索引地址寫上
pod lib lint --sources="cocoapods庫地址,私有庫遠(yuǎn)程地址" --allow-warnings

若組件依賴的第三方庫又依賴了其他的庫,需要命令如下
pod lib lint --sources="cocoapods庫地址,私有庫遠(yuǎn)程地址" --use-libraries --allow-warnings

@localhost MAMonitor % pod lib lint --allow-warnings

 -> MAMonitor (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | url: There was a problem validating the URL https://github.com/mayuee/MAMonitor.git.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
MAMonitor passed validation.

到這里,本地校驗(yàn)通過。

五、遠(yuǎn)程校驗(yàn)

先創(chuàng)建一個(gè) Release版本,基于前面創(chuàng)建的 tag 0.1.0。


image.png
image.png

最后點(diǎn)擊 · Publish·


image.png

遠(yuǎn)程校驗(yàn)和本地校驗(yàn)類似,只要把lib字段改成spec

pod spec lint --sources="cocoapods庫地址,私有庫遠(yuǎn)程地址" --use-libraries --allow-warnings

例如:

pod spec lint --sources="https://github.com/gitxxx/MAMonitor.git" --use-libraries --allow-warnings

如下則表示驗(yàn)證通過了。

 % pod spec lint --sources="https://github.com/xxxx/MAMonitor.git" --use-libraries --allow-warnings
 -> MAMonitor (0.1.0)
    - WARN  | summary: The summary is not meaningful.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
Analyzed 1 podspec.
MAMonitor.podspec passed validation.

其間有幾次報(bào)錯(cuò)

fatal: unable to access 'https://github.com/mayuee/MAMonitor.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 

試了幾個(gè)網(wǎng)絡(luò)上的方案,最靠譜的方案是 多試幾次。。。。

六、提交索引文件到遠(yuǎn)程索引庫

所有驗(yàn)證通過之后,要將spec文件推送到最開始創(chuàng)建的私有庫索引庫當(dāng)中

cd 到私有庫項(xiàng)目目錄(例如:cd MAMonitor)

pod repo push <本地索引庫名稱> <索引文件名> --verbose --allow-warnings

例如:pod repo push MAMonitorSpec MAMonitor.podspec --verbose --allow-warnings

注意:<本地索引庫名稱>是 /Users/XXX/.cocoapods/repos下的私有庫索引項(xiàng)目名稱
<索引文件名> 就是以 podspec 結(jié)尾的

推送成功后,在本地索引庫中如下圖


image.png

添加成功后,索引庫 MAMonitorSpec 中會(huì)自動(dòng)出現(xiàn) MAMonitor,MAMonitor 中只包含 MAMonitor.podspec 文件。

image.png

七、驗(yàn)證私有庫

  1. pod repo update 先更新一下pod庫
    這一步報(bào)錯(cuò):
[!] CDN: trunk Repo update failed - 123 error(s):
CDN: trunk URL couldn't be downloaded: https://cdn.cocoapods.org/AlgoliaSearch.yml Response: Timeout was reached
  1. 解決報(bào)錯(cuò)權(quán)宜方法

//移除了文件夾 /Users/user/.cocoapods/repos/trunk
pod repo remove trunk
重新安裝 //或更新
pod install // or pod update

這樣改了之后, 工程的 Podfile 里需要添加一句話
source 'https://github.com/CocoaPods/Specs.git'

  1. 搜索私有庫 pod search MAMonitor
-> MAMonitor (0.1.0)
   A short description of MAMonitor.
   pod 'MAMonitor', '~> 0.1.0'
   - Homepage: https://github.com/xxxx/MAMonitor.git
   - Source:   https://github.com/gitxxx/MAMonitor.git
   - Versions: 0.1.0 [MAMonitorSpec repo]
(END)

八、其他項(xiàng)目使用私有庫

新建 demo 工程,使用pod安裝 MAMonitor

source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/gitxxx/MAMonitor.git'

target 'demo' do
  use_frameworks!

pod 'MAMonitor'

end

執(zhí)行 pod install(pod update)報(bào)錯(cuò)

[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `cocoapods`.
You can try adding it manually in `/Users/mazhibao/.cocoapods/repos` or via `pod repo add`.

這是因?yàn)榍懊嬉瞥?repos目錄下的原本默認(rèn)的 trunk,但是master 沒有,后面需要手動(dòng)添加,解決方案:
1、cd ~/.cocoapods/repos
2、git clone https://github.com/CocoaPods/Specs.git master

移除項(xiàng)目下的 pods(如果已經(jīng)生成的話) 文件夾,重新 pod install

九、更新

對已有庫進(jìn)行更新,需要?jiǎng)?chuàng)建新的 Release 版本,更改 MAMonitor.podspec 文件版本號,重新執(zhí)行 pod repo push MAMonitorSpec MAMonitor.podspec --verbose --allow-warnings 即可。

————————————————

其他

  1. 驗(yàn)證 podspec :執(zhí)行:pod spec lint --allow-warnings
  2. 刪除原來的tag 0.0.1
git tag -d 0.0.1  //刪除本地 tag
git push origin :refs/tags/0.0.1    //刪除遠(yuǎn)程 tag
  1. 清理pod緩存:pod cache clean MAMonitor

  2. 如果私有庫組件庫過多,需要分層文件夾顯示,則需要使用 subspec

  #spec.source_files     = "Classes", "Classes/**/*" 
  #spec.resources        = "Resources/*.png"

  #spec.source_files  = "Classes", "Classes/**/*.{h,m}"
  #spec.exclude_files = "Classes/Exclude"

  spec.subspec 'GOImagesCarouselView' do |s|
     s.source_files = "Classes/GOImagesCarouselView/**/*"
     s.resources    = "Resources/GOImagesCarouselView/*.png"
  end

???


  1. git push ,在輸入 Username 和 Password 之后,Github 返回如下一個(gè)錯(cuò)誤
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/xxxx/MAMonitor.git/'

查資料,大概意思就是,以前的密碼認(rèn)證從2021年8月13日開始就不能用了,必須使用個(gè)人訪問令牌(personal access token),即就是把密碼替換成token。

背景是近年來,GitHub 客戶受益于 GitHub.com 的多項(xiàng)安全增強(qiáng)功能,例如雙重身份驗(yàn)證、登錄警報(bào)、驗(yàn)證設(shè)備、防止使用泄露密碼和WebAuthn 支持。這些功能使攻擊者更難獲取在多個(gè)網(wǎng)站上重復(fù)使用的密碼并使用它來嘗試訪問您的 GitHub 帳戶。盡管有這些改進(jìn),但由于歷史原因,未啟用雙因素身份驗(yàn)證的客戶仍然能夠僅使用其 GitHub 用戶名和密碼繼續(xù)對 Git 和 API 操作進(jìn)行身份驗(yàn)證。

從 2021 年 8 月 13 日開始,在對 Git 操作進(jìn)行身份驗(yàn)證時(shí)將不再接受帳戶密碼,并將要求使用基于令牌的身份驗(yàn)證,例如個(gè)人訪問令牌(針對開發(fā)人員)或 OAuth 或 GitHub 應(yīng)用程序安裝令牌(針對集成商)適用于 GitHub.com 上所有經(jīng)過身份驗(yàn)證的 Git 操作。您也可以在您喜歡的地方繼續(xù)使用 SSH 密鑰。

與基于密碼的身份驗(yàn)證相比,令牌提供了許多安全優(yōu)勢:

  • 唯一 —— 令牌特定于 GitHub,可以按使用或按設(shè)備生成
  • 可撤銷 —— 令牌可以隨時(shí)單獨(dú)撤銷,無需更新未受影響的憑據(jù)
  • 有限 —— 令牌的范圍可以很窄,只允許用例所需的訪問權(quán)限
  • 隨機(jī) —— 令牌不受字典類型或暴力嘗試的影響,您需要記住或定期輸入的更簡單的密碼可能是

問題解決

  1. 找到個(gè)人Settings頁面:
image.png
  1. 找到Developer settings,選擇個(gè)人訪問令牌Personal access tokens,然后選中生成令牌Generate new token


    image.png
  1. 設(shè)置token的特性,比如:標(biāo)題,有效期,token權(quán)限等等


    image.png
  1. 點(diǎn)擊最下面 生成令牌 Generate token
    image.png

把token復(fù)制下來
之后提交代碼的時(shí)候,在之前輸入密碼的地方輸入這個(gè)token就可以了。

也可以 把token直接添加遠(yuǎn)程倉庫鏈接中,這樣就可以避免同一個(gè)倉庫每次提交代碼都要輸入token了:

git remote set-url origin https://<your_token>@github.com/<USERNAME>/<REPO>.git

  • <your_token>:換成你自己得到的token
  • <USERNAME>:是你自己github的用戶名
  • <REPO>:是你的倉庫名稱

還有一點(diǎn),新創(chuàng)建的repo里面,默認(rèn)分支是 main 不是 master

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 我用的是碼云 一、首先我們先在碼云上建一個(gè)遠(yuǎn)程私有索引倉庫 創(chuàng)建后的頁面: 二、創(chuàng)建一個(gè)項(xiàng)目 創(chuàng)建后頁面: 三、打...
    69a8e4612fc7閱讀 957評論 1 1
  • 參考鏈接:http://blog.csdn.net/qxuewei/article/details/5441211...
    海浪萌物閱讀 2,366評論 1 8
  • 一、創(chuàng)建私有的Spec Repo(git倉庫1) 二、創(chuàng)建Pod項(xiàng)目工程,并且又可以訪問的項(xiàng)目版本控制地址(git...
    一畝三分甜閱讀 2,447評論 2 2
  • 私有庫的是隨著公司在多個(gè)項(xiàng)目開展的時(shí)候,把一些常用的工具類制作成pod,方便在多個(gè)項(xiàng)目中使用,避免了來回拖入代碼造...
    zl_xust閱讀 604評論 0 1
  • 1、在Git倉庫分別創(chuàng)建2個(gè)私有庫,一個(gè)索引庫,一個(gè)是pod代碼庫 2、將遠(yuǎn)程索引庫添加到本地 a. 首先進(jìn)入本地...
    我是一根聰閱讀 630評論 0 1

友情鏈接更多精彩內(nèi)容