# Steps for restoring project cache

steps:
  - bash: 'mkdir -p $(Build.StagingDirectory)'
    condition: and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch'])))
    displayName: '[Cache][Publish] Create cache directory'

  # TODO: This can be done in parallel with installing node, and esy
  # (which would save a bunch of time on windows)
  - task: Bash@3
    condition: and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch'])))
    displayName: '[Cache][Restore] Restoring build cache using REST API'
    continueOnError: true
    inputs:
      targetType: 'inline' # Optional. Options: filePath, inline
      script: |
        # If org name is reasonml then REST_BASE will be: https://dev.azure.com/reasonml/
        REST_BASE="${SYSTEM_TEAMFOUNDATIONCOLLECTIONURI}"
        PROJ="$SYSTEM_TEAMPROJECT"
        ART_NAME="cache-${AGENT_OS}-install"
        fetchLatestBuild() {
          PREFIX="branchName=refs%2Fheads%2F"
          BRANCH=${PREFIX}${SYSTEM_PULLREQUEST_TARGETBRANCH}
          FILTER='deletedFilter=excludeDeleted&statusFilter=completed&resultFilter=succeeded'
          LATEST='queryOrder=finishTimeDescending&$top=1'
          REST_BUILDS="$REST_BASE/$PROJ/_apis/build/builds?${FILTER}&${BRANCH}&${LATEST}&api-version=4.1"
          echo "Rest call for builds: $REST_BUILDS"
          REST_BUILDS_RESP=$(curl "$REST_BUILDS")
          if [[ $REST_BUILDS_RESP =~ (\"web\":\{\"href\":\")([^\"]*) ]]; then LATEST_BUILD_PAGE="${BASH_REMATCH[2]}"; else LATEST_BUILD_PAGE=""; fi
          if [[ $REST_BUILDS_RESP =~ (\"badge\":\{\"href\":\")([^\"]*) ]]; then LATEST_BUILD_BADGE="${BASH_REMATCH[2]}"; else LATEST_BUILD_BADGE=""; fi
          if [[ $REST_BUILDS_RESP =~ (\"id\":)([^,]*) ]]; then LATEST_BUILD_ID="${BASH_REMATCH[2]}"; else LATEST_BUILD_ID=""; fi
        }
        fetchLatestBuild
        fetchArtifactURL() {
          REST_ART="$REST_BASE/$PROJ/_apis/build/builds/$LATEST_BUILD_ID/artifacts?artifactName=$ART_NAME&api-version=4.1"
          echo "Rest call for artifacts: $REST_ART"
          if [[ $(curl $REST_ART) =~ (downloadUrl\":\")([^\"]*) ]]; then LATEST_ART_URL="${BASH_REMATCH[2]}"; else LATEST_ART_URL=""; fi
        }
        downloadArtifactAndContinue() {
          if [ -z "$LATEST_ART_URL" ]
          then
            echo "No latest artifact for merge-target branch found at URL $REST_ART"
          else
            curl "$LATEST_ART_URL" > "${BUILD_STAGINGDIRECTORY}/$ART_NAME.zip"
            PROJECT_DIR=$PWD
            cd $BUILD_STAGINGDIRECTORY
            unzip "$ART_NAME.zip"
            echo "Using Dependency cache for buildID: $LATEST_BUILD_ID"
            echo "Build log for build that produced the cache: $LATEST_BUILD_PAGE"
            echo "Build badge for build that produced the cache: $LATEST_BUILD_BADGE"
            echo "Build artifact from build that produced the cache: $LATEST_ART_URL"
            echo "Restoring build cache into:"
            mkdir -p $ESY__CACHE_INSTALL_PATH
            echo $ESY__CACHE_INSTALL_PATH
            echo "##vso[task.setvariable variable=esy_export_dir_to_import]${BUILD_STAGINGDIRECTORY}/${ART_NAME}/_export"
            if [[ -d ${ART_NAME}/_export ]]; then
              echo "Cached builds to import from ${ART_NAME}/_export in next CI step"
              ls ${ART_NAME}/_export
              mv ${ART_NAME}/_export ${PROJECT_DIR}/_export
            else
              echo "No _export directory to import from build cache"
              echo "Here's the contents of build cache:"
              find ${BUILD_STAGINGDIRECTORY}
            fi
          fi
        }
        fetchArtifactURL
        downloadArtifactAndContinue
      
  - powershell: esy.cmd import-dependencies
    continueOnError: true
    condition: and(eq(variables['AGENT.OS'], 'Windows_NT'), and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch']))))
    displayName: "esy import-dependencies if windows (build cache from CI cache)"

  - bash: esy import-dependencies
    continueOnError: true
    condition: and(ne(variables['AGENT.OS'], 'Windows_NT'), and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch']))))
    displayName: "esy import-dependencies if not windows (build cache from CI cache)"
    
  # Remove as soon as https://github.com/esy/esy/pull/969 is resolved.
  # For now, windows won't use build cache for problematic package.
  - task: Bash@3
    continueOnError: true
    condition: and(eq(variables['AGENT.OS'], 'Windows_NT'), and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch']))))
    displayName: 'Remove ocamlfind prebuilts if on Windows'
    inputs:
      targetType: 'inline' # Optional. Options: filePath, inline
      script: |
        ls ${ESY__CACHE_INSTALL_PATH}
        rm -rf ${ESY__CACHE_INSTALL_PATH}/opam__s__ocamlfind*

  - bash: 'rm -rf _import'
    continueOnError: true
    condition: and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch'])))
    displayName: 'Remove import directory'

  - bash: 'rm -rf *'
    continueOnError: true
    workingDirectory: '$(Build.StagingDirectory)'
    condition: and(eq(variables['Build.Reason'], 'PullRequest'), and(succeeded(), ne(variables['Build.SourceBranch'], variables['System.PullRequest.TargetBranch'])))
    displayName: '[Cache][Restore] Clean up staging dir'
