# aka.ms/yaml
trigger:
- master
- develop



variables:
  is_original_repo: ${{ and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}
  is_fork: ${{ not(and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop')))) }}



jobs:

#
# Initialization
#

- job: Initialization
  pool:
    vmImage: 'windows-2019'
  steps:
  - template: azure-pipelines-build-number.yml

#
# CodeFormatValidation
#

- job: CodeFormatValidation
  dependsOn: Initialization
  pool:
    vmImage: 'macOS-latest'
  steps:
  - script: |
      brew install clang-format
    displayName: 'Install dependencies'
  - script: |
      cd $(Build.SourcesDirectory)
      find . \( -name '*.h' -or -name '*.cpp' -or -name '*.c' -or -name '*.mm' -or -name '*.m' \) -print0 | xargs -0 clang-format -style=file -i
      git diff $(Build.SourceVersion)
      if [ "$(git diff $(Build.SourceVersion))" != "" ]; then exit 1; fi
    displayName: 'clang-format'

#
# Linux
#

- job: Linux
  dependsOn: CodeFormatValidation
  pool:
    vmImage: 'ubuntu-22.04'
  steps:
  - script: |
      sudo apt-get update
      sudo apt-get install -y qtchooser qtbase5-dev-tools qt5-qmake \
      qtbase5-dev qtmultimedia5-dev libpoppler-qt5-dev qttools5-dev-tools \
      libqt5opengl5-dev libunarr-dev qtdeclarative5-dev libqt5svg5-dev qtquickcontrols2-5-dev
    displayName: 'Install dependencies'
  - script: |
      cd $(Build.SourcesDirectory)
      VERSION="$(cat common/yacreader_global.h | grep '#define VERSION "' | tr -d '#define VERSION' | tr -d '"' )"
      ./mktarball.sh $VERSION.$(Build.BuildNumber)
      mkdir tarball
      cp yacreader-*-src.tar.xz* tarball\
    displayName: 'Create tarball'
  - script: |
      export DEFINES_VAR=DEFINES+\=\"BUILD_NUMBER=\\\\\\\"$(Build.BuildNumber)\\\\\\\"\"
      qmake CONFIG+="unarr" $DEFINES_VAR
      make
    displayName: 'Build'
  - script: |
      make check TESTARGS="-maxwarnings 100000"
    displayName: 'Run tests'
  - task: CopyFiles@2
    inputs:
      sourceFolder: $(Build.SourcesDirectory)/tarball
      contents: '*'
      targetFolder: $(Build.ArtifactStagingDirectory)
  - task: PublishPipelineArtifact@1
    inputs:
      path: $(Build.ArtifactStagingDirectory)
      artifactName: src $(Build.BuildNumber) tarball

#
# Linux qt6
#

- job: Linux_qt6
  dependsOn: CodeFormatValidation
  pool:
    vmImage: 'ubuntu-22.04'
  steps:
  - script: |
      sudo apt-get update
      sudo apt-get install -y qtchooser qt6-tools-dev qt6-base-dev-tools qmake6 qmake6-bin \
      qt6-base-dev qt6-multimedia-dev qt6-tools-dev-tools libgl-dev qt6-l10n-tools \
      libqt6opengl6-dev libunarr-dev qt6-declarative-dev libqt6svg6-dev libqt6core5compat6-dev
    displayName: 'Install dependencies'
  - script: |
      qtchooser -list-versions
      export DEFINES_VAR=DEFINES+\=\"BUILD_NUMBER=\\\\\\\"$(Build.BuildNumber)\\\\\\\"\"
      qmake6 CONFIG+="unarr no_pdf" $DEFINES_VAR
      qmake6 -v
      make
    displayName: 'Build'
  - script: |
      make check TESTARGS="-maxwarnings 100000"
    displayName: 'Run tests'

#
# MacOS qt6
#

- job: MacOS_qt6
  dependsOn: CodeFormatValidation
  variables:
  - ${{ if and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}:
    - group: macos-codesign
  pool:
    vmImage: 'macOS-latest'
  steps:
  - script: |
      pip3 install aqtinstall
      python3 -m aqt install 6.3.1 mac desktop -m qt5compat qtmultimedia qtimageformats
      echo '##vso[task.prependpath]$(Build.SourcesDirectory)/6.3.1/macos/bin'
      brew install create-dmg
      brew install node
      brew link --overwrite node
      npm install -g appdmg
      mkdir $(Build.SourcesDirectory)\compressed_archive
      wget "https://github.com/YACReader/yacreader-7z-deps/blob/main/p7zip_16.02_src_all.tar.bz2?raw=true" -O $(Build.SourcesDirectory)/compressed_archive/p7zip_16.02_src_all.tar.bz2
      tar xjf $(Build.SourcesDirectory)/compressed_archive/p7zip_16.02_src_all.tar.bz2 -C $(Build.SourcesDirectory)/compressed_archive
      mv $(Build.SourcesDirectory)/compressed_archive/p7zip_16.02 $(Build.SourcesDirectory)/compressed_archive/libp7zip
    displayName: 'Install dependencies'
  - ${{ if and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}:
    - task: InstallAppleCertificate@2
      inputs:
        certSecureFile: 'developerID_application.p12'
        certPwd: $(P12Password)
  - script: |
      cd $(Build.SourcesDirectory)
      VERSION="$(cat common/yacreader_global.h | grep '#define VERSION "' | tr -d '#define VERSION' | tr -d '"' )"
      echo "Trying to know if we are in a forked repo: $IS_FORK"
      SKIP_CODESIGN="$(tr [A-Z] [a-z] <<< "$IS_FORK")"
      ./compileOSX.sh $VERSION $(Build.BuildNumber) $SKIP_CODESIGN Qt6
    displayName: 'Build'
  - script: |
      cd $(Build.SourcesDirectory)/tests
      qmake
      make check TESTARGS="-maxwarnings 100000"
    displayName: 'Build and run tests'
  - script: |
      SKIP_CODESIGN="$(tr [A-Z] [a-z] <<< "$IS_FORK")"
      if [ "$SKIP_CODESIGN" = false ]; then
        xcrun notarytool submit *.dmg --apple-id $(AppleId) --team-id $(TeamId) --password $(AppPassword) --wait
        xcrun stapler staple *.dmg
      fi
    displayName: 'Notarize'
  - task: CopyFiles@2
    inputs:
      contents: '*.dmg'
      targetFolder: $(Build.ArtifactStagingDirectory)
  - task: PublishPipelineArtifact@1
    inputs:
      path: $(Build.ArtifactStagingDirectory)
      artifactName: Macos qt6 $(Build.BuildNumber) dmg

#
# MacOS
#

- job: MacOS
  dependsOn: CodeFormatValidation
  variables:
  - ${{ if and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}:
    - group: macos-codesign
  pool:
    vmImage: 'macOS-latest'
  steps:
  - script: |
      brew install qt@5
      brew link qt@5 --force
      brew install create-dmg
      brew install node
      brew link --overwrite node
      npm install -g appdmg
      mkdir $(Build.SourcesDirectory)\compressed_archive
      wget "https://github.com/YACReader/yacreader-7z-deps/blob/main/p7zip_16.02_src_all.tar.bz2?raw=true" -O $(Build.SourcesDirectory)/compressed_archive/p7zip_16.02_src_all.tar.bz2
      tar xjf $(Build.SourcesDirectory)/compressed_archive/p7zip_16.02_src_all.tar.bz2 -C $(Build.SourcesDirectory)/compressed_archive
      mv $(Build.SourcesDirectory)/compressed_archive/p7zip_16.02 $(Build.SourcesDirectory)/compressed_archive/libp7zip
    displayName: 'Install dependencies'
  - ${{ if and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}:
    - task: InstallAppleCertificate@2
      inputs:
        certSecureFile: 'developerID_application.p12'
        certPwd: $(P12Password)
  - script: |
      cd $(Build.SourcesDirectory)
      VERSION="$(cat common/yacreader_global.h | grep '#define VERSION "' | tr -d '#define VERSION' | tr -d '"' )"
      echo "Trying to know if we are in a forked repo: $IS_FORK"
      SKIP_CODESIGN="$(tr [A-Z] [a-z] <<< "$IS_FORK")"
      ./compileOSX.sh $VERSION $(Build.BuildNumber) $SKIP_CODESIGN Qt5
    displayName: 'Build'
  - script: |
      cd $(Build.SourcesDirectory)/tests
      qmake
      make check TESTARGS="-maxwarnings 100000"
    displayName: 'Build and run tests'
  - script: |
      SKIP_CODESIGN="$(tr [A-Z] [a-z] <<< "$IS_FORK")"
      if [ "$SKIP_CODESIGN" = false ]; then
        xcrun notarytool submit *.dmg --apple-id $(AppleId) --team-id $(TeamId) --password $(AppPassword) --wait
        xcrun stapler staple *.dmg
      fi
    displayName: 'Notarize'
  - task: CopyFiles@2
    inputs:
      contents: '*.dmg'
      targetFolder: $(Build.ArtifactStagingDirectory)
  - task: PublishPipelineArtifact@1
    inputs:
      path: $(Build.ArtifactStagingDirectory)
      artifactName: Macos $(Build.BuildNumber) dmg

#
# Windows x64
#

- template: azure-pipelines-windows-template.yml
  parameters:
    name: Windows_x64
    architecture: 'x64'
    qt_version: '5.15.2'
    qt_spec: 'msvc2019_64'
    qt_aqt_spec: 'win64_msvc2019_64'
    vc_redist_url: 'https://aka.ms/vs/16/release/vc_redist.x64.exe'
    vc_redist_file_name: 'vc_redist.x64.exe'
    vc_vars: 'vcvars64.bat'

#
# Windows x64 qt6
#

- template: azure-pipelines-windows-template-qt6.yml
  parameters:
    name: Windows_x64_qt6
    architecture: 'x64'
    qt_version: '6.3.1'
    qt_spec: 'msvc2019_64'
    qt_aqt_spec: 'win64_msvc2019_64'
    vc_redist_url: 'https://aka.ms/vs/16/release/vc_redist.x64.exe'
    vc_redist_file_name: 'vc_redist.x64.exe'
    vc_vars: 'vcvars64.bat'

#
# Windows x86
#

- template: azure-pipelines-windows-template.yml
  parameters:
    name: Windows_x86
    architecture: 'x86'
    qt_version: '5.15.2'
    qt_spec: 'msvc2019'
    qt_aqt_spec: 'win32_msvc2019'
    vc_redist_url: 'https://aka.ms/vs/16/release/vc_redist.x86.exe'
    vc_redist_file_name: 'vc_redist.x86.exe'
    vc_vars: 'vcvars32.bat'

#
# Dev builds publication
#

- ${{ if and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}:
  - job: PublishDevBuilds
    dependsOn:
    - Linux
    - MacOS
    - MacOS_qt6
    - Windows_x86
    - Windows_x64
    - Windows_x64_qt6
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/develop'), true)
    variables:
    - group: github-releases
    pool:
      vmImage: 'ubuntu-20.04'
    steps:
    - task: DownloadPipelineArtifact@2
      inputs:
        buildType: 'current'
        targetPath: $(Build.ArtifactStagingDirectory)
    - script: |
        find $(Build.ArtifactStagingDirectory) -name '*.*' -exec cp {} $(Build.ArtifactStagingDirectory) \;
      displayName: 'flatten artifact staging directory'
    - script: |
        VERSION="$(cat common/yacreader_global.h | grep '#define VERSION "' | tr -d '#define VERSION' | tr -d '"' ).$(Build.BuildNumber)"
        echo "##vso[task.setvariable variable=VERSION]$VERSION"
      displayName: 'Version'
    - task: GitHubRelease@0
      inputs:
        gitHubConnection: yacreader-releases
        repositoryName: YACReader/yacreader-dev-builds
        title: $(VERSION)
        target: '25313e3d4d03fcbe44d3943db23bc03bbd1a5205'
        tagSource: 'manual'
        tag: $(VERSION)
        addChangeLog: false

#
# Release builds publication
#

- ${{ if and(eq(variables['System.TeamFoundationCollectionUri'], 'https://dev.azure.com/luisangelsm/'), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['Build.SourceBranch'], 'refs/heads/develop'))) }}:
  - job: Release
    dependsOn:
    - Linux
    - MacOS
    - MacOS_qt6
    - Windows_x86
    - Windows_x64
    - Windows_x64_qt6
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
    variables:
    - group: github-releases
    pool:
      vmImage: 'ubuntu-20.04'
    steps:
    - task: DownloadPipelineArtifact@2
      inputs:
        buildType: 'current'
        targetPath: $(Build.ArtifactStagingDirectory)
    - script: |
        find $(Build.ArtifactStagingDirectory) -name '*.*' -exec cp {} $(Build.ArtifactStagingDirectory) \;
      displayName: 'flatten artifact staging directory'
    - script: |
        VERSION="$(cat common/yacreader_global.h | grep '#define VERSION "' | tr -d '#define VERSION' | tr -d '"' )"
        echo "##vso[task.setvariable variable=VERSION]$VERSION"
      displayName: 'Version'
    - task: GitHubRelease@0
      inputs:
        gitHubConnection: yacreader-releases
        title: $(VERSION)
        tagSource: 'manual'
        tag: $(VERSION)
        addChangeLog: false
