From 0958e8c48988cdf9ef25e32cfafead34d703bfbd Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 00:58:29 -0700 Subject: [PATCH 01/14] remove static directory, move script to run_example.sh --- examples/.gitignore | 4 ++++ examples/{build.sh => run_example.sh} | 6 +++--- examples/static/.gitignore | 6 ------ examples/static/index.html | 12 ------------ 4 files changed, 7 insertions(+), 21 deletions(-) create mode 100644 examples/.gitignore rename examples/{build.sh => run_example.sh} (91%) delete mode 100644 examples/static/.gitignore delete mode 100644 examples/static/index.html diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000000..8155d122af7 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,4 @@ +*/static/package.json +*/static/wasm_bg.wasm +*/static/wasm_bg.d.ts +*/static/wasm.d.ts diff --git a/examples/build.sh b/examples/run_example.sh similarity index 91% rename from examples/build.sh rename to examples/run_example.sh index 27d1c202f43..0bd01c9ff64 100755 --- a/examples/build.sh +++ b/examples/run_example.sh @@ -5,6 +5,7 @@ EXAMPLE=${1%\/} # Optimization level. Can be either "--debug" or "--release". Defaults to debug. PROFILE=${2:---debug} + # src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1 set -e # error -> trap -> exit function info() { echo -e "[\033[0;34m $* \033[0m]"; } # blue: [ info message ] @@ -38,11 +39,10 @@ cargo_build() { # wasm-pack build if [[ $EXAMPLE == *_wp ]]; then info "Building: $EXAMPLE using wasm-pack" - # wasm-pack overwrites .gitignore -> save -> restore - cp "$SRCDIR/static/.gitignore" "$SRCDIR/static/.gitignore.copy" wasm-pack build "$PROFILE" --target web --out-name wasm --out-dir "$SRCDIR/static/" - rm "$SRCDIR/static/.gitignore"; mv "$SRCDIR/static/.gitignore.copy" "$SRCDIR/static/.gitignore" # restore .gitignore + cd static + python3 ../start_example_server.py # multi_thread build -> two binary/wasm files elif [[ $EXAMPLE == multi_thread ]]; then info "Building: $EXAMPLE app using wasm-bindgen" diff --git a/examples/static/.gitignore b/examples/static/.gitignore deleted file mode 100644 index 4625e257bf3..00000000000 --- a/examples/static/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -*.wasm -*.js -*.json -*.ts -*.md -snippets/ diff --git a/examples/static/index.html b/examples/static/index.html deleted file mode 100644 index e1925eb8819..00000000000 --- a/examples/static/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - Yew example - - - - From 706824329cdd56948340aa39524bfefee22332d0 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:04:11 -0700 Subject: [PATCH 02/14] gitignore --- examples/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/.gitignore b/examples/.gitignore index 8155d122af7..3dd4131ef4b 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -2,3 +2,4 @@ */static/wasm_bg.wasm */static/wasm_bg.d.ts */static/wasm.d.ts +*/static/wasm.js From 4bac70e4535eba9a42b0f4fbf51bd154090bac32 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:04:19 -0700 Subject: [PATCH 03/14] server infra --- examples/run_example.sh | 15 ++++-- examples/start_example_server.py | 84 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 examples/start_example_server.py diff --git a/examples/run_example.sh b/examples/run_example.sh index 0bd01c9ff64..642afb648af 100755 --- a/examples/run_example.sh +++ b/examples/run_example.sh @@ -39,22 +39,27 @@ cargo_build() { # wasm-pack build if [[ $EXAMPLE == *_wp ]]; then info "Building: $EXAMPLE using wasm-pack" - wasm-pack build "$PROFILE" --target web --out-name wasm --out-dir "$SRCDIR/static/" + wasm-pack build "$PROFILE" --target web --out-name wasm --out-dir "$SRCDIR/$EXAMPLE/static/" cd static - python3 ../start_example_server.py + python3 ../../start_example_server.py # multi_thread build -> two binary/wasm files elif [[ $EXAMPLE == multi_thread ]]; then info "Building: $EXAMPLE app using wasm-bindgen" cargo_build --bin multi_thread_app - wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/static/" --out-name wasm "$TARGET_DIR/multi_thread_app.wasm" + wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name wasm "$TARGET_DIR/multi_thread_app.wasm" info "Building: $EXAMPLE worker using wasm-bindgen" cargo_build --bin multi_thread_worker - wasm-bindgen --target no-modules --no-typescript --out-dir "$SRCDIR/static/" --out-name worker "$TARGET_DIR/multi_thread_worker.wasm" + wasm-bindgen --target no-modules --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name worker "$TARGET_DIR/multi_thread_worker.wasm" + cd static + python3 ../../start_example_server.py else # Default wasm-bindgen build info "Building: $EXAMPLE using wasm-bindgen" cargo_build - wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/static/" --out-name wasm "$TARGET_DIR/$EXAMPLE.wasm" + wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name wasm "$TARGET_DIR/$EXAMPLE.wasm" + + cd static + python3 ../../start_example_server.py fi diff --git a/examples/start_example_server.py b/examples/start_example_server.py new file mode 100644 index 00000000000..7311011987f --- /dev/null +++ b/examples/start_example_server.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +from http.server import BaseHTTPRequestHandler, HTTPServer + +import os +import sys +import logging +import webbrowser + +PORT=8080 +STATIC_RESOURCES = { "/wasm.js", "/wasm_bg.wasm", "/favicon.ico" } +DEFAULT_INDEX_HTML = """ + + + + Yew example + + + +""".encode('utf-8') + +class BasicResource(object): + def __init__(self, mime, content): + self.mime = mime + self.content = content + +class S(BaseHTTPRequestHandler): + def _set_response(self, code, mime, charset='utf-8'): + self.send_response(code) + self.send_header('Content-type', f'{mime}; charset={charset}') + self.send_header('Cache-Control', 'private, max-age=0, must-revalidate') + self.end_headers() + + def do_GET(self): + path = self.path + if path in ('', '/'): + path = '/index.html' + + relative_path = f'.{path}' + print(relative_path) + + if os.path.isfile(relative_path): + with open(relative_path, "rb") as f: + mime = 'text/plain' + if relative_path.endswith('.html'): + mime = 'text/html' + elif relative_path.endswith('.wasm'): + mime = 'application/wasm' + elif relative_path.endswith('.js'): + mime = 'text/javascript' + elif relative_path.endswith('.json'): + mime = 'application/json' + elif relative_path.endswith('.css'): + mime = 'text/css' + elif relative_path.endswith('.toml'): + mime = 'application/toml' + self._set_response(200, mime) + self.wfile.write(f.read()) + elif path == '/index.html': + self._set_response(200, 'text/html') + self.wfile.write(DEFAULT_INDEX_HTML) + else: + self._set_response(404, mime='text/plain') + self.wfile.write(b"404 file not found") + +def run(server_class=HTTPServer, handler_class=S): + logging.basicConfig(level=logging.INFO) + server_address = ('', PORT) + httpd = server_class(server_address, handler_class) + webbrowser.open(f"http://localhost:{PORT}") + logging.info('Starting web server...\n') + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() + logging.info('Stopping web server...\n') + +if __name__ == '__main__': + from sys import argv + + run() From f0e72cbf8310dba298ea1659b9861a6855237e0f Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:35:36 -0700 Subject: [PATCH 04/14] Support --build-only --- examples/README.md | 3 +-- examples/run_example.sh | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/examples/README.md b/examples/README.md index 997b94c9201..c794b30ba5c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -16,8 +16,7 @@ Have a look at Yew's [starter templates](https://yew.rs/docs/getting-started/sta ```sh git clone https://github.com/yewstack/yew.git cd yew/examples -./build.sh minimal # example subfolder -cd static && python3 -m http.server # open localhost:8000 in browser +./run_example.sh minimal # builds and opens the "minimal" example in browser ``` diff --git a/examples/run_example.sh b/examples/run_example.sh index 642afb648af..f8084e24007 100755 --- a/examples/run_example.sh +++ b/examples/run_example.sh @@ -2,9 +2,34 @@ # The example to build. EXAMPLE=${1%\/} +shift + # Optimization level. Can be either "--debug" or "--release". Defaults to debug. -PROFILE=${2:---debug} +PROFILE="--debug" + +# Whether to open a browser window after building +START_BROWSER=1 +while (( "$#" )); do + case "$1" in + --release) + PROFILE="--release" + shift + ;; + --debug) + PROFILE="--debug" + shift + ;; + --build-only) + START_BROWSER=0 + shift + ;; + -*|--*=) # unsupported flags + echo "Error: Unsupported flag $1" >&2 + exit 1 + ;; + esac +done # src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1 set -e # error -> trap -> exit @@ -41,8 +66,6 @@ if [[ $EXAMPLE == *_wp ]]; then info "Building: $EXAMPLE using wasm-pack" wasm-pack build "$PROFILE" --target web --out-name wasm --out-dir "$SRCDIR/$EXAMPLE/static/" - cd static - python3 ../../start_example_server.py # multi_thread build -> two binary/wasm files elif [[ $EXAMPLE == multi_thread ]]; then info "Building: $EXAMPLE app using wasm-bindgen" @@ -53,13 +76,13 @@ elif [[ $EXAMPLE == multi_thread ]]; then cargo_build --bin multi_thread_worker wasm-bindgen --target no-modules --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name worker "$TARGET_DIR/multi_thread_worker.wasm" - cd static - python3 ../../start_example_server.py else # Default wasm-bindgen build info "Building: $EXAMPLE using wasm-bindgen" cargo_build wasm-bindgen --target web --no-typescript --out-dir "$SRCDIR/$EXAMPLE/static/" --out-name wasm "$TARGET_DIR/$EXAMPLE.wasm" - - cd static - python3 ../../start_example_server.py fi + +cd static +if [[ $START_BROWSER == 1 ]]; then + python3 ../../start_example_server.py $FLAGS +fi \ No newline at end of file From 8b53640141b1ad346ee84e813f6180ab8989b048 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:38:26 -0700 Subject: [PATCH 05/14] fix npm_and_rest example --- examples/npm_and_rest/static/index.html | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/npm_and_rest/static/index.html b/examples/npm_and_rest/static/index.html index 91c8c3688d6..5ba3f05eeff 100644 --- a/examples/npm_and_rest/static/index.html +++ b/examples/npm_and_rest/static/index.html @@ -4,8 +4,10 @@ Yew • npm and REST + - - - - + + \ No newline at end of file From 36f32997ea8ef715e6bdf78b61a5acb5911e7df8 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:49:52 -0700 Subject: [PATCH 06/14] Fix various static index.html's --- examples/js_callback/static/index.html | 9 ++++++--- examples/keyed_list/static/index.html | 9 ++++++--- examples/large_table/static/index.html | 9 ++++++--- examples/multi_thread/static/index.html | 10 +++++++--- examples/nested_list/static/index.html | 7 +++++-- examples/npm_and_rest/static/index.html | 5 +++-- examples/start_example_server.py | 1 + examples/todomvc/static/index.html | 4 ++-- examples/two_apps/static/index.html | 8 +++++--- 9 files changed, 41 insertions(+), 21 deletions(-) diff --git a/examples/js_callback/static/index.html b/examples/js_callback/static/index.html index 8e47c484fcc..c0f52bfd845 100644 --- a/examples/js_callback/static/index.html +++ b/examples/js_callback/static/index.html @@ -2,10 +2,13 @@ - (Asynchronous) callback from JavaScript + Yew • (Asynchronous) callback from JavaScript - + - + \ No newline at end of file diff --git a/examples/keyed_list/static/index.html b/examples/keyed_list/static/index.html index 43316a8e3e0..2d9c6e22f30 100644 --- a/examples/keyed_list/static/index.html +++ b/examples/keyed_list/static/index.html @@ -2,10 +2,13 @@ - Keyed list example + Yew • Keyed list - + - + \ No newline at end of file diff --git a/examples/large_table/static/index.html b/examples/large_table/static/index.html index f8b3189590a..259c8dcd537 100644 --- a/examples/large_table/static/index.html +++ b/examples/large_table/static/index.html @@ -2,10 +2,13 @@ - Table100x100 Test + Yew • Table 100x100 - + - + \ No newline at end of file diff --git a/examples/multi_thread/static/index.html b/examples/multi_thread/static/index.html index 86775e39e04..c844ea55c6a 100644 --- a/examples/multi_thread/static/index.html +++ b/examples/multi_thread/static/index.html @@ -3,7 +3,11 @@ Yew • Multi-Thread - - - + + + + \ No newline at end of file diff --git a/examples/nested_list/static/index.html b/examples/nested_list/static/index.html index 0d34f497c81..9cdf52270fb 100644 --- a/examples/nested_list/static/index.html +++ b/examples/nested_list/static/index.html @@ -6,6 +6,9 @@ - + - + \ No newline at end of file diff --git a/examples/npm_and_rest/static/index.html b/examples/npm_and_rest/static/index.html index 5ba3f05eeff..c933da511d9 100644 --- a/examples/npm_and_rest/static/index.html +++ b/examples/npm_and_rest/static/index.html @@ -4,10 +4,11 @@ Yew • npm and REST + + - - + \ No newline at end of file diff --git a/examples/start_example_server.py b/examples/start_example_server.py index 7311011987f..bd84ecbb352 100644 --- a/examples/start_example_server.py +++ b/examples/start_example_server.py @@ -42,6 +42,7 @@ def do_GET(self): print(relative_path) if os.path.isfile(relative_path): + print('is a file:', relative_path) with open(relative_path, "rb") as f: mime = 'text/plain' if relative_path.endswith('.html'): diff --git a/examples/todomvc/static/index.html b/examples/todomvc/static/index.html index ace3fb6e986..91723198ecb 100644 --- a/examples/todomvc/static/index.html +++ b/examples/todomvc/static/index.html @@ -1,7 +1,7 @@ - + Yew • TodoMVC @@ -12,4 +12,4 @@ init() - + \ No newline at end of file diff --git a/examples/two_apps/static/index.html b/examples/two_apps/static/index.html index 10b17a87142..07e58bb1969 100644 --- a/examples/two_apps/static/index.html +++ b/examples/two_apps/static/index.html @@ -3,11 +3,13 @@ Yew • Two Apps -
- + - + \ No newline at end of file From a11e78810af500a0fc51123bfb0544dc4c9d1260 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:52:45 -0700 Subject: [PATCH 07/14] Fix spacing --- examples/js_callback/static/index.html | 6 +++--- examples/keyed_list/static/index.html | 6 +++--- examples/large_table/static/index.html | 6 +++--- examples/multi_thread/static/index.html | 6 +++--- examples/nested_list/static/index.html | 6 +++--- examples/npm_and_rest/static/index.html | 6 +++--- examples/two_apps/static/index.html | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/examples/js_callback/static/index.html b/examples/js_callback/static/index.html index c0f52bfd845..fc12d569dd7 100644 --- a/examples/js_callback/static/index.html +++ b/examples/js_callback/static/index.html @@ -6,9 +6,9 @@ + import init from "./wasm.js" + init() + \ No newline at end of file diff --git a/examples/keyed_list/static/index.html b/examples/keyed_list/static/index.html index 2d9c6e22f30..7b9d23f5e80 100644 --- a/examples/keyed_list/static/index.html +++ b/examples/keyed_list/static/index.html @@ -7,8 +7,8 @@ + import init from "./wasm.js" + init() + \ No newline at end of file diff --git a/examples/large_table/static/index.html b/examples/large_table/static/index.html index 259c8dcd537..73b88b095c6 100644 --- a/examples/large_table/static/index.html +++ b/examples/large_table/static/index.html @@ -7,8 +7,8 @@ + import init from "./wasm.js" + init() + \ No newline at end of file diff --git a/examples/multi_thread/static/index.html b/examples/multi_thread/static/index.html index c844ea55c6a..606b610c968 100644 --- a/examples/multi_thread/static/index.html +++ b/examples/multi_thread/static/index.html @@ -6,8 +6,8 @@ + import init from "./wasm.js" + init() + \ No newline at end of file diff --git a/examples/nested_list/static/index.html b/examples/nested_list/static/index.html index 9cdf52270fb..cc19af61a7b 100644 --- a/examples/nested_list/static/index.html +++ b/examples/nested_list/static/index.html @@ -7,8 +7,8 @@ + import init from "./wasm.js" + init() + \ No newline at end of file diff --git a/examples/npm_and_rest/static/index.html b/examples/npm_and_rest/static/index.html index c933da511d9..a5cebb85bdb 100644 --- a/examples/npm_and_rest/static/index.html +++ b/examples/npm_and_rest/static/index.html @@ -7,8 +7,8 @@ + import init from "./wasm.js" + init() + \ No newline at end of file diff --git a/examples/two_apps/static/index.html b/examples/two_apps/static/index.html index 07e58bb1969..b92627c612d 100644 --- a/examples/two_apps/static/index.html +++ b/examples/two_apps/static/index.html @@ -8,8 +8,8 @@
+ import init from "./wasm.js" + init() + \ No newline at end of file From e9d9b937d2746ef01b11776854482739fdb798a2 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 02:59:52 -0700 Subject: [PATCH 08/14] newlines at the end --- examples/js_callback/static/index.html | 2 +- examples/keyed_list/static/index.html | 2 +- examples/large_table/static/index.html | 2 +- examples/multi_thread/static/index.html | 2 +- examples/nested_list/static/index.html | 2 +- examples/npm_and_rest/static/index.html | 2 +- examples/todomvc/static/index.html | 2 +- examples/two_apps/static/index.html | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/js_callback/static/index.html b/examples/js_callback/static/index.html index fc12d569dd7..71c70493589 100644 --- a/examples/js_callback/static/index.html +++ b/examples/js_callback/static/index.html @@ -11,4 +11,4 @@ - \ No newline at end of file + diff --git a/examples/keyed_list/static/index.html b/examples/keyed_list/static/index.html index 7b9d23f5e80..bc46533d2c9 100644 --- a/examples/keyed_list/static/index.html +++ b/examples/keyed_list/static/index.html @@ -11,4 +11,4 @@ init() - \ No newline at end of file + diff --git a/examples/large_table/static/index.html b/examples/large_table/static/index.html index 73b88b095c6..20960ab02f4 100644 --- a/examples/large_table/static/index.html +++ b/examples/large_table/static/index.html @@ -11,4 +11,4 @@ init() - \ No newline at end of file + diff --git a/examples/multi_thread/static/index.html b/examples/multi_thread/static/index.html index 606b610c968..5c452a8fab4 100644 --- a/examples/multi_thread/static/index.html +++ b/examples/multi_thread/static/index.html @@ -10,4 +10,4 @@ init() - \ No newline at end of file + diff --git a/examples/nested_list/static/index.html b/examples/nested_list/static/index.html index cc19af61a7b..68db027c7b4 100644 --- a/examples/nested_list/static/index.html +++ b/examples/nested_list/static/index.html @@ -11,4 +11,4 @@ init() - \ No newline at end of file + diff --git a/examples/npm_and_rest/static/index.html b/examples/npm_and_rest/static/index.html index a5cebb85bdb..adcec3b02db 100644 --- a/examples/npm_and_rest/static/index.html +++ b/examples/npm_and_rest/static/index.html @@ -11,4 +11,4 @@ init() - \ No newline at end of file + diff --git a/examples/todomvc/static/index.html b/examples/todomvc/static/index.html index 91723198ecb..0c23f1fa28b 100644 --- a/examples/todomvc/static/index.html +++ b/examples/todomvc/static/index.html @@ -12,4 +12,4 @@ init() - \ No newline at end of file + diff --git a/examples/two_apps/static/index.html b/examples/two_apps/static/index.html index b92627c612d..c9454dcc222 100644 --- a/examples/two_apps/static/index.html +++ b/examples/two_apps/static/index.html @@ -12,4 +12,4 @@ init() - \ No newline at end of file + From 6074878a2a2be51f178e9587121610a13e590097 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 03:01:06 -0700 Subject: [PATCH 09/14] Cleanup --- examples/start_example_server.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/examples/start_example_server.py b/examples/start_example_server.py index bd84ecbb352..67a123de04f 100644 --- a/examples/start_example_server.py +++ b/examples/start_example_server.py @@ -21,11 +21,6 @@ """.encode('utf-8') -class BasicResource(object): - def __init__(self, mime, content): - self.mime = mime - self.content = content - class S(BaseHTTPRequestHandler): def _set_response(self, code, mime, charset='utf-8'): self.send_response(code) @@ -37,9 +32,7 @@ def do_GET(self): path = self.path if path in ('', '/'): path = '/index.html' - relative_path = f'.{path}' - print(relative_path) if os.path.isfile(relative_path): print('is a file:', relative_path) From 5b535b30215e39f56e5e798446df385346cb0f94 Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 03:02:48 -0700 Subject: [PATCH 10/14] tabs -> spaces --- examples/start_example_server.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/start_example_server.py b/examples/start_example_server.py index 67a123de04f..4ffe1ae852a 100644 --- a/examples/start_example_server.py +++ b/examples/start_example_server.py @@ -14,9 +14,9 @@ Yew example + import init from "./wasm.js" + init() + """.encode('utf-8') From 555bbce75e5c88d66bc896adc4bb074b20f1417a Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Sat, 18 Jul 2020 03:04:00 -0700 Subject: [PATCH 11/14] line ending --- examples/run_example.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/run_example.sh b/examples/run_example.sh index f8084e24007..47138538b47 100755 --- a/examples/run_example.sh +++ b/examples/run_example.sh @@ -85,4 +85,4 @@ fi cd static if [[ $START_BROWSER == 1 ]]; then python3 ../../start_example_server.py $FLAGS -fi \ No newline at end of file +fi From 6256ea155ba9b919240c36f14799552a0834f83d Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Mon, 20 Jul 2020 22:31:12 -0700 Subject: [PATCH 12/14] remove build_examples.sh --- ci/build_examples.sh | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100755 ci/build_examples.sh diff --git a/ci/build_examples.sh b/ci/build_examples.sh deleted file mode 100755 index 9a49eef1c19..00000000000 --- a/ci/build_examples.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash - -# Script to build all examples in yew/examples -# -# Not building yew-router nor yewtil examples - -# src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1 -set -e # error -> trap -> exit -function info() { echo -e "[\033[0;34m $@ \033[0m]"; } # blue: [ info message ] -function fail() { FAIL="true"; echo -e "[\033[0;31mFAIL\033[0m] $@"; } # red: [FAIL] -trap 'LASTRES=$?; LAST=$BASH_COMMAND; if [[ LASTRES -ne 0 ]]; then fail "Command: \"$LAST\" exited with exit code: $LASTRES"; elif [ "$FAIL" == "true" ]; then fail finished with error; else echo -e "[\033[0;32m Finished $@ \033[0m]";fi' EXIT -SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # this source dir - -cd $SRCDIR/../examples # switch to examples folder - -for EXAMPLE in * -do - if [[ $EXAMPLE == static ]] || [[ $EXAMPLE == server ]] || [[ $EXAMPLE == target ]]; then - echo -e "Skipping folder: $EXAMPLE" - elif [ -d ${EXAMPLE} ]; then - ./build.sh ${EXAMPLE} $@ - fi -done From 5e1c0121e684f0d36b1e99e4551babc6b5a55f5b Mon Sep 17 00:00:00 2001 From: Philip Peterson Date: Tue, 21 Jul 2020 20:38:22 -0700 Subject: [PATCH 13/14] Apply suggestions from code review Co-authored-by: Simon --- examples/run_example.sh | 7 +++- examples/start_example_server.py | 62 ++++++++++++++++---------------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/examples/run_example.sh b/examples/run_example.sh index 47138538b47..8559e40a4b1 100755 --- a/examples/run_example.sh +++ b/examples/run_example.sh @@ -24,7 +24,7 @@ while (( "$#" )); do START_BROWSER=0 shift ;; - -*|--*=) # unsupported flags + -*) # unsupported flags echo "Error: Unsupported flag $1" >&2 exit 1 ;; @@ -84,5 +84,10 @@ fi cd static if [[ $START_BROWSER == 1 ]]; then + if ! [ -x "$(command -v python3)" ]; then + echo "WARNING: python3 not found! Please manually start a web server for the $SRCDIR/$EXAMPLE/static directory." + echo " Use '--build-only' to suppress this message." + exit 1 + fi python3 ../../start_example_server.py $FLAGS fi diff --git a/examples/start_example_server.py b/examples/start_example_server.py index 4ffe1ae852a..6bfa8349929 100644 --- a/examples/start_example_server.py +++ b/examples/start_example_server.py @@ -6,9 +6,10 @@ import logging import webbrowser -PORT=8080 +PORT = 8080 STATIC_RESOURCES = { "/wasm.js", "/wasm_bg.wasm", "/favicon.ico" } -DEFAULT_INDEX_HTML = """ +DEFAULT_INDEX_HTML = b""" + @@ -19,60 +20,61 @@ -""".encode('utf-8') + +""" class S(BaseHTTPRequestHandler): - def _set_response(self, code, mime, charset='utf-8'): + def _set_response(self, code, mime, charset="utf-8"): self.send_response(code) - self.send_header('Content-type', f'{mime}; charset={charset}') - self.send_header('Cache-Control', 'private, max-age=0, must-revalidate') + self.send_header("Content-type", f"{mime}; charset={charset}") + self.send_header("Cache-Control", "private, max-age=0, must-revalidate") self.end_headers() def do_GET(self): path = self.path - if path in ('', '/'): - path = '/index.html' - relative_path = f'.{path}' + if path in ("", "/"): + path = "/index.html" + relative_path = f".{path}" if os.path.isfile(relative_path): - print('is a file:', relative_path) + logging.info("is a file: %s", relative_path) with open(relative_path, "rb") as f: - mime = 'text/plain' - if relative_path.endswith('.html'): - mime = 'text/html' - elif relative_path.endswith('.wasm'): - mime = 'application/wasm' - elif relative_path.endswith('.js'): - mime = 'text/javascript' - elif relative_path.endswith('.json'): - mime = 'application/json' - elif relative_path.endswith('.css'): - mime = 'text/css' - elif relative_path.endswith('.toml'): - mime = 'application/toml' + mime = "text/plain" + if relative_path.endswith(".html"): + mime = "text/html" + elif relative_path.endswith(".wasm"): + mime = "application/wasm" + elif relative_path.endswith(".js"): + mime = "text/javascript" + elif relative_path.endswith(".json"): + mime = "application/json" + elif relative_path.endswith(".css"): + mime = "text/css" + elif relative_path.endswith(".toml"): + mime = "application/toml" self._set_response(200, mime) self.wfile.write(f.read()) - elif path == '/index.html': - self._set_response(200, 'text/html') + elif path == "/index.html": + self._set_response(200, "text/html") self.wfile.write(DEFAULT_INDEX_HTML) else: - self._set_response(404, mime='text/plain') + self._set_response(404, mime="text/plain") self.wfile.write(b"404 file not found") def run(server_class=HTTPServer, handler_class=S): logging.basicConfig(level=logging.INFO) - server_address = ('', PORT) + server_address = ("", PORT) httpd = server_class(server_address, handler_class) webbrowser.open(f"http://localhost:{PORT}") - logging.info('Starting web server...\n') + logging.info("Starting web server...\n") try: httpd.serve_forever() except KeyboardInterrupt: pass httpd.server_close() - logging.info('Stopping web server...\n') + logging.info("Stopping web server...\n") -if __name__ == '__main__': +if __name__ == "__main__": from sys import argv run() From 5b9047849a685ea5807a5650e331553b1ece4ea6 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 22 Jul 2020 13:13:11 +0200 Subject: [PATCH 14/14] format file and improve EXIT trap --- examples/run_example.sh | 60 ++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/examples/run_example.sh b/examples/run_example.sh index 8559e40a4b1..345c71573d9 100755 --- a/examples/run_example.sh +++ b/examples/run_example.sh @@ -10,33 +10,55 @@ PROFILE="--debug" # Whether to open a browser window after building START_BROWSER=1 -while (( "$#" )); do - case "$1" in +while (("$#")); do + case "$1" in --release) - PROFILE="--release" - shift - ;; + PROFILE="--release" + shift + ;; --debug) - PROFILE="--debug" - shift - ;; + PROFILE="--debug" + shift + ;; --build-only) - START_BROWSER=0 - shift - ;; + START_BROWSER=0 + shift + ;; -*) # unsupported flags - echo "Error: Unsupported flag $1" >&2 - exit 1 - ;; - esac + echo "Error: Unsupported flag $1" >&2 + exit 1 + ;; + esac done # src: https://gist.github.com/fbucek/f986da3cc3a9bbbd1573bdcb23fed2e1 set -e # error -> trap -> exit -function info() { echo -e "[\033[0;34m $* \033[0m]"; } # blue: [ info message ] -function fail() { FAIL="true"; echo -e "[\033[0;31mFAIL\033[0m] $*"; } # red: [FAIL] -trap 'LASTRES=$?; LAST=$BASH_COMMAND; if [[ LASTRES -ne 0 ]]; then fail "Command: \"$LAST\" exited with exit code: $LASTRES"; elif [ "$FAIL" == "true" ]; then fail finished with error; else echo -e "[\033[0;32m Finished! Run $EXAMPLE by serving the generated files in examples/static/ \033[0m]";fi' EXIT -SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" # this source dir + +info() { + # blue: [ info message ] + echo -e "[\033[0;34m $* \033[0m]" +} +fail() { + FAIL="true" + # red: [FAIL] + echo -e "[\033[0;31mFAIL\033[0m] $*" +} + +on_exit() { + LASTRES=$? + LAST=$BASH_COMMAND + if [[ LASTRES -ne 0 ]]; then + fail "Command: \"$LAST\" exited with exit code: $LASTRES" + elif [ "$FAIL" == "true" ]; then + fail finished with error + elif [[ $START_BROWSER != 1 ]]; then + echo -e "[\033[0;32m Finished! Run $EXAMPLE by serving the generated files in examples/static/ \033[0m]" + fi +} + +trap on_exit EXIT + +SRCDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" # this source dir cd "$SRCDIR/$EXAMPLE" # "$SRCDIR" ensures that this script can be run from anywhere.