name: Release

on:
  push:
    branches:
      - 'release-v*'

jobs:
  unix:
    runs-on: ${{ matrix.os }}
    continue-on-error: false
    timeout-minutes: 45

    strategy:
      matrix:
        os: [macos-11, ubuntu-latest]

    env:
      DISPLAY: ":99.0"
      MARKTEXT_IS_STABLE: 1
      MARKTEXT_EXIT_ON_ERROR: 1
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2

      - name: Install Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16
          check-latest: true
          cache: yarn
          cache-dependency-path: yarn.lock

      - name: Install build dependencies
        if: runner.os == 'Linux'
        #                         electron-builder                 | keyboard-layout         | keytar                      | fontmanager     | RPM on Ubuntu
        run: |
          sudo apt-get -y update
          sudo apt-get install -y icnsutils graphicsmagick xz-utils libx11-dev libxkbfile-dev gnome-keyring libsecret-1-dev libfontconfig-dev rpm

      - name: Cache node_modules
        uses: actions/cache@v2
        with:
          path: ${{ github.workspace }}/node_modules
          key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
          restore-keys: |
            ${{ runner.os }}-node_modules-cache-v2-

      - name: Cache Electron
        uses: actions/cache@v2
        with:
          path: ${{ env.HOME }}/.cache/electron
          key: ${{ runner.os }}-electron-cache-v2-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}

      - name: Cache Electron-Builder
        uses: actions/cache@v2
        with:
          path: ${{ env.HOME }}/.cache//electron-builder
          key: ${{ runner.os }}-electron-builder-cache-v2-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}

      - name: Install dependencies
        run: yarn install --check-files --frozen-lockfile

      - name: Strip ripgrep on Linux
        if: runner.os == 'Linux'
        run: strip node_modules/vscode-ripgrep/bin/rg

      - name: Lint
        run: |
          yarn run lint
          yarn run validate-licenses

      - name: Run unit and E2E tests
        uses: GabrielBB/xvfb-action@v1
        with:
          run: yarn run test

      - name: Build and release
        run: |
              if [ "$RUNNER_OS" == "Linux" ]; then
                yarn run release:linux --publish always
              elif [ "$RUNNER_OS" == "macOS" ]; then
                yarn run release:mac --publish always
              else
                echo "$RUNNER_OS not supported"
                exit 1
              fi

      - name: Calculate checksums
        if: runner.os == 'Linux'
        run: |
          sha256sum build/marktext-x64.tar.gz
          sha256sum build/marktext-x86_64.AppImage
          sha256sum build/marktext-*.deb
          sha256sum build/marktext-*.rpm
        shell: bash

      - name: Calculate checksums
        if: runner.os == 'macOS'
        run: |
          shasum -a 256 build/marktext-arm64-mac.zip
          shasum -a 256 build/marktext-x64-mac.zip
          shasum -a 256 build/marktext-arm64.dmg
          shasum -a 256 build/marktext-x64.dmg
        shell: bash

  windows:
    runs-on: windows-latest
    continue-on-error: false
    timeout-minutes: 45

    env:
      MARKTEXT_IS_STABLE: 1
      MARKTEXT_EXIT_ON_ERROR: 1
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v2

      - name: Install Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 16
          check-latest: true
          cache: yarn
          cache-dependency-path: yarn.lock

      # Workaround: Fix native build failure due to outdated node-gyp version.
      - name: Fix node-gyp
        run: |
          npm install --global node-gyp@latest
          npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}
          node-gyp install
        shell: pwsh

      - name: Cache node_modules
        uses: actions/cache@v2
        with:
          path: ${{ github.workspace }}\node_modules
          key: ${{ runner.os }}-node_modules-cache-v2-${{ hashFiles(format('{0}{1}', github.workspace, '\yarn.lock')) }}
          restore-keys: |
            ${{ runner.os }}-node_modules-cache-v2-

      - name: Cache Electron
        uses: actions/cache@v2
        with:
          path: ${{ env.LOCALAPPDATA }}\electron\Cache
          key: ${{ runner.os }}-electron-cache-v2-${{ hashFiles(format('{0}{1}', github.workspace, '\yarn.lock')) }}

      - name: Cache Electron-Builder
        uses: actions/cache@v2
        with:
          path: ${{ env.LOCALAPPDATA }}\electron-builder\cache
          key: ${{ runner.os }}-electron-builder-cache-v2-${{ hashFiles(format('{0}{1}', github.workspace, '\yarn.lock')) }}

      - name: Install dependencies
        run: yarn install --check-files --frozen-lockfile

      - name: Lint
        run: |
          yarn run lint
          yarn run validate-licenses

      - name: Run unit and E2E tests
        run: yarn run test

      - name: Build and release
        run: yarn run release:win --publish always

      - name: Calculate checksums
        run: |
          get-filehash -Algorithm SHA256 "build\marktext-setup.exe"
          get-filehash -Algorithm SHA256 "build\marktext-*-win.zip"
        shell: pwsh
