[Cocoapods]Podfile和Podspec技巧

Podfile

  • Podfile是一个ruby文件,因此可以使用ruby的相关能力

  • pod 命令是gem 模块

$ gem list|grep cocoapods
cocoapods (1.5.3)
cocoapods-core (1.5.3)
cocoapods-deintegrate (1.0.2)
cocoapods-downloader (1.2.1)
cocoapods-packager (1.5.0)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.3.0)
cocoapods-try (1.1.0)

pod 下载的代码库安装在~/.cocoapods/

  • Podfile具体实例
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'

install! 'cocoapods', :deterministic_uuids => false

#这里可以执行以下预处理命令,比如从其他地方复制podspec文件到根目录
#由于Cocoapods设计限制,所有的source_files必须是基于Pods根目录的,不能使用Pods目录之外。因此这里将一个独立的模块复制到根目录。

system("cp -rf ../node_modules/react-native-dop/ios/Dop.podspec .")

target 'myapp' do
    pod 'SDVersion'
   #加载自定义模块
    pod 'Dop',  :path => "."
end

另外,官方已经定义了pre_install,post_install来做预处理,注意放置在target/end之外,否则将会达不到预期,可以添加 system(‘echo “hello,world”’)调试代码查看编译过程和执行顺序。

pod install的时候会先加载本地的podspec文件生成podspec.json文件到Pods/Local Podspec目录下,因此pod 'Dop', :path => "." 解析时,该Dop.podspec文件必须存在根目录下,否则编译错误,如果该文件需要从别处复制过来,那么需要提前复制到根目录下。

由于项目需要,将某个独立模块做成static library 然后放置在node_modules下由npm管理,因此,该static library依赖的podspec模块也不能存在根目录下,在pod install时从node_modules下复制过来,post_install时删除它,避免污染代码。

Dop.podspec

Pod::Spec.new do |s|
  s.name     = "Dop"
  s.version  = "0.1.1"
  s.license  = "Apache License, Version 2.0"
  s.authors  = { 'gRPC contributors' => 'grpc-io@googlegroups.com' }
  s.homepage = "https://grpc.io/"
  s.summary = "grpc service"
  #s.source = { :git => 'https://github.com/grpc/grpc.git' }
  s.source = { :git => 'https://github.com/sebyddd/SDVersion.git' }

  s.ios.deployment_target = "7.1"
  s.osx.deployment_target = "10.9"

  # Base directory where the .proto files are.
  src = "../node_modules/react-native-dop/proto"

  # Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients.
  s.dependency "!ProtoCompiler-gRPCPlugin", "~> 1.0"

  # Pods directory corresponding to this app's Podfile, relative to the location of this podspec.
  pods_root = 'Pods'

  # Path where Cocoapods downloads protoc and the gRPC plugin.
  protoc_dir = "#{pods_root}/!ProtoCompiler"
  protoc = "#{protoc_dir}/protoc"
  plugin = "#{pods_root}/!ProtoCompiler-gRPCPlugin/grpc_objective_c_plugin"

  # Directory where the generated files will be placed.
  dir = "#{pods_root}/#{s.name}"
   
#预处理命令,可以做一些文件操作
  s.prepare_command = <<-CMD
  #创建目录 Pods/Dop
    mkdir -p #{dir}
# 使用protoc编译proto文件,生成相应的Objective-C代码(.m/.h)
    #{protoc} \
        --plugin=protoc-gen-grpc=#{plugin} \
        --objc_out=#{dir} \
        --grpc_out=#{dir} \
        -I #{src}/public \
        -I #{protoc_dir} \
        #{src}/**/*.proto
  CMD

#这里是生成Pod项目的关键,根据subspec生成Messages Group
  # Files generated by protoc
  s.subspec "Messages" do |ms|
    ms.source_files = "#{dir}/*.pbobjc.{h,m}", "#{dir}/**/*.pbobjc.{h,m}"
    ms.header_mappings_dir = dir
    ms.requires_arc = false
    # The generated files depend on the protobuf runtime.
    ms.dependency "Protobuf"
  end

  # Files generated by the gRPC plugin
  s.subspec "Services" do |ss|
    ss.source_files = "#{dir}/*.pbrpc.{h,m}", "#{dir}/**/*.pbrpc.{h,m}"
    ss.header_mappings_dir = dir
    ss.requires_arc = true
    # The generated files depend on the gRPC runtime, and on the files generated by protoc.
    ss.dependency "gRPC-ProtoRPC"
    ss.dependency "#{s.name}/Messages"
  end
  
  s.pod_target_xcconfig = {
    # This is needed by all pods that depend on Protobuf:
    'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1',
    # This is needed by all pods that depend on gRPC-RxLibrary:
    'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES',
  }
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值