From 34d955e9ac914e3c94370bfd18cf25e1a6f2e69b Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari <tushar27192@gmail.com> Date: Sat, 31 Jul 2021 22:02:48 +0530 Subject: [PATCH] Refactor examples list in CMake (#170) * Reduce example list duplication * Add COEP and COOP headers in local HTTP server * Revert Examples URL in readme --- README.md | 2 +- doc/CMakeLists.txt | 5 +- examples/CMakeLists.txt | 17 ++++--- examples/component/CMakeLists.txt | 6 +-- .../{util => component}/print_key_press.cpp | 0 examples/dom/CMakeLists.txt | 5 +- examples/index.html | 48 ++----------------- examples/run_webassembly.py | 20 ++++++++ examples/run_webassembly.sh | 6 --- examples/util/CMakeLists.txt | 6 --- 10 files changed, 40 insertions(+), 75 deletions(-) rename examples/{util => component}/print_key_press.cpp (100%) create mode 100755 examples/run_webassembly.py delete mode 100755 examples/run_webassembly.sh delete mode 100644 examples/util/CMakeLists.txt diff --git a/README.md b/README.md index d8c6e506..042ee0b8 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ A simple C++ library for terminal based user interface. - [Starter example project](https://github.com/ArthurSonzogni/ftxui-starter) - [Documentation](https://arthursonzogni.github.io/FTXUI/) -- [Examples (WebAssembly)](https://arthursonzogni.github.io/FTXUI/examples/) +- [Examples (WebAssembly)](https://arthursonzogni.com/FTXUI/examples/) - [Build using CMake](https://arthursonzogni.com/FTXUI/doc/#build-using-cmake) - [Build using nxxm](https://arthursonzogni.com/FTXUI/doc/#build-using-cmake) diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index b47de235..3062e2a1 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -3,10 +3,9 @@ if (DOXYGEN_FOUND) # Generate example list for documentation set(EXAMPLE_LIST "${CMAKE_CURRENT_BINARY_DIR}/example_list.md") file(WRITE ${EXAMPLE_LIST} "# Examples") - file(GLOB_RECURSE EXAMPLES RELATIVE ${PROJECT_SOURCE_DIR} - "${PROJECT_SOURCE_DIR}/examples/*.cpp") + get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES) foreach(EXAMPLE IN LISTS EXAMPLES) - file(APPEND ${EXAMPLE_LIST} "\n@example ${EXAMPLE}") + file(APPEND ${EXAMPLE_LIST} "\n@example examples/${EXAMPLE}.cpp") endforeach(EXAMPLE IN LISTS EXAMPLES) configure_file(Doxyfile.in Doxyfile @ONLY) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 41e32077..c30eb671 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,14 +1,19 @@ +set(EXAMPLES_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +function(example name) + add_executable(${name} ${name}.cpp) + target_link_libraries(${name} PUBLIC ${DIRECTORY_LIB}) + file(RELATIVE_PATH dir ${EXAMPLES_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) + set_property(GLOBAL APPEND PROPERTY FTXUI::EXAMPLES ${dir}/${name}) +endfunction(example) + add_subdirectory(component) add_subdirectory(dom) -add_subdirectory(util) if (EMSCRIPTEN) + get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES) foreach(file "index.html" - "run_webassembly.sh") - configure_file( - ${CMAKE_CURRENT_SOURCE_DIR}/${file} - ${CMAKE_CURRENT_BINARY_DIR}/${file} - ) + "run_webassembly.py") + configure_file(${file} ${file}) endforeach(file) endif() diff --git a/examples/component/CMakeLists.txt b/examples/component/CMakeLists.txt index 30702f58..29f61ab0 100644 --- a/examples/component/CMakeLists.txt +++ b/examples/component/CMakeLists.txt @@ -1,7 +1,4 @@ -function(example name) - add_executable(${name} ${name}.cpp) - target_link_libraries(${name} PUBLIC component) -endfunction(example) +set(DIRECTORY_LIB component) example(button) example(checkbox) @@ -23,3 +20,4 @@ example(tab_horizontal) example(tab_vertical) example(toggle) example(resizable_split) +example(print_key_press) diff --git a/examples/util/print_key_press.cpp b/examples/component/print_key_press.cpp similarity index 100% rename from examples/util/print_key_press.cpp rename to examples/component/print_key_press.cpp diff --git a/examples/dom/CMakeLists.txt b/examples/dom/CMakeLists.txt index e750925f..c1297ea3 100644 --- a/examples/dom/CMakeLists.txt +++ b/examples/dom/CMakeLists.txt @@ -1,7 +1,4 @@ -function(example name) - add_executable(${name} ${name}.cpp) - target_link_libraries(${name} PUBLIC dom) -endfunction(example) +set(DIRECTORY_LIB dom) example(border) example(color_gallery) diff --git a/examples/index.html b/examples/index.html index a4a427d9..fe903b62 100644 --- a/examples/index.html +++ b/examples/index.html @@ -23,52 +23,10 @@ </div> </body> <script> - let example_list = [ - "./component/button.js", - "./component/checkbox.js", - "./component/checkbox_in_frame.js", - "./component/gallery.js", - "./component/homescreen.js", - "./component/input.js", - "./component/menu.js", - "./component/menu2.js", - "./component/menu_style.js", - "./component/modal_dialog.js", - "./component/radiobox.js", - "./component/radiobox_in_frame.js", - "./component/slider.js", - "./component/tab_horizontal.js", - "./component/tab_vertical.js", - "./component/toggle.js", - "./dom/border.js", - "./dom/color_gallery.js", - "./dom/color_info_palette256.js", - "./dom/color_truecolor_HSV.js", - "./dom/color_truecolor_RGB.js", - "./dom/dbox.js", - "./dom/gauge.js", - "./dom/graph.js", - "./dom/hflow.js", - "./dom/html_like.js", - "./dom/package_manager.js", - "./dom/paragraph.js", - "./dom/separator.js", - "./dom/size.js", - "./dom/spinner.js", - "./dom/style_blink.js", - "./dom/style_bold.js", - "./dom/style_color.js", - "./dom/style_dim.js", - "./dom/style_gallery.js", - "./dom/style_inverted.js", - "./dom/style_underlined.js", - "./dom/vbox_hbox.js", - "./dom/window.js", - "./util/print_key_press.js", - ]; + const example_list = "@EXAMPLES@".split(";"); const url_search_params = new URLSearchParams(window.location.search); - const example = url_search_params.get("file") || "./dom/color_gallery.js" + const example = url_search_params.get("file") || "dom/color_gallery"; const select = document.getElementById("selectExample"); for(var i = 0; i < example_list.length; i++) { @@ -113,7 +71,7 @@ postRun: [], onRuntimeInitialized: () => {}, }; - document.querySelector("#example_script").src = example + document.querySelector("#example_script").src = example + '.js'; </script> <style> diff --git a/examples/run_webassembly.py b/examples/run_webassembly.py new file mode 100755 index 00000000..5e1a732f --- /dev/null +++ b/examples/run_webassembly.py @@ -0,0 +1,20 @@ +#! /usr/bin/python3 +from http.server import HTTPServer, SimpleHTTPRequestHandler +import sys +import webbrowser + +PORT = 8888 + +class CustomHTTPRequestHandler(SimpleHTTPRequestHandler): + def end_headers(self): + self.send_header("cross-origin-embedder-policy", "require-corp") + self.send_header("cross-origin-opener-policy", "same-origin") + SimpleHTTPRequestHandler.end_headers(self) + +with HTTPServer(("", PORT), CustomHTTPRequestHandler) as httpd: + try: + webbrowser.open("http://localhost:%s" % PORT) + print("serving at port", PORT) + httpd.serve_forever() + finally: + sys.exit(0) diff --git a/examples/run_webassembly.sh b/examples/run_webassembly.sh deleted file mode 100755 index 488c23c4..00000000 --- a/examples/run_webassembly.sh +++ /dev/null @@ -1,6 +0,0 @@ -#! /bin/bash -python3 -m http.server 8888 & -P1=$! -trap 'kill 0' SIGINT; P1 -python3 -m webbrowser http://localhost:8888 -wait $P1 diff --git a/examples/util/CMakeLists.txt b/examples/util/CMakeLists.txt deleted file mode 100644 index 7b2db4f6..00000000 --- a/examples/util/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -function(example name) - add_executable(${name} ${name}.cpp) - target_link_libraries(${name} PUBLIC component) -endfunction(example) - -example(print_key_press) -- GitLab