---
format_version: 4
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
trigger_map:
- push_branch: dev
  workflow: internal
- push_branch: release/next
  workflow: test_external
- push_branch: main
  workflow: external
- pull_request_source_branch: "*"
  workflow: test
workflows:
  test:
    steps:
    - build-number-adjust:
        inputs:
        - change: ''
        - increase: "$FW_BUILD_NUMBER_OFFSET"
        - decrease: ''
    - activate-ssh-key:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - script:
        title: Set GEM_HOME env var
        inputs:
        - content: |-
            #!/bin/bash
            set -ev
            envman add --key GEM_HOME --value "$(gem environment gemdir)"
    - git-clone: {}
    - recreate-user-schemes:
        inputs:
        - project_path: "$BITRISE_PROJECT_PATH"
    - cocoapods-install: {}
    - xcode-test:
        inputs:
        - generate_code_coverage_files: 'yes'
    - git::https://github.com/FutureWorkshops/steps-github-status.git@master:
        title: Git Hub status
        run_if: .IsCI
        inputs:
        - auth_token: "$FW_GITHUB_STATUS_AUTH_TOKEN"
        - commit_hash: "$GIT_CLONE_COMMIT_HASH"
    - deploy-to-bitrise-io:
        inputs:
        - notify_email_list: ''
  _add_iconoverlay:
    steps:
    - git::https://github.com/FutureWorkshops/bitrise-step-icon-overlay.git@master:
        title: Git Commit Icon Overlay
        inputs:
        - iconsbundle_name: AppIcon.appiconset
        - overlay_text: "$GIT_CLONE_COMMIT_HASH"
  internal:
    envs:
    - FW_ENVIRONMENT: STAGING
      opts:
        is_expand: true
    - EXPORT_TYPE: enterprise
      opts:
        is_expand: true
    - CODESIGN_IDENTITY: 'iPhone Distribution: Future Workshops Limited'
      opts:
        is_expand: true
    - KEYCHAIN_NAME: fw_enterprise.p12
      opts:
        is_expand: true
    - PRODUCT_BUNDLE_IDENTIFIER: com.futureworkshops.<%= config[:project_name] %>
      opts:
        is_expand: true
    - PROVISIONING_PROFILE_NAME: <%= config[:project_name] %> Internal
      opts:
        is_expand: true
    - DEVPORTAL_TEAM: BQHMJ2C6RL
      opts:
        is_expand: true
    - BITRISE_SCHEME: <%= config[:project_name] %>
    - SWIFT_FLAGS: '"-DSTAGING"'
    after_run:
    - _preflight
    - _add_iconoverlay
    - _build
  test_external:
    envs:
    - FW_ENVIRONMENT: STAGING
      opts:
        is_expand: true
    - EXPORT_TYPE: app-store
      opts:
        is_expand: true
    - CODESIGN_IDENTITY: 'Apple Distribution: Future Workshops Ltd (F94732585Z)'
      opts:
        is_expand: true
    - KEYCHAIN_NAME: fw_ltd.p12
      opts:
        is_expand: true
    - PRODUCT_BUNDLE_IDENTIFIER: com.futureworkshops.<%= config[:project_name] %>.external
      opts:
        is_expand: true
    - PROVISIONING_PROFILE_NAME: <%= config[:project_name] %> External
      opts:
        is_expand: true
    - PROVISIONING_PROFILE_TYPE: app-store
      opts:
        is_expand: true
    - ITUNESCONNECT_APPID: 
      opts:
        is_expand: true
    - DEVPORTAL_TEAM: F94732585Z
      opts:
        is_expand: true
    - ITUNES_TEAM_ID: '462819'
    - BITRISE_SCHEME: <%= config[:project_name] %>
    - SWIFT_FLAGS: '"-DSTAGING"'
    after_run:
    - _preflight
    - _build
    - _deploy_itunes
  external:
    envs:
    - FW_ENVIRONMENT: PRODUCTION
      opts:
        is_expand: true
    - EXPORT_TYPE: app-store
      opts:
        is_expand: true
    - CODESIGN_IDENTITY: 'Apple Distribution: Future Workshops Ltd (F94732585Z)'
      opts:
        is_expand: true
    - KEYCHAIN_NAME: fw_ltd.p12
      opts:
        is_expand: true
    - PRODUCT_BUNDLE_IDENTIFIER: com.futureworkshops.<%= config[:project_name] %>.external
      opts:
        is_expand: true
    - PROVISIONING_PROFILE_NAME: <%= config[:project_name] %> External
      opts:
        is_expand: true
    - PROVISIONING_PROFILE_TYPE: app-store
      opts:
        is_expand: true
    - ITUNESCONNECT_APPID: ''
      opts:
        is_expand: true
    - DEVPORTAL_TEAM: F94732585Z
      opts:
        is_expand: true
    - FW_BUILD_NUMBER_OFFSET: '0'
    - ITUNES_TEAM_ID: '462819'
    - BITRISE_SCHEME: <%= config[:project_name] %>
    - SWIFT_FLAGS: '"-DPRODUCTION"'
    after_run:
    - _preflight
    - _build
    - _deploy_itunes
  _preflight:
    steps:
    - script:
        title: Set GEM_HOME env var
        inputs:
        - content: |-
            #!/bin/bash
            set -ev
            envman add --key GEM_HOME --value "$(gem environment gemdir)"
    - build-number-adjust:
        inputs:
        - change: ''
        - increase: "$FW_BUILD_NUMBER_OFFSET"
        - decrease: ''
    - activate-ssh-key:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - git-clone: {}
    - recreate-user-schemes:
        inputs:
        - project_path: "$BITRISE_PROJECT_PATH"
    - install-bundler@1.0.0: {}
  _build:
    steps:
    - git::https://github.com/FutureWorkshops/bitrise-step-extract-xcode-settings@master:
        title: Extract Info.plist path
        inputs:
        - xcode_project_path: "$FW_PROJECT_DIR/$FW_XCODEPROJ_NAME"
        - target: "$BITRISE_SCHEME"
        - configuration: Release
        - xcode_setting_key: INFOPLIST_FILE
        - target_variable: INFO_PLIST_PATH
    - set-xcode-build-number:
        inputs:
        - plist_path: "$FW_PROJECT_DIR/$INFO_PLIST_PATH"
        - build_version_offset: ''
        - build_short_version_string: "$VERSION_NUMBER"
    - git::https://github.com/FutureWorkshops/bitrise-step-s3-download.git@master:
        title: Download keychain
        run_if: .IsCI
        inputs:
        - aws_access_key: "$AWS_ACCESS_KEY_ID"
        - aws_secret_access_key: "$AWS_SECRET_ACCESS_KEY"
        - s3_bucket: fw.ci.certificates
        - s3_filepath: "$KEYCHAIN_NAME"
    - certificate-and-profile-installer:
        run_if: .IsCI
        inputs:
        - certificate_url: file://./$KEYCHAIN_NAME
        - provisioning_profile_url: "$BITRISE_PROVISION_URL"
    - git::https://github.com/FutureWorkshops/bitrise-step-change-xcode-settings@master:
        title: Change Bundle ID
        inputs:
        - xcode_project_path: "$FW_PROJECT_DIR/$FW_XCODEPROJ_NAME"
        - target: "$BITRISE_SCHEME"
        - configuration: Release
        - xcode_setting_keys: PRODUCT_BUNDLE_IDENTIFIER
        - new_values: "$PRODUCT_BUNDLE_IDENTIFIER"
    - git::https://github.com/FutureWorkshops/bitrise-step-change-xcode-settings@master:
        title: Set Provisioning Profile and team ID
        inputs:
        - xcode_project_path: "$FW_PROJECT_DIR/$FW_XCODEPROJ_NAME"
        - target: "$BITRISE_SCHEME"
        - configuration: Release
        - xcode_setting_keys: PROVISIONING_PROFILE_SPECIFIER | DEVELOPMENT_TEAM | CODE_SIGN_IDENTITY
        - new_values: "$PROVISIONING_PROFILE_NAME | $DEVPORTAL_TEAM | $CODESIGN_IDENTITY"
    - cocoapods-install: {}
    - script:
        title: Set environment
        inputs:
        - content: |-
            #!/bin/bash
            # fail if any commands fails
            set -e
            # debug log
            set -x
            xconfig="$BITRISE_SOURCE_DIR/$FW_PROJECT_DIR/$PROJECT_NAME/Supporting Files/fw-shared.xcconfig"

            echo "Updating xcconfig file $xconfig"
            echo "" >> "$xconfig"
            echo "OTHER_SWIFT_FLAGS = $SWIFT_FLAGS" >> "$xconfig"
            echo "" >> "$xconfig"
            echo "GCC_PREPROCESSOR_DEFINITIONS = \"CI_BUILD $FW_ENVIRONMENT\"" >> "$xconfig"
            echo "" >> "$xconfig"

            echo "Final file"
            pbcopy < "$xconfig"
            pbpaste
    - xcode-archive@3:
        inputs:
        - upload_bitcode: 'no'
        - compile_bitcode: 'no'
        - team_id: "$DEVPORTAL_TEAM"
        - configuration: Release
        - output_dir: "$BITRISE_DEPLOY_DIR/$PROJECT_NAME"
        - force_team_id: ''
        - force_code_sign_identity: "$CODESIGN_IDENTITY"
        - force_provisioning_profile_specifier: ''
        - force_provisioning_profile: ''
        - is_clean_build: 'yes'
        - artifact_name: "$PROJECT_NAME-$BITRISE_BUILD_NUMBER"
        - export_method: "$EXPORT_TYPE"
        - custom_export_options_plist_content: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE
            plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist
            version=\"1.0\">\n<dict>\n\t<key>method</key>\n\t<string>$EXPORT_TYPE</string>\n\t<key>teamID</key>\n\t<string>$DEVPORTAL_TEAM</string>\n\t<key>thinning</key>\n\t<string>&lt;none&gt;</string>\n\t<key>signingStyle</key>\n\t<string>manual</string>\n\t<key>signingCertificate</key>\n\t<string>$CODESIGN_IDENTITY</string>\n\t<key>provisioningProfiles</key>\n\t<dict>\n\t\t<key>$PRODUCT_BUNDLE_IDENTIFIER</key>\n\t\t<string>$PROVISIONING_PROFILE_NAME</string>\n\t</dict>\n</dict>\n</plist>"
    - git::https://github.com/FutureWorkshops/steps-github-status.git@master:
        title: Git Hub status
        run_if: .IsCI
        inputs:
        - auth_token: "$FW_GITHUB_STATUS_AUTH_TOKEN"
        - commit_hash: "$GIT_CLONE_COMMIT_HASH"
    - deploy-to-bitrise-io:
        title: Deploy to Bitrise.io - IPA
        inputs:
        - is_enable_public_page: 'false'
    - deploy-to-bitrise-io:
        title: Deploy to Bitrise.io - IPA
        inputs:
        - deploy_path: "$BITRISE_DEPLOY_DIR/$PROJECT_NAME"
        - is_enable_public_page: 'false'
  _deploy_itunes:
    steps:
    - git::https://github.com/FutureWorkshops/bitrise-step-s3-download.git@master:
        title: Download API Key
        run_if: .IsCI
        inputs:
        - aws_access_key: "$AWS_ACCESS_KEY_ID"
        - aws_secret_access_key: "$AWS_SECRET_ACCESS_KEY"
        - s3_bucket: fw.ci.certificates
        - output_location: "$BITRISE_SOURCE_DIR"
        - s3_filepath: "$FW_ITUNES_CONNECT_API_KEY"
    - deploy-to-itunesconnect-application-loader@0:
        run_if: .IsCI
        inputs:
        - api_issuer: "$FW_ITUNES_CONNECT_API_KEY_ISSUER_ID"
        - api_key_path: file://$BITRISE_SOURCE_DIR/$FW_ITUNES_CONNECT_API_KEY
app:
  envs:
  - opts:
      is_expand: false
    BITRISE_PROJECT_PATH: <%= config[:project_name] %>.xcworkspace
  - opts:
      is_expand: false
    BITRISE_SCHEME: <%= config[:project_name] %>
  - opts:
      is_expand: false
    FW_PROJECT_DIR: <%= config[:project_name] %>
  - opts:
      is_expand: true
    FW_XCODEPROJ_NAME: <%= config[:project_name] %>.xcodeproj
  - opts:
      is_expand: true
    FW_BUILD_NUMBER_OFFSET: '0'
  - PROJECT_NAME: <%= config[:project_name] %>
    opts:
      is_expand: true
  - VERSION_NUMBER: 1.0.0