# This is ci/actions-templates/windows-builds-template.yaml
# Do not edit this file in .github/workflows

name: Windows (master) # skip-pr skip-stable

on:
  push:            # skip-pr
    branches:      # skip-pr
      - master     # skip-pr skip-stable
  schedule: # skip-pr skip-stable
    - cron: "30 0 * * 1" # Every Monday at half past midnight UTC skip-pr skip-stable

jobs:
  build:
    name: Build
    runs-on: windows-latest
    env:
      RUSTFLAGS: -Ctarget-feature=+crt-static
    strategy:
      fail-fast: false
      matrix:
        target:
          - x86_64-pc-windows-msvc
          - aarch64-pc-windows-msvc # skip-pr
          - x86_64-pc-windows-gnu # skip-pr
        mode:
          - dev
          - release         
        include:
          - target: x86_64-pc-windows-msvc
            run_tests: YES
          - target: x86_64-pc-windows-gnu  # skip-pr
            mingw: https://ci-mirrors.rust-lang.org/rustc/x86_64-6.3.0-release-posix-seh-rt_v5-rev2.7z  # skip-pr
            mingwdir: mingw64  # skip-pr
    steps:
      - uses: actions/checkout@v3
        # v2 defaults to a shallow checkout, but we need at least to the previous tag
        with:
          fetch-depth: 0
      - name: Acquire tags for the repo
        run: |
          git fetch --no-tags --prune --depth=1 origin +refs/tags/*:refs/tags/*
      - name: Display the current git status
        run: |
          git status
          git describe
      - name: Prep cargo dirs
        run: |
          New-Item "${env:USERPROFILE}\.cargo\registry" -ItemType Directory -Force
          New-Item "${env:USERPROFILE}\.cargo\git" -ItemType Directory -Force
        shell: powershell
      - name: Install mingw
        run: |
          # We retrieve mingw from the Rust CI buckets
          # Disable the download progress bar which can cause perf issues
          $ProgressPreference = "SilentlyContinue"
          Invoke-WebRequest ${{ matrix.mingw }} -OutFile mingw.7z
          7z x -y mingw.7z -oC:\msys64 | Out-Null
          del mingw.7z
          echo "C:\msys64\usr\bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8
          echo "C:\msys64\${{ matrix.mingwdir }}\bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8
        shell: powershell
        if: matrix.mingw != ''
      - name: Set PATH
        run: |
          echo "%USERPROFILE%\.cargo\bin" | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8
          echo "TARGET=${{ matrix.target }}" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8
      - name: Skip tests
        if: matrix.run_tests == '' || matrix.mode == 'release'
        run: |
          echo "SKIP_TESTS=yes" >> $GITHUB_ENV
        shell: bash
      - name: Cache cargo registry and git trees
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Get rustc commit hash
        id: cargo-target-cache
        run: |
          echo "{rust_hash}={$(rustc -Vv | grep commit-hash | awk '{print $2}')}" >> $GITHUB_OUTPUT
        shell: bash
      - name: Cache cargo build
        uses: actions/cache@v3
        with:
          path: target
          key: ${{ github.base_ref }}-${{ github.head_ref }}-${{ matrix.target }}-${{ matrix.mode }}-cargo-target-dir-${{ steps.cargo-target-cache.outputs.rust_hash }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ github.base_ref }}-${{ matrix.target }}-${{ matrix.mode }}-cargo-target-dir-${{ steps.cargo-target-cache.outputs.rust_hash }}-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rustup using win.rustup.rs
        run: |
          # Disable the download progress bar which can cause perf issues
          $ProgressPreference = "SilentlyContinue"
          Invoke-WebRequest https://win.rustup.rs/ -OutFile rustup-init.exe
          .\rustup-init.exe -y --default-host=x86_64-pc-windows-msvc --profile=minimal
          del rustup-init.exe
        shell: powershell
      - name: Ensure stable toolchain is up to date
        run: rustup update stable
        shell: bash
      - name: Install the target
        run: |
          rustup target install ${{ matrix.target }}
      - name: Run a full build
        env:
          TARGET: ${{ matrix.target }} 
          BUILD_PROFILE: ${{ matrix.mode }}
        run: bash ci/run.bash
      - name: Run cargo check and clippy
        if: matrix.mode != 'release'
        env:
          TARGET: ${{ matrix.target }} 
        # os-specific code leads to lints escaping if we only run this in one target
        run: |
          cargo check --all --all-targets
          git ls-files -- '*.rs' | xargs touch
          cargo clippy --workspace --all-targets      
      - name: Upload the built artifact
        if: matrix.mode == 'release'
        uses: actions/upload-artifact@v3
        with:
          name: rustup-init-${{ matrix.target }}
          path: |
            target/${{ matrix.target }}/release/rustup-init.exe
          retention-days: 7
      - name: Acquire the AWS tooling
        if: github.event_name == 'push' && github.ref == 'refs/heads/stable' && matrix.mode == 'release'
        run: |
          choco upgrade awscli
      - name: Prepare the dist
        if: github.event_name == 'push' && github.ref == 'refs/heads/stable' && matrix.mode == 'release'
        run: |
          .\ci\prepare-deploy.ps1
        shell: powershell
      - name: Deploy build to dev-static dist tree for release team
        if: github.event_name == 'push' && github.ref == 'refs/heads/stable' && matrix.mode == 'release'
        run: |
          aws --debug s3 cp --recursive dist s3://dev-static-rust-lang-org/rustup/dist
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: us-west-1
      - name: Clear the cargo caches
        run: |
          cargo install cargo-cache --no-default-features --features ci-autoclean
          cargo-cache
