iOS组件化-私有库制作

1,992 阅读4分钟

背景:

公司项目越来越大,App承接了越来越多的业务,有一些业务模块需要复制到公司的其他App上,我们决定对App进行组件化改造。

切换Ruby源

为了提高效率,我们将 gem source切换为国内中科大的Ruby源。

gem sources  # 列出默认源

gem sources --remove https://rubygems.org/  #移除默认源

gem sources -a https://mirrors.ustc.edu.cn/rubygems/ #添加科大源

切换Cocoapods Spec源

默认的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

截屏2021-09-27 下午8.15.09.png

制作私有库

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应用

关联组件仓库地址

在gitee上面创建一个私有的代码仓库LYHelloComponent,将本地代码与远程私有代码库关连并上传

截屏2021-09-27 下午8.32.05.png

 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           = { '1260197127@qq.com' => '1260197127@qq.com' }
  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: pod库的版本。

s.homepage: 作者的主页地址,该地址一定要保证能够正常访问,否则,无法校验通过。

s.source: 源代码的存放的地址。

添加业务代码

我们在LYHelloComponentClasses目录下,新建 MyClass.swift,并添加如下代码:

截屏2021-09-27 下午8.56.26.png

// 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源地址。

创建远程私有Spec源

创建 Spec 索引仓库

截屏2021-09-27 下午9.20.41.png

创建本地私有库索引并与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的文件夹。

截屏2021-09-27 下午9.25.25.png

发布和升级私有库

创建好远程索引库后,我们需要将本地的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,分别指向两个Spec源,一个为官方的Cocopods源,另一个为我们的私有源

这样我们就大功告成了!!!!

在这里,我们主要介绍了私有Pod库的制作流程。在下一篇文章中,我会把我在组件化过程中遇到的问题作一个总结。