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
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"animations",
"bundle-wry",
"canvas_stress_test",
"canvas_waves",
"clipboard",
Expand Down
1 change: 1 addition & 0 deletions examples/bundle-wry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
45 changes: 45 additions & 0 deletions examples/bundle-wry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "bundle-wry"
version = "0.1.0"
edition = "2021"
description = "Ratzilla web and wry desktop app example"
publish = false

[package.metadata.bundle]
name = "bundle-wry"
identifier = "rs.ratatui.bundle-wry"
icon = ["icons/icon.png"]
category = "Utility"
short_description = "ratatui-ratzilla bundle wry example"

[package.metadata.bundle.bin.bundle-wry]
name = "bundle-wry"
identifier = "rs.ratatui.bundle-wry"
icon = ["icons/icon.png"]
category = "Utility"
short_description = "bundle-wry"
resources = ["dist"]

[features]
default = ["bundle-wry"]
bundle-wry = ["dep:tao", "dep:wry", "dep:muda"]

[dependencies]
critical-section = { workspace = true, features = ["std"] }
muda = { version = "0.19", optional = true }
rand = { version = "0.9.2", default-features = false, features = ["small_rng"] }
ratzilla = { workspace = true }
tachyonfx = { version = "0.23.0", default-features = false, features = ["wasm"] }
tao = { version = "0.35.3", optional = true }
wasm-bindgen = "0.2.108"
## web-sys = { version = "0.3.81", features = [
## "Window",
## "Document",
## "Element",
## "HtmlElement",
## "Location",
## "Url",
## "UrlSearchParams",
## ], optional = true }
web-time = "1.1.0"
wry = { version = "0.55.1", optional = true }
59 changes: 59 additions & 0 deletions examples/bundle-wry/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[build]
# The index HTML file to drive the bundling process.
target = "index.html"
# Build in release mode (equivalent to --release).
release = false
# The output directory for all final assets.
dist = "dist"
# The public URL from which assets are to be served.
public_url = "./"
# Whether to include hash values in the output file names.
filehash = true

[watch]
# Paths to watch for changes (beyond the build.target parent folder).
watch = ["src"]#, "assets"]
# Paths to ignore.
ignore = []

[serve]
addresses = ["127.0.0.1"]
aliases = ["rs.ratatui.bundle-wry", "localhost"]
# The port to serve on.
port = 8080
# Open a browser tab once the initial build is complete.
open = true
# Disable auto-reload of the web app.
no_autoreload = true

[clean]
# The output directory to clean.
dist = "dist"
# Whether to perform a cargo clean as well.
cargo = false

[tools]
# Versions of tools to download if not present in the system.
sass = "1.69.5"
wasm_bindgen = "0.2.126"
wasm_opt = "version_126"

# --- Proxies ---
# Proxy requests from the development server to a backend.
[[proxy]]
# Requests to /api/ will be proxied to the backend.
backend = "http://localhost:9000/api/"
# The prefix to strip from the incoming request URI before sending it to the backend.
rewrite = "/api/"

# --- Hooks ---
# Hooks are executed as part of the Trunk pipeline.
[[hooks]]
stage = "pre_build"
command = "echo"
command_arguments = ["Starting build process..."]

[[hooks]]
stage = "build"
command = "sh"
command_arguments = ["-c", "echo Staging directory: $TRUNK_STAGING_DIR"]
Binary file added examples/bundle-wry/icons/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions examples/bundle-wry/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ratzilla Demo</title>
<link data-trunk rel="rust" data-bin="bundle-wry" data-cargo-no-default-features />
<style>
html {
height: 100%;
}
body {
position: relative;
height: calc(100% - 40px);
margin: 20px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
background-color: #121212;
}
canvas {
display: block;
width: calc(100vw - 40px);
height: 100%;
}
pre {
font-family: "Fira Code", monospace;
font-size: 16px;
margin: 0px;
}
.topbar {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
</head>
<body>
</body>
</html>
163 changes: 163 additions & 0 deletions examples/bundle-wry/scripts/bundle.sh

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not prefer to have custom scripts for each example. I think we should somehow unify them either in a justfile or in the top-level scripts/ directory.

Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/usr/bin/env bash

set -euo pipefail

source "$(dirname "${BASH_SOURCE[0]}")/common.sh"

usage() {
cat <<'EOF'
Usage: scripts/bundle.sh [--build] [--run] [--test]

Builds the macOS app bundle for bundle-wry.
--run opens the bundled macOS app.
--test opens the bundled macOS app, curls the bundled index.html, and verifies the HTML.
EOF
}

build=false
run=false
test=false

while [[ $# -gt 0 ]]; do
case "$1" in
--build)
build=true
shift
;;
--run)
run=true
shift
;;
--test)
test=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done

if [[ "$build" == false && "$run" == false && "$test" == false ]]; then
usage
exit 1
fi

root="$(repo_root)"
cd "$root"

bundle_root="$(cargo_target_dir)/release/bundle"
bundle_name="bundle-wry"
bundle_app_path() {
find "$bundle_root" -type d -name "${bundle_name}.app" -print -quit
}

build_bundle() {
ensure_wasm_target
ensure_cargo_bundle
ensure_trunk

trunk build --release --dist dist
cargo bundle --bin bundle-wry --release
}

ensure_bundle() {
if [[ -z "$(bundle_app_path)" ]]; then
build_bundle
fi
}

run_bundle() {
local app_path
app_path="$(bundle_app_path)"
if [[ -z "$app_path" ]]; then
echo "Missing bundled app; run scripts/bundle.sh --build first." >&2
exit 1
fi

open "$app_path"
}

test_bundle() {
local app_path
local html
local app_pid=""
local url="http://127.0.0.1:8080"

app_path="$(bundle_app_path)"
if [[ -z "$app_path" ]]; then
echo "Missing bundled app; run scripts/bundle.sh --build first." >&2
exit 1
fi

if lsof -nP -iTCP:8080 -sTCP:LISTEN >/dev/null 2>&1; then
echo "Port 8080 is already in use; stop the existing listener first." >&2
exit 1
fi

cleanup() {
if [[ -n "$app_pid" ]] && kill -0 "$app_pid" >/dev/null 2>&1; then
kill "$app_pid" >/dev/null 2>&1 || true
wait "$app_pid" >/dev/null 2>&1 || true
fi
}

trap cleanup EXIT

open "$app_path"

for _ in $(seq 1 60); do
app_pid="$(pgrep -n -f "${bundle_name}.app/Contents/MacOS/${bundle_name}" || true)"
if [[ -n "$app_pid" ]]; then
break
fi
sleep 1
done

if [[ -z "$app_pid" ]]; then
echo "Timed out waiting for bundled app process to start." >&2
exit 1
fi

for _ in $(seq 1 60); do
if html="$(curl -fsS "$url" 2>/dev/null)"; then
break
fi
sleep 1
done

if [[ -z "${html:-}" ]]; then
echo "Timed out waiting for $url" >&2
exit 1
fi

printf '%s' "$html" | grep -F '<title>Ratzilla Demo</title>'
js_path="$(printf '%s' "$html" | grep -oE "./[^']+\.js" | head -n 1)"
wasm_path="$(printf '%s' "$html" | grep -oE "./[^']+_bg\.wasm" | head -n 1)"
curl -fsS "$url/${js_path#./}" >/dev/null
curl -fsS "$url/${wasm_path#./}" >/dev/null

cleanup
trap - EXIT
printf 'Verified %s\n' "$url"
}

if [[ "$build" == true ]]; then
build_bundle
fi

if [[ "$run" == true ]]; then
ensure_bundle
run_bundle
fi

if [[ "$test" == true ]]; then
ensure_bundle
test_bundle
fi
Loading
Loading