step-env: &step-env
  run:
    # prevent Wine popup dialogs about installing additional packages
    name: Setup Environment Variables
    command: |
      echo 'export WINEDLLOVERRIDES="mscoree,mshtml="' >> $BASH_ENV
      echo 'export WINEDEBUG="-all"' >> $BASH_ENV

step-restore-brew-cache: &step-restore-brew-cache
  restore_cache:
    name: Restoring Homebrew cache
    paths:
      - /usr/local/Homebrew
    keys:
      - v1-brew-cache-{{ arch }}

step-save-brew-cache: &step-save-brew-cache
  save_cache:
    name: Persisting Homebrew cache
    paths:
      - /usr/local/Homebrew
    key: v1-brew-cache-{{ arch }}

step-restore-cache: &step-restore-cache
  restore_cache:
    keys:
      - v1-dependencies-{{ arch }}-{{ checksum "yarn.lock" }}
      - v1-dependencies-{{ arch }}-

step-save-cache: &step-save-cache
  save_cache:
    paths:
      - node_modules
    key: v1-dependencies-{{ arch }}-{{ checksum "yarn.lock" }}

step-install-os-dependencies: &step-install-os-dependencies
  run:
    name: Install OS Dependencies
    command: |
      case "$(uname)" in
        Linux)
          sudo dpkg --add-architecture i386
          sudo apt-get -qq update
          sudo apt-get install --no-install-recommends -y wine64 wine32 wine mono-devel
          wine64 hostname
        ;;
        Darwin)
          brew cask install xquartz wine-stable
          brew install mono
          wine64 hostname
        ;;
      esac

steps-linux-win: &steps-linux-win
  steps:
    - *step-env
    - *step-install-os-dependencies
    - checkout
    - *step-restore-cache
    - run: yarn
    - *step-save-cache
    - run: yarn test

steps-mac: &steps-mac
  steps:
    - *step-env
    - *step-restore-brew-cache
    - *step-install-os-dependencies
    - *step-save-brew-cache
    - checkout
    - *step-restore-cache
    - run: yarn
    - *step-save-cache
    - run: yarn test

version: 2.1
orbs:
  win: circleci/windows@1.0.0
jobs:
  test-linux:
    docker:
      - image: circleci/node:8
    <<: *steps-linux-win
  test-mac:
    macos:
      xcode: "11.1.0"
    <<: *steps-mac
  test-windows:
    executor:
      name: win/vs2019
      shell: bash.exe
    <<: *steps-linux-win

  release:
    docker:
      - image: circleci/node:10.15
    steps:
      - checkout
      - *step-restore-cache
      - run: yarn
      - run: npx semantic-release
workflows:
  version: 2
  test_and_release:
    # Run the test jobs first, then the release only when all the test jobs are successful
    jobs:
      - test-linux
      - test-mac
      - test-windows
      - release:
          requires:
            - test-linux
            - test-mac
            - test-windows
          filters:
            branches:
              only:
                - master
