From 5b5f3cadd558d4e082332d7f7076e8516053ad51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?book=E8=AF=97=E6=84=8F?= Date: Wed, 22 Nov 2023 18:09:01 +0800 Subject: [PATCH] Support `rinf message --watch` command --- flutter_ffi_plugin/bin/rinf.dart | 64 +++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/flutter_ffi_plugin/bin/rinf.dart b/flutter_ffi_plugin/bin/rinf.dart index af9b7100..75d2ec1c 100755 --- a/flutter_ffi_plugin/bin/rinf.dart +++ b/flutter_ffi_plugin/bin/rinf.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'package:path/path.dart'; import 'package:package_config/package_config.dart'; Future main(List args) async { @@ -16,7 +17,11 @@ Future main(List args) async { } break; case "message": - await _generateMessageCode(); + if (args.contains("--watch") || args.contains("-w")) { + await _watchAndGenerateMessageCode(); + } else { + await _generateMessageCode(); + } break; case "wasm": if (args.contains("--release") || args.contains("-r")) { @@ -33,6 +38,8 @@ Future main(List args) async { print(" template Applies Rust template to current project."); print(" -b, --bridge Only applies `bridge` Rust module."); print(" message Generates message code from `.proto` files."); + print( + " -w, --watch Continuously watching `.proto` files changes then generates message code."); print(" wasm Builds webassembly module."); print(" -r, --release Builds in release mode."); default: @@ -41,6 +48,61 @@ Future main(List args) async { } } +Future _watchAndGenerateMessageCode() async { + final currentDirectory = Directory.current; + final messagesPath = join(currentDirectory.path, "messages"); + var messagesDirectory = Directory(messagesPath); + + var generated = true; + var watcher; + + void startWatch() { + watcher = messagesDirectory + .watch(recursive: true) + .listen((FileSystemEvent event) { + if (event.path.endsWith(".proto") && generated) { + String event_type_str = ""; + switch (event.type) { + case FileSystemEvent.create: + event_type_str = "Created"; + break; + case FileSystemEvent.modify: + event_type_str = "Modified"; + break; + case FileSystemEvent.delete: + event_type_str = "Deleted"; + break; + case FileSystemEvent.move: + event_type_str = "Moved"; + break; + } + print("${event_type_str}: ${relative(event.path, from: messagesPath)}"); + generated = false; + } + }); + } + + void stopWatch() { + watcher.cancel(); + } + + // Linux platform doesn't support recursive watch + print( + "Start watching ${Platform.isLinux ? "without recursive" : "with recursive"}:${messagesDirectory.path}"); + startWatch(); + while (true) { + await Future.delayed(Duration(seconds: 1)); + if (!generated) { + stopWatch(); + print("Generating message code..."); + await _generateMessageCode().then((x) { + generated = true; + startWatch(); + }); + } + } +} + /// Creates new folders and files to an existing Flutter project folder. Future _applyRustTemplate({bool onlyBridge = false}) async { // Get the path of the current project directory