Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM mcr.microsoft.com/devcontainers/base:trixie

ENV PIP_BREAK_SYSTEM_PACKAGES=1

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
micropython \
python3-pip \
curl \
ca-certificates \
&& pip3 install --no-cache-dir hatch \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install Node.js 24 LTS via nvm and enable Yarn globally through Corepack.
RUN su - vscode -c 'curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash \
&& . "$HOME/.nvm/nvm.sh" \
&& nvm install 24 \
&& corepack enable yarn'
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Rinkhals WebUI",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"remoteUser": "vscode",
"postCreateCommand": ".devcontainer/post-create.sh ${containerWorkspaceFolder}",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"tamasfe.even-better-toml",
"svelte.svelte-vscode"
],
"settings": {
"python.defaultInterpreterPath": "${containerWorkspaceFolder}/.venv/bin/python"
}
}
}
}
90 changes: 90 additions & 0 deletions .devcontainer/install_micropython_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env micropython
"""Install MicroPython packages via mip."""

import mip

PACKAGES = [
"__future__",
"abc",
"argparse",
"base64",
"binascii",
"bisect",
"cmd",
"collections",
"collections-defaultdict",
"contextlib",
"copy",
"curses.ascii",
"datetime",
"errno",
"fnmatch",
"functools",
"gzip",
"hashlib",
"hashlib-core",
"hashlib-sha224",
"hashlib-sha256",
"hashlib-sha384",
"hashlib-sha512",
"heapq",
"hmac",
"html",
"inspect",
"io",
"itertools",
"keyword",
"locale",
"logging",
"operator",
"os",
"os-path",
"pathlib",
"pickle",
"pkg_resources",
"pkgutil",
"pprint",
"quopri",
"random",
"shutil",
"ssl",
"stat",
"string",
"struct",
"tarfile",
"tarfile-write",
"tempfile",
"textwrap",
"threading",
"time",
"traceback",
"types",
"unittest",
"unittest-discover",
"uu",
"venv",
"warnings",
"zlib",
"github:josverl/micropython-stubs/mip/typing.json",
]


def install_all():
failures = []
for package in PACKAGES:
print("Installing {}".format(package))
try:
mip.install(package)
except Exception as exc:
failures.append((package, exc))
print("Failed installing {}: {}".format(package, exc))
if failures:
print("Completed with failures:")
for package, exc in failures:
print(" {}: {}".format(package, exc))
raise SystemExit(1)
print("All packages installed successfully.")


if __name__ == "__main__":
install_all()
15 changes: 15 additions & 0 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail

workspace_dir="${1:-}"

if [ -z "${workspace_dir}" ]; then
echo "post-create.sh requires the workspace path as its first argument" >&2
exit 1
fi

git config --global --add safe.directory "${workspace_dir}"

cd "${workspace_dir}"
hatch -v env create
micropython "${workspace_dir}/.devcontainer/install_micropython_lib.py"
18 changes: 9 additions & 9 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ on:

jobs:
build:
uses: ./.github/workflows/build.yml

release:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Build
run: |
docker run --rm \
-v ${{ github.workspace }}/build:/build \
-v ${{ github.workspace }}/rinkhals-webui:/rinkhals-webui \
ghcr.io/jbatonnet/rinkhals/build \
/bin/bash -c "chmod +x /build/build-swu.sh && /build/build-swu.sh /rinkhals-webui"
- name: Download artifacts
uses: actions/download-artifact@v8
with:
name: swu-packages
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -29,4 +29,4 @@ jobs:
--title="${GITHUB_REPOSITORY#*/} ${tag}" \
--generate-notes \
--draft \
build/dist/update-*.swu
update-*.swu
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build

on:
pull_request:
workflow_call:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: '24'
- uses: actions/setup-python@v6
with:
python-version: '3.x'
- name: Install build tools
run: |
corepack enable yarn
pip install hatch mpy-cross
- name: Build
run: hatch run build
- name: Upload artifacts
uses: actions/upload-artifact@v7
with:
name: swu-packages
path: build/dist/update-*.swu
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
.venv/
.ruff_cache/
.yarn/
.pnp.*
__pycache__/
node_modules/
build/dist/
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,23 @@ The web UI can be accessed at: `http://<printer-ip>:1414`

## Development

To package the application as an `update.swu` file, run the following command from the repository root:
This project uses a [dev container](https://containers.dev/) with build dependencies pre-configured.

To get started, open this repository in VS Code and select **Reopen in Container** when prompted (or run the **Dev Containers: Reopen in Container** command). This requires [Docker](https://www.docker.com/) and the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers).

### Build

To build the full `update.swu` packages:

```
docker run --rm -it -v .\build:/build -v .\rinkhals-webui:/rinkhals-webui ghcr.io/jbatonnet/rinkhals/build /build/build-swu.sh "/rinkhals-webui"
hatch run build
```

This runs three steps in sequence:
1. `build-frontend`: builds the web frontend
2. `build-mpy`: compiles Micropython bytecode
3. `build-swu`: creates installation packages

Individual steps can also be run separately: `hatch run <step-name>`.

Output files are written to `build/dist/`.
106 changes: 72 additions & 34 deletions build/build-swu.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,52 +1,90 @@
#!/bin/sh

# From a Windows machine:
# docker run --rm -it -v .\build:/build -v .\files:/files -v .\apps:/apps ghcr.io/jbatonnet/rinkhals/build /build/build-swu.sh "APP_PATH"
# Create SWU update packages for Rinkhals WebUI
#
# Expects build artifacts to already exist:
# - build/dist/mpy/
# - build/dist/frontend/
#
# Run `hatch run build` for the full pipeline, or call individual steps:
# - `hatch run build-frontend`
# - `hatch run build-mpy`
# - `hatch run build-swu`

set -e

# Prepare update
mkdir -p /tmp/update_swu
rm -rf /tmp/update_swu/*

cp /build/update.sh /tmp/update_swu/update.sh

APP_ROOT=$1
APP=$(basename $APP_ROOT)
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
STAGING_DIR=$(mktemp -d)
DIST_DIR="$SCRIPT_DIR/dist"
MPY_DIR="$DIST_DIR/mpy"

cleanup() {
rm -rf "$STAGING_DIR"
}
trap cleanup EXIT

# Prerequisites
for cmd in zip tar; do
if ! command -v "$cmd" > /dev/null 2>&1; then
echo "Error: $cmd is not installed" >&2
exit 1
fi
done

# Verify build artifacts
if [ ! -d "$DIST_DIR/frontend" ]; then
echo "Error: Frontend not built (build/dist/frontend/ missing)" >&2
echo "Run 'hatch run build-frontend' first" >&2
exit 1
fi

if [ ! -f $APP_ROOT/app.sh ]; then
echo "No app found in $APP_ROOT. Exiting SWU build"
if [ ! -d "$MPY_DIR" ]; then
echo "Error: Compiled bytecode not found (build/dist/mpy/ missing)" >&2
echo "Run 'hatch run build-mpy' first" >&2
exit 1
fi

echo "Preparing update package for $APP..."
# Stage the package
echo "Staging update package..."

mkdir -p /tmp/update_swu/$APP
cp -r $APP_ROOT/* /tmp/update_swu/$APP/
APP_STAGE="$STAGING_DIR/rinkhals-webui"
mkdir -p "$APP_STAGE/lib"

# Run app build script
if [ -f $APP_ROOT/build.sh ]; then
cd /tmp/update_swu/$APP
chmod +x build.sh
./build.sh
fi
# Add top-level app files
cp "$PROJECT_ROOT/rinkhals-webui/app.json" "$APP_STAGE/"
cp "$PROJECT_ROOT/rinkhals-webui/app.sh" "$APP_STAGE/"
chmod +x "$APP_STAGE/app.sh"

# Add compiled Micropython modules
cp -r "$MPY_DIR/rinkhals_webui" "$APP_STAGE/lib/rinkhals_webui"
cp -r "$MPY_DIR/microdot" "$APP_STAGE/lib/microdot"

# Add frontend
cp -r "$DIST_DIR/frontend" "$APP_STAGE/static"

# Create the setup.tar.gz
echo "Building update package..."
# Add update.sh at root of staging
cp "$SCRIPT_DIR/update.sh" "$STAGING_DIR/update.sh"

mkdir -p /build/dist/update_swu
rm -rf /build/dist/update_swu/*
# Create setup.tar.gz
echo "Creating setup.tar.gz..."
mkdir -p "$DIST_DIR"
rm -rf "$DIST_DIR/update_swu"
mkdir -p "$DIST_DIR/update_swu"

cd /tmp/update_swu
tar -czf /build/dist/update_swu/setup.tar.gz --exclude='setup.tar.gz' .
cd "$STAGING_DIR"
tar -czf "$DIST_DIR/update_swu/setup.tar.gz" .

# Create encrypted SWU packages
echo "Creating SWU packages..."
cd "$DIST_DIR"
rm -f update-k2p-k3.swu update-ks1.swu update-k3m.swu

# Create the update.swu
rm -rf /build/dist/update.swu
zip -P 'U2FsdGVkX19deTfqpXHZnB5GeyQ/dtlbHjkUnwgCi+w=' -r update-k2p-k3.swu update_swu
zip -P 'U2FsdGVkX1+lG6cHmshPLI/LaQr9cZCjA8HZt6Y8qmbB7riY' -r update-ks1.swu update_swu
zip -P '4DKXtEGStWHpPgZm8Xna9qluzAI8VJzpOsEIgd8brTLiXs8fLSu3vRx8o7fMf4h6' -r update-k3m.swu update_swu

cd /build/dist
zip -P U2FsdGVkX19deTfqpXHZnB5GeyQ/dtlbHjkUnwgCi+w= -r update-k2p-k3.swu update_swu
zip -P U2FsdGVkX1+lG6cHmshPLI/LaQr9cZCjA8HZt6Y8qmbB7riY -r update-ks1.swu update_swu
zip -P 4DKXtEGStWHpPgZm8Xna9qluzAI8VJzpOsEIgd8brTLiXs8fLSu3vRx8o7fMf4h6 -r update-k3m.swu update_swu
#rm -rf "$DIST_DIR/update_swu"

echo "Done, your update packages are ready: build/dist/update-*.swu"
echo "Done! SWU packages:"
ls -1 "$DIST_DIR"/update-*.swu
17 changes: 17 additions & 0 deletions build/compile-mpy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
# Compile all .py files in rinkhals-webui/src/ to .mpy bytecode in build/dist/mpy/
set -e

SRC=rinkhals-webui/src
OUT=build/dist/mpy

rm -rf "$OUT"

find "$SRC" -name '*.py' | while read -r f; do
rel="${f#$SRC/}"
out="$OUT/${rel%.py}.mpy"
mkdir -p "$(dirname "$out")"
mpy-cross "$f" -o "$out"
done

echo "Compiled $(find "$OUT" -name '*.mpy' | wc -l) files to $OUT/"
Empty file modified build/update.sh
100644 → 100755
Empty file.
Loading