fastlane
的強大帶咱們很多的便利,但事無人願。總有些不同的需求,今天就給你們帶來的是fastlane
的action
和插件。ios
這也是fastlane
精髓部分,它使fastlane
具備強大擴展性,以保證變化不斷的個性化需求。git
action
在項目中,能夠建立自定義的action
擴展fastlane
的功能性。建立的這個action
跟fastlane
內置的action
在使用上面來講沒多大區別。下面來個例子:github
action
更新 build 版本號,格式就以年月日時分。在終端輸入下面命令:shell
fastlane new_action
複製代碼
action
實現分析在後面會被要求輸入action
的名字,輸入update_build_version
按回車後,fastlane
會在fastlane/actions
目錄下面建立後綴爲.ruby
文件。請看下面的文件內容ruby
module Fastlane
module Actions
module SharedValues
UPDATE_BUILD_VERSION = :UPDATE_BUILD_VERSION_CUSTOM_VALUE
end
class UpdateBuildVersionAction < Action
def self.run(params) # 這個方法爲Action的主方法,在這裏我們寫入更新版本號的內容
if params[:version_number]
new_version = params[:version_number]
else
# 格式化時間
new_version = Time.now.strftime("%Y%M%d")
end
command = "agvtool new-vresion -all #{new_version}" #使用蘋果的 agvtool 工具更新版本號
Actions.sh(command) #執行上面的 shell 命令
Actions.lane_context[SharedValues::UPDATE_BUILD_VERSION] = new_version # 更新全局變量,供其餘的Actions使用
end
def self.description # 對於該Action小於80字符的簡短描述
"A short description with <= 80 characters of what this action does"
end
def self.details # 對於該Action的詳細描述
# Optional: 可選
end
def self.available_options # 定義外部輸入的參數,在這裏我們定義一個指定版本號的參數
[
FastlaneCore::ConfigItem.new(key: :version_number, # run方法裏面根據該key獲取參數
env_name: "FL_UPDATE_BUILD_VERSION_VERSION_NUMBER", # 環境變量
description: "Change to a specific version", # 參數簡短描述
optional: true),
]
end
def self.output # 輸入值描述,若是在 run 方法更新 SharedValues 模塊裏面自定義的變量,供其餘的 Action 使用,可選
[
['UPDATE_BUILD_VERSION_CUSTOM_VALUE', 'A description of what this value contains']
]
end
def self.return_value # 返回值描述, 指的 run 方法會有返回值。可選
end
def self.authors # 做者
["ChenJzzz"]
end
def self.is_supported?(platform) # 支持的平臺
# you can do things like
#
# true
#
# platform == :ios
#
# [:ios, :mac].include?(platform)
#
platform == :ios
end
end
end
end
複製代碼
從上面的方法上來看,主要的仍是run
方法和available_options
方法。若是看不懂上面的代碼,那去補一下ruby
相關的語法。OK,這個action
跟其餘的action
同樣,在Fastlane
直接使用就能夠了。在終端輸入fastlane action update_build_version
,會像下面同樣,打印出action
的相關信息bash
順便提一下要在另外的項目上使用,直接複製過去就好了。至於要提交到fastlane
的官方庫,仍是相對來講門檻較高。工具
上面的action
在共享這方面,只能靠複製這一手段,至關之不優雅。那麼插件是咱們最好的選擇。測試
建立插件ui
進入一個新的目錄this
fastlane new_plugin [plugin_name]
複製代碼
fastlane
建立Ruby gem
庫目錄lib/fastlane/plugin/[plugin_name]/actions/[plugin_name].rb
這個文件是咱們要實現的action
文件插件跟action
都是一樣的寫法。在這裏就不重複描述了。
在當前目錄下, 能夠運行fastlane test
,測試插件是否正確
RubyGems
的插件fastlane add_plugin [name]
複製代碼
fastlane
會執行如下步驟
fastlane/Pluginfile
./Gemfile
文件正確引用fastlane/Pluginfile
fastlane install_plugins
安裝插件以及須要的依賴Gemfile
、Gemfile.lock
和fastlane/Pluginfile
正如上面所說,在項目裏面的fastlane/Pluginfile
添加下面內容
# 安裝發佈到 Github 的插件
gem "fastlane-plugin-example", git: "https://github.com/fastlane/fastlane-plugin-example"
# 安裝本地插件
gem "fastlane-plugin-xcversion", path: "../fastlane-plugin-xcversion"
複製代碼
在終端運行fastlane/Pluginfile
(或者 bundle exec fastlane/Pluginfile
),安裝插件以及相關依賴
action
的出現,大大的加強了fastlane
的擴展性。使咱們適應本身的業務,定製所須要action
。另外,Plugin
使fastlane
在有強大的擴展性同量,使用更加靈活。
總的來講,若是是單單的項目,action
能夠解決問題。若是是多個項目,使用plugins
是不二選擇。
小Tips:若是看不懂,去補一下Ruby的語法。還有就是多點看一下網上action和plugin寫法。
參考文檔: