trigger:
  branches:
    include:
      - 'master'
      - 'next'

# https://developercommunity.visualstudio.com/comments/949241/view.html
pr:
  branches:
    include:
      - '*'

pool:
  vmImage: 'ubuntu-latest'

variables:
  DOCS_NEXT_CACHE_FOLDER: $(Build.SourcesDirectory)/docs/.next/cache

steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '10.x'
    displayName: 'Install Node.js'

  - script: |
      yarn install
    displayName: 'install dependencies'

  - script: |
      yarn lerna run --ignore @material-ui/icons --parallel --scope "@material-ui/*" build
    displayName: 'build @material-ui packages'

  - script: |
      cd packages/material-ui/build
      npm version 0.0.0-canary.$(Build.SourceVersion) --no-git-tag-version
      npm pack
      mv material-ui-core-0.0.0-canary.$(Build.SourceVersion).tgz ../../../material-ui-core.tgz
    displayName: 'create @material-ui/core canary distributable'

  - task: S3Upload@1
    inputs:
      regionName: 'eu-central-1'
      bucketName: 'eps1lon-material-ui'
      globExpressions: '*.tgz'
      targetFolder: 'artifacts/$(Build.SourceBranchName)/$(Build.SourceVersion)'
      filesAcl: 'public-read'
    displayName: 'Upload distributables to S3'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
    env:
      AWS_ACCESS_KEY_ID: $(AWS_ACCESS_KEY_ID)
      AWS_SECRET_ACCESS_KEY: $(AWS_SECRET_ACCESS_KEY)

  - task: PublishPipelineArtifact@1
    inputs:
      artifactName: 'canaries'
      targetPath: 'material-ui-core.tgz'

  - task: Cache@2
    inputs:
      key: 'yarn | "$(Agent.OS)" | yarn.lock'
      restoreKeys: |
        yarn | "$(Agent.OS)"
        yarn
      path: $(DOCS_NEXT_CACHE_FOLDER)
    displayName: Cache nextjs build

  - script: |
      mkdir -p scripts/sizeSnapshot/build	
      yarn docs:build | tee scripts/sizeSnapshot/build/docs.next
    displayName: 'build docs for size snapshot'
    env:
      NODE_OPTIONS: "--max_old_space_size=4096"

  - script: |
      yarn size:snapshot
    displayName: 'create a size snapshot'

  - task: AmazonWebServices.aws-vsts-tools.S3Upload.S3Upload@1
    displayName: 'persist size snapshot'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
    inputs:
      awsCredentials: 's3 artifacts'
      regionName: 'eu-central-1'
      bucketName: 'eps1lon-material-ui'
      sourceFolder: '$(System.DefaultWorkingDirectory)'
      globExpressions: 'size-snapshot.json'
      targetFolder: 'artifacts/$(Build.SourceBranchName)/$(Build.SourceVersion)/'
      filesAcl: 'public-read'
      contentType: application/json
      logRequest: true
      logResponse: true

  - task: AmazonWebServices.aws-vsts-tools.S3Upload.S3Upload@1
    displayName: 'symlink size-snapshot to latest'
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
    inputs:
      awsCredentials: 's3 artifacts'
      regionName: 'eu-central-1'
      bucketName: 'eps1lon-material-ui'
      sourceFolder: '$(System.DefaultWorkingDirectory)'
      globExpressions: 'size-snapshot.json'
      targetFolder: 'artifacts/$(Build.SourceBranchName)/latest/'
      filesAcl: 'public-read'
      contentType: application/json
      logRequest: true
      logResponse: true

  - script: |
      yarn danger ci
    displayName: 'run danger on PRs'
    condition: and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
    env:
      DANGER_GITHUB_API_TOKEN: $(GITHUB_API_TOKEN)
