バックグラウンド:
会社のプロジェクトはどんどん大きくなり、アプリはますます多くのビジネスを引き受けています。一部のビジネスモジュールは会社の他のアプリにコピーする必要があります。アプリをコンポーネントに変換することにしました。
Rubyソースを切り替える
効率を上げるgem source
ために、国内のUSTCRuby
ソースに切り替えます。
gem sources # 列出默认源
gem sources --remove https://rubygems.org/ #移除默认源
gem sources -a https://mirrors.ustc.edu.cn/rubygems/ #添加科大源
复制代码
ToggleCocoapodsSpecソース
デフォルトのCocoapodsソースでは'https://github.com/CocoaPods/Specs.git'
、実行すると、公式アドレスからの更新速度が非常に遅いためpod update
、端末がそこに留まります。効率を向上させるために、公式アドレスをSpec
国内の清華大学のイメージに置き換えました。
$ cd ~/.cocoapods/repos
$ pod repo remove master
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
复制代码
私立図書館を作る
cocoapodテンプレートプロジェクト
pod lib create
コマンドによって作成されpod库工程
ます。
pod lib create LYHelloComponent
复制代码
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and click links to open in a browser. )
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> Swift
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Quick / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
复制代码
質問と回答に従って、対応するオプションを入力します强烈建议一定要包含一个demo应用
。
関連するコンポーネント倉庫の住所
giteeLYHelloComponent
にプライベートコードリポジトリを作成し、ローカルコードをリモートプライベートコードリポジトリに関連付けてアップロードします
admin@MacBook-Pro ~/Desktop: cd LYHelloComponent
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git remote add origin https://gitee.com/DevalLee/lyhello-component.git
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git add .
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git commit -a -m "初次提交"
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git pull origin master --allow-unrelated-histories
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git push origin master
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git tag 0.1.0
admin@MacBook-Pro ~/Desktop/LYHelloComponent master: git push origin 0.1.0
复制代码
異なるバージョンに応じて対応するコードを取得できるように、プロジェクトプロジェクトを呼び出す必要があることは言及する価値があります。tag
cocoapods
tag
podspecを変更する
Pod::Spec.new do |s|
s.name = 'LYHelloComponent'
s.version = '0.1.1'
s.summary = 'hello 测试工程'
# 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 = 'https:/www.baidu.com'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '[email protected]' => '[email protected]' }
s.source = { :git => 'https://gitee.com/DevalLee/lyhello-component.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
s.source_files = 'LYHelloComponent/Classes/**/*'
# s.resource_bundles = {
# 'LYHelloComponent' => ['LYHelloComponent/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
复制代码
最後のものpodspec
は上記のようになります:
s.version
:ポッドライブラリのバージョン。
s.homepage
:作成者のホームページアドレス。アドレスは通常どおりアクセス可能であることが保証されている必要があります。そうでない場合、検証に合格できません。
s.source
:源代码
ストレージアドレス。
ビジネスコードを追加する
ディレクトリにいて、新しいディレクトリLYHelloComponent
を作成し、次のコードを追加します。Classes
MyClass.swift
// MyClass.swift
public struct MyClass {
public init (){
}
public func sayHello(){
print("say Hello")
}
}
复制代码
主工程
引用LYHelloComponent
:_
// ViewController.swift
import UIKit
import LYHelloComponent
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let v = MyClass()
v.sayHello()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
复制代码
テストに問題がなければ、リモートコードリポジトリに送信して新しいバージョンを設定しますtag 0.1.1
。
podspecを確認する
ローカルテストに問題がなければ、検証にpob lib lint
バリデーターを使用する必要がありますpodspec
。
pod lib lint --allow-warnings --sources=https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
复制代码
-> LYHelloComponent (0.1.1)
- WARN | description: The description is shorter than the summary.
- WARN | url: There was a problem validating the URL https:/www.baidu.com.
- WARN | [iOS] swift: The validator used Swift `4.0` by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_versions` attribute in your podspec. Note that usage of a `.swift-version` file is now deprecated.
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Building targets in parallel
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | xcodebuild: note: Build preparation complete
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Analyzing workspace
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
LYHelloComponent passed validation.
复制代码
1 、 :警告--allow-warnings
を許可します。podspec
2 、:送信元のアドレスを--sources=https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
指定します。Spec
podspec
ファイル検証に合格すると、ローカルプライベートライブラリの作成が完了したことを意味します。次に、ローカルライブラリをリモートプライベートソースアドレスにpodspec
プッシュする必要があります。Spec
リモートプライベートスペックソースを作成する
スペックインデックスリポジトリの作成
ローカルプライベートリポジトリインデックスを作成し、gitに関連付けます
pod repo add LYSpecsPrivate https://gitee.com/DevalLee/lyprivate-spec.git
Ignoring psych-3.2.0 because its extensions are not built. Try: gem pristine psych --version 3.2.0
Cloning spec repo `LYSpecsPrivate` from `https://gitee.com/DevalLee/lyprivate-spec.git`
复制代码
この時点で、.cocoapods
ディレクトリ新しいLYSpecsPrivate
フォルダが追加されます。
プライベートライブラリを公開およびアップグレードする
リモートインデックスライブラリを作成したら、ローカルpodspecファイルをライブラリにプッシュする必要があります远程索引库
。
pod repo push LYSpecsPrivate *.podspec --allow-warnings
Updating the `LYSpecsPrivate' repo
Adding the spec to the `LYSpecsPrivate' repo
- [Add] LYHelloComponent (0.1.1)
Pushing the `LYSpecsPrivate' repo
复制代码
これまでのところ、ローカルプライベートライブラリをリモートプライベートライブラリにプッシュすることに成功しています。
プライベートライブラリを使用する
Podfile
テストプロジェクトに戻り、のローカル参照を削除します。Podfile
内容は以下の通りです。
source "https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git"
source "https://gitee.com/DevalLee/lyprivate-spec.git"
use_frameworks!
platform :ios, '9.0'
target 'LYHelloComponent_Example' do
pod 'LYHelloComponent'
target 'LYHelloComponent_Tests' do
inherit! :search_paths
end
end
复制代码
- 1、それぞれ2つを指し
Spec源
、1つはで、もう1つは官方的Cocopods源
です我们的私有源
。
これで完了です。!!!
ここでは、主にプライベートポッドライブラリの作成プロセスを紹介します。次の記事では、コンポーネント化プロセスで発生した問題を要約します。