Skip to content

Commit

Permalink
feat: support for automatic update KCL dependencies on kcl.mod file save
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Kumar <abhishek22512@gmail.com>
  • Loading branch information
octonawish-akcodes committed Mar 8, 2024
1 parent 29b421f commit 075702a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
22 changes: 21 additions & 1 deletion kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
serde_json = "1.0.86"
notify = "6.1.1"

kclvm-config ={ path = "../config"}
kclvm-runtime ={ path = "../runtime"}
Expand Down
64 changes: 60 additions & 4 deletions kclvm/driver/src/kpm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::kcl;
use crate::lookup_the_nearest_file_dir;
use anyhow::{bail, Result};
use notify::{RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf, process::Command, sync::mpsc::channel};

use crate::{kcl, lookup_the_nearest_file_dir};
use kclvm_config::modfile::KCL_MOD_FILE;
use kclvm_parser::LoadProgramOptions;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf, process::Command};

/// [`fill_pkg_maps_for_k_file`] will call `kpm metadata` to obtain the metadata
/// of all dependent packages of the kcl package where the current file is located,
Expand Down Expand Up @@ -107,3 +108,58 @@ pub fn update_dependencies(work_dir: PathBuf) -> Result<()> {
),
}
}

/// Monitor changes to the kcl.mod file and automatically update dependencies.
pub fn watch_kcl_mod(directory: PathBuf) -> Result<()> {
let (sender, receiver) = channel();
let mut watcher = notify::recommended_watcher(move |res| {
if let Err(err) = sender.send(res) {
eprintln!("Error sending event to channel: {:?}", err);
}
})?;

watcher.watch(&directory, RecursiveMode::NonRecursive)?;

loop {
match receiver.recv() {
Ok(event) => match event {
Ok(event) => match event.kind {
notify::event::EventKind::Modify(modify_kind) => {
if let notify::event::ModifyKind::Data(data_change) = modify_kind {
if data_change == notify::event::DataChange::Content {
println!("kcl.mod file content modified. Updating dependencies...");
update_dependencies(directory.clone())?; // Pass directory to update_dependencies
}
}
}
_ => {}
},
Err(err) => {
println!("Watcher error: {:?}", err);
}
},
Err(e) => {
println!("Receiver error: {:?}", e);
}
}
}
}

/// Tracking function for the kcl_mod_file change.
pub fn kcl_mod_file_track(work_dir: PathBuf) -> Result<()> {
let directory = match lookup_the_nearest_file_dir(work_dir.clone(), KCL_MOD_FILE) {
Some(mod_dir) => mod_dir,
None => {
eprintln!(
"Manifest file '{}' not found in directory hierarchy",
KCL_MOD_FILE
);
return Ok(());
}
};

if let Err(err) = watch_kcl_mod(directory) {
eprintln!("Error watching kcl.mod file: {:?}", err);
}
Ok(())
}

0 comments on commit 075702a

Please sign in to comment.