name: Build
on: [push, workflow_dispatch, pull_request]
jobs:
    macos:
        runs-on: macOS-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v3
              with:
                submodules: recursive
            - name: Notify Gitlab Start
              env:
                GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN }}
              run: |
                pip3 install requests
                ./deploy/gitlab_commit_status.py running
            - name: Install Deps
              run: |
                brew install coreutils ed findutils gawk gnu-sed gnu-tar grep make
                export PATH="$(echo /usr/local/opt/*/libexec/gnubin | tr ' ' :):$PATH"
                ./deploy/mac_build.sh deps
            - name: Build
              env:
                API_KEY_GOOGLE_FONTS: ${{ secrets.API_KEY_GOOGLE_FONTS }}
              run: |
                export PATH="$(echo /usr/local/opt/*/libexec/gnubin | tr ' ' :):$PATH"
                ./deploy/mac_build.sh configure "mac-${GITHUB_SHA:0:8}"
                ./deploy/mac_build.sh build
                cp build/checksum.txt build/checksum-mac.txt
            - name: Build Python
              if: false
              run: |
                cd build
                CI_COMMIT_BRANCH="${GITHUB_REF#refs/*/}"
                CI_COMMIT_SHORT_SHA="${GITHUB_SHA:0:8}"
                cmake .. -DVERSION_SUFFIX="git.`date +%Y%m%d%H%M%S`.$CI_COMMIT_BRANCH.$CI_COMMIT_SHORT_SHA"
                cd ..
                export PATH="$(echo /usr/local/opt/*/libexec/gnubin | tr ' ' :):$PATH"
                ./deploy/mac_build.sh pypi
            # Artifacts are always zipped, even if you only have 1 file... Just adding them so it's easier to verify them
            - name: Job Artifacts
              uses: actions/upload-artifact@v2
              with:
                name: glaxnimate-mac
                path: |
                    build/checksum.txt
                    build/glaxnimate.dmg
                    build/bin/python/dist/
            - name: Update Release
              uses: johnwbyrd/update-release@v1.0.0
              if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/pre-release' || github.ref == 'refs/heads/release' || startsWith(github.ref, 'refs/tags/') }}
              with:
                    token: ${{ secrets.GITHUB_TOKEN }}
                    files: ./build/checksum-mac.txt ./build/glaxnimate.dmg
            - name: Upload Artifacts
              if: false # ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/pre-release' || github.ref == 'refs/heads/release' }}
              env:
                SSH_PRIV_KEY: ${{ secrets.SSH_PRIV_KEY }}
                BUTLER_API_KEY: ${{ secrets.BUTLER_API_KEY }}
              run: |
                echo "$SSH_PRIV_KEY" >privkey
                chmod 600 privkey
                branch="${GITHUB_REF#refs/*/}"
                export PATH="$(echo /usr/local/opt/*/libexec/gnubin | tr ' ' :):$PATH"
                ./deploy/mac_build.sh deploy "$branch" "-i $PWD/privkey"
            - name: Upload Pypi
              if: false
              env:
                TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD_GITLAB }}
                TWINE_USERNAME: __token__
              run: |
                python3 -m twine upload build/bin/python/dist/*.whl --repository-url https://gitlab.com/api/v4/projects/19921167/packages/pypi --verbose
            - name: Notify Gitlab Finish
              if: ${{ always() }}
              env:
                GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN }}
                RESULT: ${{ job.status }}
              run: |
                ./deploy/gitlab_commit_status.py $RESULT
    windows:
        runs-on: windows-latest
        defaults:
            run:
                shell: msys2 {0}
        steps:
            - name: Setup msys2
              uses: msys2/setup-msys2@v2
              with:
                path-type: strict
                update: true
            - name: Checkout
              uses: actions/checkout@v3
              with:
                submodules: recursive
            - name: Notify Gitlab Start
              env:
                GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN }}
              run: |
                pacman --noconfirm -S mingw-w64-x86_64-python-pip
                pip install requests
                ./deploy/gitlab_commit_status.py running
            - name: Install Deps
              run: |
                ./deploy/win_build.sh deps
            - name: Build
              env:
                API_KEY_GOOGLE_FONTS: ${{ secrets.API_KEY_GOOGLE_FONTS }}
              run: |
                (cd ./data/icons/breeze-icons/ && git config core.symlinks true && git reset --hard &>/dev/null)
                ./deploy/win_build.sh configure "win-${GITHUB_SHA:0:8}"
                ./deploy/win_build.sh build
                cp build/checksum.txt build/checksum-win.txt
            - name: Build Python
              if: false
              run: |
                cd build
                CI_COMMIT_BRANCH="${GITHUB_REF#refs/*/}"
                CI_COMMIT_SHORT_SHA="${GITHUB_SHA:0:8}"
                cmake .. -DVERSION_SUFFIX="git.`date +%Y%m%d%H%M%S`.$CI_COMMIT_BRANCH.$CI_COMMIT_SHORT_SHA"
                cd ..
                ./deploy/win_build.sh pypi
            # Artifacts are always zipped, even if you only have 1 file... Just adding them so it's easier to verify them
            - name: Job Artifacts
              uses: actions/upload-artifact@v2
              with:
                name: glaxnimate-win
                path: |
                    build/checksum.txt
                    build/glaxnimate-x86_64.zip
                    build/bin/python/dist/
            - name: Update Release
              uses: johnwbyrd/update-release@v1.0.0
              if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/pre-release' || github.ref == 'refs/heads/release' || startsWith(github.ref, 'refs/tags/') }}
              with:
                    token: ${{ secrets.GITHUB_TOKEN }}
                    files: ./build/checksum-win.txt ./build/glaxnimate-x86_64.zip
            - name: Upload Artifacts
              if: false # ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/pre-release' || github.ref == 'refs/heads/release' }}
              env:
                SSH_PRIV_KEY: ${{ secrets.SSH_PRIV_KEY }}
                BUTLER_API_KEY: ${{ secrets.BUTLER_API_KEY }}
              run: |
                echo "$SSH_PRIV_KEY" >privkey
                chmod 600 privkey
                branch="${GITHUB_REF#refs/*/}"
                ./deploy/win_build.sh deploy "$branch" "-i $PWD/privkey"
            - name: Upload Pypi
              if: false
              env:
                TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD_GITLAB }}
                TWINE_USERNAME: __token__
              run: |
                python3 -m twine upload build/bin/python/dist/*.whl --repository-url https://gitlab.com/api/v4/projects/19921167/packages/pypi --verbose
            - name: Notify Gitlab Finish
              if: ${{ always() }}
              env:
                GITLAB_ACCESS_TOKEN: ${{ secrets.GITLAB_ACCESS_TOKEN }}
                RESULT: ${{ job.status }}
              run: |
                ./deploy/gitlab_commit_status.py $RESULT
