name: CI

on:
  push:
    branches:
      - 'main'
      
  pull_request:
    branches:
      - 'main'

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        include:
          - os: ubuntu-latest
            flutter_profile: development-linux-x86
          - os: macos-latest
            flutter_profile: development-mac
    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v2
      
      - id: rust_toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: 'stable-2022-01-20'
      
      - id: flutter
        uses: subosito/flutter-action@v2
        with:
          channel: 'stable'
          cache: true

      - name: Cache Cargo
        uses: actions/cache@v2
        with: 
          path: |
            ~/.cargo
          key: ${{ runner.os }}-cargo-${{ steps.rust_toolchain.outputs.rustc_hash }}-${{ hashFiles('./frontend/rust-lib/Cargo.toml') }}

      - name: Cache Rust
        uses: actions/cache@v2
        with: 
          path: |
            frontend/rust-lib/target
            shared-lib/target
          key: ${{ runner.os }}-rust-rust-lib-share-lib-${{ steps.rust_toolchain.outputs.rustc_hash }}-${{ hashFiles('./frontend/rust-lib/Cargo.toml') }}    

      - name: Setup Environment
        run:   |
          if [ "$RUNNER_OS" == "Linux" ]; then
            sudo wget -qO /etc/apt/trusted.gpg.d/dart_linux_signing_key.asc https://dl-ssl.google.com/linux/linux_signing_key.pub
            sudo wget -qO /etc/apt/sources.list.d/dart_stable.list https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list
            sudo apt-get update
            sudo apt-get install -y dart curl build-essential libsqlite3-dev libssl-dev clang cmake ninja-build pkg-config libgtk-3-dev
          elif [ "$RUNNER_OS" == "macOS" ]; then
            echo 'do nothing'
          fi
        shell: bash

      - name: Deps
        working-directory: frontend
        run: |
          cargo install cargo-make
          cargo install duckscript_cli
          cargo make flowy_dev

      - name: Config Flutter
        run: |
          if [ "$RUNNER_OS" == "Linux" ]; then
            flutter config --enable-linux-desktop
          elif [ "$RUNNER_OS" == "macOS" ]; then
            flutter config --enable-macos-desktop
          fi
        shell: bash

      - name: Build
        working-directory: frontend
        run: |
          cargo make --profile ${{ matrix.flutter_profile }} appflowy-dev