name: Windows Build

on:
  push:
    branches:
      - master
      - release_v*
    tags:
    - '*'
  pull_request:
    
env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release
  SW_CLOUD_LOGIN: ${{ secrets.CLOUD_LOGIN }}
  CACHE_HOST: ${{ secrets.SSH_HOST }}
  GA_MEASUREMENT_ID: ${{ secrets.GA_MEASUREMENT_ID }}
  GA_API_SECRET: ${{ secrets.GA_API_SECRET }}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
  
jobs:
  build:

    runs-on: windows-2022
    
    steps:

    - name: Add to path
      shell: bash -l {0}
      run: echo "C:\Miniconda\condabin" >> $GITHUB_PATH

    - name: Check path
      shell: bash -l {0}
      run: echo "$PATH"

    - name: Init conda
      shell: bash -l {0}
      run: /c/Miniconda/condabin/conda.bat init --all

    # - name: link
    #   shell: bash -l {0}
    #   run: cd /usr/bin && ln -s /c/Miniconda/condabin/conda.bat conda
      
    # - name: Check Conda
    #   shell: bash -l {0}
    #   run: /c/Miniconda/condabin/conda.bat info

    # - name: Check path
    #   shell: bash -l {0}
    #   run: ls /c/Miniconda/condabin

    # - name: Check path
    #   shell: bash -l {0}
    #   run: which conda.bat

    - name: Check Conda
      shell: bash -l {0}
      run: conda info
      
    # - name: Setup Conda
    #   uses: conda-incubator/setup-miniconda@v2.2.0
    #   with:
    #       miniconda-version: 'latest'

    - name: Checkout code
      uses: actions/checkout@v3
      with:
        lfs: true

    - name: Get tags
      run: git fetch --unshallow origin +refs/tags/*:refs/tags/*

    - name: Download test data
      shell: bash -l {0}
      run: .github/workflows/download_test_data.sh

    - name: Get NSIS
      working-directory: ${{runner.workspace}}
      run: |
        curl -o nsis.zip https://www.sci.utah.edu/~amorris/NSISPortable_3.04.zip
        Expand-Archive -LiteralPath nsis.zip -DestinationPath .
          
    - name: Conda Installs
      shell: bash -l {0}
      run: .github/workflows/gha_conda.sh

    - name: Restore Dependencies Cache
      id: cache-deps-restore
      uses: actions/cache/restore@v3
      with:
        path: C:\deps
        key: ${{ runner.os }}-deps-${{ hashFiles('.github/workflows/gha_deps.sh', 'install_shapeworks.sh', 'python_requirements.txt', 'build_dependencies.sh') }}

    - name: Build Dependencies
      if: steps.cache-deps-restore.outputs.cache-hit != 'true'
      shell: bash -l {0}
      run: .github/workflows/gha_deps.sh

    - name: Save Dependencies Cache
      if: steps.cache-deps-restore.outputs.cache-hit != 'true'
      uses: actions/cache/save@v3
      with:
        path: C:\deps
        key: ${{ runner.os }}-deps-${{ hashFiles('.github/workflows/gha_deps.sh', 'install_shapeworks.sh', 'python_requirements.txt', 'build_dependencies.sh') }}
      
    - name: Create Build Environment
      # Some projects don't allow in-source building, so create a separate build directory
      # We'll use this as our working directory for all subsequent commands
      run: cmake -E make_directory "C:/build"

    - name: Check Disk Space1
      working-directory: ${{runner.workspace}}
      shell: bash -l {0}
      run: df -h
      
    - name: Configure CMake
      # Use a bash shell so we can use the same syntax for environment variable
      # access regardless of the host operating system
      shell: bash -l {0}
      working-directory: "C:/build"
      # Note the current convention is to use the -S and -B options here to specify source 
      # and build directories, but this is only available with CMake 3.13 and higher.  
      # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
      run: conda activate shapeworks && cmake $GITHUB_WORKSPACE -DCMAKE_CXX_FLAGS="-FS" -DCMAKE_C_FLAGS="-FS" -DCMAKE_CXX_FLAGS_RELEASE="-FS /Zm500 /Zi /GL /MD /O2 /Ob3 /DNDEBUG /EHsc" -DCMAKE_C_FLAGS_RELEASE="-FS /Zi /GL /MD /O2 /Ob3 /DNDEBUG /EHsc" -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="-LTCG /DEBUG" -DCMAKE_EXE_LINKER_FLAGS_RELEASE="-LTCG /DEBUG" -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DITK_DIR="C:\deps\lib\cmake\ITK-5.4" -DVTK_DIR="C:\deps\lib\cmake\vtk-9.5" -DXLNT_DIR="C:\deps" -DLIBIGL_DIR="C:\deps" -DJKQTCommonSharedLib_DIR="C:/deps/lib/cmake/JKQTCommonSharedLib" -DJKQTMathTextSharedLib_DIR="C:/deps/lib/cmake/JKQTMathTextSharedLib" -DJKQTPlotterSharedLib_DIR="C:/deps/lib/cmake/JKQTPlotterSharedLib" -DOpenVDB_DIR="C:\deps\lib\cmake\OpenVDB" -DGEOMETRYCENTRAL_DIR="C:\deps" -DACVD_DIR="C:\deps" -DBuild_Studio=ON -DGA_MEASUREMENT_ID=$GA_MEASUREMENT_ID -DGA_API_SECRET=$GA_API_SECRET

    - name: Build
      working-directory: "C:/build"
      shell: bash -l {0}
      # Execute the build.  You can specify a specific target with "--target <NAME>"
      run: conda activate shapeworks && cmake --build . --config $BUILD_TYPE --parallel

    - name: Check Disk Space2
      working-directory: ${{runner.workspace}}
      shell: bash -l {0}
      run: df -h

    - name: Copy Libraries
      working-directory: "C:/build"
      run: xcopy /f C:\deps\bin\*.dll C:\build\bin\Release

    - name: Build Binary Package
      shell: bash -l {0}
      env:
        PR_NUMBER: ${{ github.event.number }}
      run: conda activate shapeworks && source ${GITHUB_WORKSPACE}/devenv.sh ./bin/Release && pwd ; ls ; cp docs/users/Windows_README.txt . ; cp Support/shapeworks.nsi . ; conda activate shapeworks && source ${GITHUB_WORKSPACE}/devenv.sh ./bin/Release && ./Support/package_windows.sh tag

    - name: Check Disk Space3
      working-directory: ${{runner.workspace}}
      shell: bash -l {0}
      run: df -h

    - name: Download test data
      shell: bash -l {0}
      run: .github/workflows/download_test_data.sh
      
    - name: Test
      working-directory: "C:/build"
      shell: bash -l {0}
      # Execute tests defined by the CMake configuration.  
      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
      run: conda activate shapeworks && source ${GITHUB_WORKSPACE}/devenv.sh ./bin/Release && ctest --output-on-failure -VV -C $BUILD_TYPE --debug
    
    - name: Setup tmate session on failure
      if: ${{ failure() }}
      uses: mxschmitt/action-tmate@v3
      timeout-minutes: 30

    - uses: actions/upload-artifact@v4
      with:
        name: artifact-${{github.sha}}-windows
        path: ${{runner.workspace}}\ShapeWorks\artifacts

    - name: Check Disk Space4
      working-directory: ${{runner.workspace}}
      shell: bash -l {0}
      run: df -h
        
    - name: Deploy
      if: github.ref == 'refs/heads/master'
      uses: marvinpinto/action-automatic-releases@latest
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        automatic_release_tag: "dev-windows"
        prerelease: true
        title: "Development Build for Windows"
        files: |
             d:/a/ShapeWorks/ShapeWorks/artifacts/*.exe