# Copyright 2016-2020 the Tectonic Project
# Licensed under the MIT License.
#
# Azure Pipelines template for a standard build-and-test workflow once
# dependencies have been set up. Besides the parameters, the following variables
# are expected:
#
# - TARGET: the build target triple (e.g. "x86_86-unknown-linux-gnu")
# - TOOLCHAIN: the rust toolchain type (e.g., "stable",
#   "beta-x86_64-pc-windows-msvc")

parameters:
- name: canaryBuild
  type: boolean
  default: false
- name: primaryBuild
  type: boolean
  default: false
- name: defaultFeatures
  type: boolean
  default: true
- name: explicitFeatures
  type: string
  default: ''

steps:
# We use two variables for feature flagging just because I'm worried about
# quoting the `--features` argument, which will contain spaces.
- bash: |
    ffs=
    fts=

    if [[ $DEFAULT_FEATURES_FLAG == False ]] ; then
      ffs="--no-default-features"
    fi

    if [[ $EXPLICIT_FEATURES == _all_ ]] ; then
      ffs="--all-features"
    else
      fts="$EXPLICIT_FEATURES"
    fi

    echo "Cargo features for this build: $ffs --features=\"$fts\""
    echo "##vso[task.setvariable variable=CARGO_FEATURES_EXPLICIT;]$fts"
    echo "##vso[task.setvariable variable=CARGO_FEATURES_FLAGS;]$ffs"
  displayName: Set feature flags
  env:
    DEFAULT_FEATURES_FLAG: ${{ parameters.defaultFeatures }}
    EXPLICIT_FEATURES: ${{ parameters.explicitFeatures }}

- bash: cargo build --all --release $CARGO_FEATURES_FLAGS --features="$CARGO_FEATURES_EXPLICIT" -v
  displayName: "cargo build"

- bash: cargo test --all --release $CARGO_FEATURES_FLAGS --features="$CARGO_FEATURES_EXPLICIT"
  displayName: "cargo test"

# For non-canary builds, export artifacts.

- ${{ if eq(parameters.canaryBuild, false) }}:
  - bash: |
      git add .
      cranko release-workflow commit
      git show HEAD
    displayName: Make release commit

  - bash: |
      artifact_dir="$(Build.ArtifactStagingDirectory)/binary-$TARGET"
      mkdir -p "$artifact_dir"
      cranko cargo package-released-binaries -t $TARGET $artifact_dir -- build --release
    displayName: Package binaries

  - task: PublishPipelineArtifact@1
    displayName: Publish packaged binary artifact(s)
    inputs:
      targetPath: '$(Build.ArtifactStagingDirectory)/binary-$(TARGET)'
      artifactName: binary-$(TARGET)

  # If, further, we're the primary build, do some stuff that should only happen
  # once among all of the build jobs.

  - ${{ if eq(parameters.primaryBuild, true) }}:
    - bash: |
        artifact_dir="$(Build.ArtifactStagingDirectory)/appimage"
        mkdir -p "$artifact_dir"

        if [[ $SOURCE_BRANCH == master ]] ; then
          export TECTONIC_APPIMAGE_TAG=continuous
          export UPDATE_INFORMATION="gh-releases-zsync|tectonic-typesetting|tectonic|continuous|tectonic-*.AppImage.zsync"
        fi

        ./dist/appimage/build.sh
        cp dist/appimage/tectonic-*.AppImage* "$artifact_dir"
      displayName: Build AppImage
      env:
        SOURCE_BRANCH: ${{ variables['Build.SourceBranchName'] }}

    - task: PublishPipelineArtifact@1
      displayName: Publish AppImage artifact(s)
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/appimage'
        artifactName: appimage

    - bash: |
        artifact_dir="$(Build.ArtifactStagingDirectory)/git-release"
        mkdir -p "$artifact_dir"
        git bundle create "$artifact_dir/release.bundle" origin/master..HEAD
      displayName: Bundle release commit

    - task: PublishPipelineArtifact@1
      displayName: Publish git bundle artifact
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)/git-release'
        artifactName: git-release
