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
endgym 操作将 absolute_output_path 赋值到 lane_context 的 IPA_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.