feat(editor): finalize M2 content workspace UX
This commit is contained in:
1 parent
93e23ba194
commit
106d947c98
69 files changed
+5893
-4343
No files matched your search
@@ -10,7 +10,8 @@ readonly STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/blacksite-editor"
|
||||
readonly LOG_FILE="$STATE_DIR/launch.log"
|
||||
readonly STATUS_FILE="$STATE_DIR/splash.status"
|
||||
readonly SPLASH="$REPO/scripts/editor/splash.py"
|
||||
readonly CARGO_LANE="$REPO/scripts/codex/cargo_lane.py"
|
||||
readonly BUILD_PROGRESS="$REPO/scripts/editor/build_progress.py"
|
||||
readonly BUILD_STORAGE="$REPO/scripts/codex/build_storage.py"
|
||||
|
||||
build_only=false
|
||||
project_browser=false
|
||||
@@ -78,27 +79,55 @@ close_splash() {
|
||||
}
|
||||
trap close_splash EXIT
|
||||
|
||||
focus_existing() {
|
||||
find_existing() {
|
||||
local pid executable
|
||||
while read -r pid; do
|
||||
[[ -n "$pid" ]] || continue
|
||||
executable="$(readlink -f "/proc/$pid/exe" 2>/dev/null || true)"
|
||||
if [[ "$executable" == "$binary" ]]; then
|
||||
if command -v hyprctl >/dev/null 2>&1; then
|
||||
hyprctl dispatch focuswindow "pid:$pid" >/dev/null 2>&1 || true
|
||||
fi
|
||||
printf '%s\n' "$pid"
|
||||
return 0
|
||||
fi
|
||||
done < <(pgrep -x "$process_name" 2>/dev/null || true)
|
||||
return 1
|
||||
}
|
||||
|
||||
if ! $build_only && focus_existing; then
|
||||
exit 0
|
||||
fi
|
||||
focus_process() {
|
||||
local pid="$1"
|
||||
if command -v hyprctl >/dev/null 2>&1; then
|
||||
hyprctl dispatch focuswindow "pid:$pid" >/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
running_binary_is_current() {
|
||||
local pid="$1"
|
||||
local running_identity current_identity
|
||||
running_identity="$(stat -Lc '%d:%i' "/proc/$pid/exe" 2>/dev/null || true)"
|
||||
current_identity="$(stat -Lc '%d:%i' "$binary" 2>/dev/null || true)"
|
||||
[[ -n "$running_identity" && "$running_identity" == "$current_identity" ]]
|
||||
}
|
||||
|
||||
build_current_editor() {
|
||||
write_status "Checking build storage" "Validating the managed development lane" 6
|
||||
if ! python3 "$BUILD_STORAGE" enforce --phase pre >>"$LOG_FILE" 2>&1; then
|
||||
write_status "Build storage limit reached" \
|
||||
"Open the launch log for the checked storage report" 6 error
|
||||
return 1
|
||||
fi
|
||||
|
||||
python3 "$BUILD_PROGRESS" "$STATUS_FILE" "$LOG_FILE"
|
||||
local build_result=$?
|
||||
|
||||
if ! python3 "$BUILD_STORAGE" enforce --phase post >>"$LOG_FILE" 2>&1; then
|
||||
printf '%s\n' \
|
||||
"warning: build-storage postflight failed; see the report above" \
|
||||
>>"$LOG_FILE"
|
||||
fi
|
||||
return "$build_result"
|
||||
}
|
||||
|
||||
mkdir -p "$STATE_DIR"
|
||||
exec 9>"$STATE_DIR/launch.lock"
|
||||
exec 9>"$STATE_DIR/build.lock"
|
||||
if ! flock -n 9; then
|
||||
exit 0
|
||||
fi
|
||||
@@ -117,13 +146,50 @@ if ! cd "$REPO"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Capture the process before Cargo may atomically replace the on-disk executable. A launcher click
|
||||
# always checks/builds source updates first; it never silently focuses a stale process and makes the
|
||||
# user believe the running editor already contains the rebuilt UI.
|
||||
existing_pid=""
|
||||
if ! $build_only; then
|
||||
existing_pid="$(find_existing || true)"
|
||||
fi
|
||||
|
||||
if ! build_current_editor; then
|
||||
if $build_only; then
|
||||
tail -n 120 "$LOG_FILE" >&2
|
||||
else
|
||||
keep_splash=true
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -n "$existing_pid" ]] && kill -0 "$existing_pid" 2>/dev/null; then
|
||||
focus_process "$existing_pid"
|
||||
if running_binary_is_current "$existing_pid"; then
|
||||
write_status "Editor already open" "Focused the current development build" 100 close
|
||||
else
|
||||
write_status "Update ready" "Close and reopen the editor to activate it" 100 close
|
||||
if command -v notify-send >/dev/null 2>&1; then
|
||||
notify-send --app-name="Blacksite Editor" \
|
||||
"Blacksite update ready" \
|
||||
"Close the running editor normally, then reopen it to activate the rebuilt UI." \
|
||||
>/dev/null 2>&1 || true
|
||||
fi
|
||||
fi
|
||||
# The status above already asked the splash process to close. Prevent the EXIT trap from
|
||||
# replacing the meaningful "already open" or "update ready" result with a startup message.
|
||||
splash_pid=""
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if $build_only; then
|
||||
exec python3 "$CARGO_LANE" exec dev -- cargo build -p editor --bin editor
|
||||
printf 'Blacksite editor and project browser are current.\n'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ ! -x "$binary" ]]; then
|
||||
write_status "Editor build unavailable" \
|
||||
"Run blacksite-editor --build-only from a terminal" 100 error
|
||||
"The managed build completed without the requested binary" 100 error
|
||||
keep_splash=true
|
||||
exit 1
|
||||
fi
|
||||
@@ -175,6 +241,11 @@ watch_editor_window() {
|
||||
"The process is still running; check the launch log" 100 error
|
||||
}
|
||||
|
||||
# The lock protects the managed build and startup handoff, not the editor runtime. Leaving this
|
||||
# descriptor inherited across `exec` prevents later build-only updates for as long as the editor is
|
||||
# open.
|
||||
flock -u 9
|
||||
exec 9>&-
|
||||
watch_editor_window "$$" &
|
||||
exec "$binary" >>"$LOG_FILE" 2>&1
|
||||
|
||||
|
||||
@@ -8,9 +8,14 @@ import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Iterable
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
DEFAULT_CARGO_LANE = ROOT / "scripts/codex/cargo_lane.py"
|
||||
|
||||
|
||||
def write_status(
|
||||
path: Path,
|
||||
phase: str,
|
||||
@@ -27,10 +32,6 @@ def write_status(
|
||||
os.replace(temporary, path)
|
||||
|
||||
|
||||
def unique_crates(tree_output: str) -> set[str]:
|
||||
return {line.strip() for line in tree_output.splitlines() if line.strip()}
|
||||
|
||||
|
||||
def completed_crate(
|
||||
message: object, completed_package_ids: set[str]
|
||||
) -> tuple[bool, str]:
|
||||
@@ -46,39 +47,111 @@ def completed_crate(
|
||||
return True, package_id.rsplit("#", 1)[-1]
|
||||
|
||||
|
||||
def cargo_tree(cargo: str) -> list[str]:
|
||||
def managed_cargo(cargo_lane: Path, lane: str, *arguments: str) -> list[str]:
|
||||
return [
|
||||
cargo,
|
||||
"tree",
|
||||
"-p",
|
||||
"editor",
|
||||
"--features",
|
||||
"dev",
|
||||
"--edges",
|
||||
"normal,build",
|
||||
"--prefix",
|
||||
"none",
|
||||
"--format",
|
||||
"{p}",
|
||||
"--no-dedupe",
|
||||
"--color",
|
||||
"never",
|
||||
sys.executable,
|
||||
str(cargo_lane),
|
||||
"exec",
|
||||
lane,
|
||||
"--",
|
||||
"cargo",
|
||||
*arguments,
|
||||
]
|
||||
|
||||
|
||||
def cargo_build(cargo: str) -> list[str]:
|
||||
return [
|
||||
cargo,
|
||||
def host_target() -> str:
|
||||
completed = subprocess.run(
|
||||
["rustc", "-vV"],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=False,
|
||||
)
|
||||
for line in completed.stdout.splitlines():
|
||||
if line.startswith("host:"):
|
||||
return line.partition(":")[2].strip()
|
||||
return ""
|
||||
|
||||
|
||||
def cargo_metadata(cargo_lane: Path, lane: str, target: str) -> list[str]:
|
||||
arguments = ["metadata", "--format-version", "1"]
|
||||
if target:
|
||||
arguments.extend(["--filter-platform", target])
|
||||
return managed_cargo(
|
||||
cargo_lane,
|
||||
lane,
|
||||
*arguments,
|
||||
)
|
||||
|
||||
|
||||
def cargo_build(cargo_lane: Path, lane: str) -> list[str]:
|
||||
return managed_cargo(
|
||||
cargo_lane,
|
||||
lane,
|
||||
"build",
|
||||
"-p",
|
||||
"editor",
|
||||
"--bins",
|
||||
"--features",
|
||||
"dev",
|
||||
"--message-format=json-render-diagnostics",
|
||||
"--color",
|
||||
"never",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def editor_package_closure(metadata_output: str) -> set[str]:
|
||||
document = json.loads(metadata_output)
|
||||
packages = document.get("packages")
|
||||
resolve = document.get("resolve")
|
||||
if not isinstance(packages, list) or not isinstance(resolve, dict):
|
||||
raise ValueError("Cargo metadata did not include packages and a resolve graph")
|
||||
|
||||
editor_ids = {
|
||||
package.get("id")
|
||||
for package in packages
|
||||
if isinstance(package, dict)
|
||||
and package.get("name") == "editor"
|
||||
and isinstance(package.get("id"), str)
|
||||
}
|
||||
if len(editor_ids) != 1:
|
||||
raise ValueError("Cargo metadata did not identify exactly one editor package")
|
||||
root_id = next(iter(editor_ids))
|
||||
|
||||
nodes = resolve.get("nodes")
|
||||
if not isinstance(nodes, list):
|
||||
raise ValueError("Cargo metadata resolve graph did not include nodes")
|
||||
nodes_by_id = {
|
||||
node["id"]: node
|
||||
for node in nodes
|
||||
if isinstance(node, dict) and isinstance(node.get("id"), str)
|
||||
}
|
||||
|
||||
closure: set[str] = set()
|
||||
pending = [root_id]
|
||||
while pending:
|
||||
package_id = pending.pop()
|
||||
if package_id in closure:
|
||||
continue
|
||||
closure.add(package_id)
|
||||
node = nodes_by_id.get(package_id)
|
||||
if not isinstance(node, dict):
|
||||
continue
|
||||
dependencies = node.get("deps")
|
||||
if not isinstance(dependencies, list):
|
||||
continue
|
||||
for dependency in dependencies:
|
||||
if not isinstance(dependency, dict):
|
||||
continue
|
||||
dependency_id = dependency.get("pkg")
|
||||
kinds = dependency.get("dep_kinds")
|
||||
if not isinstance(dependency_id, str) or not isinstance(kinds, list):
|
||||
continue
|
||||
if any(
|
||||
isinstance(kind, dict) and kind.get("kind") in {None, "build"}
|
||||
for kind in kinds
|
||||
):
|
||||
pending.append(dependency_id)
|
||||
return closure
|
||||
|
||||
|
||||
def rendered_lines(message: object, raw_line: str) -> Iterable[str]:
|
||||
@@ -93,25 +166,33 @@ def rendered_lines(message: object, raw_line: str) -> Iterable[str]:
|
||||
yield rendered.rstrip("\n")
|
||||
|
||||
|
||||
def run(status_path: Path, log_path: Path, cargo: str) -> int:
|
||||
def run(status_path: Path, log_path: Path, cargo_lane: Path, lane: str) -> int:
|
||||
write_status(
|
||||
status_path,
|
||||
"Preparing build",
|
||||
"Resolving editor crate graph",
|
||||
8,
|
||||
)
|
||||
tree = subprocess.run(
|
||||
cargo_tree(cargo),
|
||||
metadata = subprocess.run(
|
||||
cargo_metadata(cargo_lane, lane, host_target()),
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
check=False,
|
||||
)
|
||||
crates = unique_crates(tree.stdout) if tree.returncode == 0 else set()
|
||||
try:
|
||||
crates = (
|
||||
editor_package_closure(metadata.stdout)
|
||||
if metadata.returncode == 0
|
||||
else set()
|
||||
)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
crates = set()
|
||||
if not crates:
|
||||
with log_path.open("a", encoding="utf-8") as log:
|
||||
log.write(tree.stdout)
|
||||
log.write(tree.stderr)
|
||||
log.write(metadata.stdout)
|
||||
log.write(metadata.stderr)
|
||||
write_status(
|
||||
status_path,
|
||||
"Build graph failed",
|
||||
@@ -119,7 +200,7 @@ def run(status_path: Path, log_path: Path, cargo: str) -> int:
|
||||
8,
|
||||
"error",
|
||||
)
|
||||
return tree.returncode or 1
|
||||
return metadata.returncode or 1
|
||||
|
||||
total = len(crates)
|
||||
write_status(
|
||||
@@ -131,7 +212,8 @@ def run(status_path: Path, log_path: Path, cargo: str) -> int:
|
||||
)
|
||||
|
||||
process = subprocess.Popen(
|
||||
cargo_build(cargo),
|
||||
cargo_build(cargo_lane, lane),
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
@@ -154,7 +236,7 @@ def run(status_path: Path, log_path: Path, cargo: str) -> int:
|
||||
changed, crate_name = completed_crate(message, completed_package_ids)
|
||||
if not changed:
|
||||
continue
|
||||
completed = min(len(completed_package_ids), total)
|
||||
completed = len(completed_package_ids.intersection(crates))
|
||||
progress = min(78, 12 + (66 * completed // total))
|
||||
write_status(
|
||||
status_path,
|
||||
@@ -165,7 +247,7 @@ def run(status_path: Path, log_path: Path, cargo: str) -> int:
|
||||
)
|
||||
|
||||
return_code = process.wait()
|
||||
completed = min(len(completed_package_ids), total)
|
||||
completed = len(completed_package_ids.intersection(crates))
|
||||
if return_code != 0:
|
||||
write_status(
|
||||
status_path,
|
||||
@@ -191,9 +273,15 @@ def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("status_path", type=Path)
|
||||
parser.add_argument("log_path", type=Path)
|
||||
parser.add_argument("--cargo", default="/usr/bin/cargo")
|
||||
parser.add_argument("--cargo-lane", type=Path, default=DEFAULT_CARGO_LANE)
|
||||
parser.add_argument("--lane", default="dev")
|
||||
arguments = parser.parse_args()
|
||||
return run(arguments.status_path, arguments.log_path, arguments.cargo)
|
||||
return run(
|
||||
arguments.status_path,
|
||||
arguments.log_path,
|
||||
arguments.cargo_lane,
|
||||
arguments.lane,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -6,6 +6,7 @@ import unittest
|
||||
|
||||
|
||||
MODULE_PATH = Path(__file__).with_name("build_progress.py")
|
||||
LAUNCHER_PATH = Path(__file__).with_name("blacksite-editor")
|
||||
SPEC = importlib.util.spec_from_file_location("build_progress", MODULE_PATH)
|
||||
assert SPEC is not None and SPEC.loader is not None
|
||||
BUILD_PROGRESS = importlib.util.module_from_spec(SPEC)
|
||||
@@ -13,11 +14,94 @@ SPEC.loader.exec_module(BUILD_PROGRESS)
|
||||
|
||||
|
||||
class BuildProgressTests(unittest.TestCase):
|
||||
def test_unique_crates_deduplicates_repeated_tree_entries(self) -> None:
|
||||
output = "editor v0.1.0\nserde v1.0.0\nserde v1.0.0\n"
|
||||
def test_desktop_wrapper_releases_the_build_lock_before_editor_exec(self) -> None:
|
||||
wrapper = LAUNCHER_PATH.read_text(encoding="utf-8")
|
||||
|
||||
self.assertIn('exec 9>"$STATE_DIR/build.lock"', wrapper)
|
||||
self.assertNotIn('exec 9>"$STATE_DIR/launch.lock"', wrapper)
|
||||
unlock = wrapper.index("flock -u 9")
|
||||
close = wrapper.index("exec 9>&-")
|
||||
editor_exec = wrapper.index('exec "$binary"')
|
||||
self.assertLess(unlock, close)
|
||||
self.assertLess(close, editor_exec)
|
||||
|
||||
def test_desktop_wrapper_builds_before_focusing_an_existing_editor(self) -> None:
|
||||
wrapper = LAUNCHER_PATH.read_text(encoding="utf-8")
|
||||
|
||||
capture_existing = wrapper.index('existing_pid="$(find_existing || true)"')
|
||||
build = wrapper.index("if ! build_current_editor")
|
||||
existing_handoff = wrapper.index(
|
||||
'if [[ -n "$existing_pid" ]] && kill -0 "$existing_pid"'
|
||||
)
|
||||
|
||||
self.assertLess(capture_existing, build)
|
||||
self.assertLess(build, existing_handoff)
|
||||
self.assertIn("running_binary_is_current", wrapper)
|
||||
self.assertIn("Blacksite update ready", wrapper)
|
||||
self.assertNotIn("if ! $build_only && focus_existing", wrapper)
|
||||
|
||||
def test_build_commands_use_the_managed_default_feature_lane(self) -> None:
|
||||
cargo_lane = Path("scripts/codex/cargo_lane.py")
|
||||
metadata = BUILD_PROGRESS.cargo_metadata(
|
||||
cargo_lane, "dev", "x86_64-unknown-linux-gnu"
|
||||
)
|
||||
build = BUILD_PROGRESS.cargo_build(cargo_lane, "dev")
|
||||
|
||||
self.assertEqual(
|
||||
BUILD_PROGRESS.unique_crates(output),
|
||||
{"editor v0.1.0", "serde v1.0.0"},
|
||||
metadata[:7],
|
||||
[
|
||||
BUILD_PROGRESS.sys.executable,
|
||||
str(cargo_lane),
|
||||
"exec",
|
||||
"dev",
|
||||
"--",
|
||||
"cargo",
|
||||
"metadata",
|
||||
],
|
||||
)
|
||||
self.assertIn("--filter-platform", metadata)
|
||||
self.assertIn("--bins", build)
|
||||
self.assertNotIn("--features", metadata)
|
||||
self.assertNotIn("--features", build)
|
||||
|
||||
def test_editor_package_closure_includes_normal_and_build_edges(self) -> None:
|
||||
metadata = {
|
||||
"packages": [
|
||||
{"id": "editor-id", "name": "editor"},
|
||||
{"id": "normal-id", "name": "normal"},
|
||||
{"id": "build-id", "name": "build"},
|
||||
{"id": "dev-id", "name": "dev"},
|
||||
],
|
||||
"resolve": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "editor-id",
|
||||
"deps": [
|
||||
{
|
||||
"pkg": "normal-id",
|
||||
"dep_kinds": [{"kind": None}],
|
||||
},
|
||||
{
|
||||
"pkg": "build-id",
|
||||
"dep_kinds": [{"kind": "build"}],
|
||||
},
|
||||
{
|
||||
"pkg": "dev-id",
|
||||
"dep_kinds": [{"kind": "dev"}],
|
||||
},
|
||||
],
|
||||
},
|
||||
{"id": "normal-id", "deps": []},
|
||||
{"id": "build-id", "deps": []},
|
||||
{"id": "dev-id", "deps": []},
|
||||
]
|
||||
},
|
||||
}
|
||||
self.assertEqual(
|
||||
BUILD_PROGRESS.editor_package_closure(
|
||||
BUILD_PROGRESS.json.dumps(metadata)
|
||||
),
|
||||
{"editor-id", "normal-id", "build-id"},
|
||||
)
|
||||
|
||||
def test_completed_crate_counts_each_package_once(self) -> None:
|
||||
|
||||
Reference in new issue
Block a user