Skip to content

Commit

Permalink
Add script to simplify local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
doctor-g committed Feb 1, 2024
1 parent 045b7b3 commit 7e0e5aa
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# Emacs backup files
*~

build/
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ Having set up that hook will ensure that all the automated tests are run prior t
That executable is now on your default path, so you can now run `godot` from the command line and the automated tests will work.
You can still create a shortcut to that executable on your Desktop for ease of mouse access, though there's real value to just opening the project you want from the command line.

### Locally testing the HTML build

[The Godot documentation](https://docs.godotengine.org/en/stable/tutorials/export/exporting_for_web.html#serving-the-files) describes the approach that we can use
to locally test a web build. This requires using the [`serve.py`](build/web/serve.py)
Python script, which comes from the [Godot Engine repository](https://raw.githubusercontent.com/godotengine/godot/master/platform/web/serve.py) and is copied locally into
the [`build/web`](build/web) folder.

First, export the project as an HTML5 Web build as normal.
Then, from the [build/web](build/web) directory, run:
```bash
python serve.py --root .
```

It should bring up a browser that loads the web build from the build folder.

### Per-Developer Project Settings Override

Expand Down
55 changes: 55 additions & 0 deletions build/web/serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

from http.server import HTTPServer, SimpleHTTPRequestHandler, test # type: ignore
from pathlib import Path
import os
import sys
import argparse
import subprocess


class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Access-Control-Allow-Origin", "*")
super().end_headers()


def shell_open(url):
if sys.platform == "win32":
os.startfile(url)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, url])


def serve(root, port, run_browser):
os.chdir(root)

if run_browser:
# Open the served page in the user's default browser.
print("Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this).")
shell_open(f"http://127.0.0.1:{port}")

test(CORSRequestHandler, HTTPServer, port=port)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", help="port to listen on", default=8060, type=int)
parser.add_argument(
"-r", "--root", help="path to serve as root (relative to `platform/web/`)", default="../../bin", type=Path
)
browser_parser = parser.add_mutually_exclusive_group(required=False)
browser_parser.add_argument(
"-n", "--no-browser", help="don't open default web browser automatically", dest="browser", action="store_false"
)
parser.set_defaults(browser=True)
args = parser.parse_args()

# Change to the directory where the script is located,
# so that the script can be run from any location.
os.chdir(Path(__file__).resolve().parent)

serve(args.root, args.port, args.browser)

0 comments on commit 7e0e5aa

Please sign in to comment.