CocoaPods-subspec

优质
小牛编辑
116浏览
2023-12-01

CocoaPods-subspec

SBLibrary这个类库为例

  • 不使用subspecCocoaPods集成类库后的目录结构

    without subspec

  • 使用subspecCocoaPods集成类库后的目录结构

    with subspec


图片不稳定, 文本演示一下区别

当使用了subspec后, 项目中集成类库后的效果如下

  • 在我们的Pods项目中, Pods/SBLibrary真实路径是这样的
.
└── SBLibrary
    └── Classes
        └── Core
            ├── FileManager+sbl.swift
            └── Protocol
                └── SandboxReadable.swift
  • 在我们的Pods项目中, Pods/SBLibrary引用路径是这样的
.
Pods
└── SBLibrary
    └── Core
        ├── FileManager+sbl.swift
        └── SandboxReadable.swift

在做framework开的时候, 我的类库项目文件目录结构

  • 每个subspec一个独立文件夹管理
  • 每个subspec下可能还会根据功能等纬度拆分为各个文件夹存放文件
    • 在真正项目中集成后, 会按照subspec压平, 每个subspec文件统一在一个文件夹中

subspec的实现也很简单: subspec

  • 关键参数设置示例

      s.default_subspec = "Core"
      s.requires_arc = true
    
      s.subspec "Core" do |ss|
        ss.source_files = ['SBLibrary/Classes/Core/*.swift', 'SBLibrary/Classes/Core/Protocol/*.swift']
        ss.framework  = "Foundation"
      end
    
      s.subspec "UI" do |ss|
        ss.source_files = "SBLibrary/Classes/UI"
        ss.framework  = "UIKit"
      end
    
  • 使用时, 安装指定模块

    pod 'SBLibrary/UI'
    
  • 指定要安装的子模块的集合

    pod 'SBLibrary', :subspecs => ['Core', 'UI']
    

  • 在搜索类库时的展示也会显示出Subspecs
-> SBLibrary (0.2.2)
   A lightweight Basic library of Swift.
   pod 'SBLibrary', '~> 0.2.2'
   - Homepage: https://github.com/ShenYj/SBLibrary
   - Source:   https://github.com/ShenYj/SBLibrary.git
   - Versions: 0.2.2, 0.2.1, 0.1.1, 0.1.0 [trunk repo]
   - Subspecs:
     - SBLibrary/Core (0.2.2)
     - SBLibrary/UI (0.2.2)

  • subspec依赖关系

这里提到的依赖关系主要指的是我们自己开发的类库模块之间

例如:

  s.ios.deployment_target = '10.0'
  s.swift_version = '5.0'
  s.requires_arc = true
  s.frameworks = 'Foundation', 'CFNetwork'
  s.dependency 'Moya', '~> 14.0.0'
  s.dependency 'SwiftyJSON', '~> 5.0.0'
  s.default_subspec = "Core"

  # 核心库
  s.subspec "Core" do |ss|
    ss.source_files  = "Componentization/Classes/Core/"
  end

  # 课程相关业务
  s.subspec "Course" do |ss|
    ss.source_files = "Componentization/Classes/Course/"
    ss.dependency "Componentization/Core"
  end

  # 应用内购
  s.subspec "IAP" do |ss|
    ss.source_files = "Componentization/Classes/IAP/"
    ss.dependency "Componentization/Core"
  end

示例中每个subspec模块都需要依赖于三方库: MoyaSwiftyJSON 同时Core又是我们类库中的核心, 其余两个都要基于Core

私有库本地集成

pod 'Componentization', :subspecs => ['Core', 'Course', 'IAP'], :path => '/Users/shenyj/Documents/Project/Componentization'

项目中pod集成后目录结构

subspec