React Native

Podspec语法参考 v1.2.0.beta.1

2021-10-26  本文已影响0人  有毒的程序猿

CocoaPods官网
spec文件描述了Pod库的版本。它包括有关从哪里获取source、要使用哪些文件、应用程序构建设置以及其他通用元数据(如名称、版本和描述)的详细信息。

一、Specification 规范说明

#创建spec文件
pod spec create DemoSpec

1、podspec示例

Pod::Spec.new do |spec|
  spec.name         = 'Reachability'
  spec.version      = '3.1.0'
  spec.license      = { :type => 'BSD' }
  spec.homepage     = 'https://github.com/tonymillion/Reachability'
  spec.authors      = { 'Tony Million' => 'tonymillion@gmail.com' }
  spec.summary      = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
  spec.source       = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0' }
  spec.module_name  = 'Rich'

  spec.ios.deployment_target  = '9.0'
  spec.osx.deployment_target  = '10.10'

  spec.source_files       = 'Reachability/common/*.swift'
  spec.ios.source_files   = 'Reachability/ios/*.swift', 'Reachability/extensions/*.swift'
  spec.osx.source_files   = 'Reachability/osx/*.swift'

  spec.framework      = 'SystemConfiguration'
  spec.ios.framework  = 'UIKit'
  spec.osx.framework  = 'AppKit'

  spec.dependency 'SomeOtherPod'
end

二、语法

1、Root specification 根规范

“根”规范存储有关库的特定版本的信息。此组中的属性只能写入“根”规范,而不是“子规范”。在这个组中列出的属性是podspec需要的唯一属性。其他组的属性被用来改进podspec并遵循一个关于配置方法的约定。根规范可以直接通过“sub-specifications”来描述这些属性。

#跟podspec文件名相同
spec.name = 'AFNetworking'

spec.version = '0.0.1'

spec.cocoapods_version = '>= 0.36'

spec.author = 'Darth Vader'

#多个作者
spec.authors = 'Darth Vader', 'Wookiee'

#多个作者,以及其邮箱
spec.authors = { 'Darth Vader' => 'darthvader@darkside.com',
                 'Wookiee'     => 'wookiee@aggrrttaaggrrt.com' }

#许可证默认与spec文件在同一目录下
#MIT是一个比较宽泛的开源许可协议
spec.license = 'MIT'

#指定许可文件
spec.license = { :type => 'MIT', :file => 'MIT-LICENSE.txt' }

#指定了许可证文件的内容
spec.license = { :type => 'MIT', :text => <<-LICENSE
                   Copyright 2012
                   Permission is granted to...
                 LICENSE
               }

spec.homepage = 'http://www.example.com'

#通过tag指定Git源 用的最多的方式!!
#tag值与spec.version一致。
spec.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => spec.version.to_s }

以下的方式,项目中没有用到,不能过多的解释,更多参照source

#使用v前缀的tag值和子模块
spec.source = { :git => 'https://github.com/typhoon-framework/Typhoon.git', :tag => "v#{spec.version}", :submodules => true }

#使用svn源
spec.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => '4.8.8' }

#使用HTTP下载代码的压缩文件。它支持zip、tgz、bz2、txz和tar。
spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip' }

#使用HTTP下载文件,使用hash来验证下载。它支持sha1和sha256。
spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip',:sha1 => '7e21857fe11a511f472cfd7cfa2d979bd7ab7d96' }

支持的key:
:git => :tag, :branch, :commit, :submodules
:svn => :folder, :tag, :revision
:hg => :revision
:http => :flatten, :type, :sha256, :sha1
:path

spec.summary = 'Computes the meaning of life.'

spec.description = <<-DESC
                     Computes the meaning of life.
                     Features:
                     1. Is self aware
                     ...
                     42. Likes candies.
                   DESC

# 单个截图
spec.screenshot  = 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png'

#多个截图
spec.screenshots = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png', 'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]

spec.documentation_url = 'http://www.example.com/docs.html'

# 指定脚本文件,ruby build_files.rb是脚本文件名
spec.prepare_command = 'ruby build_files.rb'

这里用到了ruby命令

#sed命令是利用script来处理文本文件
#i :插入
#s :取代
#sed 's/要被取代的字串/新的字串/g'
#第一个sed 语句表示将当前目录下的所有.h文件中的MyNameSpacedHeader替换成Header
spec.prepare_command = <<-CMD
                        sed -i 's/MyNameSpacedHeader/Header/g' ./**/*.h
                        sed -i 's/MyNameOtherSpacedHeader/OtherHeader/g' ./**/*.h
                   CMD

spec.deprecated = true

spec.deprecated_in_favor_of = 'NewMoreAwesomePod'

2、Platform

规范应该指出使用pod支持的平台和相应的部署目标。
如果没有在子规范中定义,该分组的属性将继承父类的值。

#只能在OS系统使用,要求系统版本至少10.8
spec.platform = :osx, '10.8'

#也可以只指定平台,不指定版本
spec.platform = :iOS

spec.ios.deployment_target = '6.0'

spec.osx.deployment_target = '10.8'

3、Build settings

接下来列出了与构建库所用的构建环境相关的属性。

spec.dependency 'AFNetworking', '~> 1.0'

#依赖RestKit库中的子模块
spec.dependency 'RestKit/CoreData', '~> 0.20.0'

#指定某个平台的依赖
spec.ios.dependency 'MBProgressHUD', '~> 0.5'

#不支持ARC
spec.requires_arc = false

#指定某个文件夹支持ARC的
spec.requires_arc = 'Classes/Arc'

#指定某些文件支持ARC
spec.requires_arc = ['Classes/*ARC.m', 'Classes/ARC.mm']

spec.ios.framework = 'CFNetwork'

#多个库
spec.frameworks = 'QuartzCore', 'CoreData'

spec.ios.library = 'xml2'

#多个库,其中xml2对应ibxml2.tbd       z对应libz.tbd
#所以lib库 省略lib与后缀。
spec.libraries = 'xml2', 'z'

#-D是前缀,-Wno-format这个是gcc编译警告的参数
spec.compiler_flags = '-DOS_OBJECT_USE_OBJC=0', '-Wno-format'

#OTHER_LDFLAGS  对应buildsetting的other linker flags
spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }

spec.user_target_xcconfig = { 'MY_SUBSPEC' => 'YES' }

image
spec.prefix_header_contents = '#import <UIKit/UIKit.h>'

spec.prefix_header_contents = '#import <UIKit/UIKit.h>', '#import <Foundation/Foundation.h>'

//可以将多行内容放到两个EOS中间
s.prefix_header_contents = <<-EOS
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#else
EOS

s.prefix_header_contents = <<-EOS
#define HHHH @"测试代码"
EOS

image image
spec.prefix_header_file = 'iphone/include/prefix.pch'

spec.module_name = 'Three20'

4、File patterns 文件格式

Podspecs应该放在pod库的根目录,文件模式不支持遍历父目录(. .),要相对于pod库的根来指定文件的路径。

#包含Classes目录下的所有.h .m文件
spec.source_files = 'Classes/**/*.{h,m}'

#包含Classes和More_Classes目录下的所有.h .m文件
spec.source_files = 'Classes/**/*.{h,m}', 'More_Classes/**/*.{h,m}'

通配符*:

匹配所有文件
c
匹配名字以c开头的文件。
*c 匹配名字以c结束的文件。
c 匹配名字含有c的,包含c在开头和结尾的情况。

通配符**:

目录递归地匹配。也就是包含子目录

通配符?:

匹配任何一个字符
与正则中 /.{1}/ 一致

通配符[set]:

匹配多个字符。匹配在字符集中的任何一个字符。
跟正则中的字符集一样,也可以取反 [^a-z]

通配符{p,q}:

匹配文件名包含p或q的,可以写两个或多个字

通配符\:

跳过下一个元字符

#以JSONKit为例
"JSONKit.?"    #=> ["JSONKit.h", "JSONKit.m"]
"*.[a-z][a-z]" #=> ["CHANGELOG.md", "README.md"]
"*.[^m]*"      #=> ["JSONKit.h"]
"*.{h,m}"      #=> ["JSONKit.h", "JSONKit.m"]
"*"            #=> ["CHANGELOG.md", "JSONKit.h", "JSONKit.m", "README.md"]

spec.public_header_files = 'Headers/Public/*.h'

spec.private_header_files = 'Headers/Private/*.h'

spec.ios.vendored_frameworks = 'Frameworks/MyFramework.framework'

spec.vendored_frameworks = 'MyFramework.framework', 'TheirFramework.framework'

spec.ios.vendored_library = 'Libraries/libProj4.a'

spec.vendored_libraries = 'libProj4.a', 'libJavaScriptCore.a'

spec.ios.resource_bundle = { 'MapBox' => 'MapView/Map/Resources/*.png' }

#多个路径
spec.resource_bundles = {
    'MapBox' => ['MapView/Map/Resources/*.png'],
    'OtherResources' => ['MapView/Map/OtherResources/*.png']
  }

spec.resource = 'Resources/HockeySDK.bundle'

#多个,注意是resources复数形式,也可以不加[ ]
spec.resources = ['Images/*.png', 'Sounds/*']

spec.ios.exclude_files = 'Classes/osx'

spec.exclude_files = 'Classes/**/unused.{h,m}'

spec.preserve_path = 'IMPORTANT.txt'

spec.preserve_paths = 'Frameworks/*.framework'

spec.module_map = 'source/module.modulemap'

5、Subspecs

可以理解为pod库中的子模块。
一个库可以指定对另一个库的依赖,另一个库的子规范,或者是它自身的子规范。

#安装ShareKit,会包括ShareKit / Evernote,ShareKit / Facebook等,因为它们被定义为subspecs。
pod 'ShareKit', '2.0'

#只安装ShareKit中的某个子库,这种情况下subspec需要源文件,依赖和其他在根spec中定义的属性,不过cocoapods能帮我们处理这些问题。
pod 'ShareKit/Twitter',  '2.0'
pod 'ShareKit/Pinboard', '2.0'

#有不同源文件的subspec
subspec 'Twitter' do |sp|
  sp.source_files = 'Classes/Twitter'
end

subspec 'Pinboard' do |sp|
  sp.source_files = 'Classes/Pinboard'
end

#子库的spec依赖其他subspec·
Pod::Spec.new do |s|
  s.name = 'RestKit'

  s.subspec 'Core' do |cs|
    cs.dependency 'RestKit/ObjectMapping'
    cs.dependency 'RestKit/Network'
    cs.dependency 'RestKit/CoreData'
  end

  s.subspec 'ObjectMapping' do |os|
  end
end

#嵌套的Subspec
Pod::Spec.new do |s|
  s.name = 'Root'

  s.subspec 'Level_1' do |sp|
    sp.subspec 'Level_2' do |ssp|
    end
  end
end

spec.default_subspec = 'Core'

spec.default_subspecs = 'Core', 'UI'

6、Multi-Platform support

所有标识支持多平台的,都可以针对平台设置参数

spec.ios.resources = 'Resources_ios/**/*.png'
spec.osx.source_files = 'Classes/osx/**/*.{h,m}'
spec.tvos.source_files = 'Classes/tvos/**/*.{h,m}'
spec.watchos.source_files = 'Classes/watchos/**/*.{h,m}'

上一篇 下一篇

猜你喜欢

热点阅读