use fusabi_plugin_runtime::{
PluginRegistry, RegistryConfig, PluginWatcher, WatchConfig, WatchEvent,
};
use std::path::PathBuf;
use std::time::Duration;
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
info!("Starting hot-reload example");
let registry_config = RegistryConfig::new()
.with_max_plugins(100)
.with_allow_overwrite(true);
let _registry = PluginRegistry::new(registry_config);
let watch_config = WatchConfig::new()
.with_debounce(Duration::from_millis(500))
.with_recursive(true)
.with_auto_reload(true);
let mut watcher = PluginWatcher::new(watch_config)?;
watcher.on_change(|event| {
match &event {
WatchEvent::Created { path } => {
info!("Plugin file created: {:?}", path);
}
WatchEvent::Modified { path } => {
info!("Plugin file modified: {:?}", path);
}
WatchEvent::Removed { path } => {
info!("Plugin file removed: {:?}", path);
}
WatchEvent::Renamed { from, to } => {
info!("Plugin file renamed: {:?} -> {:?}", from, to);
}
}
});
let plugins_dir = PathBuf::from("./plugins");
if plugins_dir.exists() {
info!("Watching plugins directory: {:?}", plugins_dir);
watcher.watch(&plugins_dir)?;
} else {
info!("Plugins directory not found, create ./plugins/ and add .fsx files");
info!("Example: mkdir -p ./plugins && echo '// Test plugin' > ./plugins/test.fsx");
}
watcher.start()?;
info!("Watching for file changes. Press Ctrl+C to exit.");
loop {
std::thread::sleep(Duration::from_millis(100));
}
}