Background:
As the company’s projects are getting bigger and bigger, App has taken on more and more businesses, and some business modules need to be copied to other apps of the company, so we decided to make component transformation of App.
To switch between Ruby source
In order to improve efficiency, we switched the gem source to the Ruby source of USTC.
Gem sources --remove https://rubygems.org/ # remove default gem sources -a https://mirrors.ustc.edu.cn/rubygems/ # add source at hkustCopy the code
Switch to the Cocoapods Spec source
The default Cocoapods source for ‘https://github.com/CocoaPods/Specs.git’, when we perform pod update terminal is stuck in there, because from the official address update speed is slow, in order to improve the efficiency, We have replaced the official Spec with a mirror image of tsinghua University in China.
$ cd ~/.cocoapods/repos
$ pod repo remove master
$ git clone https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git master
Copy the code
Making a private library
Cocoapod template project
Create a POD library project by using the pod lib create command.
pod lib create LYHelloComponent
Copy the code
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
Copy the code
According to the q&A, enter the corresponding options. It is strongly recommended to include a demo application.
Associated component warehouse address
Create a private repository, LYHelloComponent, on Top of Gitee to associate and upload local code to a remote private repository
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 "first submit" 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 from admin @ MacBook Pro ~ / Desktop/LYHelloComponent master: git push origin 0.1.0 fromCopy the code
It is worth mentioning that we need to tag the project so that Cocoapods can retrieve the code corresponding to the tag for different versions.
Modify podspec
Pod: : Spec. New do | s | s.n ame = 'LYHelloComponent' s.v ersion = 'while' s.s ummary = 'hello test engineering' # This description is 2 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! Spyware doctor escription = < < - DESC test engineering DESC Sheldon horowitz omepage = 'https:/www.baidu.com' # s.s creenshots = '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', To_s} # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>' S.iso. 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' endCopy the code
The final PodSpec looks like this:
S. sion: version of the POD library.
S. homepage: the home address of the author. You must ensure that this address can be accessed normally; otherwise, the epage cannot be verified.
S.ource: the location where the source code is stored.
Add business code
In the LYHelloComponent Classes directory, create myClass.swift and add the following code:
// MyClass.swift
public struct MyClass {
public init (){
}
public func sayHello(){
print("say Hello")
}
}
Copy the code
Reference LYHelloComponent in the main project:
// 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.
}
}
Copy the code
When the tests were fine, we committed to the remote repository and set up a new version, Tag 0.1.1.
Verify podspec
After the local tests work fine, we need to use POB lib Lint to validate the PodSpec.
pod lib lint --allow-warnings --sources=https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git
Copy the code
- > LYHelloComponent (while) - 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.Copy the code
1, –allow-warnings: allows podSpecs to have warnings.
2, — sources=https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git: Spec source address specified.
After the podSpec file is verified, our local private library is complete. Next, we will push the local PodSpec to the remote private Spec source address.
Create a remote private Spec source
Create the Spec index repository
Create a local private library index and associate it with Git
Pod repo add LYSpecsPrivate https://gitee.com/DevalLee/lyprivate-spec.git Ignoring intersection - 3.2.0 because its extensions are not built. Try: Pristine psych --version 3.2.0 Cloning spec repo 'LYSpecsPrivate' from pristine pristine psych --version 3.2.0 Cloning spec repo 'LYSpecsPrivate' from pristine pristine psych `https://gitee.com/DevalLee/lyprivate-spec.git`Copy the code
At this point, a new LYSpecsPrivate folder will be created in the.cocoapods directory.
Publish and upgrade private libraries
After creating the remote index library, we need to push the local podSpec file to the remote index library.
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
Copy the code
So, we’ve successfully pushed the local private library to the remote private library.
Using a private library
Back in the test project, remove the local reference from the Podfile. The Podfile reads 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 'LyHello_component_example' do pod 'LYHelloComponent' target 'lyHello_tests' do inherit! :search_paths end endCopy the code
- 1, I’m pointing to both of them
Source of the Spec
, aOfficial Cocopods source
And the other one isOur private source
.
So we’re done !!!!
Here, we mainly introduce the production process of private Pod library. In the next article, I will summarize my problems with componentization.