语法错误,意外 =>,期待结束

syntax error, unexpected =>, expecting end

我正在尝试添加一个 if 语句来“传递”fastlane 的操作。

我将 fastlane 与 bitrise 一起使用,我遇到的问题是,当我将 if 语句添加到工作语句时,它总是抛出此语法错误:“语法错误,意外 =>,期待结束”

这是我拥有的 Fastfile 代码:

default_platform(:ios)

platform :ios do
  desc "TEST"
    lane :testEnv do

        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: {
                if ENV['RELEASE_NOTES_ES'] != nil
                    "es-ES" => ENV['RELEASE_NOTES_ES'],
                end
                if ENV['RELEASE_NOTES_EN'] != nil
                    "en-GB" => ENV['RELEASE_NOTES_EN'],
                end
                if ENV['RELEASE_NOTES_PT'] != nil
                    "pt-BR" => ENV['RELEASE_NOTES_PT'],
                end
                if ENV['RELEASE_NOTES_ZH'] != nil
                    "zh-Hans" => ENV['RELEASE_NOTES_ZH'],
                end
                if ENV['RELEASE_NOTES_CA'] != nil
                    "ca" => ENV['RELEASE_NOTES_CA'],
                end
            },

            privacy_url: {
                if ENV['PRIVACY_URL_ES'] != nil
                    "es-ES" => ENV['PRIVACY_URL_ES']
                end
                if ENV['PRIVACY_URL_EN'] != nil
                    "en-GB" => ENV['PRIVACY_URL_EN']
                end
                if ENV['PRIVACY_URL_PT'] != nil
                    "pt-BR" => ENV['PRIVACY_URL_PT']
                end
                if ENV['PRIVACY_URL_EN'] != nil
                    "zh-Hans" => ENV['PRIVACY_URL_EN']
                end
                if ENV['PRIVACY_URL_ES'] != nil
                    "ca" => ENV['PRIVACY_URL_ES']
                end
            },

            support_url: {
                if ENV['SUPPORT_URL_ES'] != nil
                    "es-ES" => ENV['SUPPORT_URL_ES'],
                end
                if ENV['SUPPORT_URL_EN'] != nil
                    "en-GB" => ENV['SUPPORT_URL_EN'],
                end
                if ENV['SUPPORT_URL_PT'] != nil
                    "pt-BR" => ENV['SUPPORT_URL_PT'],
                end
                if ENV['SUPPORT_URL_EN'] != nil
                    "zh-Hans" => ENV['SUPPORT_URL_EN'],
                end
                if ENV['SUPPORT_URL_ES'] != nil
                    "ca" => ENV['SUPPORT_URL_ES'],
                end
            },

            #APP METADATA INFORMATION
            name: {
                if ENV['DESCRIPTION_ES'] != nil
                    "es-ES" => ENV['APP_NAME'],
                end
                if ENV['DESCRIPTION_EN'] != nil
                    "en-GB" => ENV['APP_NAME'],
                end
                if ENV['DESCRIPTION_PT'] != nil
                    "pt-BR" => ENV['APP_NAME'],
                end
                if ENV['DESCRIPTION_ZH'] != nil
                    "zh-Hans" => ENV['APP_NAME'],
                end
                if ENV['DESCRIPTION_CA'] != nil
                    "ca" => ENV['APP_NAME'],
                end
            },

            description: {
                if ENV['DESCRIPTION_ES'] != nil
                    "es-ES" => ENV['DESCRIPTION_ES'],
                end
                if ENV['DESCRIPTION_EN'] != nil
                    "en-GB" => ENV['DESCRIPTION_EN'],
                end
                if ENV['DESCRIPTION_PT'] != nil
                    "pt-BR" => ENV['DESCRIPTION_PT'],
                end
                if ENV['DESCRIPTION_ZH'] != nil
                    "zh-Hans" => ENV['DESCRIPTION_ZH'],
                end
                if ENV['DESCRIPTION_CA'] != nil
                    "ca" => ENV['DESCRIPTION_CA'],
                end
            },

            keywords: {
                if ENV['KEYWORDS_EN'] != nil
                    "en-GB" => ENV['KEYWORDS_EN'],
                end
                if ENV['KEYWORDS_ES'] != nil
                    "es-ES" => ENV['KEYWORDS_ES'],
                end
                if ENV['KEYWORDS_PT'] != nil
                    "pt-BR" => ENV['KEYWORDS_PT'],
                end
                if ENV['KEYWORDS_ZH'] != nil
                    "zh-Hans" => ENV['KEYWORDS_ZH'],
                end
                if ENV['KEYWORDS_CA'] != nil
                    "ca" => ENV['KEYWORDS_ES'],
                end
            },

            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

这是我完整的 Fastfile,问题是在我的项目中我们有很多针对不同客户的目标,每个客户都有不同的语言。我所做的是将此信息保存在 bitrise 机器上的环境变量中(它们在 bitrise yml 中定义)。

如您所见,我正在尝试根据环境变量添加不同的语言,如果它存在也写这种语言,但它不起作用。我不完全知道问题出在哪里,因为当我没有 ifs 时,它就像这个例子一样工作:

description: {
                "es-ES" => ENV['DESCRIPTION_ES'],
                "en-GB" => ENV['DESCRIPTION_EN'],
                "pt-BR" => ENV['DESCRIPTION_PT'],
                "zh-Hans" => ENV['DESCRIPTION_ZH'],
                "ca" => ENV['DESCRIPTION_CA'],
        },

这段代码工作正常。

我对 ruby 语法了解不多,所以也许在您的帮助下我可以做到这一点,这会节省我很多时间。

谢谢!

不幸的是,这是无效的ruby语法:

{
  if ENV['RELEASE_NOTES_ES'] != nil
    "es-ES" => ENV['RELEASE_NOTES_ES'],
  end
}

several ways you could add conditional elements to a hash in ruby,如:

{}.tap do |my_hash| 
  my_hash[:a] = 'a'
  my_hash[:b] = 'b' if condition
end

或:

{:a => 'a'}.merge( condition ? {:b => 'b'} : {} )

或:

hash = { a: 'a', b: 'b' }
hash[:c] = 'c' if condition

...但是对于您的用例,由于您要做的只是删除具有 nil 值的哈希元素,因此我会使用 Hash#compact:

release_notes: {
  "es-ES" => ENV['RELEASE_NOTES_ES'],
  "en-GB" => ENV['RELEASE_NOTES_EN'],
  "pt-BR" => ENV['RELEASE_NOTES_PT'],
  "zh-Hans" => ENV['RELEASE_NOTES_ZH'],
  "ca" => ENV['RELEASE_NOTES_CA']
}.compact