Skip to content

Commit

Permalink
Automate client builds (#66)
Browse files Browse the repository at this point in the history
* Updated .gitignore

* Auto confirm yarn install

* Ignore nodeenv when linting

* Added nodeenv to gitignore

* Always use local node, build heuristics, build before publish

* Typo

---------

Co-authored-by: Brent Yi <[email protected]>
  • Loading branch information
origamiman72 and brentyi committed Jul 20, 2023
1 parent cc4bb26 commit ee96371
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 5,850 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
pip install --upgrade pip
pip install -e ".[dev]"
pip install build twine
# Build client files.
python -c "import viser; viser.ViserServer()"
- name: Build and publish
env:
PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ htmlcov
.DS_Store
.envrc
.lvimrc
viser/client/build
viser/client/.nodeenv
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = [
"gdown>=4.6.6",
"rich>=13.3.3",
"trimesh>=3.21.7",
"nodeenv>=1.8.0",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -71,9 +72,13 @@ profile = "black"
python_version = "3.8"
ignore_missing_imports = true
warn_unused_configs = true
exclude="viser/client/.nodeenv"

[tool.pyright]
exclude = ["./docs/**/*", "./examples/assets/**/*"]
exclude = ["./docs/**/*", "./examples/assets/**/*", "./viser/client/.nodeenv"]

[tool.black]
exclude = "viser/client/.nodeenv"

[tool.ruff]
select = [
Expand All @@ -99,3 +104,4 @@ ignore = [
"PLW0603", # Globa statement updates are discouraged.
"PLW2901", # For loop variable overwritten.
]
exclude = [ ".nodeenv" ]
64 changes: 64 additions & 0 deletions viser/_client_autobuild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import subprocess
import sys
from pathlib import Path

client_dir = Path(__file__).absolute().parent / "client"
build_dir = client_dir / "build"


def ensure_client_is_built() -> None:
"""Ensure that the client is built."""

if not (client_dir / "src").exists():
assert (build_dir / "index.html").exists(), (
"Something went wrong! At least one of the client source or build"
" directories should be present."
)
return

# Do we need to re-trigger a build?
build = False
if not (build_dir / "index.html").exists():
print("[viser] No client build found. Building now...")
build = True
elif _modified_time_recursive(client_dir / "src") > _modified_time_recursive(
build_dir
):
print("[viser] Client build looks out of date. Building now...")
build = True

# Install nodejs and build if necessary.
if build:
env_dir = _install_sandboxed_node()
npx_path = env_dir / "bin" / "npx"
subprocess.run(
args=(
f"source {env_dir / 'bin' / 'activate'};"
f"{npx_path} yarn install;"
"yarn run build;"
),
cwd=client_dir,
shell=True,
)


def _install_sandboxed_node() -> Path:
"""Install a sandboxed copy of nodejs using nodeenv, and return a path to the
environment root."""
env_dir = client_dir / ".nodeenv"
if (env_dir / "bin" / "npx").exists():
print("[viser] nodejs already set up!")
return env_dir

subprocess.run([sys.executable, "-m", "nodeenv", "--node=20.4.0", env_dir])
subprocess.run(
args=[env_dir / "bin" / "npm", "install", "yarn"],
input="y\n".encode(),
)
assert (env_dir / "bin" / "npx").exists()
return env_dir


def _modified_time_recursive(dir: Path) -> float:
"""Recursively get the last time a file was modified in a directory."""
return max([f.stat().st_mtime for f in dir.glob("**/*")])
4 changes: 3 additions & 1 deletion viser/_viser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy.typing as npt
from typing_extensions import override

from . import _messages, infra
from . import _client_autobuild, _messages, infra
from . import transforms as tf
from ._message_api import MessageApi, cast_vector
from ._scene_handle import FrameHandle, _SceneNodeHandleState
Expand Down Expand Up @@ -227,6 +227,8 @@ def __init__(self, host: str = "0.0.0.0", port: int = 8080):
)
super().__init__(server)

_client_autobuild.ensure_client_is_built()

state = _ViserServerState(server)
self._state = state
self._client_connect_cb: List[Callable[[ClientHandle], None]] = []
Expand Down
5,769 changes: 0 additions & 5,769 deletions viser/client/build/assets/index-27c5f284.js

This file was deleted.

1 change: 0 additions & 1 deletion viser/client/build/assets/index-a02dfc21.css

This file was deleted.

33 changes: 0 additions & 33 deletions viser/client/build/favicon.svg

This file was deleted.

Binary file removed viser/client/build/hdri/potsdamer_platz_1k.hdr
Binary file not shown.
27 changes: 0 additions & 27 deletions viser/client/build/index.html

This file was deleted.

15 changes: 0 additions & 15 deletions viser/client/build/manifest.json

This file was deleted.

3 changes: 0 additions & 3 deletions viser/client/build/robots.txt

This file was deleted.

0 comments on commit ee96371

Please sign in to comment.