iOS componentization - private library production

background:

The company's projects are getting bigger and bigger, and the app has undertaken more and more business. Some business modules need to be copied to other apps of the company. We decided to transform the app into components.

Switch Ruby source

In order to improve efficiency, we will gem sourceswitch to the domestic USTC Rubysource.

gem sources  # 列出默认源

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

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

Toggle Cocoapods Spec source

The default Cocoapods source is 'https://github.com/CocoaPods/Specs.git'that when we execute , pod updatethe terminal is stuck there because the update speed from the official address is very slow. In order to improve efficiency, we Specreplaced the official one with the image of our domestic Tsinghua University.

$ cd ~/.cocoapods/repos
$ pod repo remove master
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
复制代码

Screenshot 2021-09-27 8.15.09 pm.png

make private library

cocoapod template project

Created by pod lib createcommand 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
复制代码

According to the question and answer, enter the corresponding option, 强烈建议一定要包含一个demo应用.

Associated component warehouse address

Create a private code repository on gitee LYHelloComponent, associate the local code with the remote private code repository and upload it

Screenshot 2021-09-27 8.32.05 pm.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
复制代码

It is worth mentioning that we need to call the project project tag, so cocoapodsthat the corresponding tagcode can be obtained according to different versions.

Modify 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
复制代码

The final one podspeclooks like above :

s.version: The version of the pod library.

s.homepage: The author's homepage address, the address must be guaranteed to be accessible normally, otherwise, the verification cannot be passed.

s.source: 源代码The storage address.

Add business code

We are in LYHelloComponentthe Classesdirectory , create a new one MyClass.swift, and add the following code:

Screenshot 2021-09-27 8.56.26 pm.png

// MyClass.swift

public struct MyClass {
    public init (){
    }

    public func sayHello(){
        print("say Hello")
    }
}
复制代码

主工程Quoted in 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.

    }
}
复制代码

If there is no problem with the test, we will submit it to the remote code repository and set up a new version tag 0.1.1.

Verify podspec

After the local test has no problem, we need to use the pob lib lintvalidator podspecfor validation.

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: allow podspecwarnings.

2, --sources=https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git: Specifies Specthe address of the source.

podspecAfter the file verification is passed, it means that the production of our local private library has been completed. Next, we need to podspecpush the local one to the remote private Specsource address.

Create a remote private Spec source

Create Spec Index Repository

Screenshot 2021-09-27 9.20.41 pm.png

Create a local private repository index and associate with 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`
复制代码

At this point, under the .cocoapodsdirectory , a new LYSpecsPrivatefolder will be added.

Screenshot 2021-09-27 9.25.25 pm.png

Publish and upgrade private libraries

After creating the remote index library, we need to push the local podspec file to 远程索引库it.

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
复制代码

So far, we have successfully pushed the local private library to the remote private library.

Use private library

Back in the test project, remove Podfilethe local references in. PodfileThe content is as follows:

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, respectively point to two Spec源, one is 官方的Cocopods源and the other is 我们的私有源.

So we're done! ! ! !

Here, we mainly introduce the production process of the private Pod library. In the next article, I will summarize the problems I encountered in the componentization process.

Guess you like

Origin juejin.im/post/7012608202370088968