From 7794ed65d2afe1c80debdec7a040c6ea8e85a799 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Tue, 13 May 2025 22:08:19 +0200 Subject: [PATCH] Added build instructions using the new helpers. Removed old python script. Added test compose file --- .gitignore | 3 +- Dockerfile | 62 +++++++++++++++++++++++----- build_scripts/prepare_nuget.py | 75 ---------------------------------- compose.yml | 41 +++++++++++++++++++ 4 files changed, 94 insertions(+), 87 deletions(-) delete mode 100644 build_scripts/prepare_nuget.py create mode 100644 compose.yml diff --git a/.gitignore b/.gitignore index a2dcc62..3490e4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -**/.idea/** \ No newline at end of file +**/.idea/** +**/data/** \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index a53ab7e..4c8d2ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Prepare runtime docker image -FROM cgr.dev/chainguard/dotnet-runtime:latest AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled AS base WORKDIR /app # Prepare build image @@ -9,11 +9,12 @@ FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build # should be put here for caching reasons # Install nodejs and npm so we can build tailwind -RUN apt-get update && apt-get install nodejs npm git python3 -y && apt-get clean +RUN apt-get update && apt-get install nodejs npm git -y && apt-get clean # === Configuration options === # Usefull for custom forks ARG BUILD_CONFIGURATION=Release +ARG PACK_BUILD_CONFIGURATION=Debug ARG MOONLIGHT_REPO=https://github.com/Moonlight-Panel/Moonlight ARG MOONLIGHT_BRANCH=v2_ChangeArchitecture @@ -23,19 +24,29 @@ ARG MOONLIGHT_BRANCH=v2_ChangeArchitecture RUN mkdir -p /src && \ mkdir -p /src/Moonlight && \ mkdir -p /src/Plugins && \ - mkdir /src/nuget && \ - mkdir -p /src/build_scripts + mkdir -p /src/pluginNuget && \ + mkdir -p /src/moonlightNuget WORKDIR /src # === Building === -# Copying build scripts -COPY build_scripts/* /src/build_scripts - # Clone the main moonlight repo RUN git clone --branch $MOONLIGHT_BRANCH $MOONLIGHT_REPO /src/Moonlight +# Install npm packages +WORKDIR /src/Moonlight/Moonlight.Client/Styles +RUN npm i + +WORKDIR /src + +# Build moonlight as nuget packages +RUN dotnet run --project Moonlight/Resources/Scripts/Scripts.csproj -- pack /src/Moonlight /src/moonlightNuget --build-configuration $PACK_BUILD_CONFIGURATION + +# Make the moonlight nuget accessible for the compilation +RUN dotnet nuget add source /src/moonlightNuget -n moonlightNuget + +# Copy plugin links COPY plugins.txt /src/plugins.txt # Clone plugins @@ -43,8 +54,37 @@ RUN grep -v '^#' plugins.txt | \ while read -r repo; \ do \ git clone "$repo" /src/Plugins/$(basename "$repo" .git); \ - done + done -# Build plugins as source only nuget packages -WORKDIR /src/Plugins -RUN python3 /src/build_scripts/prepare_nuget.py \ No newline at end of file +# Build plugin nuget packages +RUN dotnet run --project Moonlight/Resources/Scripts/Scripts.csproj -- pack /src/Plugins /src/pluginNuget --build-configuration $PACK_BUILD_CONFIGURATION + +# Make the plugin nuget accessible for the compilation and remove the moonlight nuget source +RUN dotnet nuget remove source moonlightNuget +RUN dotnet nuget add source /src/pluginNuget -n pluginNuget + +# Prepare moonlight for compilation +RUN dotnet run --project Moonlight/Resources/Scripts/Scripts.csproj -- prebuild /src/Moonlight /src/pluginNuget + +# Build tailwind +WORKDIR /src/Moonlight/Moonlight.Client/Styles +RUN npm run tailwind + +# Build moonlight +WORKDIR "/src/Moonlight/Moonlight.ApiServer" +RUN dotnet build "Moonlight.ApiServer.csproj" -c $BUILD_CONFIGURATION -o /app/build/ + +# Publish application +FROM build AS publish + +ARG BUILD_CONFIGURATION=Release + +RUN dotnet publish "Moonlight.ApiServer.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +# Create final minimal image +FROM base AS final + +WORKDIR /app +COPY --from=publish /app/publish . + +ENTRYPOINT ["dotnet", "Moonlight.ApiServer.dll"] \ No newline at end of file diff --git a/build_scripts/prepare_nuget.py b/build_scripts/prepare_nuget.py deleted file mode 100644 index 8697d5d..0000000 --- a/build_scripts/prepare_nuget.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/bin/python3 - -import os -import xml.etree.ElementTree as ET -import subprocess -from pathlib import Path - -OUTPUT_DIR = Path("/src/nuget") -MOONLIGHT_DIR = Path("/src/Moonlight") -TARGET_PROJECTS = { - "api server": MOONLIGHT_DIR / "Moonlight.ApiServer.csproj", - "frontend": MOONLIGHT_DIR / "Moonlight.Client.csproj", - "shared": MOONLIGHT_DIR / "Moonlight.Shared.csproj", -} - -def get_version_and_tags(csproj_path): - tree = ET.parse(csproj_path) - root = tree.getroot() - ns = {"msbuild": "http://schemas.microsoft.com/developer/msbuild/2003"} - version = None - tags = "" - - for elem in root.iter(): - tag_name = elem.tag.split("}")[-1] - if tag_name == "Version": - version = elem.text.strip() - if tag_name in ("PackageTags", "Tags"): - tags = elem.text.strip().lower() - - return version, tags - -def pack_project(csproj_path): - subprocess.run([ - "dotnet", "pack", str(csproj_path), "-o", str(OUTPUT_DIR) - ], check=True, stdout=subprocess.DEVNULL) - -def add_package_reference(project_path, package_id, version): - tree = ET.parse(project_path) - root = tree.getroot() - - # Ensure there's at least one ItemGroup - item_groups = [e for e in root.findall(".//") if e.tag.endswith("ItemGroup")] - if not item_groups: - item_group = ET.SubElement(root, "ItemGroup") - else: - item_group = item_groups[0] - - pkg_ref = ET.SubElement(item_group, "PackageReference", Include=package_id, Version=version) - comment = ET.Comment(" Added by script ") - item_group.insert(list(item_group).index(pkg_ref), comment) - - ET.indent(tree, space=" ", level=0) - tree.write(project_path, encoding="utf-8", xml_declaration=True) - -def main(): - OUTPUT_DIR.mkdir(parents=True, exist_ok=True) - - for root, dirs, files in os.walk("."): - for file in files: - if file.endswith(".csproj") and not str(Path(root, file)).startswith(str(MOONLIGHT_DIR)): - csproj_path = Path(root) / file - version, tags = get_version_and_tags(csproj_path) - - if version: - print(f"Packing {csproj_path}") - pack_project(csproj_path) - - package_id = csproj_path.stem - for keyword, target_proj in TARGET_PROJECTS.items(): - if keyword in tags: - print(f"→ Adding {package_id} to {target_proj}") - add_package_reference(target_proj, package_id, version) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..2a3fd09 --- /dev/null +++ b/compose.yml @@ -0,0 +1,41 @@ +services: + api-server: + user: 0:0 + restart: always + image: moonlightpanel/panel:custom + build: + context: . + dockerfile: ./Moonlight.ApiServer/Dockerfile + ports: + - "9069:8080" + depends_on: + db: + condition: service_healthy + environment: + - MOONLIGHT_DATABASE_HOST=db + - MOONLIGHT_DATABASE_PORT=5432 + - MOONLIGHT_DATABASE_USERNAME=moonlight + - MOONLIGHT_DATABASE_PASSWORD=s3cret + - MOONLIGHT_DATABASE_DATABASE=moonlight + - MOONLIGHT_PUBLICURL=http://localhost:9069 + - MOONLIGHT_AUTHENTICATION_OAUTH2_ACCESSENDPOINT=http://localhost:8080 # Use this when moonlight is using local oauth2 and a different port as the public url + volumes: + - ./data/moonlight:/app/storage + links: + - db + pull_policy: never + + db: + image: postgres:latest + restart: always + environment: + - POSTGRES_USER=moonlight + - POSTGRES_DB=moonlight + - POSTGRES_PASSWORD=s3cret + volumes: + - ./data/database:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + interval: 10s + timeout: 5s + retries: 5 \ No newline at end of file