name: Rust

on:
  pull_request:
  push:
    branches:
      - master

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: stable
    - uses: actions/checkout@v2
    - name: cargo fmt
      run: cargo fmt -- --check

  unit_tests:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
    steps:
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: stable
    - uses: actions/checkout@v2
    - name: Run tests
      run: cargo test -- --skip=feature_tests

  e2e_tests:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
    steps:
    - name: Install Fish and Zsh using brew
      if: "startsWith(matrix.os, 'macOS')"
      run: brew install fish zsh
    - name: Install Fish and Zsh using apt
      if: "startsWith(matrix.os, 'ubuntu')"
      run: sudo apt-get install -y fish zsh
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: stable
    - uses: actions/checkout@v2
    - name: Run tests
      run: cargo test -- feature_tests

  build_release:
    runs-on: windows-latest
    name: "Release build for Windows"
    steps:
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: stable
    - uses: actions/checkout@v2
    - name: Build release binary
      run: cargo build --release
      env:
        RUSTFLAGS: "-C target-feature=+crt-static"
    - uses: actions/upload-artifact@v2
      with:
        name: fnm-windows
        path: target/release/fnm.exe

  build_macos_release:
    runs-on: macOS-latest
    name: "Release build for macOS"
    steps:
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: stable
    - uses: actions/checkout@v2
    - name: Build release binary
      run: cargo build --release
      env:
        LZMA_API_STATIC: "true"
    - name: Strip binary from debug symbols
      run: strip target/release/fnm
    - name: List dynamically linked libraries
      run: otool -L target/release/fnm
    - uses: actions/upload-artifact@v2
      with:
        name: fnm-macos
        path: target/release/fnm

  build_static_linux_binary:
    name: "Build static Linux binary"
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Build static binary
      run: |
        sudo chown -R 1000:1000 .
        docker run --rm -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder:stable \
          cargo build --target x86_64-unknown-linux-gnu --release
        sudo chown -R $(whoami):$(whoami) .
    - uses: actions/upload-artifact@v2
      with:
        name: fnm-linux
        path: target/x86_64-unknown-linux-gnu/release/fnm

  build_static_arm_binary:
    name: "Build ARM binary"
    strategy:
      matrix:
        include:
          - arch: arm64
            rust_target: aarch64-unknown-linux-musl
            docker_image: arm64v8/ubuntu
            docker_platform: aarch64
          - arch: arm32
            rust_target: armv7-unknown-linux-gnueabihf
            docker_image: arm32v7/ubuntu
            docker_platform: armv7
    runs-on: ubuntu-latest
    env:
      RUST_TARGET: ${{ matrix.rust_target }}
    steps:
    - name: Set up QEMU
      id: qemu
      uses: docker/setup-qemu-action@v1
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: stable
    - name: 'Download `cross` crate'
      run: cargo install cross
    - uses: actions/checkout@v2
    - name: "Build release"
      run: cross build --target $RUST_TARGET --release
    - name: Compress binary using UPX
      run: |
        sudo apt-get install -y upx
        upx target/$RUST_TARGET/release/fnm
    - uses: uraimo/run-on-arch-action@v2.0.9
      name: Sanity test
      with:
        arch: ${{matrix.docker_platform}}
        distro: ubuntu18.04

        # Not required, but speeds up builds by storing container images in
        # a GitHub package registry.
        githubToken: ${{ github.token }}

        env: |
          RUST_LOG: fnm=debug

        dockerRunArgs: |
            --volume "${PWD}/target/${{matrix.rust_target}}/release:/artifacts"

        # Set an output parameter `uname` for use in subsequent steps
        run: |
          echo "Hello from $(uname -a)"
          /artifacts/fnm --version
          echo "fnm install 12.0.0"
          /artifacts/fnm install 12.0.0
          echo "fnm exec --using=12 -- node --version"
          /artifacts/fnm exec --using=12 -- node --version

    - uses: actions/upload-artifact@v2
      with:
        name: fnm-${{ matrix.arch }}
        path: target/${{ env.RUST_TARGET }}/release/fnm

  # benchmark_on_linux:
  #   name: Performance Benchmarks (Linux)
  #   needs: [build_static_linux_binary]
  #   runs-on: ubuntu-latest
  #   steps:
  #     - uses: octokit/request-action@v2.x
  #       id: get_master_workflows
  #       with:
  #         route: GET /repos/:repository/actions/runs
  #         repository: ${{ github.repository }}
  #         branch: master
  #         status: completed
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #     - uses: octokit/request-action@v2.x
  #       id: get_latest_artifacts
  #       with:
  #         route: GET /repos/:repository/actions/runs/:workflow_id/artifacts
  #         repository: ${{ github.repository }}
  #         workflow_id: ${{ fromJson(steps.get_master_workflows.outputs.data).workflow_runs[0].id }}
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  #     - name: "Take the output"
  #       run: |
  #         URL=$(echo '${{ steps.get_latest_artifacts.outputs.data }}' | jq -r '.artifacts | map(select(.name == "fnm-linux")) | .[0].archive_download_url')
  #         curl -L $URL -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' > /tmp/fnm-latest.zip
  #         mkdir ~/.fnm-latest
  #         unzip -d ~/.fnm-latest /tmp/fnm-latest.zip
  #         ls -lah ~/.fnm-latest
  #     - uses: actions/checkout@v2
  #     - name: Install Hyperfine
  #       run: |
  #         wget https://github.com/sharkdp/hyperfine/releases/download/v1.10.0/hyperfine_1.10.0_amd64.deb
  #         sudo dpkg -i hyperfine_1.10.0_amd64.deb
  #     - name: Install fnm-reason for Linux
  #       run: curl -fsSL https://github.com/Schniz/fnm/raw/master/.ci/install.sh | bash -s -- --skip-shell
  #     - name: Install nvm
  #       run: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
  #     - uses: actions/download-artifact@v2
  #       with:
  #         name: fnm-linux
  #         path: target/release/
  #     - name: "Run benchmarks"
  #       run: bash benchmarks/run

  #     - name: Read basic.md for the generated report
  #       id: basic_result
  #       uses: juliangruber/read-file-action@v1
  #       with:
  #         path: benchmarks/results/basic.md

  #     - uses: octokit/request-action@v2.x
  #       id: get_latest_release
  #       with:
  #         route: POST /repos/:repository/commits/:commit_sha/comments
  #         repository: ${{ github.repository }}
  #         commit_sha: ${{ github.sha }}
  #         body: |
  #           |
  #           ${{ steps.basic_result.outputs.content }}
  #       env:
  #         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
