parameters:
  rust_version: stable
  github:
    isPreRelease: false
    repositoryName: "$(Build.Repository.Name)"
  displayName: "Release to GitHub"
  archiveName: "$(Build.Repository.Name)"

jobs:
  - job: ${{ parameters.name }}
    displayName: ${{ parameters.displayName }}
    pool:
      vmImage: ubuntu-16.04
    steps:
      - template: install-cross-rust.yml

      - bash: |
          MY_TAG="$(Build.SourceBranch)"
          MY_TAG=${MY_TAG#refs/tags/}
          echo $MY_TAG
          echo "##vso[task.setvariable variable=build.my_tag]$MY_TAG"
          DATE="$(date +%Y-%m-%d)"
          echo "##vso[task.setvariable variable=build.date]$DATE"
        displayName: "Create date and tag variables"
      - ${{ each build_target  in parameters.targets }}:
          - ? ${{ if not(or(eq(build_target, 'x86_64-apple-darwin'), eq(build_target, 'x86_64-pc-windows-msvc'))) }}
            : - script: |
                  echo Start building ${{ build_target }}
                  cross build  --target  ${{ build_target }} --release
                  ls -l
                  ls -l target/${{ build_target }}/release/*
                displayName: Relase build for target ${{ build_target }}
              - task: CopyFiles@2
                displayName: Copy files for target ${{ build_target }}
                inputs:
                  sourceFolder: "$(Build.SourcesDirectory)/target/${{ build_target }}/release"
                  contents: ${{ parameters.contents }}
                  targetFolder: "$(Build.BinariesDirectory)/${{ build_target }}"
              - task: ArchiveFiles@2
                displayName: Gather assets
                inputs:
                  rootFolderOrFile: "$(Build.BinariesDirectory)/${{ build_target }}"
                  archiveType: "tar"
                  tarCompression: "gz"
                  archiveFile: "$(Build.ArtifactStagingDirectory)/${{ parameters.archiveName }}-$(build.my_tag)-${{ build_target }}.tar.gz"

      - task: GitHubRelease@0
        displayName: Create release
        inputs:
          gitHubConnection: ${{ parameters.github.gitHubConnection }}
          tagSource: manual
          title: "$(build.my_tag) - $(build.date)"
          tag: "$(build.my_tag)"
          assetUploadMode: replace
          action: edit
          assets: "$(Build.ArtifactStagingDirectory)/${{ parameters.archiveName }}*"
          repositoryName: ${{ parameters.github.repositoryName }}
          isPreRelease: ${{ parameters.github.isPreRelease }}

  - ${{ each build_target in parameters.targets }}:
      - ${{ if eq(build_target, 'x86_64-apple-darwin') }}:
          - job: ${{ parameters.name }}_macOS
            displayName: ${{ parameters.displayName }} (macOS)
            pool:
              vmImage: macOS-10.13
            steps:
              - template: install-rust.yml

              - bash: |
                  MY_TAG="$(Build.SourceBranch)"
                  MY_TAG=${MY_TAG#refs/tags/}
                  echo $MY_TAG
                  echo "##vso[task.setvariable variable=build.my_tag]$MY_TAG"
                  DATE="$(date +%Y-%m-%d)"
                  echo "##vso[task.setvariable variable=build.date]$DATE"
                displayName: "Create date and tag variables"
              - script: |
                  echo Start building ${{ build_target }}
                  cargo build --release
                  ls -l
                  ls -l target/release/*
                displayName: Relase build for target ${{ build_target }}
              - task: CopyFiles@2
                displayName: Copy files for target ${{ build_target }}
                inputs:
                  sourceFolder: "$(Build.SourcesDirectory)/target/release"
                  contents: ${{ parameters.contents }}
                  targetFolder: "$(Build.BinariesDirectory)/${{ build_target }}"
              - task: ArchiveFiles@2
                displayName: Gather assets
                inputs:
                  rootFolderOrFile: "$(Build.BinariesDirectory)/${{ build_target }}"
                  archiveType: "tar"
                  tarCompression: "gz"
                  archiveFile: "$(Build.ArtifactStagingDirectory)/${{ parameters.archiveName }}-$(build.my_tag)-${{ build_target }}.tar.gz"

              - task: GitHubRelease@0
                displayName: Create release
                inputs:
                  gitHubConnection: ${{ parameters.github.gitHubConnection }}
                  tagSource: manual
                  title: "$(build.my_tag) - $(build.date)"
                  tag: "$(build.my_tag)"
                  assetUploadMode: replace
                  action: edit
                  assets: "$(Build.ArtifactStagingDirectory)/${{ parameters.archiveName }}*"
                  repositoryName: ${{ parameters.github.repositoryName }}
                  isPreRelease: ${{ parameters.github.isPreRelease }}

      - ${{ if eq(build_target, 'x86_64-pc-windows-msvc') }}:
          - job: ${{ parameters.name }}_msvc
            displayName: ${{ parameters.displayName }} (Windows)
            pool:
              vmImage: vs2017-win2016
            steps:
              - template: install-rust.yml

              - bash: |
                  MY_TAG="$(Build.SourceBranch)"
                  MY_TAG=${MY_TAG#refs/tags/}
                  echo $MY_TAG
                  echo "##vso[task.setvariable variable=build.my_tag]$MY_TAG"
                  DATE="$(date +%Y-%m-%d)"
                  echo "##vso[task.setvariable variable=build.date]$DATE"
                displayName: "Create date and tag variables"
              - script: |
                  echo Start building ${{ build_target }}
                  cargo build --release
                  ls -l
                  ls -l target/release/*
                displayName: Relase build for target ${{ build_target }}
              - task: CopyFiles@2
                displayName: Copy files for target ${{ build_target }}
                inputs:
                  sourceFolder: "$(Build.SourcesDirectory)/target/release"
                  contents: ${{ parameters.contents }}
                  targetFolder: "$(Build.BinariesDirectory)/${{ build_target }}"
              - task: ArchiveFiles@2
                displayName: Gather assets
                inputs:
                  rootFolderOrFile: "$(Build.BinariesDirectory)/${{ build_target }}"
                  archiveType: "zip"
                  tarCompression: "None"
                  archiveFile: "$(Build.ArtifactStagingDirectory)/${{ parameters.archiveName }}-$(build.my_tag)-${{ build_target }}.zip"

              - task: GitHubRelease@0
                displayName: Create release
                inputs:
                  gitHubConnection: ${{ parameters.github.gitHubConnection }}
                  tagSource: manual
                  title: "$(build.my_tag) - $(build.date)"
                  tag: "$(build.my_tag)"
                  assetUploadMode: replace
                  action: edit
                  assets: "$(Build.ArtifactStagingDirectory)/${{ parameters.archiveName }}*"
                  repositoryName: ${{ parameters.github.repositoryName }}
                  isPreRelease: ${{ parameters.github.isPreRelease }}
