fastlane 中的一些疑问

fastlane 中的一些疑问

最近项目中用到了fastlane 打包, Fastfile 如下

ruby 复制代码
default_platform(:ios)
platform :ios do
  
scheme_name = "DF_HWSQ"
app_version = get_version_number(target: scheme_name)
app_build = get_build_number
app_version_build = "#{app_version}(#{app_build})"
time_string = Time.now.strftime('%Y-%m-%d_%H:%M:%S')
app_ipa_name = "#{scheme_name}_#{app_version_build}_#{time_string}"
​
desc "打包命令:fastlane pgy config:DEV"
    lane :pgy do |option|
    #按照配置打包
    gym(
        scheme: "#{scheme_name}",
        export_method: "ad-hoc",#打包所选的种类(就是App Store,生产测试,企业,开发测试那四种), app-store,ad-hoc,enterprise,development
          output_directory: "./build",
          output_name: "#{app_ipa_name}.ipa",
          configuration: option[:config],
        )
    #上传蒲公英
    pgyer(
      api_key: "xxxx",
      update_description: scheme_name
     )
    #清零构建产物
    clean_build_artifacts
  end
​
​
end

问题1, lane 这里好像一个关键字或是Class. 实际上却是个方法, 这个是如何实现的.

自定义lane的写法如下

ruby 复制代码
default_platform(:ios)
​
platform :ios do
  lane :custom_lane do
    # add actions here: https://docs.fastlane.tools/actions
  end
end

实际上,lane 是一个 Ruby 方法,在 Fastlane 中定义为一个特殊的方法。在 Fastfile 文件中定义一个 lane 时,实际上是在调用 Fastlane 中特定的 lane 方法,并传递您指定的 lane 名称和操作代码块作为参数。

打开brew 安装目录 /usr/local/Cellar, 在 fastlane 中的fast_file.rb 可以找到lane 的实现

ruby 复制代码
def lane(lane_name, &block)
      UI.user_error!("You have to pass a block using 'do' for lane '#{lane_name}'. Make sure you read the docs on GitHub.") unless block
​
      self.runner.add_lane(Lane.new(platform: self.current_platform,
                                       block: block,
                                 description: desc_collection,
                                        name: lane_name,
                                  is_private: false))
​
      @desc_collection = nil # reset the collected description again for the next lane
end

问题2, pgyer 这个方法,没有传递参数. 那它是如何知道需要上传的ipa文件的地址的.

还是从源码来分析

找到找到fastlane-plugin-pgyer的源码路径, brew安装目录下找到 /usr/local/Cellar/fastlane/2.211.0_2/libexec/gems/fastlane-plugin-pgyer-0.2.5

在目录下找到 pgyer_action.rb 文件.

有这么一段代码

ruby 复制代码
FastlaneCore::ConfigItem.new(key: :ipa,
                                       env_name: "PGYER_IPA",
                                       description: "Path to your IPA file. Optional if you use the _gym_ or _xcodebuild_ action. For Mac zip the .app. For Android provide path to .apk file",
                                       default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH],
                                       optional: true,
                                       verify_block: proc do |value|
                                         UI.user_error!("Couldn't find ipa file at path '#{value}'") unless File.exist?(value)
                                       end,
                                       conflicting_options: [:apk],
                                       conflict_block: proc do |value|
                                         UI.user_error!("You can't use 'ipa' and '#{value.key}' options in one run")
                                       end),

这里的configItem 就是pgy上传的ipa参数, 在这段代码中,PGYER_IPA 用来表示用户传递给 pgyer 动作的 IPA 文件的路径。如果用户没有传递该值,那么该参数的默认值就是通过 lane_context 获取的 SharedValues::IPA_OUTPUT_PATH 环境变量的值。

lane_context 是 fastlane 中的一个 Hash 对象,它是用来在 fastlane 动作之间共享数据的。它存储了当前 fastlane 执行过程中的一些关键信息,比如输出文件路径、代码签名配置等等。当你需要在一个 fastlane 动作中访问另一个 fastlane 动作的输出结果时,可以通过 lane_context 来实现数据共享。

例如,在上面的代码中,我们通过 Actions.lane_context[SharedValues::IPA_OUTPUT_PATH] 来获取当前 fastlane 执行过程中生成的 IPA 文件的输出路径,然后将其作为 PGYER_IPA 参数的默认值。

很明显了, 此前的gym 应该对这个IPA_OUTPUT_PATH赋了值, 继续验证一下.

bash 复制代码
grep -i -r "IPA_OUTPUT_PATH" /usr/local/Cellar/fastlane/2.211.0_2/libexec/gems/fastlane-2.212.2

找到对IPA_OUTPUT_PATH赋值的文件, 其路径是 /usr/local/Cellar/fastlane/2.211.0_2/libexec/gems/fastlane-2.212.2/fastlane/lib/fastlane/actions/build_app.rb

Fastfile 中的 gym 就是 alias for build_app

在build_app.rb 中找到

clip1:

ruby 复制代码
if File.extname(absolute_output_path) == ".ipa"
  absolute_dsym_path = absolute_output_path.gsub(/.ipa$/, ".app.dSYM.zip")
​
  Actions.lane_context[SharedValues::IPA_OUTPUT_PATH] = absolute_output_path
  ENV[SharedValues::IPA_OUTPUT_PATH.to_s] = absolute_output_path # for deliver
elsif File.extname(absolute_output_path) == ".pkg"
  absolute_dsym_path = absolute_output_path.gsub(/.pkg$/, ".dSYM.zip")
​
  Actions.lane_context[SharedValues::PKG_OUTPUT_PATH] = absolute_output_path
  ENV[SharedValues::PKG_OUTPUT_PATH.to_s] = absolute_output_path # for deliver
end

gym 操作将 absolute_output_path 赋值到 lane_contextIPA_OUTPUT_PATH 中.

继续找 absolute_output_path

clip2:

arduino 复制代码
gym_output_path = Gym::Manager.new.work(values)
        if gym_output_path.nil?
          UI.important("No output path received from gym")
          return nil
        end
​
absolute_output_path = File.expand_path(gym_output_path)

Gym::Manager 是 fastlane 中内置的一个类,用于管理 gym 的操作。Gym::Manager.new 创建一个新的 Gym::Manager 对象,work 方法用于执行 gym 的操作,返回生成的 .ipa 文件的路径。

that explains it.

相关推荐
子兮曰13 小时前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰13 小时前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万13 小时前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝13 小时前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋14 小时前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁14 小时前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王952716 小时前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大16 小时前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师16 小时前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学17 小时前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端