# Build image for the AudioManager client pipeline.
#
# Build and push it with scripts/build-ci-image.sh after changing this file or
# the Android/Kotlin versions.
FROM gradle:8.14.3-jdk21

ARG NODE_VERSION=22.14.0
ARG ANDROID_CMDLINE_TOOLS=11076708

ENV ANDROID_HOME=/opt/android-sdk \
    ANDROID_SDK_ROOT=/opt/android-sdk \
    GRADLE_USER_HOME=/root/.gradle

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        git \
        python3 \
        xz-utils \
        unzip \
        zip \
    && rm -rf /var/lib/apt/lists/*

# JavaScript actions (checkout, upload-artifact) run inside the job container.
RUN curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz" \
        -o /tmp/node.tar.xz \
    && tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \
    && rm /tmp/node.tar.xz \
    && node --version \
    && npm --version

RUN mkdir -p "$ANDROID_HOME/cmdline-tools" \
    && curl -fsSL "https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_CMDLINE_TOOLS}_latest.zip" \
        -o /tmp/cmdline-tools.zip \
    && unzip -q /tmp/cmdline-tools.zip -d "$ANDROID_HOME/cmdline-tools" \
    && mv "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/latest" \
    && rm /tmp/cmdline-tools.zip \
    && yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null \
    && "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \
        "platform-tools" \
        "platforms;android-35" \
        "build-tools;35.0.0" \
    && rm -rf "$ANDROID_HOME/.temp" "$ANDROID_HOME/.downloadIntermediates"

# Warm the Gradle dependency cache and the Kotlin/Wasm (node, yarn, binaryen)
# toolchain so CI jobs do not download them on every run. Rebuild this image
# when dependencies change.
COPY . /warmup
WORKDIR /warmup
# Warm dependencies and the common/Android/desktop compilers here. The Wasm
# executable build is memory hungry and stays in the web job, but node and yarn
# are cached: Kotlin downloads them from GitHub, which has been flaky during CI.
RUN sed -i 's/kotlin.daemon.jvmargs=-Xmx3g/kotlin.daemon.jvmargs=-Xmx2g/' gradle.properties \
    && gradle --no-daemon --max-workers=2 \
        :kotlinNodeJsSetup \
        :kotlinYarnSetup \
        :composeApp:desktopTest \
        :composeApp:assembleDebug \
        :composeApp:compileKotlinWasmJs \
        --console=plain \
    && rm -rf /warmup

WORKDIR /workspace

CMD ["sleep", "infinity"]
