name: Magisk Setup
inputs:
  is-asset-build:
    required: false
    default: false
runs:
  using: "composite"
  steps:
    - name: Set up JDK 21
      uses: actions/setup-java@v4
      with:
        distribution: "temurin"
        java-version: "21"

    - name: Set up Python 3
      uses: actions/setup-python@v5
      with:
        python-version: "3.x"

    - name: Install GNU make
      if: runner.os == 'macOS'
      shell: bash
      run: |
        brew install make
        echo 'GNUMAKE=gmake' >> "$GITHUB_ENV"

    - name: Cache sccache
      uses: actions/cache@v4
      with:
        path: .sccache
        key: sccache-${{ runner.os }}-${{ github.sha }}
        restore-keys: sccache-${{ runner.os }}-

    - name: Set up sccache
      shell: bash
      env:
        SCCACHE_DIRECT: false
        SCCACHE_DIR: ${{ github.workspace }}/.sccache
        SCCACHE_CACHE_SIZE: 2G
        SCCACHE_IDLE_TIMEOUT: 0
      run: |
        bash $GITHUB_ACTION_PATH/sccache.sh
        sccache --start-server
        sccache -z

    - name: Show sccache stats
      uses: gacts/run-and-post-run@v1
      with:
        run: sccache -s
        post: sccache -s

    - name: Set GRADLE_USER_HOME
      shell: bash
      run: echo "GRADLE_USER_HOME=$GITHUB_WORKSPACE/.gradle" >> "$GITHUB_ENV"

    - name: Cache Gradle dependencies
      uses: actions/cache@v4
      if: inputs.is-asset-build == 'true'
      with:
        path: |
          .gradle/caches
          .gradle/wrapper
          !.gradle/caches/build-cache-*
        key: gradle-cache-${{ hashFiles('gradle/**') }}
        restore-keys: gradle-cache-

    - name: Restore Gradle dependencies
      uses: actions/cache/restore@v4
      if: inputs.is-asset-build == 'false'
      with:
        path: |
          .gradle/caches
          .gradle/wrapper
          !.gradle/caches/build-cache-*
        key: gradle-cache-${{ hashFiles('gradle/**') }}
        restore-keys: gradle-cache-
        enableCrossOsArchive: true

    - name: Cache Gradle build cache
      uses: actions/cache@v4
      if: inputs.is-asset-build == 'true'
      with:
        path: |
          .gradle/caches/build-cache-*
        key: gradle-build-cache-${{ github.sha }}
        restore-keys: gradle-build-cache-

    - name: Restore Gradle build cache
      uses: actions/cache/restore@v4
      if: inputs.is-asset-build == 'false'
      with:
        path: |
          .gradle/caches/build-cache-*
        key: gradle-build-cache-${{ github.sha }}
        restore-keys: gradle-build-cache-
        enableCrossOsArchive: true

    - name: Set up NDK
      shell: bash
      run: python build.py -v ndk
