Skip to content

Commit

Permalink
Support rinf message --watch command
Browse files Browse the repository at this point in the history
  • Loading branch information
bookshiyi committed Nov 22, 2023
1 parent 5b3c233 commit 5b5f3ca
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion flutter_ffi_plugin/bin/rinf.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'package:path/path.dart';
import 'package:package_config/package_config.dart';

Future<void> main(List<String> args) async {
Expand All @@ -16,7 +17,11 @@ Future<void> main(List<String> 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")) {
Expand All @@ -33,6 +38,8 @@ Future<void> main(List<String> 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:
Expand All @@ -41,6 +48,61 @@ Future<void> main(List<String> args) async {
}
}

Future<void> _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<void> _applyRustTemplate({bool onlyBridge = false}) async {
// Get the path of the current project directory
Expand Down

0 comments on commit 5b5f3ca

Please sign in to comment.