# 当修改构建脚本 (包括修改依赖版本) 时检查配置是否能正常发版
# 发版检查非常慢, 因此不在 build.yml 做

name: Check Publishing


on:
  push:
    paths:
      - '**/**.gradle.kts'
      - '**/gradle.properties'
      - 'buildSrc/**'
  pull_request:
    paths:
      - '**/**.gradle.kts'
      - '**/gradle.properties'
      - 'buildSrc/**'

jobs:

  check-publishing:
    name: "Check Publishing (${{ matrix.os }})"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
#          - windows-2022
          - ubuntu-20.04
          - macos-12
        include:
          #          - os: windows-2022
          #            targetName: mingwX64
          #            parallelCompilation: false
          - os: ubuntu-20.04
            targetName: linuxX64
            parallelCompilation: false
          - os: macos-12
            targetName: macosX64
            parallelCompilation: true # macOS machine has 14G
    env:
      # FIXME there must be two or more targets, or we'll get error on `@OptionalExpectation`
      # > Declaration annotated with '@OptionalExpectation' can only be used in common module sources
      gradleArgs: --scan  "-Dmirai.target=jvm;${{ matrix.targetName }};~others" "-Pkotlin.compiler.execution.strategy=in-process" "-Dorg.gradle.jvmargs=-Xmx4096m" "-Dfile.encoding=UTF-8"
      isMac: ${{ startsWith(matrix.os, 'macos') }}
      isWindows: ${{ startsWith(matrix.os, 'windows') }}
      isUbuntu: ${{ startsWith(matrix.os, 'ubuntu') }}
      isUnix: ${{ startsWith(matrix.os, 'macos') || startsWith(matrix.os, 'ubuntu') }}
      VCPKG_DEFAULT_BINARY_CACHE: ${{ startsWith(matrix.os, 'windows') && 'C:\vcpkg\binary_cache' || '/usr/local/share/vcpkg/binary_cache' }}
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: 'recursive'

      - uses: actions/setup-java@v3
        with:
          distribution: 'adopt-openj9'
          java-version: '17'

      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2

      - name: Cache konan
        uses: pat-s/always-upload-cache@v3.0.11
        with:
          path: ~/.konan
          key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Prepare to cache vcpkg
        if: ${{ env.isWindows == 'true' }}
        run: mkdir -p ${{ env.VCPKG_DEFAULT_BINARY_CACHE }}

      - name: Cache vcpkg
        if: ${{ env.isWindows == 'true' }}
        uses: pat-s/always-upload-cache@v3.0.11
        with:
          path: ${{ env.VCPKG_DEFAULT_BINARY_CACHE }}
          key: ${{ runner.os }}-vcpkg-binary-cache-${{ github.job }}
          restore-keys: |
            ${{ runner.os }}-vcpkg-binary-cache-

      - if: ${{ env.isUnix == 'true' }}
        run: chmod -R 777 *

      # Prepare environment for linking on Ubuntu
      - if: ${{ env.isUbuntu == 'true' }}
        name: Install OpenSSL on Ubuntu
        run: sudo apt install libssl-dev -y

      # Prepare environment for linking on Windows
      - if: ${{ env.isWindows == 'true' }}
        name: Setup Memory Environment on Windows
        run: >
          wmic pagefileset where name="D:\\pagefile.sys" set InitialSize=1024,MaximumSize=9216 &
          net stop mongodb
        shell: cmd
        continue-on-error: true

      - if: ${{ env.isWindows == 'true' }}
        name: Install OpenSSL & cURL on Windows
        run: |
          echo "set(VCPKG_BUILD_TYPE release)" | Out-File -FilePath "$env:VCPKG_INSTALLATION_ROOT\triplets\x64-windows.cmake" -Encoding utf8 -Append
          vcpkg install openssl:x64-windows curl[core,ssl]:x64-windows
          New-Item -Path $env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib\crypto.lib -ItemType SymbolicLink -Value $env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib\libcrypto.lib
          New-Item -Path $env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib\ssl.lib -ItemType SymbolicLink -Value $env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib\libssl.lib
          New-Item -Path $env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib\curl.lib -ItemType SymbolicLink -Value $env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib\libcurl.lib
          echo "$env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

      - name: Clean and download dependencies
        run: ./gradlew clean ${{ env.gradleArgs }}

      - name: Prepare deps test build
        run: ./gradlew :mirai-deps-test:updateProjectVersionForLocalDepsTest ${{ env.gradleArgs }} "-Porg.gradle.parallel=${{ matrix.parallelCompilation }}"

      - name: Build and Publish Local Artifacts
        run: ./gradlew :mirai-deps-test:publishMiraiArtifactsToMavenLocal ${{ env.gradleArgs }} "-Porg.gradle.parallel=${{ matrix.parallelCompilation }}"

      - name: Check Publication
        run: ./gradlew :mirai-deps-test:check ${{ env.gradleArgs }} "-Dmirai.deps.test.must.run=true" "-Porg.gradle.parallel=${{ matrix.parallelCompilation }}"
