FROM docker.io/library/maven:3-eclipse-temurin-11-alpine AS builder
# Install build dependencies
RUN apk add --no-cache python3 git
# Checkout the versioned git tag
RUN git clone --depth 1 --single-branch https://github.com/valentjn/ltex-ls.git /ltex-ls
WORKDIR /ltex-ls
ARG LTEX_LS_VERSION
RUN git fetch origin tag "${LTEX_LS_VERSION}" \
    && git switch --detach "${LTEX_LS_VERSION}"
# Generate completion lists
RUN python3 -u tools/createCompletionLists.py
# Build
RUN mvn --quiet --errors package -DskipTests
# Package binary
RUN tar -xzf target/ltex-ls-${LTEX_LS_VERSION}.tar.gz \
    && mv -v ltex-ls-${LTEX_LS_VERSION} /ltex-ls-out

FROM docker.io/library/eclipse-temurin:11-jre-alpine
# Add a normal user to run the program
RUN adduser -D -s /sbin/nologin -h /ltex-ls ltex
USER ltex
# Copy artifacts from builder
WORKDIR /ltex-ls
COPY --from=builder /ltex-ls-out/ ./
# Entrypoint
ENTRYPOINT ["/ltex-ls/bin/ltex-ls"]
