iOS之深入解析如何编写自己的CocoaPods插件

一、前言

  • Cocoapods 有很多比较实用的小插件,比如 cocoapods-open ( 执行 pod open 可以直接打开 .xcworkspace 文件),这些插件 gem 都有特定的目录分层。一开始以为自己要从零开始配置,后来发现 cocoapods-plugin 本身就提供了用来创建一个模版工程的 create 命令。
  • 输入以下命令即可创建一个模版工程:
// 安装
gem install cocoapods-plugins
// 创建
pod plugins create NAME [TEMPLATE_URL]
  • 在这个需求中,创建一个 author 子命令,输入:
pod plugins create author
  • 当前路径下会生成 cocoapods-author 目录,其结构如下:
.
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── cocoapods-author.gemspec
├── lib
│   ├── cocoapods-author
│   │   ├── command
│   │   │   └── author.rb
│   │   ├── command.rb
│   │   └── gem_version.rb
│   ├── cocoapods-author.rb
│   └── cocoapods_plugin.rb
└── spec
    ├── command
    │   └── author_spec.rb
    └── spec_helper.rb
  • author.rb 的初始内容如下:
module Pod
  class Command
    # This is an example of a cocoapods plugin adding a top-level subcommand
    # to the 'pod' command.
    #
    # You can also create subcommands of existing or new commands. Say you
    # wanted to add a subcommand to `list` to show newly deprecated pods,
    # (e.g. `pod list deprecated`), there are a few things that would need
    # to change.
    #
    # - move this file to `lib/pod/command/list/deprecated.rb` and update
    #   the class to exist in the the Pod::Command::List namespace
    # - change this class to extend from `List` instead of `Command`. This
    #   tells the plugin system that it is a subcommand of `list`.
    # - edit `lib/cocoapods_plugins.rb` to require this file
    #
    # @todo Create a PR to add your plugin to CocoaPods/cocoapods.org
    #       in the `plugins.json` file, once your plugin is released.
    #
    class Author < Command
      # pod plugins list 时,展示的概要信息
      self.summary = 'Short description of cocoapods-author.'

      # --help / 命令错误时展示的描述信息
      self.description = <<-DESC
        Longer description of cocoapods-author.
      DESC

      # --help / 命令错误时展示的参数信息
      self.arguments = 'NAME'

      def initialize(argv)
        @name = argv.shift_argument
        super
      end

      # 校验方法(查看文件是否存在等)
      def validate!
        super
        help! 'A Pod name is required.' unless @name
      end

      # 运行命令
      def run
        UI.puts "Add your implementation for the cocoapods-author plugin in #{__FILE__}"
      end
    end
  end
end
  • 如何创建 list 的子命令 deprecated:
    • 移动目标文件至 lib/pod/command/list/deprecated.rb,并且将这个子命令类放进 Pod::Command::List 命名空间中;
    • 更改子命令类的父类为 List;
    • 在 lib/cocoapods_plugins.rb 中载入该文件。
  • 如果不需要子命令,直接继承 Command 就可以。

二、获取 Podfile untagged 组件名

  • 如下所示的人方法返回没有依赖 tag 的组件哈希,为了简单处理,直接获取 target_definitions 第一个元素的 dependencies 作为工程的依赖进行遍历:
UNTAGGED_FLAGS = [:path, :git, :branch, :commit]

def load_untageed_dependencies_hash
  file_path = Dir.pwd + '/Podfile'

  raise %Q[没有找到 Podfile,请确认是否在 Podfile 所在文件夹\n] unless File.file?(file_path)

  podfile = Podfile.from_file(file_path)
  dependencies = podfile.to_hash['target_definitions'].first['dependencies']

  # UI.puts dependencies

  untageed_dependencies = dependencies.select do |dependency|
    tagged = true
    if dependency.kind_of? Hash
      first = dependency.values.first.first
      if first.kind_of? Hash 
        tagged = first.keys.reduce(true) do |result, flag|
          !UNTAGGED_FLAGS.include?(flag) & result
        end
      elsif first.kind_of? String
        tagged = true   
      end
    elsif dependency.is_a?(String)
      tagged = true 
    end
    !tagged
  end

  untageed_dependencies.reduce({}) do |result, dependency|
    result.merge(dependency)
  end
end

三、获取本地私有源获取组件的作者

  • 获取本地私有源的组件作者:
def load_pod_authors(spec_files)
  author_hash = {}  
  spec_files.each do |file|
    if !file.nil? && File.file?(file)
      podspec = Specification.from_file(file)   
      pod_authors = podspec.attributes_hash['authors']

      if pod_authors.kind_of? Hash
        author = pod_authors.keys.first
      elsif pod_authors.kind_of? Array
        author = pod_authors.first
      else
        author = pod_authors
      end
      author = author.downcase

      if !author.nil? && !podspec.name.nil?
        author_hash[author] =  author_hash[author].nil? ? [] : author_hash[author]
        author_hash[author].append(podspec.name)
      end
    end
  end
  author_hash
end
  • 获取组员信息:
def load_boss_keeper_members
  member_hash = {}
  @memebrs_hash.uniq.map.each do |nickname| 

    reform_memeber_hash_proc = -> nickname, realname do
      nickname = nickname.downcase unless !nickname.nil?
      pinyin = Pinyin.t(nickname, splitter: '').downcase
      if pinyin != nickname 
        member_hash[nickname] = realname
      end

      member_hash[pinyin] = realname
    end

    if nickname.kind_of? Hash
      name = nickname.keys.first.dup.force_encoding("UTF-8")
      nickname.values.first.append(name).each do |nickname|
        reform_memeber_hash_proc.call(nickname, name)
      enddef load_pod_authors(spec_files)
  author_hash = {}  
  spec_files.each do |file|
    if !file.nil? && File.file?(file)
      podspec = Specification.from_file(file)   
      pod_authors = podspec.attributes_hash['authors']

      if pod_authors.kind_of? Hash
        author = pod_authors.keys.first
      elsif pod_authors.kind_of? Array
        author = pod_authors.first
      else
        author = pod_authors
      end
      author = author.downcase

      if !author.nil? && !podspec.name.nil?
        author_hash[author] =  author_hash[author].nil? ? [] : author_hash[author]
        author_hash[author].append(podspec.name)
      end
    end
  end
  author_hash
end
    else
      reform_memeber_hash_proc.call(nickname, nickname)
    end
  end
  member_hash
end
  • 如果 @ 组员需要手机号码,那么该部分数据可以从本地的 yaml 文件读取:
file_path = File.join(File.dirname(__FILE__),"boss_keeper_members.yaml")
yaml_hash =     
begin
  YAML.load_file(file_path)
rescue Psych::SyntaxError, Errno::EACCES, Errno::ENOENT
  {}
end

@memebrs_hash = yaml_hash['members']
@mobiles = yaml_hash["mobiles"].reduce({}){ |result, mobile| result.merge(mobile) }
  • 由于作者一栏存在用花名、原名、昵称的情况,所以 yaml 文件基本格式如下:
members:
- 青木:
  - tripleCC

mobiles:
- 青木:
  - 1xxxxxxx

四、匹配本地存储的组员名

  • 首先匹配是组内组员管理的组件,如果是其它组创建的组件,暂时忽略:
def load_valid_pod_authors_hash
  authors_hash = {}
  @pod_authors.each do |name, pods|
    member_name = @boss_keeper_members[name.downcase]
    if !member_name.nil?
      member_name = member_name.downcase
      author_pods = authors_hash[member_name].nil? ? [] : authors_hash[member_name]
      authors_hash[member_name] = author_pods.append(pods).flatten
    end
  end
  authors_hash
end
  • 然后将结果和未指定 tag 的组件哈希匹配:
def load_untageed_dependencies_authors_hash
  authors_hash = {}
  valid_pod_authors = load_valid_pod_authors_hash
  untageed_dependencies_hash = load_untageed_dependencies_hash
  untageed_dependencies_hash.keys.each do |pod|
    valid_pod_authors.each do  |author, pods|
      if pods.include?(pod)
        authors_hash[author] = authors_hash[author].nil? ? [] : authors_hash[author]
        authors_hash[author].append(pod)
      end
    end
  end
  authors_hash
end

五、整合发送

  • 通过 git rev-parse --abbrev-ref HEAD 获取当前所在分支,然后根据作者、组件名、手机号码创建 curl 命令并执行:
def post_message_to_ding_talk
  current_branch = `git rev-parse --abbrev-ref HEAD`.strip

  untageed_dependencies_authors_hash = load_untageed_dependencies_authors_hash
  untageed_dependencies_authors_hash.each do |author, pods|
    content = author + ",#{current_branch}分支" + ",下面的仓库打个版本:" + pods.join(',')
    if !@mobiles[author].nil?
      mobile = @mobiles[author].first
    end

    curl = %Q[
    curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx' \
       -H 'Content-Type: application/json' \
       -d '
      {
        "msgtype": "text", 
        "text": {
            "content": "#{content}"
         },
         "at": {
            "atMobiles": [
              "#{mobile}"
            ], 
            "isAtAll": false
        }
      }']
      # UI.puts curl
    Kernel.system curl
  end
end
  • 如果后续对这个插件进行扩展的话,还应该考虑根据私有源自动生成 Podfile,解决封版时需要手动修改 Podfile 的情况,麻烦而且可能存在把版本号写错的情况。
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

╰つ栺尖篴夢ゞ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值