steps:
- script: |
    mkdir -p .build
    echo -n $BUILD_SOURCEVERSION > .build/commit
    echo -n $VSCODE_QUALITY > .build/quality
  displayName: Prepare cache flag

- task: 1ESLighthouseEng.PipelineArtifactCaching.RestoreCacheV1.RestoreCache@1
  inputs:
    keyfile: 'build/.cachesalt, .build/commit, .build/quality'
    targetfolder: '.build, out-build, out-vscode-min, out-vscode-reh-min, out-vscode-reh-web-min'
    vstsFeed: 'npm-vscode'
    platformIndependent: true
    alias: 'Compilation'
    dryRun: true

- task: NodeTool@0
  inputs:
    versionSpec: "12.13.0"
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- task: geeklearningio.gl-vsts-tasks-yarn.yarn-installer-task.YarnInstaller@2
  inputs:
    versionSpec: "1.x"
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- task: AzureKeyVault@1
  displayName: 'Azure Key Vault: Get Secrets'
  inputs:
    azureSubscription: 'vscode-builds-subscription'
    KeyVaultName: vscode
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    cat << EOF > ~/.netrc
    machine github.com
    login vscode
    password $(github-distro-mixin-password)
    EOF

    git config user.email "vscode@microsoft.com"
    git config user.name "VSCode"
  displayName: Prepare tooling
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    git remote add distro "https://github.com/$(VSCODE_MIXIN_REPO).git"
    git fetch distro
    git merge $(node -p "require('./package.json').distro")
  displayName: Merge distro
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- task: 1ESLighthouseEng.PipelineArtifactCaching.RestoreCacheV1.RestoreCache@1
  inputs:
    keyfile: 'build/.cachesalt, .yarnrc, remote/.yarnrc, **/yarn.lock, !**/node_modules/**/yarn.lock, !**/.*/**/yarn.lock'
    targetfolder: '**/node_modules, !**/node_modules/**/node_modules'
    vstsFeed: 'npm-vscode'
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    CHILD_CONCURRENCY=1 yarn --frozen-lockfile
  displayName: Install dependencies
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'), ne(variables['CacheRestored'], 'true'))

- task: 1ESLighthouseEng.PipelineArtifactCaching.SaveCacheV1.SaveCache@1
  inputs:
    keyfile: 'build/.cachesalt, .yarnrc, remote/.yarnrc, **/yarn.lock, !**/node_modules/**/yarn.lock, !**/.*/**/yarn.lock'
    targetfolder: '**/node_modules, !**/node_modules/**/node_modules'
    vstsFeed: 'npm-vscode'
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'), ne(variables['CacheRestored'], 'true'))

- script: |
    set -e
    yarn generate-github-config
  displayName: Generate GitHub config
  condition: succeeded()
  env:
    OSS_GITHUB_ID: "a5d3c261b032765a78de"
    OSS_GITHUB_SECRET: $(oss-github-client-secret)
    INSIDERS_GITHUB_ID: "31f02627809389d9f111"
    INSIDERS_GITHUB_SECRET: $(insiders-github-client-secret)
    STABLE_GITHUB_ID: "baa8a44b5e861d918709"
    STABLE_GITHUB_SECRET: $(stable-github-client-secret)
    EXPLORATION_GITHUB_ID: "94e8376d3a90429aeaea"
    EXPLORATION_GITHUB_SECRET: $(exploration-github-client-secret)
    VSO_GITHUB_ID: "3d4be8f37a0325b5817d"
    VSO_GITHUB_SECRET: $(vso-github-client-secret)
    VSO_PPE_GITHUB_ID: "eabf35024dc2e891a492"
    VSO_PPE_GITHUB_SECRET: $(vso-ppe-github-client-secret)
    VSO_DEV_GITHUB_ID: "84383ebd8a7c5f5efc5c"
    VSO_DEV_GITHUB_SECRET: $(vso-dev-github-client-secret)
    GITHUB_APP_ID: "Iv1.ae51e546bef24ff1"
    GITHUB_APP_SECRET: $(github-app-client-secret)

- script: |
    set -e
    yarn postinstall
  displayName: Run postinstall scripts
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'), eq(variables['CacheRestored'], 'true'))

# Mixin must run before optimize, because the CSS loader will
# inline small SVGs
- script: |
    set -e
    node build/azure-pipelines/mixin
  displayName: Mix in quality
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    yarn gulp hygiene
    yarn monaco-compile-check
    yarn valid-layers-check
  displayName: Run hygiene, monaco compile & valid layers checks
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'), eq(variables['VSCODE_STEP_ON_IT'], 'false'))

- script: |
    set -
    ./build/azure-pipelines/common/extract-telemetry.sh
  displayName: Extract Telemetry
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    AZURE_WEBVIEW_STORAGE_ACCESS_KEY="$(vscode-webview-storage-key)" \
    ./build/azure-pipelines/common/publish-webview.sh
  displayName: Publish Webview
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    yarn gulp compile-build
    yarn gulp compile-extensions-build
    yarn gulp minify-vscode
    yarn gulp minify-vscode-reh
    yarn gulp minify-vscode-reh-web
  displayName: Compile
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    AZURE_STORAGE_ACCESS_KEY="$(ticino-storage-key)" \
      node build/azure-pipelines/upload-sourcemaps
  displayName: Upload sourcemaps
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- script: |
    set -e
    VERSION=`node -p "require(\"./package.json\").version"`
    AZURE_DOCUMENTDB_MASTERKEY="$(builds-docdb-key-readwrite)" \
      node build/azure-pipelines/common/createBuild.js $VERSION
  displayName: Create build
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))

- task: 1ESLighthouseEng.PipelineArtifactCaching.SaveCacheV1.SaveCache@1
  inputs:
    keyfile: 'build/.cachesalt, .build/commit, .build/quality'
    targetfolder: '.build, out-build, out-vscode-min, out-vscode-reh-min, out-vscode-reh-web-min'
    vstsFeed: 'npm-vscode'
    platformIndependent: true
    alias: 'Compilation'
  condition: and(succeeded(), ne(variables['CacheExists-Compilation'], 'true'))
