Skip to content

Commit 8504e4a

Browse files
committed
webflash: combine build and serve commands
1 parent ba154d4 commit 8504e4a

File tree

7 files changed

+95
-31
lines changed

7 files changed

+95
-31
lines changed

.github/workflows/deploy.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v4
1313
- run: git submodule update --init
14-
- run: |
15-
mkdir build
16-
cp src/index.html src/bg.jpg build
17-
cp tiliqua/gateware/src/tiliqua/flash_core.py build
18-
cp coi-serviceworker/coi-serviceworker.js build
14+
- name: Setup Python
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: "3.13"
18+
- name: Install PDM
19+
run: pip install pdm
20+
- name: Build application
21+
run: pdm build-site
1922
- name: Deploy to GitHub Pages
2023
uses: peaceiris/actions-gh-pages@v4
2124
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__/
44
.pytest_cache/
55
.ruff_cache/
66
.pdm-python
7+
build/

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ license = {text = "MIT"}
1313

1414
[tool.pdm]
1515
distribution = false
16+
17+
[tool.pdm.scripts]
18+
build-site = "python scripts/server.py --build-only"
19+
serve = "python scripts/server.py"

scripts/server.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import shutil
4+
import os
5+
from pathlib import Path
6+
7+
8+
def build_application():
9+
project_root = Path(__file__).parent.parent
10+
build_dir = project_root / "build"
11+
12+
if build_dir.exists():
13+
shutil.rmtree(build_dir)
14+
build_dir.mkdir()
15+
16+
files_to_copy = [
17+
("src/index.html", "index.html"),
18+
("src/bg.jpg", "bg.jpg"),
19+
("tiliqua/gateware/src/tiliqua/flash_core.py", "flash_core.py"),
20+
("coi-serviceworker/coi-serviceworker.js", "coi-serviceworker.js"),
21+
]
22+
23+
for src_path, dest_name in files_to_copy:
24+
src = project_root / src_path
25+
dest = build_dir / dest_name
26+
27+
if not src.exists():
28+
raise FileNotFoundError(f"Source file not found: {src}")
29+
30+
shutil.copy2(src, dest)
31+
print(f"Copied {src_path} -> build/{dest_name}")
32+
33+
print(f"Build completed successfully in {build_dir}")
34+
35+
36+
def serve_application():
37+
from flask import Flask, send_from_directory
38+
39+
build_dir = Path(__file__).parent.parent / "build"
40+
41+
if not build_dir.exists():
42+
raise FileNotFoundError(f"Build directory not found: {build_dir}. Run with --build-only first.")
43+
44+
app = Flask(__name__)
45+
46+
@app.after_request
47+
def add_security_headers(response):
48+
response.headers['Cross-Origin-Opener-Policy'] = 'same-origin'
49+
response.headers['Cross-Origin-Embedder-Policy'] = 'require-corp'
50+
return response
51+
52+
@app.route('/')
53+
def serve_index():
54+
return send_from_directory(build_dir, 'index.html')
55+
56+
@app.route('/<path:path>')
57+
def serve_file(path):
58+
return send_from_directory(build_dir, path)
59+
60+
port = int(os.environ.get('PORT', 8000))
61+
print(f"Serving from {build_dir} on http://localhost:{port}")
62+
app.run(host='localhost', port=port, debug=True)
63+
64+
65+
def main():
66+
parser = argparse.ArgumentParser(description='Build and/or serve tiliqua-webflash')
67+
parser.add_argument('--build-only', action='store_true',
68+
help='Only build the application, do not serve')
69+
70+
args = parser.parse_args()
71+
72+
build_application()
73+
74+
if args.build_only:
75+
print("Build-only mode: exiting after build")
76+
return
77+
78+
serve_application()
79+
80+
81+
if __name__ == "__main__":
82+
main()

src/coi-serviceworker.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/flash_core.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/server.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)