ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

fastlane 内置 Actions 目录解析:自动检测机制与自定义 Action 创建指南

fastlane 内置 Actions 目录解析:自动检测机制与自定义 Action 创建指南 fastlane 内置 Actions 目录解析自动检测机制与自定义 Action 创建指南【免费下载链接】fastlane The easiest way to automate building and releasing your iOS and Android apps项目地址: https://gitcode.com/GitHub_Trending/fa/fastlanefastlane 的核心扩展能力建立在 “action” 这一最小执行单元之上每一个.rb文件对应一个可复用的自动化步骤从构建签名gym、match到上传发布deliver、pilot都由 action 编排完成。本文以 fastlane/lib/fastlane/actions/README.md 为主线深入解析内置 actions 目录的组织约定、fastlane 如何自动检测并加载该目录下的文件、fastlane new_action命令的完整生成流程以及一个 Action 类需要实现哪些接口才能被 fastlane 正确识别、执行和生成文档。内置 Actions 目录fastlane 集成的事实中心actions/README.md 说明了该目录的定位All built-in integrations are available in this directory. Use thefastlane new_actioncommand to create a new action.fastlanewill automatically detect the files in this folder也就是说fastlane/lib/fastlane/actions/ 目录是 fastlane 全部内置集成的存放地且 fastlane 会自动检测该目录下的文件开发者无需手动注册。当前仓库中该目录包含数百个 action 文件例如build_app.rb构建 App 的通用实现供各平台 action 复用gym.rbiOS 构建capture_screenshots.rb截图采集deliver.rb元数据与构建上传 App Store Connect值得注意的是许多 action 文件内部通过require复用更底层的实现例如gym.rb首行即require fastlane/actions/build_apppilot.rb复用upload_to_testflight。从源码结构看目录内存在两层内容直接面向用户调用的 action与供其他 action 内部require的共享实现如 upload_to_app_store.rb 被deliver与appstore共同依赖这也是阅读该目录时需要区分的关键点。该目录下还有一个 docs/ 子目录存放特定 action 的补充文档如 run_tests.md、upload_to_app_store.md.erb、upload_to_testflight.md供文档生成流程合并进各 action 的说明页。自动检测机制fastlane 如何“发现”一个 ActionREADME 中“自动检测”的说法对应 actions_helper.rb 中的三个关键方法1. 加载内置 actionsload_default_actionsdef self.load_default_actions Dir[File.expand_path(*.rb, File.dirname(__FILE__))].each do |file| require file end end实现非常直接对actions/目录下所有*.rb文件逐一require。这就是“自动检测”的全部含义——文件名即注册信息只要文件内定义了符合命名约定的 Action 子类加载后即可通过名字调用。2. 官方 action 清单get_all_official_actionsdef self.get_all_official_actions Dir[File.expand_path(*.rb, File.dirname(__FILE__))].collect do |file| File.basename(file).gsub(.rb, ).to_sym end end同样的目录扫描被用来生成官方 action 名称列表如:gym、:scan供插件系统区分内置与插件 action。action_collector.rb 中的determine_version也依赖这一约定带插件前缀fastlane-plugin-的名字解析为插件版本否则统一归为内置 action 并返回Fastlane::VERSION。3. 按名字反查类action_class_refdef self.action_class_ref(action_name) class_name action_name.to_s.fastlane_class Action # ... class_ref Fastlane::Actions.const_get(class_name) end调用方只写小写名字如gymfastlane 将其转换为驼峰类名并追加Action后缀GymAction再到Fastlane::Actions命名空间下查找。这一约定与 action.rb 中的action_name互为逆操作# instead of AddGitAction, this will return add_git to print it to the user def self.action_name self.name.split(::).last.gsub(/Action$/, ).fastlane_underscore end因此文件名必须与 action 名字一致、类名必须以Action结尾否则加载器load_external_actions会明确报错Could not find ClassName class defined. Action file_name is damaged!该错误处理同时出现在 actions_helper.rb 的load_external_actions中它负责加载项目本地fastlane/actions/目录下的自定义 action对每个文件做require捕获SyntaxError并高亮出错行再校验类存在且实现了run方法。使用fastlane new_action创建新 ActionREADME 推荐的创建方式是fastlane new_action命令。该命令在 commands_generator.rb 中注册语法为fastlane new_action支持传入可选的名字参数最终调用 new_action.rb 中的Fastlane::NewAction.run。命名校验规则如果不带参数运行命令会进入交互式输入无论哪种方式名字都必须通过同一个校验def self.name_valid?(name) name ~ /^[a-z0-9_]$/ end即只能包含小写字母、数字和下划线交互提示中也给出了示例testflight、upload_to_s3。不符合规则会提示 Name is invalid. Please ensure the name is all lowercase, free of spaces and without special characters! 并要求重新输入。模板渲染与文件落盘generate_action从内置模板生成 action 文件def self.generate_action(name) template File.read(#{Fastlane::ROOT}/lib/assets/custom_action_template.rb) template.gsub!([[NAME]], name) template.gsub!([[NAME_UP]], name.upcase) template.gsub!([[NAME_CLASS]], name.fastlane_class Action) actions_path File.join((FastlaneCore::FastlaneFolder.path || Dir.pwd), actions) FileUtils.mkdir_p(actions_path) unless File.directory?(actions_path) path File.join(actions_path, #{name}.rb) File.write(path, template) UI.success(Created new action file #{path}. Edit it to implement your custom action.) end三个模板占位符分别被替换为占位符替换值示例name upload_to_s3[[NAME]]原样名字upload_to_s3[[NAME_UP]]大写化用于 SharedValues 常量、环境变量前缀UPLOAD_TO_S3[[NAME_CLASS]]驼峰化 Action后缀UploadToS3Action注意生成位置优先写入项目的fastlane/目录下的actions/子目录FastlaneCore::FastlaneFolder.path找不到 fastlane 文件夹时回退到当前工作目录。这正好对接前述load_external_actions的加载路径——生成在项目的fastlane/actions/里运行时被自动加载形成完整闭环。拆解 Action 模板一个合法 Action 的最小接口生成文件的内容来自 custom_action_template.rb它完整展示了一个 Action 需要或建议实现的所有静态方法也是理解内置 action 源码的通用钥匙。以模板为例module Fastlane module Actions module SharedValues UPLOAD_TO_S3_CUSTOM_VALUE :UPLOAD_TO_S3_CUSTOM_VALUE end class UploadToS3Action Action def self.run(params) # fastlane will take care of reading in the parameter and # fetching the environment variable: UI.message(Parameter API Token: #{params[:api_token]}) # sh shellcommand ./path # Actions.lane_context[SharedValues::UPLOAD_TO_S3_CUSTOM_VALUE] my_val end def self.description A short description with 80 characters of what this action does end def self.available_options [ FastlaneCore::ConfigItem.new(key: :api_token, env_name: FL_UPLOAD_TO_S3_API_TOKEN, description: API Token, verify_block: proc do |value| unless value !value.empty? UI.user_error!(No API token given) end end), FastlaneCore::ConfigItem.new(key: :development, env_name: FL_UPLOAD_TO_S3_DEVELOPMENT, description: Create a development certificate, is_string: false, default_value: false) ] end def self.output [[UPLOAD_TO_S3_CUSTOM_VALUE, A description of what this value contains]] end def self.is_supported?(platform) platform :ios end end end end对照基类 action.rb 可以逐条确认各方法的职责与缺省行为方法作用基类缺省行为run(params)实际逻辑入口params中同时包含 Fastfile 传参与环境变量解析结果空实现子类必须覆写description文档中的一行简短描述返回红色警告 No description providedaction.rbdetails可选的长描述可含 markdownnilavailable_options声明所有参数每项是FastlaneCore::ConfigItemniloutput声明 action 写入共享区lane_context的键值nilreturn_value/return_type描述返回值return_type取值受限于RETURN_TYPES:string、:array_of_strings、:hash、:bool、:int等见 action.rbnilauthors作者署名nilis_supported?(platform)声明支持的平台:ios、:mac、:android或true直接UI.crash!即必须实现action.rbcategory文档分类取值来自AVAILABLE_CATEGORIEStesting、building、code_signing、notifications等deprecated必须最后:undefineddeprecated_notes标记废弃 action 时给用户的迁移说明nil几个模板中的细节值得展开ConfigItem的env_name约定参数会同时从 Fastfile 调用参数和环境变量两处读取模板推荐用FL_ACTION_NAME_UPPER作为环境变量前缀如FL_UPLOAD_TO_S3_API_TOKEN避免与系统变量冲突。verify_block是参数校验钩子在值被使用前执行可直接UI.user_error!终止并给出带使用示例的提示。is_string: false表示参数接受非字符串值如布尔default_value提供缺省值。lane_context是 action 之间的共享数据区output中声明的键在运行时写入Actions.lane_context下游 action 可直接读取。基类中Action.lane_context只是对Actions.lane_context的转发action.rb底层是一个支持敏感值隐藏的特殊 Hash 实现LaneContextValuesactions_helper.rb用于存放 token 之类不宜明文打印的数据。在 action 内部调用其他 action模板注释中提示other_action.xxx。这是由基类的method_missing兜底逻辑保证的——若直接裸调xxx会触发UI.user_error!(To call another action from an action use \other_action.#{method_sym} instead)action.rb。sh一行即用基类通过def_delegator(Actions, :sh_control_output, :sh)把sh委托给 Actions 助手使自定义 action 可以直接执行 shell 命令当项目存在Gemfile时 shell out 会自动尝试使用bundle execshell_out_should_use_bundle_exec?action.rb。运行时的执行与追踪action 被调用时会经过 actions_helper.rb 中的execute_action包装打印Step: step_name标题、计时、捕获异常并在executed_actions中记录每一步的名字、耗时与错误堆栈。这份记录同时服务于终端输出与 JUnit 报告生成因此自定义 action 即使不实现step_text缺省返回action_name也会出现在执行报告中。此外Actions.alias_actionsactions_helper.rb会收集所有声明了aliases的 action支持为 action 提供别名调用。文档生成与文档站actions/README.md 最后一段说明所有 action 会在文档站集中列出并逐一生成文档页。结合仓库实现来看文档内容的数据来源正是上表中的各个静态方法description、details、available_options、output、example_code、sample_return_value等。基类为此还扩展了String的 markdown 辅助方法markdown_sample、markdown_details等action.rb用于把details/example_code中返回的 heredoc 字符串规范化为文档页格式assets 等 ERB 模板则负责最终渲染。因此写好description建议 ≤80 字符与details本身就是 action 交付物的一部分这也是模板注释反复强调的原因。小结回到 actions/README.md 的三句话本文给出了完整的源码级印证内置集成集中在fastlane/lib/fastlane/actions/文件即注册通过load_default_actions/get_all_official_actions的目录扫描被自动发现创建新 action 使用fastlane new_action命名只允许[a-z0-9_]文件由custom_action_template.rb渲染后写入项目的fastlane/actions/再由load_external_actions自动加载并校验run方法的存在一个可被文档化、可执行的 action需要继承Fastlane::Action、实现run与is_supported?并通过ConfigItem声明参数、通过output声明共享值——模板 custom_action_template.rb 已把这些骨架全部备齐开发者只需填充run内的实际逻辑。【免费下载链接】fastlane The easiest way to automate building and releasing your iOS and Android apps项目地址: https://gitcode.com/GitHub_Trending/fa/fastlane创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表