iosrubyfastlanebitrise

syntax error, unexpected =>, expecting end


I'm trying to add an if statement to "deliver" action of fastlane.

I use fastlane with bitrise and the problem I'm having is when I add the if statement to a working sentence it always throws this syntax error: "syntax error, unexpected =>, expecting end"

This is the code I have of the 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

This is my complete Fastfile, the problem is that in my project we have a lot of targets for different clients, each of them with different languages. What I did was to save this information on environment varibles on the bitrise machine (they are defined on the bitrise yml).

As you see I'm trying to add different languages depending on the environment variable, if it exists write this language too, but it is not working. I don't exactly know where is the problem because when I have it without ifs, it works as this example:

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'],
        },

This piece of code works correctly.

I don't have so much knowledge about ruby syntax, so maybe with your help I can achieve this, it would save me lot of time.

Thank you!


Solution

  • Unfortunately, this is not valid ruby syntax:

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

    There are several ways you could add conditional elements to a hash in ruby, such as:

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

    Or:

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

    Or:

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

    ...But for your use case, since all you're trying to do is delete hash elements with a nil value, I would make use of 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