文章

pod插件制作

Cocoapods plugins

Cocoapods plugins是一个Ruby gem(Ruby gem是Ruby的一个包管理器,它提供一个分发Ruby程序和库的标准格式,还提供一个管理程序包安装的工具。),并且CocoaPods也是通过Ruby构建的。

安装模板插件cocoapods-plugins,来建立插件,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pod-plugin|⇒ sudo gem install cocoapods-plugins
Password:
Successfully installed cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-plugins-1.0.0
Done installing documentation for cocoapods-plugins after 0 seconds
1 gem installed
pod-plugin|⇒ ll
pod-plugin|⇒ pod plugins create cocoapods-rambo
-> Creating `cocoapods-rambo` plugin

[!] using template 'https://github.com/CocoaPods/cocoapods-plugin-template.git'
-> Configuring template
Configuring cocoapods-rambo
user name:裘俊云
user email:hzqiujunyun@corp.netease.com
year:2021

[!] Don't forget to create a Pull Request on https://github.com/CocoaPods/cocoapods-plugins
 to add your plugin to the plugins.json file once it is released!
pod-plugin|⇒ ll
total 0
drwxr-xr-x  11 rambo  staff  352  4 12 19:48 cocoapods-rambo
pod-plugin|⇒ cd cocoapods-rambo
cocoapods-rambo|master ⇒ tree -L 6
.
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── cocoapods-rambo.gemspec
├── lib
│   ├── cocoapods-rambo
│   │   ├── command
│   │   │   └── rambo.rb
│   │   ├── command.rb
│   │   └── gem_version.rb
│   ├── cocoapods-rambo.rb
│   └── cocoapods_plugin.rb
└── spec
    ├── command
    │   └── rambo_spec.rb
    └── spec_helper.rb

5 directories, 12 files

查看rambo.rb的初始话内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 Rambo < Command
      # pod plugins list 时,展示的概要信息
      self.summary = 'Short description of cocoapods-rambo.'
			# --help / 命令错误时展示的描述信息
      self.description = <<-DESC Longer description of cocoapods-rambo. 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-rambo plugin in #{__FILE__}"
      end
    end
  end
end

编译生成gem文件,安装并查看是否安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
cocoapods-rambo|master ⇒ sudo gem build cocoapods-rambo.gemspec
Password:
WARNING:  open-ended dependency on rake (>= 0, development) is not recommended
  if rake is semantically versioned, use:
    add_development_dependency 'rake', '~> 0'
WARNING:  See http://guides.rubygems.org/specification-reference/ for help
  Successfully built RubyGem
  Name: cocoapods-rambo
  Version: 0.0.1
  File: cocoapods-rambo-0.0.1.gem
cocoapods-rambo|master⚡ ⇒ ll
total 56
-rw-r--r--  1 rambo  staff   217  4 12 19:48 Gemfile
-rw-r--r--  1 rambo  staff  1097  4 12 19:48 LICENSE.txt
-rw-r--r--  1 rambo  staff   146  4 12 19:48 README.md
-rw-r--r--  1 rambo  staff   214  4 12 19:48 Rakefile
-rw-r--r--  1 root   staff  7168  4 20 17:47 cocoapods-rambo-0.0.1.gem
-rw-r--r--  1 rambo  staff   925  4 12 19:48 cocoapods-rambo.gemspec
drwxr-xr-x  5 rambo  staff   160  4 12 19:48 lib
drwxr-xr-x  4 rambo  staff   128  4 12 19:48 spec
cocoapods-rambo|master⚡ ⇒ sudo gem install cocoapods-rambo-0.0.1.gem
Successfully installed cocoapods-rambo-0.0.1
Parsing documentation for cocoapods-rambo-0.0.1
Installing ri documentation for cocoapods-rambo-0.0.1
Done installing documentation for cocoapods-rambo after 0 seconds
1 gem installed
cocoapods-rambo|master⚡ ⇒ pod plugins installed | grep rambo
[!] The specification of arguments as a string has been deprecated Pod::Command::Rambo: `NAME`
    - cocoapods-rambo       : 0.0.1

以上流程就演示完一个pod插件的生成和安装的流程。

编写Ruby实现自己的插件功能

参考文章

编写自己的CocoaPods插件

Cocoapods-plugins

CocoaPods Plugins 开发

编写自己的CocoaPods插件

Cocoapods Plugins 插件开发过程

本文由作者按照 CC BY 4.0 进行授权