mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 05:51:39 +00:00
Refactor RefPathIndex initialization and attribute indexing
- Removed the `populate_initial_ref_path_index` method from `ChangeProcessor` and integrated its functionality into the `RefPathIndex` struct with a new `populate_from_dir` method. - Updated the `ServeSession` to initialize `RefPathIndex` by populating it directly from the project root directory, ensuring pre-existing `Rojo_Ref_*` attributes are indexed correctly. - This refactor enhances code organization and improves the efficiency of attribute indexing during session initialization.
This commit is contained in:
parent
201b971101
commit
c5a5e973ca
3 changed files with 61 additions and 61 deletions
|
|
@ -111,11 +111,6 @@ impl ChangeProcessor {
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
log::trace!("ChangeProcessor thread started");
|
log::trace!("ChangeProcessor thread started");
|
||||||
|
|
||||||
// Populate the RefPathIndex from existing meta/model files
|
|
||||||
// so that renames of instances with pre-existing Rojo_Ref_*
|
|
||||||
// attributes are handled correctly from the first rename.
|
|
||||||
task.populate_initial_ref_path_index();
|
|
||||||
|
|
||||||
// Tracks when to run the next reconciliation pass. Set to
|
// Tracks when to run the next reconciliation pass. Set to
|
||||||
// Some(future_instant) after VFS events arrive, cleared
|
// Some(future_instant) after VFS events arrive, cleared
|
||||||
// after reconcile_tree() runs. This ensures we only do the
|
// after reconcile_tree() runs. This ensures we only do the
|
||||||
|
|
@ -260,61 +255,6 @@ impl JobThreadContext {
|
||||||
rel_path(path, &self.project_root)
|
rel_path(path, &self.project_root)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scan all `.meta.json5`, `.model.json5`, `.meta.json`, `.model.json`
|
|
||||||
/// files under the project root for existing `Rojo_Ref_*` attributes
|
|
||||||
/// and populate the `RefPathIndex`. This ensures pre-existing ref
|
|
||||||
/// attributes (from CLI syncback, manual edits, or previous sessions)
|
|
||||||
/// are indexed so rename path updates work from the first rename.
|
|
||||||
fn populate_initial_ref_path_index(&self) {
|
|
||||||
let mut count = 0;
|
|
||||||
let mut dirs_to_visit = vec![self.project_root.clone()];
|
|
||||||
while let Some(dir) = dirs_to_visit.pop() {
|
|
||||||
let entries = match fs::read_dir(&dir) {
|
|
||||||
Ok(entries) => entries,
|
|
||||||
Err(_) => continue,
|
|
||||||
};
|
|
||||||
for entry in entries.filter_map(|e| e.ok()) {
|
|
||||||
let path = entry.path();
|
|
||||||
if path.is_dir() {
|
|
||||||
dirs_to_visit.push(path);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let name = match path.file_name().and_then(|n| n.to_str()) {
|
|
||||||
Some(n) => n,
|
|
||||||
None => continue,
|
|
||||||
};
|
|
||||||
if !(name.ends_with(".meta.json5")
|
|
||||||
|| name.ends_with(".model.json5")
|
|
||||||
|| name.ends_with(".meta.json")
|
|
||||||
|| name.ends_with(".model.json"))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if let Ok(bytes) = fs::read(&path) {
|
|
||||||
if let Ok(val) = crate::json::from_slice::<serde_json::Value>(&bytes) {
|
|
||||||
if let Some(attrs) = val.get("attributes").and_then(|a| a.as_object()) {
|
|
||||||
let mut index = self.ref_path_index.lock().unwrap();
|
|
||||||
for (key, value) in attrs {
|
|
||||||
if key.starts_with(crate::REF_PATH_ATTRIBUTE_PREFIX) {
|
|
||||||
if let Some(path_str) = value.as_str() {
|
|
||||||
index.add(path_str, &path);
|
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if count > 0 {
|
|
||||||
log::info!(
|
|
||||||
"RefPathIndex: populated {} Rojo_Ref_* entries from existing meta/model files",
|
|
||||||
count
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Find the init file inside a directory-format script folder.
|
/// Find the init file inside a directory-format script folder.
|
||||||
/// Returns the path to the first `init.*.luau` or `init.*.lua` found.
|
/// Returns the path to the first `init.*.luau` or `init.*.lua` found.
|
||||||
fn find_init_file(dir: &Path) -> Option<PathBuf> {
|
fn find_init_file(dir: &Path) -> Option<PathBuf> {
|
||||||
|
|
|
||||||
|
|
@ -298,6 +298,62 @@ impl RefPathIndex {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Scan all `.meta.json5`, `.model.json5`, `.meta.json`, `.model.json`
|
||||||
|
/// files under `root` for existing `Rojo_Ref_*` attributes and index them.
|
||||||
|
/// This ensures pre-existing ref attributes (from CLI syncback, manual
|
||||||
|
/// edits, or previous sessions) are indexed so rename path updates work
|
||||||
|
/// from the first rename.
|
||||||
|
pub fn populate_from_dir(&mut self, root: &Path) {
|
||||||
|
let mut count = 0usize;
|
||||||
|
let mut dirs_to_visit = vec![root.to_path_buf()];
|
||||||
|
while let Some(dir) = dirs_to_visit.pop() {
|
||||||
|
let entries = match std::fs::read_dir(&dir) {
|
||||||
|
Ok(entries) => entries,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
for entry in entries.filter_map(|e| e.ok()) {
|
||||||
|
let path = entry.path();
|
||||||
|
if path.is_dir() {
|
||||||
|
dirs_to_visit.push(path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let name = match path.file_name().and_then(|n| n.to_str()) {
|
||||||
|
Some(n) => n,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
if !(name.ends_with(".meta.json5")
|
||||||
|
|| name.ends_with(".model.json5")
|
||||||
|
|| name.ends_with(".meta.json")
|
||||||
|
|| name.ends_with(".model.json"))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Ok(bytes) = std::fs::read(&path) {
|
||||||
|
if let Ok(val) =
|
||||||
|
crate::json::from_slice::<serde_json::Value>(&bytes)
|
||||||
|
{
|
||||||
|
if let Some(attrs) = val.get("attributes").and_then(|a| a.as_object()) {
|
||||||
|
for (key, value) in attrs {
|
||||||
|
if key.starts_with(crate::REF_PATH_ATTRIBUTE_PREFIX) {
|
||||||
|
if let Some(path_str) = value.as_str() {
|
||||||
|
self.add(path_str, &path);
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
log::info!(
|
||||||
|
"RefPathIndex: populated {} Rojo_Ref_* entries from existing meta/model files",
|
||||||
|
count
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Find all meta/model files that contain a `Rojo_Ref_*` attribute whose
|
/// Find all meta/model files that contain a `Rojo_Ref_*` attribute whose
|
||||||
/// value equals `prefix` or starts with `prefix/`. These are the files
|
/// value equals `prefix` or starts with `prefix/`. These are the files
|
||||||
/// that need updating when an instance at `prefix` is renamed.
|
/// that need updating when an instance at `prefix` is renamed.
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,11 @@ impl ServeSession {
|
||||||
|
|
||||||
let (tree_mutation_sender, tree_mutation_receiver) = crossbeam_channel::unbounded();
|
let (tree_mutation_sender, tree_mutation_receiver) = crossbeam_channel::unbounded();
|
||||||
let suppressed_paths = Arc::new(Mutex::new(std::collections::HashMap::new()));
|
let suppressed_paths = Arc::new(Mutex::new(std::collections::HashMap::new()));
|
||||||
let ref_path_index = Arc::new(Mutex::new(crate::RefPathIndex::new()));
|
let ref_path_index = {
|
||||||
|
let mut index = crate::RefPathIndex::new();
|
||||||
|
index.populate_from_dir(root_project.folder_location());
|
||||||
|
Arc::new(Mutex::new(index))
|
||||||
|
};
|
||||||
|
|
||||||
let git_repo_root = crate::git::git_repo_root(root_project.folder_location());
|
let git_repo_root = crate::git::git_repo_root(root_project.folder_location());
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue