生活随笔
收集整理的這篇文章主要介紹了
iOS之深入解析如何编写自己的CocoaPods插件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、前言
Cocoapods 有很多比較實用的小插件,比如 cocoapods-open ( 執(zhí)行 pod open 可以直接打開 .xcworkspace 文件),這些插件 gem 都有特定的目錄分層。一開始以為自己要從零開始配置,后來發(fā)現(xiàn) cocoapods-plugin 本身就提供了用來創(chuàng)建一個模版工程的 create 命令。 輸入以下命令即可創(chuàng)建一個模版工程:
gem install cocoapods
- plugins
pod plugins create NAME
[ TEMPLATE_URL
]
在這個需求中,創(chuàng)建一個 author 子命令,輸入:
pod plugins create author
當(dāng)前路徑下會生成 cocoapods-author 目錄,其結(jié)構(gòu)如下:
.
├── 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
module Podclass 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
= << - DESCLonger description of cocoapods
- author
. DESC#
-- help
/ 命令錯誤時展示的參數(shù)信息
self . arguments
= 'NAME' def
initialize ( argv
) @ name
= argv
. shift_argument
super end# 校驗方法(查看文件是否存在等)def validate
! super help
! 'A Pod name is required.' unless
@ nameend# 運行命令def runUI
. puts
"Add your implementation for the cocoapods-author plugin in #{__FILE__}" endendend
end
如何創(chuàng)建 list 的子命令 deprecated: 移動目標(biāo)文件至 lib/pod/command/list/deprecated.rb,并且將這個子命令類放進 Pod::Command::List 命名空間中; 在 lib/cocoapods_plugins.rb 中載入該文件。 如果不需要子命令,直接繼承 Command 就可以。
二、獲取 Podfile untagged 組件名
如下所示的人方法返回沒有依賴 tag 的組件哈希,為了簡單處理,直接獲取 target_definitions 第一個元素的 dependencies 作為工程的依賴進行遍歷:
UNTAGGED_FLAGS
= [ : path
, : git
, : branch
, : commit
] def load_untageed_dependencies_hashfile_path
= Dir
. pwd
+ '/Podfile' raise
% Q
[ 沒有找到 Podfile,請確認(rèn)是否在 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
? Hashfirst
= dependency
. values
. first
. first
if first
. kind_of
? Hash tagged
= first
. keys
. reduce ( true
) do | result
, flag
| ! UNTAGGED_FLAGS
. include
? ( flag
) & resultendelsif first
. kind_of
? Stringtagged
= true endelsif dependency
. is_a
? ( String
) tagged
= true end
! taggedenduntageed_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
? Hashauthor
= pod_authors
. keys
. firstelsif pod_authors
. kind_of
? Arrayauthor
= pod_authors
. first
else author
= pod_authorsendauthor
= author
. downcase
if ! author
. nil
? && ! podspec
. name
. nil
? author_hash
[ author
] = author_hash
[ author
] . nil
? ? [ ] : author_hash
[ author
] author_hash
[ author
] . append ( podspec
. name
) endendendauthor_hash
end
def load_boss_keeper_membersmember_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
] = realnameendmember_hash
[ pinyin
] = realnameend
if nickname
. kind_of
? Hashname
= 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
? Hashauthor
= pod_authors
. keys
. firstelsif pod_authors
. kind_of
? Arrayauthor
= pod_authors
. first
else author
= pod_authorsendauthor
= author
. downcase
if ! author
. nil
? && ! podspec
. name
. nil
? author_hash
[ author
] = author_hash
[ author
] . nil
? ? [ ] : author_hash
[ author
] author_hash
[ author
] . append ( podspec
. name
) endendendauthor_hash
end
else reform_memeber_hash_proc
. call ( nickname
, nickname
) endendmember_hash
end
如果 @ 組員需要手機號碼,那么該部分?jǐn)?shù)據(jù)可以從本地的 yaml 文件讀取:
file_path
= File
. join ( File
. dirname ( __FILE__ ) , "boss_keeper_members.yaml" )
yaml_hash
=
beginYAML
. 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
:
- 青木
: - tripleCCmobiles
:
- 青木
: - 1 xxxxxxx
四、匹配本地存儲的組員名
首先匹配是組內(nèi)組員管理的組件,如果是其它組創(chuàng)建的組件,暫時忽略:
def load_valid_pod_authors_hashauthors_hash
= { } @ pod_authors
. each
do | name
, pods
| member_name
= @ boss_keeper_members
[ name
. downcase
] if ! member_name
. nil
? member_name
= member_name
. downcaseauthor_pods
= authors_hash
[ member_name
] . nil
? ? [ ] : authors_hash
[ member_name
] authors_hash
[ member_name
] = author_pods
. append ( pods
) . flattenendendauthors_hash
end
然后將結(jié)果和未指定 tag 的組件哈希匹配:
def load_untageed_dependencies_authors_hashauthors_hash
= { } valid_pod_authors
= load_valid_pod_authors_hashuntageed_dependencies_hash
= load_untageed_dependencies_hashuntageed_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
) endendendauthors_hash
end
五、整合發(fā)送
通過 git rev-parse --abbrev-ref HEAD 獲取當(dāng)前所在分支,然后根據(jù)作者、組件名、手機號碼創(chuàng)建 curl 命令并執(zhí)行:
def post_message_to_ding_talkcurrent_branch
= `git rev
- parse
-- abbrev
- ref HEAD`
. stripuntageed_dependencies_authors_hash
= load_untageed_dependencies_authors_hashuntageed_dependencies_authors_hash
. each
do | author
, pods
| content
= author
+ ",#{current_branch}分支" + ",下面的倉庫打個版本:" + pods
. join ( ',' ) if ! @ mobiles
[ author
] . nil
? mobile
= @ mobiles
[ author
] . firstendcurl
= % Q
[ curl 'https
: { "msgtype" : "text" , "text" : { "content" : "#{content}" } , "at" : { "atMobiles" : [ "#{mobile}" ] , "isAtAll" : false
} } '
] # UI. puts curl Kernel
. system curlend
end
如果后續(xù)對這個插件進行擴展的話,還應(yīng)該考慮根據(jù)私有源自動生成 Podfile,解決封版時需要手動修改 Podfile 的情況,麻煩而且可能存在把版本號寫錯的情況。
總結(jié)
以上是生活随笔 為你收集整理的iOS之深入解析如何编写自己的CocoaPods插件 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。