iOS组件化之搭建私有库(Gitlab+Cocoapods)

搭建私有库(PrivateSpec)

前言:

多项目多工程组件化之路,之前搭建了公司内部私有库,有空整理了下资料

1.名词解释

1.1 索引库 repo

1.2 索引库中的索引文件 .podspec.json

例子 AFNetworking.podspec.json

{
  "name": "AFNetworking",
  "version": "4.0.1",
  "license": "MIT",
  "summary": "A delightful networking framework for Apple platforms.",
  "homepage": "https://github.com/AFNetworking/AFNetworking",
  "social_media_url": "https://twitter.com/AFNetworking",
  "authors": {
    "Mattt Thompson": "m@mattt.me"
  },
  "source": {
    "git": "https://github.com/AFNetworking/AFNetworking.git",
    "tag": "4.0.1"
  },
  "platforms": {
    "ios": "9.0",
    "osx": "10.10",
    "watchos": "2.0",
    "tvos": "9.0"
  },
  "ios": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
    }
  },
  "osx": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
    }
  },
  "watchos": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking-watchOS"
    }
  },
  "tvos": {
    "pod_target_xcconfig": {
      "PRODUCT_BUNDLE_IDENTIFIER": "com.alamofire.AFNetworking"
    }
  },
  "source_files": "AFNetworking/AFNetworking.h",
  "subspecs": [
    {
      "name": "Serialization",
      "source_files": "AFNetworking/AFURL{Request,Response}Serialization.{h,m}"
    },
    {
      "name": "Security",
      "source_files": "AFNetworking/AFSecurityPolicy.{h,m}"
    },
    {
      "name": "Reachability",
      "platforms": {
        "ios": "9.0",
        "osx": "10.10",
        "tvos": "9.0"
      },
      "source_files": "AFNetworking/AFNetworkReachabilityManager.{h,m}"
    },
    {
      "name": "NSURLSession",
      "dependencies": {
        "AFNetworking/Serialization": [

        ],
        "AFNetworking/Security": [

        ]
      },
      "ios": {
        "dependencies": {
          "AFNetworking/Reachability": [

          ]
        }
      },
      "osx": {
        "dependencies": {
          "AFNetworking/Reachability": [

          ]
        }
      },
      "tvos": {
        "dependencies": {
          "AFNetworking/Reachability": [

          ]
        }
      },
      "source_files": [
        "AFNetworking/AF{URL,HTTP}SessionManager.{h,m}",
        "AFNetworking/AFCompatibilityMacros.h"
      ]
    },
    {
      "name": "UIKit",
      "platforms": {
        "ios": "9.0",
        "tvos": "9.0"
      },
      "dependencies": {
        "AFNetworking/NSURLSession": [

        ]
      },
      "source_files": "UIKit+AFNetworking"
    }
  ]
}


1.3 代码库

  • code
  • .podspec
  • Example
  • 其他文件 license readme.md

1.4 代码库中的索引文件 .podspec

例子 AFNetworking.podspec

Pod::Spec.new do |s|
  s.name     = 'AFNetworking'
  s.version  = '4.0.1'
  s.license  = 'MIT'
  s.summary  = 'A delightful networking framework for Apple platforms.'
  s.homepage = 'https://github.com/AFNetworking/AFNetworking'
  s.social_media_url = 'https://twitter.com/AFNetworking'
  s.authors  = { 'Mattt Thompson' => 'm@mattt.me' }
  s.source   = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => s.version }

  s.ios.deployment_target = '9.0'
  s.osx.deployment_target = '10.10'
  s.watchos.deployment_target = '2.0'
  s.tvos.deployment_target = '9.0'

  s.ios.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
  s.osx.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }
  s.watchos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking-watchOS' }
  s.tvos.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER' => 'com.alamofire.AFNetworking' }

  s.source_files = 'AFNetworking/AFNetworking.h'

  s.subspec 'Serialization' do |ss|
    ss.source_files = 'AFNetworking/AFURL{Request,Response}Serialization.{h,m}'
  end

  s.subspec 'Security' do |ss|
    ss.source_files = 'AFNetworking/AFSecurityPolicy.{h,m}'
  end

  s.subspec 'Reachability' do |ss|
    ss.ios.deployment_target = '9.0'
    ss.osx.deployment_target = '10.10'
    ss.tvos.deployment_target = '9.0'

    ss.source_files = 'AFNetworking/AFNetworkReachabilityManager.{h,m}'
  end

  s.subspec 'NSURLSession' do |ss|
    ss.dependency 'AFNetworking/Serialization'
    ss.ios.dependency 'AFNetworking/Reachability'
    ss.osx.dependency 'AFNetworking/Reachability'
    ss.tvos.dependency 'AFNetworking/Reachability'
    ss.dependency 'AFNetworking/Security'

    ss.source_files = 'AFNetworking/AF{URL,HTTP}SessionManager.{h,m}', 'AFNetworking/AFCompatibilityMacros.h'
  end

  s.subspec 'UIKit' do |ss|
    ss.ios.deployment_target = '9.0'
    ss.tvos.deployment_target = '9.0'
    ss.dependency 'AFNetworking/NSURLSession'

    ss.source_files = 'UIKit+AFNetworking'
  end
end

2、创建索引库

1)在gitlab上创建一个新的库,这个库用来保存私有库的podspec文件,索引库取名为PrivateSpec
2)创建本地索引库文件,然后将其于刚才创建的远程索引库相关联。

pod repo add PrivateSpec https://gitlab.mydomain.com/private-cocoapods/privatespec.git 

pod repo add 本地索引库名称 远程索引库的git地址 
> 注意!!!此时的远程索引库是空的!有master分支,可以添加一个readme.md文件。

3、创建本地私有库

  • 手动添加
  • 使用pod创建

1)创建本地私有库

cd 本地私有库文件夹目录

pod lib create WLTestTool

pod lib create 私有库名称

2)根据提示一步一步配置,如图

3)配置完成后会生成一些内容,层级如图

  • code
  • .podspec
  • Example
  • 其他文件 license readme.md

4)查看并编写podspec文件里面的内容


#
# Be sure to run `pod lib lint WLTestTool.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'WLTestTool'             #私有库名称
  s.version          = '1.0.0'                  #版本号,与下面的git tag版本对应
  s.summary          = '这个是测试库WLTestTool.'

# 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
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://gitlab.mydomain.com/private-cocoapods/privatespec'        #仓库首页地址
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '园丁云' => 'gardeneryun@foxmail.com' }
  s.source           = { :git => 'https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git', :tag => s.version.to_s }  #仓库地址
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'

  s.source_files = 'WLTestTool/Classes/**/*'        #私有库代码文件目录
  
  # s.resource_bundles = {
  #   'WLTestTool' => ['WLTestTool/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

5) 将Classes文件夹下面的ReplaceMe.m文件删除掉,替换成你要上传私有库的代码。

4.验证podspec文件合法性

两种验证方式,建议先本地校验,代码本地库push到远程仓库再使用远程验证。

1)本地验证,验证文件格式、语法等

pod lib lint podName.podspec

2)远程验证,远程仓库地址、验证文件格式、语法等

pod spec lint podName.podspec

3)可选参数
--allow-warnings:允许警告
--sources=‘master,privateSpecs':指定源,比如你的私有pod同时依赖了公有库和私有库,你必须指定源才行,因为默认只会去在公有源中查找对应的依赖
--use-libraries:如果使用了静态库,记得加上它

pod lib lint WLTestTool.podspec

pod spec lint WLTestTool.podspec

pod spec lint WLTestTool.podspec --allow-warnings

pod spec lint --sources="https://github.com/CocoaPods/Specs.git,https://gitlab.mydomain.com/private-cocoapods/WLTestTool.git"  --allow-warnings


5.代码本地库push到远程仓库

1)在Gitlab上创建远程私有库WLTestTool。

2)将代码本地库推送到远程私有库。

3)本地验证之后,打tag。然后进行远程验证。

注意这里添加的tag要跟刚才在spec文件里面写的版本号一致

6.将代码库中的.podspec文件push到私有索引库

这里也会做一遍第四点验证合法性,也可以使用

pod repo push PrivateSpecName podName.podspec

pod repo push PrivateSpec WLTestTool.podspec

7.更新本地索引库

先更新pod库,不然找不到你刚上传的私有库。(此操作也会更新公共索引库)

pod repo update (此操作也会更新公共索引库)

pod repo update PrivateSpec

8.使用私有库

使用私有库需要指定私有索引库地址。

cocoapods 官方source是隐式的,一旦指定了其他source 就需要把官方的指定上。

source 'https://github.com/CocoaPods/Specs.git'

[!] Your project does not explicitly specify the CocoaPods master specs repo. Since CDN is now used as the default, you may safely remove it from your repos directory via 'pod repo remove master'. To suppress this warning please add 'warn_for_unused_master_specs_repo => false' to your Podfile.

Podfile 示例

source 'https://github.com/CocoaPods/Specs.git'
source 'https://gitlab.mydomain.com/private-cocoapods/privatespec.git' 

use_frameworks!
platform :ios, '9.0'


target 'WLTestTool_Example' do
  pod 'WLTestTool', '1.0.0'

  target 'WLTestTool_Tests' do
    inherit! :search_paths
  end
  
end

use_frameworks!
platform :ios, '9.0'


target 'WLTestTool_Example' do
  pod 'WLTestTool', '1.0.0'
  pod 'test', :git => 'https://gitlab.mydomain.com/private-cocoapods/test.git', :tag => '1.0.0'


  target 'WLTestTool_Tests' do
    inherit! :search_paths
  end
  
end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容