iOS 自定义pod库插入shell脚本

在开发私有pod库时,我们可能会有这样的需求,当目标工程安装我们的pod库之后,我们需要执行响应的操作,比如说在编译之后上传dsym文件、在运行之前修改项目资源等等,如果平时项目开发中有这种需求,我们会通过shell或者ruby脚本去执行,但是我们自定义的pod库该怎样去让target工程去执行这些脚本文件呢,下面上demo教程

1.创建Demo工程

pod lib create Yuehan

2.配置.podspec执行脚本

image.png

3.podsepc script_phase

这个脚本是作为pod的编译的一部分,但是与prepare command不同,script是作为xcodebuild的一部分执行的,脚本可以利用编译器的一切环境变量,脚本的执行顺序是按照声明顺序执行的。

像上面Demo中,我配置了一个script_phase,其中, :script=>表示所需要执行的脚本,我们通过引用,并采用CMD可以书写多行执行脚本,
:execution_position =>表示是在编译前执行还是编译之后执行,:shell_path =>表示脚本运行环境路径

上面Demo中script1执行后会写入一个字符串在tst.txt文件中,pod install->run运行结果


image.png

4.script_phase可以支持插入多条脚本

#  #script_phase2
  script1 = <<-CMD
    #Pods目录
    podsPath=$(pwd)
    echo $podsPath >> /Users/gelei/Downloads/tst.txt
  CMD
  
  script2 = <<-CMD
    echo "Hello world" >> /Users/gelei/Downloads/tst.txt
  CMD
  #shell_path指定脚本运行环境,execution_position指定遍以前还是编译后执行
#  s.script_phase = { :name => 'pod compile before', :script => script1, :shell_path =>'/bin/sh', :execution_position => :before_compile}
  
#  #script_phase2
  s.script_phase = [
  { :name => 'pod compile before1', :script => script1, :shell_path =>'/bin/sh', :execution_position => :before_compile},
  { :name => 'pod compile before2', :script => script2, :shell_path =>'/bin/sh', :execution_position => :before_compile}
  ]

4.1pod install->run运行结果

image.png

可以看到这里两个script都执行了

Demo地址

你可能感兴趣的:(iOS 自定义pod库插入shell脚本)