name: CMake

on:
  push:
    paths:
      - '**.cpp'
      - '**.cc'
      - '**.cxx'
      - '**.c'
      - '**.hpp'
      - '**.hh'
      - '**.hxx'
      - '**.h'
      - '.github/workflows/cmake.yml'
      - '**/CMakeLists.txt'

env:
  BUILD_TYPE: Release

jobs:
  build:
    runs-on: windows-2022

    steps:
      - uses: actions/checkout@v3.0.2
        with:
          fetch-depth: 1
          #submodules: 'true'

      - name: Cache Bedrock Dedicated Server Library
        id: cache-bds-lib
        uses: actions/cache@v3
        env:
          cache-name: cache-bds-lib
        with:
          path: |
            ${{ env.GITHUB_WORKSPACE }}LiteLoader/Lib/bedrock_server_api.lib
            ${{ env.GITHUB_WORKSPACE }}LiteLoader/Lib/bedrock_server_var.lib
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('Scripts/LINK.txt') }}

      - name: Download Server
        if: steps.cache-bds-lib.outputs.cache-hit != 'true'
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        run: |
          mkdir Tools/Server
          ServerLink=$(cat 'Scripts/LINK.txt')
          curl -L -o Tools/Server/server.zip "$ServerLink"
          unzip Tools/Server/server.zip -d Tools/Server/ > /dev/null
        shell: bash

      - name: Build Library
        if: steps.cache-bds-lib.outputs.cache-hit != 'true'
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        run: |
          cd Tools
          LibraryBuilder.exe Server
        shell: cmd

      - name: Change LITELOADER_VERSION_STATUS_BETA
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        if: false == startsWith(github.ref, 'refs/tags/')
        run: |
          sed -r -i 's/#define\s+LITELOADER_VERSION_STATUS\s+LITELOADER_VERSION_\w+/#define LITELOADER_VERSION_STATUS   LITELOADER_VERSION_BETA/' LiteLoader/Main/Version.h
          sed -r -i 's/#define\s+LITELOADER_VERSION_ACTIONS\s+.*/#define LITELOADER_VERSION_ACTIONS  ${{ github.run_number }}\r/' LiteLoader/Main/Version.h
          cat LiteLoader/Main/Version.h
        shell: bash

      - name: Change LITELOADER_VERSION_STATUS_RELEASE
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          sed -r -i 's/#define\s+LITELOADER_VERSION_STATUS\s+LITELOADER_VERSION_\w+/#define LITELOADER_VERSION_STATUS   LITELOADER_VERSION_RELEASE/' LiteLoader/Main/Version.h
          sed -r -i 's/#define\s+LITELOADER_VERSION_ACTIONS\s+.*/#define LITELOADER_VERSION_ACTIONS  ${{ github.run_number }}\r/' LiteLoader/Main/Version.h
        shell: bash

      - name: Get MSVC Path
        working-directory: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\
        run: echo "MSVC_VER=$(ls | tail -n 1)" >> $GITHUB_ENV
        shell: bash

      - name: Configure CMake(MSVC)
        run: |
          cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
        shell: cmd

      - name: Build
        run: |
          cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
        shell: cmd

      - name: Configure CMake(ClangCL)
        run: |
          cmake -B ${{github.workspace}}/build_clang -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -T clangcl
        shell: cmd

      - name: Build SymDBHelper
        run: cmake --build ${{github.workspace}}/build_clang --config ${{env.BUILD_TYPE}} --target SymDBHelper

      - name: Pack Release
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        run: |
          cd Scripts
          ./PackRelease.cmd action
        shell: cmd

      - name: Move PDB to path
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        run: |
          mkdir PDB
          cp x64/Release/*.pdb PDB
        shell: bash

      - name: Upload LiteLoader
        uses: actions/upload-artifact@v3.1.0
        with:
          name: LiteLoader
          path: ${{ github.workspace }}\RELEASE\

      - name: Upload PDB
        uses: actions/upload-artifact@v3.1.0
        with:
          name: PDB
          path: ${{ github.workspace }}\PDB

      - name: Prepare for creating Release
        working-directory: ${{ env.GITHUB_WORKSPACE }}
        id: rel
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          echo ::set-output name=tag::${GITHUB_REF#refs/*/}
          mv LiteLoader.zip LiteLoader-${GITHUB_REF#refs/*/}.zip
        shell: bash

      - name: Create New Release
        uses: softprops/action-gh-release@v0.1.14
        if: startsWith(github.ref, 'refs/tags/')
        with:
          body_path: ${{ github.workspace }}\CHANGELOG.md
          files: |
            ${{ github.workspace }}\LiteLoader-${{ steps.rel.outputs.tag }}.zip
            ${{ github.workspace }}\PDB.zip
        env:
          GITHUB_REPOSITORY: quizhizhe/LiteLoaderBDS-1.16.40