From e6c03ec74bef5a7ae0a85689c0fa2688c84c292a Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 19:59:40 +0900 Subject: [PATCH 01/14] Provide `tokio` by default --- automate/__main__.py | 70 +++++++++++++++---- .../example/native/hub/Cargo.toml | 5 +- .../example/native/hub/src/lib.rs | 8 +-- rust_crate/Cargo.toml | 4 +- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/automate/__main__.py b/automate/__main__.py index 5e4ecb484..5edf97559 100644 --- a/automate/__main__.py +++ b/automate/__main__.py @@ -3,6 +3,14 @@ import time +def replace_text_in_file(filepath: str, change_from: str, change_to: str): + with open(filepath, mode="r", encoding="utf8") as file: + content: str = file.read() + content = content.replace(change_from, change_to) + with open(filepath, mode="w", encoding="utf8") as file: + file.write(content) + + if len(sys.argv) == 1: print("Automation option is not provided.") print("Use `python automate --help` to see all available operations.") @@ -39,17 +47,35 @@ os.remove("Cargo.toml") + # Enable the web target, as it's not by default. + replace_text_in_file( + "native/hub/src/lib.rs", + "use tokio;", + "// use tokio;", + ) + replace_text_in_file( + "native/hub/src/lib.rs", + "// use tokio_with_wasm", + "use tokio_with_wasm", + ) + replace_text_in_file( + "native/hub/Cargo.toml", + "# wasm-bindgen", + "wasm-bindgen", + ) + replace_text_in_file( + "native/hub/Cargo.toml", + "# tokio_with_wasm", + "tokio_with_wasm", + ) + os.chdir("../") - filepath = "Cargo.toml" - with open(filepath, mode="r", encoding="utf8") as file: - content: str = file.read() - content = content.replace( + replace_text_in_file( + "Cargo.toml", "flutter_ffi_plugin/example/native/*", "test_app/native/*", ) - with open(filepath, mode="w", encoding="utf8") as file: - file.write(content) elif sys.argv[1] == "create-user-app": filepath = ".gitignore" @@ -73,17 +99,37 @@ # associated with the 'protoc_prebuilt' crate. time.sleep(60) + os.remove("Cargo.toml") + + # Enable the web target, as it's not by default. + replace_text_in_file( + "native/hub/src/lib.rs", + "use tokio;", + "// use tokio;", + ) + replace_text_in_file( + "native/hub/src/lib.rs", + "// use tokio_with_wasm", + "use tokio_with_wasm", + ) + replace_text_in_file( + "native/hub/Cargo.toml", + "# wasm-bindgen", + "wasm-bindgen", + ) + replace_text_in_file( + "native/hub/Cargo.toml", + "# tokio_with_wasm", + "tokio_with_wasm", + ) + os.chdir("../") - filepath = "Cargo.toml" - with open(filepath, mode="r", encoding="utf8") as file: - content: str = file.read() - content = content.replace( + replace_text_in_file( + "Cargo.toml", "flutter_ffi_plugin/example/native/*", "user_app/native/*", ) - with open(filepath, mode="w", encoding="utf8") as file: - file.write(content) else: print("No such option for automation is available.") diff --git a/flutter_ffi_plugin/example/native/hub/Cargo.toml b/flutter_ffi_plugin/example/native/hub/Cargo.toml index 613534767..f721cef90 100755 --- a/flutter_ffi_plugin/example/native/hub/Cargo.toml +++ b/flutter_ffi_plugin/example/native/hub/Cargo.toml @@ -14,6 +14,7 @@ crate-type = ["lib", "cdylib", "staticlib"] [dependencies] rinf = "6.9.2" prost = "0.12.3" -wasm-bindgen = "0.2.91" -tokio_with_wasm = "0.4.3" +tokio = { version = "1", features = ["rt-multi-thread", "sync", "macros"] } +# wasm-bindgen = "0.2.92" # Uncomment this line to target the web +# tokio_with_wasm = "0.4.4" # Uncomment this line to target the web sample_crate = { path = "../sample_crate" } diff --git a/flutter_ffi_plugin/example/native/hub/src/lib.rs b/flutter_ffi_plugin/example/native/hub/src/lib.rs index 11bdb60f0..45902d708 100755 --- a/flutter_ffi_plugin/example/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/example/native/hub/src/lib.rs @@ -1,14 +1,12 @@ //! This `hub` crate is the //! entry point of the Rust logic. -// This `tokio` will be used by Rinf. -// You can replace it with the original `tokio` -// if you're not targeting the web. -use tokio_with_wasm::tokio; - mod messages; mod sample_functions; +use tokio; +// use tokio_with_wasm::tokio; // Uncomment this line to target the web + rinf::write_interface!(); // Always use non-blocking async functions diff --git a/rust_crate/Cargo.toml b/rust_crate/Cargo.toml index fa0606c7f..8763ec01c 100644 --- a/rust_crate/Cargo.toml +++ b/rust_crate/Cargo.toml @@ -17,5 +17,5 @@ which = "6.0.0" allo-isolate = "0.1.24" [target.'cfg(target_family = "wasm")'.dependencies] -js-sys = "0.3.67" -wasm-bindgen = "0.2.91" +js-sys = "0.3.69" +wasm-bindgen = "0.2.92" From 254648d09a3fce19b2123a284c7fd2e8fdf814f1 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 21:39:10 +0900 Subject: [PATCH 02/14] Fix a comment --- flutter_ffi_plugin/example/native/hub/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter_ffi_plugin/example/native/hub/src/lib.rs b/flutter_ffi_plugin/example/native/hub/src/lib.rs index 45902d708..32791b301 100755 --- a/flutter_ffi_plugin/example/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/example/native/hub/src/lib.rs @@ -15,7 +15,7 @@ rinf::write_interface!(); // use `tokio::task::spawn_blocking`. async fn main() { // Repeat `tokio::spawn` anywhere in your code - // if more concurrent tasks are needed. + // to run concurrent tasks. tokio::spawn(sample_functions::tell_numbers()); tokio::spawn(sample_functions::stream_fractal()); tokio::spawn(sample_functions::run_debug_tests()); From f9ca6c3d046a05ab13014e591e76e13e2997f8cd Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 22:01:31 +0900 Subject: [PATCH 03/14] Separate template from example --- .github/workflows/example_test.yaml | 110 ++++++++++++++++++ flutter_ffi_plugin/bin/src/helpers.dart | 15 +-- .../example/native/hub/Cargo.toml | 5 +- .../example/native/hub/src/lib.rs | 7 +- .../native/hub/src/sample_functions.rs | 9 +- .../template/messages/README.md | 3 + .../template/messages/counter_number.proto | 12 ++ flutter_ffi_plugin/template/native/README.md | 7 ++ .../template/native/hub/Cargo.toml | 19 +++ .../template/native/hub/src/lib.rs | 20 ++++ 10 files changed, 179 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/example_test.yaml create mode 100644 flutter_ffi_plugin/template/messages/README.md create mode 100644 flutter_ffi_plugin/template/messages/counter_number.proto create mode 100644 flutter_ffi_plugin/template/native/README.md create mode 100644 flutter_ffi_plugin/template/native/hub/Cargo.toml create mode 100644 flutter_ffi_plugin/template/native/hub/src/lib.rs diff --git a/.github/workflows/example_test.yaml b/.github/workflows/example_test.yaml new file mode 100644 index 000000000..1a5d43d7e --- /dev/null +++ b/.github/workflows/example_test.yaml @@ -0,0 +1,110 @@ +# Verify that the example app works properly. +name: example-test + +on: + push: + branches: + - main + paths-ignore: + - "**.md" + - "**.html" + pull_request: + paths-ignore: + - "**.md" + - "**.html" + workflow_dispatch: + +concurrency: + # Cancels the workflow + # when another event in the same context happens. + # If it's a PR, context is the pull request number. + # Otherwise, it uses the Git reference(branch or tag name). + group: > + ${{ github.workflow }} + ${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + build: + name: ${{ matrix.runner }} / ${{ matrix.target }} + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false # Important + matrix: + runner: [ubuntu-latest, windows-latest, macos-latest] + target: [android, web] # On all platforms + include: + # Specify targets for each platform + - runner: ubuntu-latest + target: linux + - runner: windows-latest + target: windows + - runner: macos-latest + target: macos + - runner: macos-latest + target: ios + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + submodules: true + + - name: Setup Flutter SDK + uses: subosito/flutter-action@v2 + with: + channel: "stable" + + - name: Setup Rust toolchain + uses: dtolnay/rust-toolchain@stable + + - name: Setup Ninja and GTK3 toolchain (Only Linux target) + if: matrix.target == 'linux' + run: | + sudo apt-get update -y + sudo apt-get install -y ninja-build libgtk-3-dev + + - name: Setup Java toolchain (Only Android target) + if: matrix.target == 'android' + uses: actions/setup-java@v4 + with: + distribution: "zulu" + java-version: "11" + + - name: Install the CLI tool + working-directory: rust_crate/ + run: cargo install --path ./ + + - name: Build the example app + if: matrix.target == 'linux' + working-directory: flutter_ffi_plugin/example/ + run: flutter build linux --verbose + + - name: Build the example app + if: matrix.target == 'android' + working-directory: flutter_ffi_plugin/example/ + run: | + flutter build apk --verbose + flutter build appbundle --verbose + + - name: Build the example app + if: matrix.target == 'windows' + working-directory: flutter_ffi_plugin/example/ + run: flutter build windows --verbose + + - name: Build the example app + if: matrix.target == 'macos' + working-directory: flutter_ffi_plugin/example/ + run: flutter build macos --verbose + + - name: Build the example app + if: matrix.target == 'ios' + working-directory: flutter_ffi_plugin/example/ + run: flutter build ios --verbose --no-codesign + + - name: Build the example app + if: matrix.target == 'web' + working-directory: flutter_ffi_plugin/example/ + run: | + rinf wasm --release + flutter build web --verbose diff --git a/flutter_ffi_plugin/bin/src/helpers.dart b/flutter_ffi_plugin/bin/src/helpers.dart index 2d1ae41c0..ece9703e8 100644 --- a/flutter_ffi_plugin/bin/src/helpers.dart +++ b/flutter_ffi_plugin/bin/src/helpers.dart @@ -38,10 +38,10 @@ Future applyRustTemplate({ } // Copy basic folders needed for Rust to work - final templateSource = packagePath.join("example/native/"); + final templateSource = packagePath.join("template/native/"); final templateDestination = flutterProjectPath.join("native/"); await copyDirectory(templateSource, templateDestination); - final messagesSource = packagePath.join("example/messages/"); + final messagesSource = packagePath.join("template/messages/"); final messagesDestination = flutterProjectPath.join("messages/"); await copyDirectory(messagesSource, messagesDestination); @@ -59,17 +59,6 @@ resolver = "2" final cargoFile = File.fromUri(flutterProjectPath.join('Cargo.toml')); await cargoFile.writeAsString(cargoText); - // Disable demonstrations in sample functions - final sampleFunctionsFile = File.fromUri( - flutterProjectPath.join('native/hub/src/sample_functions.rs'), - ); - var sampleFunctionsContent = await sampleFunctionsFile.readAsString(); - sampleFunctionsContent = sampleFunctionsContent.replaceAll( - 'SHOULD_DEMONSTRATE: bool = true', - 'SHOULD_DEMONSTRATE: bool = false', - ); - await sampleFunctionsFile.writeAsString(sampleFunctionsContent); - // Add some lines to `.gitignore` final rustSectionTitle = '# Rust related'; final messageSectionTitle = '# Generated messages'; diff --git a/flutter_ffi_plugin/example/native/hub/Cargo.toml b/flutter_ffi_plugin/example/native/hub/Cargo.toml index f721cef90..d6480f566 100755 --- a/flutter_ffi_plugin/example/native/hub/Cargo.toml +++ b/flutter_ffi_plugin/example/native/hub/Cargo.toml @@ -14,7 +14,6 @@ crate-type = ["lib", "cdylib", "staticlib"] [dependencies] rinf = "6.9.2" prost = "0.12.3" -tokio = { version = "1", features = ["rt-multi-thread", "sync", "macros"] } -# wasm-bindgen = "0.2.92" # Uncomment this line to target the web -# tokio_with_wasm = "0.4.4" # Uncomment this line to target the web +wasm-bindgen = "0.2.92" # Uncomment this line to target the web +tokio_with_wasm = "0.4.4" # Uncomment this line to target the web sample_crate = { path = "../sample_crate" } diff --git a/flutter_ffi_plugin/example/native/hub/src/lib.rs b/flutter_ffi_plugin/example/native/hub/src/lib.rs index 32791b301..efaa8c626 100755 --- a/flutter_ffi_plugin/example/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/example/native/hub/src/lib.rs @@ -4,8 +4,8 @@ mod messages; mod sample_functions; -use tokio; -// use tokio_with_wasm::tokio; // Uncomment this line to target the web +// use tokio; +use tokio_with_wasm::tokio; // Uncomment this line to target the web rinf::write_interface!(); @@ -14,8 +14,7 @@ rinf::write_interface!(); // If you really need to use blocking code, // use `tokio::task::spawn_blocking`. async fn main() { - // Repeat `tokio::spawn` anywhere in your code - // to run concurrent tasks. + // Use `tokio::spawn` to run concurrent tasks. tokio::spawn(sample_functions::tell_numbers()); tokio::spawn(sample_functions::stream_fractal()); tokio::spawn(sample_functions::run_debug_tests()); diff --git a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs index dd78f3c85..d2e94b769 100755 --- a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs +++ b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs @@ -6,9 +6,6 @@ use crate::tokio; use rinf::debug_print; use tokio::sync::Mutex; -// Disabled when applied as Rinf template. -const SHOULD_DEMONSTRATE: bool = true; - // Using the `cfg` macro enables conditional statement. #[cfg(debug_assertions)] const IS_DEBUG_MODE: bool = true; @@ -53,10 +50,6 @@ pub async fn stream_fractal() { use messages::counter_number::*; use messages::fractal_art::*; - if !SHOULD_DEMONSTRATE { - return; - } - let mut current_scale: f64 = 1.0; let (sender, mut receiver) = tokio::sync::mpsc::channel(5); @@ -118,7 +111,7 @@ async fn use_messages() { // Business logic for testing various crates. pub async fn run_debug_tests() { - if !SHOULD_DEMONSTRATE || !IS_DEBUG_MODE { + if !IS_DEBUG_MODE { return; } diff --git a/flutter_ffi_plugin/template/messages/README.md b/flutter_ffi_plugin/template/messages/README.md new file mode 100644 index 000000000..85c54229c --- /dev/null +++ b/flutter_ffi_plugin/template/messages/README.md @@ -0,0 +1,3 @@ +# Protobuf Messages + +This folder contains Protobuf message files that serve as an API between Dart and Rust. `.proto` files can be compiled into Dart and Rust code with the command `rinf message`, provided by the [Rinf](https://rinf.cunarist.com) framework. diff --git a/flutter_ffi_plugin/template/messages/counter_number.proto b/flutter_ffi_plugin/template/messages/counter_number.proto new file mode 100644 index 000000000..fba402b6c --- /dev/null +++ b/flutter_ffi_plugin/template/messages/counter_number.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package counter_number; + +// [RINF:DART-SIGNAL] +message NumberInput { + string letter = 1; +} + +// [RINF:RUST-SIGNAL] +message NumberOutput { + int32 current_number = 1; +} diff --git a/flutter_ffi_plugin/template/native/README.md b/flutter_ffi_plugin/template/native/README.md new file mode 100644 index 000000000..101675f82 --- /dev/null +++ b/flutter_ffi_plugin/template/native/README.md @@ -0,0 +1,7 @@ +# Rust Crates + +This folder contains Rust crates. Entry point of the Rust logic is the `hub` library crate. These crates are integrated and compiled into the Flutter app by the [Rinf](https://rinf.cunarist.com) framework. + +- Do NOT change the name of the `hub` crate. Compilation presets expect the entry library crate to be located at `./native/hub`. +- Do NOT modify the `bridge` module inside `./native/hub/src` unless you know what you're doing. +- You CAN name crates other than `hub` as you want. diff --git a/flutter_ffi_plugin/template/native/hub/Cargo.toml b/flutter_ffi_plugin/template/native/hub/Cargo.toml new file mode 100644 index 000000000..e69dcc35e --- /dev/null +++ b/flutter_ffi_plugin/template/native/hub/Cargo.toml @@ -0,0 +1,19 @@ +[package] +# Do not change the name of this crate. +name = "hub" +version = "0.1.0" +edition = "2021" + +[lib] +# `lib` is required for non-library targets, +# such as tests and benchmarks. +# `cdylib` is for Linux, Android, Windows, and web. +# `staticlib` is for iOS and macOS. +crate-type = ["lib", "cdylib", "staticlib"] + +[dependencies] +rinf = "6.9.2" +prost = "0.12.3" +tokio = { version = "1", features = ["rt-multi-thread", "sync", "macros"] } +# wasm-bindgen = "0.2.92" # Uncomment this line to target the web +# tokio_with_wasm = "0.4.4" # Uncomment this line to target the web diff --git a/flutter_ffi_plugin/template/native/hub/src/lib.rs b/flutter_ffi_plugin/template/native/hub/src/lib.rs new file mode 100644 index 000000000..3ed86bf29 --- /dev/null +++ b/flutter_ffi_plugin/template/native/hub/src/lib.rs @@ -0,0 +1,20 @@ +//! This `hub` crate is the +//! entry point of the Rust logic. + +mod messages; + +use tokio; +// use tokio_with_wasm::tokio; // Uncomment this line to target the web + +rinf::write_interface!(); + +// Always use non-blocking async functions +// such as `tokio::fs::File::open`. +// If you really need to use blocking code, +// use `tokio::task::spawn_blocking`. +async fn main() { + // Use `tokio::spawn` to run concurrent tasks. + use messages::counter_number::*; + let _receiver = NumberInput::get_dart_signal_receiver(); + NumberOutput { current_number: 7 }.send_signal_to_dart(); +} From fd765d15c03af569a76ef198a758dfa0c6f35b77 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 22:02:30 +0900 Subject: [PATCH 04/14] Rename files --- .github/workflows/{example_test.yaml => build_example_app.yaml} | 0 .github/workflows/{build_test.yaml => build_test_app.yaml} | 0 .github/workflows/{user_installation.yaml => build_user_app.yaml} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{example_test.yaml => build_example_app.yaml} (100%) rename .github/workflows/{build_test.yaml => build_test_app.yaml} (100%) rename .github/workflows/{user_installation.yaml => build_user_app.yaml} (100%) diff --git a/.github/workflows/example_test.yaml b/.github/workflows/build_example_app.yaml similarity index 100% rename from .github/workflows/example_test.yaml rename to .github/workflows/build_example_app.yaml diff --git a/.github/workflows/build_test.yaml b/.github/workflows/build_test_app.yaml similarity index 100% rename from .github/workflows/build_test.yaml rename to .github/workflows/build_test_app.yaml diff --git a/.github/workflows/user_installation.yaml b/.github/workflows/build_user_app.yaml similarity index 100% rename from .github/workflows/user_installation.yaml rename to .github/workflows/build_user_app.yaml From 60b995090908b89e27f46895ada7c58acbc5d0bf Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 22:03:01 +0900 Subject: [PATCH 05/14] Organize CI names --- .github/workflows/build_example_app.yaml | 2 +- .github/workflows/build_test_app.yaml | 2 +- .github/workflows/build_user_app.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_example_app.yaml b/.github/workflows/build_example_app.yaml index 1a5d43d7e..3fa3f91de 100644 --- a/.github/workflows/build_example_app.yaml +++ b/.github/workflows/build_example_app.yaml @@ -1,5 +1,5 @@ # Verify that the example app works properly. -name: example-test +name: build-example-app on: push: diff --git a/.github/workflows/build_test_app.yaml b/.github/workflows/build_test_app.yaml index dd87ed22f..04f33abad 100644 --- a/.github/workflows/build_test_app.yaml +++ b/.github/workflows/build_test_app.yaml @@ -1,6 +1,6 @@ # Verify that the app build works as intended # when utilizing the code inside the repository. -name: build-test +name: build-test-app on: push: diff --git a/.github/workflows/build_user_app.yaml b/.github/workflows/build_user_app.yaml index f2a1ee010..dd0ce64b6 100644 --- a/.github/workflows/build_user_app.yaml +++ b/.github/workflows/build_user_app.yaml @@ -1,7 +1,7 @@ # Verify that the documentation steps function as intended # when users utilize released versions # instead of the code from the repository. -name: user-installation +name: build-user-app on: workflow_dispatch: From 236386c632fdeec0dbbca5b6efd862f9d68d671f Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 22:23:50 +0900 Subject: [PATCH 06/14] Fix a problem with CI --- .github/workflows/build_example_app.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/build_example_app.yaml b/.github/workflows/build_example_app.yaml index 3fa3f91de..60a453fc5 100644 --- a/.github/workflows/build_example_app.yaml +++ b/.github/workflows/build_example_app.yaml @@ -75,6 +75,10 @@ jobs: working-directory: rust_crate/ run: cargo install --path ./ + - name: Generate message files + working-directory: flutter_ffi_plugin/example/ + run: rinf message + - name: Build the example app if: matrix.target == 'linux' working-directory: flutter_ffi_plugin/example/ From f32910c62c4036108f128010896294541766110c Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:07:25 +0900 Subject: [PATCH 07/14] Fix `rinf message` in example app CI --- .github/workflows/build_example_app.yaml | 10 +++++++--- .github/workflows/build_test_app.yaml | 4 ++-- .github/workflows/build_user_app.yaml | 4 ++-- automate/__main__.py | 18 ++++++++++++++---- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build_example_app.yaml b/.github/workflows/build_example_app.yaml index 60a453fc5..8bfdf4784 100644 --- a/.github/workflows/build_example_app.yaml +++ b/.github/workflows/build_example_app.yaml @@ -71,13 +71,17 @@ jobs: distribution: "zulu" java-version: "11" + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + - name: Install the CLI tool working-directory: rust_crate/ run: cargo install --path ./ - - name: Generate message files - working-directory: flutter_ffi_plugin/example/ - run: rinf message + - name: Prepare a Flutter app for testing + run: python automate prepare-example-app - name: Build the example app if: matrix.target == 'linux' diff --git a/.github/workflows/build_test_app.yaml b/.github/workflows/build_test_app.yaml index 04f33abad..224ea4c97 100644 --- a/.github/workflows/build_test_app.yaml +++ b/.github/workflows/build_test_app.yaml @@ -81,8 +81,8 @@ jobs: working-directory: rust_crate/ run: cargo install --path ./ - - name: Create a Flutter app for testing - run: python automate create-test-app + - name: Prepare a Flutter app for testing + run: python automate prepare-test-app - name: Build the example app if: matrix.target == 'linux' diff --git a/.github/workflows/build_user_app.yaml b/.github/workflows/build_user_app.yaml index dd0ce64b6..a4b753dff 100644 --- a/.github/workflows/build_user_app.yaml +++ b/.github/workflows/build_user_app.yaml @@ -64,8 +64,8 @@ jobs: working-directory: rust_crate/ run: cargo install rinf - - name: Create a Flutter app for testing - run: python automate create-user-app + - name: Prepare a Flutter app for testing + run: python automate prepare-user-app - name: Build the example app if: matrix.target == 'linux' diff --git a/automate/__main__.py b/automate/__main__.py index 5edf97559..20d4e3a06 100644 --- a/automate/__main__.py +++ b/automate/__main__.py @@ -23,7 +23,7 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): command += " main" os.system(command) -elif sys.argv[1] == "create-test-app": +elif sys.argv[1] == "prepare-test-app": filepath = ".gitignore" with open(filepath, mode="r", encoding="utf8") as file: content: str = file.read() @@ -47,7 +47,7 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): os.remove("Cargo.toml") - # Enable the web target, as it's not by default. + # Enable the web target, since it's not enabled by default. replace_text_in_file( "native/hub/src/lib.rs", "use tokio;", @@ -77,7 +77,7 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): "test_app/native/*", ) -elif sys.argv[1] == "create-user-app": +elif sys.argv[1] == "prepare-user-app": filepath = ".gitignore" with open(filepath, mode="r", encoding="utf8") as file: content: str = file.read() @@ -101,7 +101,7 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): os.remove("Cargo.toml") - # Enable the web target, as it's not by default. + # Enable the web target, since it's not enabled by default. replace_text_in_file( "native/hub/src/lib.rs", "use tokio;", @@ -131,5 +131,15 @@ def replace_text_in_file(filepath: str, change_from: str, change_to: str): "user_app/native/*", ) +elif sys.argv[1] == "prepare-example-app": + os.chdir("./flutter_ffi_plugin/example") + + command = "rinf message" + while os.system(command) != 0: + # Retry the command in case of failure, + # possibly due to GitHub API rate limiting + # associated with the 'protoc_prebuilt' crate. + time.sleep(60) + else: print("No such option for automation is available.") From 245f7ac523625a2c5ca00d571834e205ed29870c Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:08:47 +0900 Subject: [PATCH 08/14] Update guides --- flutter_ffi_plugin/example/native/README.md | 1 - flutter_ffi_plugin/template/native/README.md | 1 - 2 files changed, 2 deletions(-) diff --git a/flutter_ffi_plugin/example/native/README.md b/flutter_ffi_plugin/example/native/README.md index 101675f82..ea506548b 100755 --- a/flutter_ffi_plugin/example/native/README.md +++ b/flutter_ffi_plugin/example/native/README.md @@ -3,5 +3,4 @@ This folder contains Rust crates. Entry point of the Rust logic is the `hub` library crate. These crates are integrated and compiled into the Flutter app by the [Rinf](https://rinf.cunarist.com) framework. - Do NOT change the name of the `hub` crate. Compilation presets expect the entry library crate to be located at `./native/hub`. -- Do NOT modify the `bridge` module inside `./native/hub/src` unless you know what you're doing. - You CAN name crates other than `hub` as you want. diff --git a/flutter_ffi_plugin/template/native/README.md b/flutter_ffi_plugin/template/native/README.md index 101675f82..ea506548b 100644 --- a/flutter_ffi_plugin/template/native/README.md +++ b/flutter_ffi_plugin/template/native/README.md @@ -3,5 +3,4 @@ This folder contains Rust crates. Entry point of the Rust logic is the `hub` library crate. These crates are integrated and compiled into the Flutter app by the [Rinf](https://rinf.cunarist.com) framework. - Do NOT change the name of the `hub` crate. Compilation presets expect the entry library crate to be located at `./native/hub`. -- Do NOT modify the `bridge` module inside `./native/hub/src` unless you know what you're doing. - You CAN name crates other than `hub` as you want. From 21eca622ca23d789a24c9d13cca18f6f55507f16 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:10:11 +0900 Subject: [PATCH 09/14] Update docs --- documentation/docs/applying-template.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/documentation/docs/applying-template.md b/documentation/docs/applying-template.md index 01cadaf87..d7f4e0ffd 100644 --- a/documentation/docs/applying-template.md +++ b/documentation/docs/applying-template.md @@ -37,17 +37,12 @@ After running the command, you'll have new files and folders as your starter Rus │ └── ... ├── linux/ + ├── messages/ -+ │ ├── sample_folder/ + │ ├── counter_number.proto -+ │ ├── fractal_art.proto + │ └── README.md + ├── native/ + │ ├── hub/ + │ │ ├── src/ + │ │ └── Cargo.toml -+ │ ├── sample_crate/ -+ │ │ ├── src/ -+ │ │ └── Cargo.toml + │ └── README.md ├── web/ ├── windows/ From 1cadc129ce2a99add8e0cbc6877717877cbce0c4 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:19:04 +0900 Subject: [PATCH 10/14] Update docs and comments --- documentation/docs/applying-template.md | 2 +- flutter_ffi_plugin/example/native/hub/src/sample_functions.rs | 3 +-- flutter_ffi_plugin/example/native/sample_crate/src/lib.rs | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/documentation/docs/applying-template.md b/documentation/docs/applying-template.md index d7f4e0ffd..0f175b129 100644 --- a/documentation/docs/applying-template.md +++ b/documentation/docs/applying-template.md @@ -53,7 +53,7 @@ After running the command, you'll have new files and folders as your starter Rus └── ... ``` -Various comments are written in the actual code to help you understand the whole structure. Also, you might want to remove `sample_crate` in production. +Various comments are written in the actual code to help you understand the whole structure. If you already have a Rust crate that you want to use here, just put it inside `./native` and set it as a dependency of the `hub` crate. diff --git a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs index d2e94b769..63075d1bc 100755 --- a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs +++ b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs @@ -1,5 +1,4 @@ -//! This module is only for Rinf demonstrations. -//! You might want to remove this module in production. +//! This crate is written for Rinf demonstrations. use crate::messages; use crate::tokio; diff --git a/flutter_ffi_plugin/example/native/sample_crate/src/lib.rs b/flutter_ffi_plugin/example/native/sample_crate/src/lib.rs index 54845d843..0e7d26483 100755 --- a/flutter_ffi_plugin/example/native/sample_crate/src/lib.rs +++ b/flutter_ffi_plugin/example/native/sample_crate/src/lib.rs @@ -1,5 +1,4 @@ -//! This crate is only for Rinf demonstrations. -//! You might want to remove this crate in production. +//! This crate is written for Rinf demonstrations. pub use fractal::draw_fractal_image; From c3def8bb5ec1ef9301813056f06f743bd222f82a Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:45:35 +0900 Subject: [PATCH 11/14] Improve starter code inside the template --- .../messages/{counter_number.proto => basic.proto} | 4 ++-- flutter_ffi_plugin/template/native/hub/src/lib.rs | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) rename flutter_ffi_plugin/template/messages/{counter_number.proto => basic.proto} (75%) diff --git a/flutter_ffi_plugin/template/messages/counter_number.proto b/flutter_ffi_plugin/template/messages/basic.proto similarity index 75% rename from flutter_ffi_plugin/template/messages/counter_number.proto rename to flutter_ffi_plugin/template/messages/basic.proto index fba402b6c..479ddc224 100644 --- a/flutter_ffi_plugin/template/messages/counter_number.proto +++ b/flutter_ffi_plugin/template/messages/basic.proto @@ -2,11 +2,11 @@ syntax = "proto3"; package counter_number; // [RINF:DART-SIGNAL] -message NumberInput { +message SmallLetter { string letter = 1; } // [RINF:RUST-SIGNAL] -message NumberOutput { +message SmallNumber { int32 current_number = 1; } diff --git a/flutter_ffi_plugin/template/native/hub/src/lib.rs b/flutter_ffi_plugin/template/native/hub/src/lib.rs index 3ed86bf29..efdab646b 100644 --- a/flutter_ffi_plugin/template/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/template/native/hub/src/lib.rs @@ -14,7 +14,9 @@ rinf::write_interface!(); // use `tokio::task::spawn_blocking`. async fn main() { // Use `tokio::spawn` to run concurrent tasks. - use messages::counter_number::*; - let _receiver = NumberInput::get_dart_signal_receiver(); - NumberOutput { current_number: 7 }.send_signal_to_dart(); + use messages::basic::*; + // Send signals to Dart like below. + SmallNumber { current_number: 7 }.send_signal_to_dart(); + // Get receivers that listen to Dart signals like below. + let _ = SmallLetter::get_dart_signal_receiver(); } From 21cf17a62050d4bac0424d1c682fa1b618f1fd42 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:52:08 +0900 Subject: [PATCH 12/14] Improve starter code --- flutter_ffi_plugin/example/native/hub/src/lib.rs | 2 +- flutter_ffi_plugin/template/messages/basic.proto | 8 ++++---- flutter_ffi_plugin/template/native/hub/src/lib.rs | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/flutter_ffi_plugin/example/native/hub/src/lib.rs b/flutter_ffi_plugin/example/native/hub/src/lib.rs index efaa8c626..26bc5a855 100755 --- a/flutter_ffi_plugin/example/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/example/native/hub/src/lib.rs @@ -9,12 +9,12 @@ use tokio_with_wasm::tokio; // Uncomment this line to target the web rinf::write_interface!(); +// Use `tokio::spawn` to run concurrent tasks. // Always use non-blocking async functions // such as `tokio::fs::File::open`. // If you really need to use blocking code, // use `tokio::task::spawn_blocking`. async fn main() { - // Use `tokio::spawn` to run concurrent tasks. tokio::spawn(sample_functions::tell_numbers()); tokio::spawn(sample_functions::stream_fractal()); tokio::spawn(sample_functions::run_debug_tests()); diff --git a/flutter_ffi_plugin/template/messages/basic.proto b/flutter_ffi_plugin/template/messages/basic.proto index 479ddc224..39e7d7231 100644 --- a/flutter_ffi_plugin/template/messages/basic.proto +++ b/flutter_ffi_plugin/template/messages/basic.proto @@ -1,12 +1,12 @@ syntax = "proto3"; -package counter_number; +package basic; // [RINF:DART-SIGNAL] -message SmallLetter { - string letter = 1; +message SmallText { + string text = 1; } // [RINF:RUST-SIGNAL] message SmallNumber { - int32 current_number = 1; + int32 number = 1; } diff --git a/flutter_ffi_plugin/template/native/hub/src/lib.rs b/flutter_ffi_plugin/template/native/hub/src/lib.rs index efdab646b..393735909 100644 --- a/flutter_ffi_plugin/template/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/template/native/hub/src/lib.rs @@ -8,15 +8,15 @@ use tokio; rinf::write_interface!(); +// Use `tokio::spawn` to run concurrent tasks. // Always use non-blocking async functions // such as `tokio::fs::File::open`. // If you really need to use blocking code, // use `tokio::task::spawn_blocking`. async fn main() { - // Use `tokio::spawn` to run concurrent tasks. use messages::basic::*; // Send signals to Dart like below. - SmallNumber { current_number: 7 }.send_signal_to_dart(); + SmallNumber { number: 7 }.send_signal_to_dart(); // Get receivers that listen to Dart signals like below. - let _ = SmallLetter::get_dart_signal_receiver(); + let _ = SmallText::get_dart_signal_receiver(); } From c4e174072ef68e45d17be9d5ef20d31124ce117d Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:55:27 +0900 Subject: [PATCH 13/14] Update docs --- documentation/docs/applying-template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/applying-template.md b/documentation/docs/applying-template.md index 0f175b129..15184b017 100644 --- a/documentation/docs/applying-template.md +++ b/documentation/docs/applying-template.md @@ -37,7 +37,7 @@ After running the command, you'll have new files and folders as your starter Rus │ └── ... ├── linux/ + ├── messages/ -+ │ ├── counter_number.proto ++ │ ├── basic.proto + │ └── README.md + ├── native/ + │ ├── hub/ From af1d46a864a1bf907d8cc7b8be9f173097e7ff6a Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Tue, 21 May 2024 23:57:22 +0900 Subject: [PATCH 14/14] Improve docs --- documentation/docs/running-and-building.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/documentation/docs/running-and-building.md b/documentation/docs/running-and-building.md index b9c7df895..2539a0105 100644 --- a/documentation/docs/running-and-building.md +++ b/documentation/docs/running-and-building.md @@ -40,9 +40,7 @@ rinf wasm --release flutter build web ``` -### Deploying On a Web Server - -When deploying your web app, ensure that your web server is configured to include cross-origin-related HTTP headers in its responses. These headers enable web browsers using your website to gain access to `SharedArrayBuffer` web API, which is something similar to shared memory on the web. +When deploying your web app on a web server, ensure that your web server is configured to include cross-origin-related HTTP headers in its responses. These headers enable web browsers using your website to gain access to `SharedArrayBuffer` web API, which is something similar to shared memory on the web. - [`Cross-Origin-Opener-Policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy): `same-origin` - [`Cross-Origin-Embedder-Policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy): `require-corp`.