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.

相关推荐
慧一居士28 分钟前
flex 布局完整功能介绍和示例演示
前端
DoraBigHead30 分钟前
小哆啦解题记——两数失踪事件
前端·算法·面试
一斤代码6 小时前
vue3 下载图片(标签内容可转图)
前端·javascript·vue
中微子6 小时前
React Router 源码深度剖析解决面试中的深层次问题
前端·react.js
光影少年6 小时前
从前端转go开发的学习路线
前端·学习·golang
中微子7 小时前
React Router 面试指南:从基础到实战
前端·react.js·前端框架
3Katrina7 小时前
深入理解 useLayoutEffect:解决 UI "闪烁"问题的利器
前端·javascript·面试
前端_学习之路8 小时前
React--Fiber 架构
前端·react.js·架构
伍哥的传说8 小时前
React 实现五子棋人机对战小游戏
前端·javascript·react.js·前端框架·node.js·ecmascript·js
qq_424409198 小时前
uniapp的app项目,某个页面长时间无操作,返回首页
前端·vue.js·uni-app