name: CI
on:
  pull_request:
  push:
    branches:
      - master
      - main
      - dev
    tags:
      - "*"

jobs:
  Test:
    if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - windows-2022
          - windows-2019
          - ubuntu-22.04
          - ubuntu-20.04
          - macos-12
          - macos-11
        node:
          - 16
        pnpm:
          - 7
        cache_reset_counter:
          - 0
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true

      - name: Cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.pnpm-store
            D:\.pnpm-store
          key: "setupcpp-cache-OS:${{ matrix.os }}-node:${{ matrix.node }}-pnpm:${{ matrix.pnpm }}-${{ hashFiles('./.npmrc') }}-deps:${{ hashFiles('./package.json') }}"
          restore-keys: |
            "setupcpp-cache-OS:${{ matrix.os }}-"

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Setup Pnpm
        uses: pnpm/action-setup@v2
        with:
          version: ${{ matrix.pnpm }}

      - name: Install and build
        run: |
          pnpm install

      # - name: Setup SSH debugging session
      #   uses: mxschmitt/action-tmate@v3

      - name: Test
        if: "${{ !contains(github.event.head_commit.message, '[skip test]') }}"
        run: |
          pnpm run test

      # Create self-contained executable that bundles Nodejs
      - name: Create Executable
        if: "${{ contains(matrix.os, 'windows-2022') || contains(matrix.os, 'ubuntu-22.04') || contains(matrix.os, 'macos-12') }}"
        run: |
          pnpm run pack.exe

      - name: Upload Artifacts
        if: ${{ (startsWith(github.ref, 'refs/tags/')) && contains(matrix.os, 'windows-2022') || contains(matrix.os, 'ubuntu-22.04') || contains(matrix.os, 'macos-12') }}
        uses: actions/upload-artifact@v3
        with:
          path: |
            ./exe
            ./dist
          retention-days: 1

  Release:
    needs: Test
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-22.04
    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v3

      - name: Place Artifacts
        run: |
          mv -v artifact/* ./
          chmod +x -R ./exe/

      - name: Draft the release
        uses: meeDamian/github-release@2.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          gzip: folders
          draft: true
          files: >
            ./exe/setup-cpp-x64-windows.exe
            ./exe/setup-cpp-x64-linux
            ./exe/setup-cpp-x64-macos
            ./dist/node12/setup-cpp.js
            ./dist/node12/setup-cpp.js.map
            ./dist/node12/
            ./dist/node16/

  Docker:
    if: ${{ !contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[skip test]') }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-20.04
        container:
          - "ubuntu_node.dockerfile"
          - "ubuntu_20.04_node.dockerfile"
          - "arch_node.dockerfile"
          - "fedora_node.dockerfile"
        node:
          - 14
        pnpm:
          - 6
        cache_reset_counter:
          - 0
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true

      - name: Cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.pnpm-store
            D:\.pnpm-store
          key: "setupcpp-docker-cache-OS:${{ matrix.os }}-node:${{ matrix.node }}-pnpm:${{ matrix.pnpm }}-${{ hashFiles('./.npmrc') }}-deps:${{ hashFiles('./package.json') }}-${{ matrix.cache_reset_counter }}"
          restore-keys: |
            "setupcpp-docker-cache-OS:${{ matrix.os }}"
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Setup Pnpm
        uses: pnpm/action-setup@v2
        with:
          version: ${{ matrix.pnpm }}

      - name: Install and build
        run: |
          pnpm install

      - name: Build
        id: docker_build
        run: |
          docker build -f ./dev/docker/${{ matrix.container }} -t setup-cpp .
