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.

相关推荐
你挚爱的强哥14 分钟前
【sgCreateCallAPIFunctionParam】自定义小工具:敏捷开发→调用接口方法参数生成工具
前端·javascript·vue.js
米老鼠的摩托车日记23 分钟前
【vue element-ui】关于删除按钮的提示框,可一键复制
前端·javascript·vue.js
猿饵块1 小时前
cmake--get_filename_component
java·前端·c++
大表哥61 小时前
在react中 使用redux
前端·react.js·前端框架
十月ooOO1 小时前
【解决】chrome 谷歌浏览器,鼠标点击任何区域都是 Input 输入框的状态,能看到输入的光标
前端·chrome·计算机外设
qq_339191141 小时前
spring boot admin集成,springboot2.x集成监控
java·前端·spring boot
pan_junbiao1 小时前
Vue使用代理方式解决跨域问题
前端·javascript·vue.js
明天…ling2 小时前
Web前端开发
前端·css·网络·前端框架·html·web
ROCKY_8172 小时前
web前端-HTML常用标签-综合案例
前端·html
海石2 小时前
从0到1搭建一个属于自己的工作流站点——羽翼渐丰(bpmn-js、Next.js)
前端·javascript·源码