# SPDX-FileCopyrightText: 2026 FocusFlow contributors
# SPDX-License-Identifier: AGPL-3.0-only

VERSION 0.8

# Earthly build pipeline for FocusFlow.
#
# The primary application artifact is the Flutter Linux desktop release bundle.
# The +build target packages that bundle into builds/focus_flow_linux_x64.zip
# and also exports the unzipped runnable bundle under builds/focus_flow_linux_x64.

setup:
    FROM ubuntu:24.04
    USER root
    ENV DEBIAN_FRONTEND=noninteractive
    ENV FLUTTER_VERSION=3.44.3
    ENV FLUTTER_HOME=/opt/flutter
    ENV PATH=/opt/flutter/bin:/opt/flutter/bin/cache/dart-sdk/bin:$PATH
    RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        ca-certificates \
        clang \
        cmake \
        curl \
        dbus-x11 \
        git \
        libgtk-3-dev \
        liblzma-dev \
        libstdc++-12-dev \
        libglu1-mesa \
        ninja-build \
        pkg-config \
        unzip \
        xz-utils \
        xvfb \
        zip \
        && rm -rf /var/lib/apt/lists/*
    RUN curl -fsSL "https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_${FLUTTER_VERSION}-stable.tar.xz" -o /tmp/flutter.tar.xz \
        && mkdir -p /opt \
        && tar -xJf /tmp/flutter.tar.xz -C /opt \
        && rm /tmp/flutter.tar.xz
    WORKDIR /workspace
    RUN git config --global --add safe.directory /opt/flutter
    RUN flutter config --no-analytics
    RUN flutter config --enable-linux-desktop
    RUN flutter --version

code:
    FROM +setup
    COPY . .
    RUN dart pub get
    RUN cd apps/focus_flow_flutter && flutter pub get

format:
    FROM +code
    RUN dart format --set-exit-if-changed packages apps scripts tool test

dart-analyze:
    FROM +code
    RUN dart analyze

flutter-analyze:
    FROM +code
    RUN cd apps/focus_flow_flutter && flutter analyze

reuse:
    FROM +code
    RUN dart run tool/check_reuse.dart

docs:
    FROM +code
    RUN dart run tool/check_docs.dart --skip-dartdoc

pre-commit:
    FROM +code
    RUN .githooks/pre-commit

lint:
    FROM +code
    BUILD +format
    BUILD +dart-analyze
    BUILD +flutter-analyze
    BUILD +reuse
    BUILD +docs
    BUILD +pre-commit

build:
    FROM +code
    RUN cd apps/focus_flow_flutter && flutter create --platforms=linux --project-name focus_flow_flutter --org com.example --no-pub .
    RUN cd apps/focus_flow_flutter && flutter build linux --release
    RUN rm -rf builds \
        && mkdir -p builds/focus_flow_linux_x64 \
        && cp -R apps/focus_flow_flutter/build/linux/x64/release/bundle/. builds/focus_flow_linux_x64/
    RUN cd builds && zip -qr focus_flow_linux_x64.zip focus_flow_linux_x64
    SAVE ARTIFACT --keep-ts builds AS LOCAL builds

run:
    FROM +build
    RUN test -x builds/focus_flow_linux_x64/focus_flow_flutter
    RUN status=0; timeout 10s xvfb-run -a builds/focus_flow_linux_x64/focus_flow_flutter || status=$?; if [ "$status" = "124" ]; then exit 0; fi; exit "$status"

all:
    BUILD +lint
    BUILD +build
