iOS组件化开发之私有库创建(上)

2018-06-18  本文已影响0人  无所不行的刷子

前言

组件化中把模块拆分之后把模块的代码存放在私有库中,私有库中存放的其实是模块也就是我们拆分出来的组件的代码索引,所以私有库也就是索引库。它里面存放的是各个组件的版本(tag)的索引文件podspec。

创建私有库我们分三步:

私有库分为两种,本文先实现第一种:

准备

创建私有索引库推送

创建自己的私有索引库,创建过程和项目是一样的,不懂可以看系列前面的文章

image.png

添加私有索引库到本地

pod repo add MyRepo git@github.com:GuoMs/mySpecs.git

源代码及tag推送

创建一个模块测试代码,组件化代码最好的情况是创建一个静态库的target工程。今天刚开始讲私有库,我们只使用一个控制器输出一句代码代替,后续的文章将带大家慢慢深入。

提交代码,并打一个1.0.0的tag


image.png

podsepc推送

1、创建podspec,怎么创建请查看前面的文章


#最简单是示例
Pod::Spec.new do |s|

  s.name         = "testDemo"
  #我们刚打tag的版本是1.0.0 所以这也是
  s.version      = "1.0.0"
  s.summary      = "测试."


  s.description  = <<-DESC
                   DESC
  #项目首页显示用不重要
  s.homepage     = "https://github.com/GuoMs/testDemo"
 
  s.license      = "MIT"
  
  s.author             = { "dd" => "gz3024@aliyun.com" }

  s.platform     = :ios, "8.0"
  #这里是源代码,也就是组件代码的git地址一定要写对
  s.source       = { :git => "git@github.com:GuoMs/testDemo.git", :tag => "#{s.version}" }
  #组件代码的路径要写对不然会找不到
  s.source_files  = "testDemo", "testDemo/**/*.{h,m}"


end

2、验证podspec是否正确,如果有出现error的话需要修改

#验证是否正常,
pod spec lint --no-clean
image.png

改到最后只有waring


image.png

通过上面的修改,你可能需要重新提交并重新打tag,我这里重新提交代码并打tag1.0.1,或者删除tag重新提交。

#修改提交完之后执行
pod lib lint --private

提示以下信息代表成功了


image.png

3、推送podspec到私有索引库

pod repo push MyRepo testDemo.podspec --verbose --allow-warnings --use-libraries

以下代表成功了

image.png
#现在可以搜索我们的组件啦
pod search testDemo
#输出我们刚创建的组件描述
-> testDemo (1.0.1)
   测试组件.
   pod 'testDemo', '~> 1.0.1'
   - Homepage: https://github.com/GuoMs/testDemo
   - Source:   git@github.com:GuoMs/testDemo.git
   - Versions: 1.0.1 [MyRepo repo]

在远端的私有库中我们也可以看到1.0.1的索引文件


image.png

实际项目使用

1、设置本地的podfile

#设置私有库源
source 'git@github.com:GuoMs/mySpecs.git'

platform :ios, '8.0'

target 'repoDemo' do
    #组件名称
    pod 'testDemo'

end

2.pod 更新

image.png

3、完成之后我们可以看到组件的代码


image.png

4、最后调用组件代码

#import "ViewController.h"
#import <testDemo/testDemo.h>

@interface ViewController ()

@end

@implementation ViewController


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    testDemo *test = [[testDemo alloc]init];
    [self.navigationController pushViewController:test animated:YES];
}


@end

输出log


image.png

调试模式

修改podfile

#设置私有库源
source 'git@github.com:GuoMs/mySpecs.git'

platform :ios, '8.0'

target 'repoDemo' do
    #设置调试模式,后面的路径指的是testDemo.podsepc的所在路径
    pod 'testDemo', :path=> '/Users/mac/Desktop/testtest'

end

pod更新之后在工程的Development Pods文件夹下看到代码就可以直接修改代码了,改文件夹代码所有调试模式的第三方库或者组件代码

image.png

后记

上面只是写了最简单的发布步骤,其实真正开发的时候很有很多的配置信息需要写在podsepc里面,代码也必须公开所有人可见,下篇我们将把代码打包成静态库发布到私有索引库,敬请期待。

上一篇 下一篇

猜你喜欢

热点阅读