You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fastlane的Fastfile加if判断报unexpected =>语法错误如何解决

问题原因

Ruby 不支持在 Hash 字面量(即{}包裹的哈希定义结构)内部直接使用if条件语句动态增删键值对,你当前的写法违反了Ruby语法规范,所以会抛出语法错误。

解决方案

你可以在调用deliver之前先动态构建所有需要的多语言哈希,再将构建好的哈希作为参数传入deliver即可。
同时注意你原有代码里存在多语言配置和环境变量对应错误的问题(比如隐私政策的中文配置错误取了英文环境变量、加泰罗尼亚语配置错误取了西班牙语环境变量等),修改后的完整代码如下:

default_platform(:ios)

platform :ios do
  desc "TEST"
  lane :testEnv do
    # 动态构建release_notes哈希
    release_notes = {}
    release_notes["es-ES"] = ENV['RELEASE_NOTES_ES'] if ENV['RELEASE_NOTES_ES']
    release_notes["en-GB"] = ENV['RELEASE_NOTES_EN'] if ENV['RELEASE_NOTES_EN']
    release_notes["pt-BR"] = ENV['RELEASE_NOTES_PT'] if ENV['RELEASE_NOTES_PT']
    release_notes["zh-Hans"] = ENV['RELEASE_NOTES_ZH'] if ENV['RELEASE_NOTES_ZH']
    release_notes["ca"] = ENV['RELEASE_NOTES_CA'] if ENV['RELEASE_NOTES_CA']

    # 动态构建privacy_url哈希,已修正原代码的变量对应错误
    privacy_url = {}
    privacy_url["es-ES"] = ENV['PRIVACY_URL_ES'] if ENV['PRIVACY_URL_ES']
    privacy_url["en-GB"] = ENV['PRIVACY_URL_EN'] if ENV['PRIVACY_URL_EN']
    privacy_url["pt-BR"] = ENV['PRIVACY_URL_PT'] if ENV['PRIVACY_URL_PT']
    privacy_url["zh-Hans"] = ENV['PRIVACY_URL_ZH'] if ENV['PRIVACY_URL_ZH']
    privacy_url["ca"] = ENV['PRIVACY_URL_CA'] if ENV['PRIVACY_URL_CA']

    # 动态构建support_url哈希,已修正原代码的变量对应错误
    support_url = {}
    support_url["es-ES"] = ENV['SUPPORT_URL_ES'] if ENV['SUPPORT_URL_ES']
    support_url["en-GB"] = ENV['SUPPORT_URL_EN'] if ENV['SUPPORT_URL_EN']
    support_url["pt-BR"] = ENV['SUPPORT_URL_PT'] if ENV['SUPPORT_URL_PT']
    support_url["zh-Hans"] = ENV['SUPPORT_URL_ZH'] if ENV['SUPPORT_URL_ZH']
    support_url["ca"] = ENV['SUPPORT_URL_CA'] if ENV['SUPPORT_URL_CA']

    # 动态构建name哈希
    app_name = {}
    app_name["es-ES"] = ENV['APP_NAME'] if ENV['DESCRIPTION_ES']
    app_name["en-GB"] = ENV['APP_NAME'] if ENV['DESCRIPTION_EN']
    app_name["pt-BR"] = ENV['APP_NAME'] if ENV['DESCRIPTION_PT']
    app_name["zh-Hans"] = ENV['APP_NAME'] if ENV['DESCRIPTION_ZH']
    app_name["ca"] = ENV['APP_NAME'] if ENV['DESCRIPTION_CA']

    # 动态构建description哈希
    description = {}
    description["es-ES"] = ENV['DESCRIPTION_ES'] if ENV['DESCRIPTION_ES']
    description["en-GB"] = ENV['DESCRIPTION_EN'] if ENV['DESCRIPTION_EN']
    description["pt-BR"] = ENV['DESCRIPTION_PT'] if ENV['DESCRIPTION_PT']
    description["zh-Hans"] = ENV['DESCRIPTION_ZH'] if ENV['DESCRIPTION_ZH']
    description["ca"] = ENV['DESCRIPTION_CA'] if ENV['DESCRIPTION_CA']

    # 动态构建keywords哈希,已修正原代码的变量对应错误
    keywords = {}
    keywords["en-GB"] = ENV['KEYWORDS_EN'] if ENV['KEYWORDS_EN']
    keywords["es-ES"] = ENV['KEYWORDS_ES'] if ENV['KEYWORDS_ES']
    keywords["pt-BR"] = ENV['KEYWORDS_PT'] if ENV['KEYWORDS_PT']
    keywords["zh-Hans"] = ENV['KEYWORDS_ZH'] if ENV['KEYWORDS_ZH']
    keywords["ca"] = ENV['KEYWORDS_CA'] if ENV['KEYWORDS_CA']

    deliver(
      app_version: ENV['APP_VERSION'],
      app_rating_config_path: "./fastlane/app_store_rating_config.json",
      submit_for_review: false,
      screenshots_path: "./fastlane/screenshots/",
      metadata_path: "./fastlane/metadata/",
      force: true,
      #overwrite_screenshots: true,
      price_tier: 0,
      build_number: ENV['BITRISE_BUILD_NUMBER'],
      precheck_include_in_app_purchases: false,
      copyright: "#{Time.now.year}",
      primary_category: "EDUCATION",
      secondary_category: "BOOKS",
      automatic_release: false,
      release_notes: release_notes,
      privacy_url: privacy_url,
      support_url: support_url,
      name: app_name,
      description: description,
      keywords: keywords,
      submission_information: {
        add_id_info_uses_idfa: false,
        content_rights_contains_third_party_content: false,
        export_compliance_platform: 'ios',
        export_compliance_compliance_required: false,
        export_compliance_encryption_updated: false,
        export_compliance_uses_encryption: false,
        content_rights_has_rights: false
      },
      app_review_information: {
        first_name: "Name",
        last_name: "Surname",
        phone_number: "+34 99999999",
        email_address: "name@mail.com",
        demo_user: ENV['DEMO_USER'],
        demo_password: ENV['DEMO_PASS'],
      }
    )
  end
end

内容的提问来源于stack exchange,提问作者Carlos Soler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 15:27:00